Browse Source

增加退款功能

Pchen. 6 days ago
parent
commit
40c986245e
1 changed files with 11 additions and 1 deletions
  1. 11 1
      lib/PaymentClient.js

+ 11 - 1
lib/PaymentClient.js

@@ -96,6 +96,15 @@ function formatPaymentRefundError(response, fallbackMessage) {
     return formatPaymentHttpError({ response }, fallbackMessage || '支付平台退款失败')
     return formatPaymentHttpError({ response }, fallbackMessage || '支付平台退款失败')
 }
 }
 
 
+function isRefundResponseSuccess(result) {
+    if (!result || typeof result !== 'object') return false
+    const code = Number(result.code)
+    // 标准易支付:code=1;部分平台(如 yaolipay):code=0 且带 refund_no
+    if (code === 1) return true
+    if (code === 0 && result.refund_no) return true
+    return false
+}
+
 async function requestPaymentRefund({ orderId, tradeNo, money, logger }) {
 async function requestPaymentRefund({ orderId, tradeNo, money, logger }) {
     const paymentConfig = getPaymentConfig()
     const paymentConfig = getPaymentConfig()
     if (!paymentConfig.url || !paymentConfig.pid || !paymentConfig.key) {
     if (!paymentConfig.url || !paymentConfig.pid || !paymentConfig.key) {
@@ -136,7 +145,7 @@ async function requestPaymentRefund({ orderId, tradeNo, money, logger }) {
         throw new Error(formatPaymentRefundError(response, '支付平台退款失败'))
         throw new Error(formatPaymentRefundError(response, '支付平台退款失败'))
     }
     }
 
 
-    if (!result || Number(result.code) !== 1) {
+    if (!isRefundResponseSuccess(result)) {
         throw new Error(result?.msg || '支付平台退款失败')
         throw new Error(result?.msg || '支付平台退款失败')
     }
     }
 
 
@@ -150,6 +159,7 @@ module.exports = {
     formatPaymentHttpError,
     formatPaymentHttpError,
     formatPaymentRefundError,
     formatPaymentRefundError,
     parsePaymentResponseBody,
     parsePaymentResponseBody,
+    isRefundResponseSuccess,
     queryPaymentOrder,
     queryPaymentOrder,
     requestPaymentRefund
     requestPaymentRefund
 }
 }