nestjs对接微信支付退款
时间: 2023-08-24 22:09:47 浏览: 45
nestjs可以通过官方提供的npm包`@nestjs/common`和第三方npm包`wechat-pay-sdk`来对接微信支付退款。
1. 安装`wechat-pay-sdk`和`@nestjs/common`。
```
npm install wechat-pay-sdk @nestjs/common
```
2. 创建微信支付退款的服务类。
```typescript
import { Injectable } from '@nestjs/common';
import { WechatPay } from 'wechat-pay-sdk';
@Injectable()
export class WechatRefundService {
private wechatPay: WechatPay;
constructor() {
this.wechatPay = new WechatPay({
appid: 'your_appid',
mchid: 'your_mchid',
partnerKey: 'your_partnerKey',
notifyUrl: 'your_notifyUrl',
pfx: fs.readFileSync('your_pfx_path'),
});
}
async refund(outTradeNo: string, outRefundNo: string, totalFee: number, refundFee: number): Promise<any> {
const result = await this.wechatPay.refund({
out_trade_no: outTradeNo,
out_refund_no: outRefundNo,
total_fee: totalFee,
refund_fee: refundFee,
});
return result;
}
}
```
3. 在需要使用退款服务的地方引入服务类并调用`refund`方法。
```typescript
import { Controller, Post, Body } from '@nestjs/common';
import { WechatRefundService } from './wechat-refund.service';
@Controller()
export class AppController {
constructor(private readonly wechatRefundService: WechatRefundService) {}
@Post('/refund')
async refund(@Body() body: any): Promise<any> {
const { outTradeNo, outRefundNo, totalFee, refundFee } = body;
const result = await this.wechatRefundService.refund(outTradeNo, outRefundNo, totalFee, refundFee);
return result;
}
}
```
以上就是nestjs对接微信支付退款的基本流程,可以根据具体的业务需求进行调整。