Просмотр исходного кода

Show native checkout progress and recover from unavailable payment service

彭峰 1 день назад
Родитель
Сommit
5e1e9bb541

+ 20 - 7
common-page/pages/native-payment/index.js

@@ -19,8 +19,18 @@ function requestPayment(params) {
     }));
 }
 
+function prepareWithDeadline(work) {
+    let timer;
+    return Promise.race([
+        work,
+        new Promise((_, reject) => {
+            timer = setTimeout(() => reject({ code: 'timeout' }), 25000);
+        }),
+    ]).finally(() => clearTimeout(timer));
+}
+
 Page({
-    data: { title: '确认微信支付', amount: '', paying: false, valid: false, error: '' },
+    data: { title: '确认微信支付', amount: '', paying: false, statusText: '', valid: false, error: '' },
 
     onLoad(options) {
         const { orderId, kind } = options;
@@ -49,14 +59,15 @@ Page({
             this.setData({ error: '请在小程序登录同一账号,然后返回订单重试。' });
             return;
         }
-        this.setData({ paying: true, error: '' });
+        this.setData({ paying: true, statusText: '正在核对订单…', error: '' });
         let phase = 'prepare';
         try {
-            const params = await Parse.Cloud.run('prepareYubanMiniPayment', {
-                ...this.payment, loginCode: await loginCode(),
-            });
+            const params = await prepareWithDeadline(loginCode().then(code => Parse.Cloud.run('prepareYubanMiniPayment', {
+                ...this.payment, loginCode: code,
+            })));
             if (this.returned) return;
             phase = 'cashier';
+            this.setData({ statusText: '正在打开微信收银台…' });
             await requestPayment(params);
             this.finish('submitted');
         } catch (error) {
@@ -66,11 +77,13 @@ Page({
                 } else {
                     this.setData({ error: phase === 'cashier' ? '微信支付未完成,请返回核对订单。' :
                         error?.code === 209 ? '小程序微信身份与订单账号不一致,请重新登录。' :
-                            error?.code === 142 ? '订单或收银台参数无法确认,请返回核对。' : '暂时无法打开微信支付,请稍后重试。' });
+                            error?.code === 142 ? '订单或收银台参数无法确认,请返回核对。' :
+                                error?.code === 141 ? '小程序支付服务暂未接通,请稍后再试。' :
+                                    error?.code === 'timeout' ? '订单核对超时。请稍后重试同一订单。' : '暂时无法打开微信支付,请稍后重试。' });
                 }
             }
         } finally {
-            if (!this.returned) this.setData({ paying: false });
+            if (!this.returned) this.setData({ paying: false, statusText: '' });
         }
     },
 

+ 1 - 1
common-page/pages/native-payment/index.wxml

@@ -2,6 +2,6 @@
   <view class="checkout-title">{{title}}</view>
   <view wx:if="{{valid}}" class="checkout-amount">¥{{amount}}</view>
   <view class="checkout-error" wx:if="{{error}}">{{error}}</view>
-  <button wx:if="{{valid}}" type="primary" bindtap="pay" loading="{{paying}}" disabled="{{paying}}">确认支付</button>
+  <button wx:if="{{valid}}" type="primary" bindtap="pay" loading="{{paying}}" disabled="{{paying}}">{{statusText || '确认支付'}}</button>
   <button bindtap="back" disabled="{{paying}}">返回订单</button>
 </view>

+ 10 - 2
test/native-webview-payment.test.js

@@ -14,7 +14,7 @@ function fixture({ result, prepareFails } = {}) {
         User: { current: () => ({ id: 'fixture-user' }) },
         Cloud: { run: async (name, input) => {
             calls.push([name, input.orderId, input.loginCode]);
-            if (prepareFails) throw { code: 209 };
+            if (prepareFails) throw { code: prepareFails === true ? 209 : prepareFails };
             return { timeStamp: '1796000000', nonceStr: 'fixture', package: 'prepay_id=fixture', paySign: 'stub', signType: 'MD5' };
         } },
     } };
@@ -28,7 +28,7 @@ function fixture({ result, prepareFails } = {}) {
         navigateBack: () => calls.push(['back']),
     };
     vm.runInNewContext(readFileSync('common-page/pages/native-payment/index.js', 'utf8'), {
-        Page: x => { definition = x; }, getApp: () => app, getCurrentPages: () => [opener, {}], wx,
+        Page: x => { definition = x; }, getApp: () => app, getCurrentPages: () => [opener, {}], wx, setTimeout, clearTimeout,
     });
     const page = { ...definition, data: { ...definition.data }, setData(patch) { Object.assign(this.data, patch); } };
     page.onLoad({ orderId: 'testOrder1', kind: 'recharge', fen: '9900' });
@@ -57,3 +57,11 @@ test('backend validation failure never invokes the native cashier', async () =>
     assert.equal(calls.length, 1);
     assert.match(page.data.error, /身份.*不一致/);
 });
+
+test('a missing deployment shows a concrete error and resets loading', async () => {
+    const { page, calls } = fixture({ prepareFails: 141 });
+    await page.pay();
+    assert.equal(calls.length, 1);
+    assert.equal(page.data.paying, false);
+    assert.match(page.data.error, /支付服务暂未接通/);
+});