Skip to content

Commit

Permalink
优化支付退款URL构造 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
hopegao authored Jul 12, 2022
1 parent fd8be38 commit 2be5dec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/service/CoaWxPayIsvService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class CoaWxPayIsvService {

// 微信支付统一下单
async unifiedOrder(data: {
accountId: string
orderId: string
price: number
appWxaId: string
Expand Down Expand Up @@ -68,7 +69,7 @@ export class CoaWxPayIsvService {
data.price ||
CoaError.message('CoaWxPayIsv.MissingField', '缺少order price'),
spbill_create_ip: '1.1.1.1',
notify_url: `${this.bin.config.notifyPay}.${data.orderId}`,
notify_url: `${this.bin.config.notifyPay}.${data.accountId || CoaError.message('CoaWxPayIsv.MissingField', '缺少accountId')}.${data.orderId}`,
trade_type: 'JSAPI',
}
const body = await this.bin.toSignedXmlParams(param)
Expand All @@ -77,6 +78,7 @@ export class CoaWxPayIsvService {

// 退款
async payRefund(data: {
accountId: string
refundId: string
orderId: string
totalPrice: number
Expand All @@ -85,6 +87,8 @@ export class CoaWxPayIsvService {
}) {
const rawData =
data.rawData || CoaError.message('CoaWxPayIsv.MissingField', '数据不存在')
const accountId =
data.accountId || CoaError.message('CoaWxPayIsv.MissingField', '缺少账户ID')
const refundId =
data.refundId ||
CoaError.message('CoaWxPayIsv.MissingField', '缺少退款ID')
Expand All @@ -96,6 +100,7 @@ export class CoaWxPayIsvService {
const refundPrice =
data.refundPrice ||
CoaError.message('CoaWxPayIsv.MissingField', '缺少退款价格')

const param = {
appid: this.bin.config.appId,
mch_id: this.bin.config.mchId,
Expand Down Expand Up @@ -124,7 +129,7 @@ export class CoaWxPayIsvService {
out_refund_no: refundId,
total_fee: totalPrice,
refund_fee: refundPrice,
notify_url: `${this.bin.config.notifyRefund}.${orderId}`,
notify_url: `${this.bin.config.notifyRefund}.${accountId}.${orderId}`,
}
const body = await this.bin.toSignedXmlParams(param)
return await this.bin.post('/secapi/pay/refund', body, {
Expand Down
2 changes: 2 additions & 0 deletions src/test/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const service = new CoaWxPayIsvService(bin)

// 统一下单
await service.unifiedOrder({
accountId: 'account000001',
orderId: 'order000001',
appWxaId: 'wx000000002',
subMchId: '1660000001',
Expand All @@ -41,6 +42,7 @@ await service.queryOrder({

// 退款
await service.payRefund({
accountId: 'account000001',
refundId: 'refund000001',
orderId: 'order000001',
price: 100,
Expand Down

0 comments on commit 2be5dec

Please sign in to comment.