Browse Source

fix: validate enabled payment methods

Pchen. 1 week ago
parent
commit
30d2f57960
4 changed files with 33 additions and 5 deletions
  1. 1 1
      apis/Order/CreateOrder.js
  2. 29 0
      apis/Order/GetPaymentMethods.js
  3. 2 2
      lib/OrderPayment.js
  4. 1 2
      lib/OrderPaymentAttempt.js

+ 1 - 1
apis/Order/CreateOrder.js

@@ -178,7 +178,7 @@ class CreateOrder extends API {
             })
             })
         } catch (err) {
         } catch (err) {
             this.logger.error(`创建订单失败:${err.stack || err}`)
             this.logger.error(`创建订单失败:${err.stack || err}`)
-            return res.json({ ...BaseStdResponse.ERR, msg: '创建订单异常,请联系管理员' })
+            return res.json({ ...BaseStdResponse.ERR, msg: err.message || '创建订单异常,请联系管理员' })
         } finally {
         } finally {
             await releaseCouponUsageLock(couponLockKey)
             await releaseCouponUsageLock(couponLockKey)
         }
         }

+ 29 - 0
apis/Order/GetPaymentMethods.js

@@ -0,0 +1,29 @@
+const API = require("../../lib/API")
+const { BaseStdResponse } = require("../../BaseStdResponse")
+const { getPaymentMethods } = require("../../lib/OrderPayment")
+
+class GetPaymentMethods extends API {
+    constructor() {
+        super()
+        this.setPath('/Order/PaymentMethods')
+        this.setMethod('GET')
+    }
+
+    async onRequest(req, res) {
+        try {
+            const methods = await getPaymentMethods()
+            return res.json({
+                ...BaseStdResponse.OK,
+                data: methods
+            })
+        } catch (error) {
+            this.logger.error(`获取支付方式失败:${error.stack || error}`)
+            return res.json({
+                ...BaseStdResponse.OK,
+                data: []
+            })
+        }
+    }
+}
+
+module.exports.GetPaymentMethods = GetPaymentMethods

+ 2 - 2
lib/OrderPayment.js

@@ -14,12 +14,12 @@ async function getPaymentMethods() {
         defaultValue: { methods: DEFAULT_PAYMENT_METHODS }
         defaultValue: { methods: DEFAULT_PAYMENT_METHODS }
     })
     })
     const methods = paymentConfig?.methods
     const methods = paymentConfig?.methods
-    if (!Array.isArray(methods) || methods.length === 0) {
+    if (!Array.isArray(methods)) {
         return DEFAULT_PAYMENT_METHODS
         return DEFAULT_PAYMENT_METHODS
     }
     }
 
 
     return methods
     return methods
-        .filter(method => method && typeof method.type === 'string' && method.type.trim())
+        .filter(method => method && method.enabled !== false && typeof method.type === 'string' && method.type.trim())
         .map(method => ({
         .map(method => ({
             type: method.type.trim(),
             type: method.type.trim(),
             name: String(method.name || method.type).trim()
             name: String(method.name || method.type).trim()

+ 1 - 2
lib/OrderPaymentAttempt.js

@@ -49,8 +49,7 @@ async function getGatewayOrderNos(orderId) {
     const values = (rows || [])
     const values = (rows || [])
         .map(row => row.gateway_order_no)
         .map(row => row.gateway_order_no)
         .filter(Boolean)
         .filter(Boolean)
-    if (!values.includes(orderId)) values.push(orderId)
-    return values
+    return values.length > 0 ? values : [orderId]
 }
 }
 
 
 module.exports = {
 module.exports = {