浏览代码

【功能完善】商城:支付宝 wap 的接入

YunaiV 11 月之前
父节点
当前提交
27260f4a75
共有 1 个文件被更改,包括 55 次插入44 次删除
  1. 55 44
      sheep/platform/pay.js

+ 55 - 44
sheep/platform/pay.js

@@ -35,7 +35,7 @@ export default class SheepPay {
         },
         },
         mock: () => {
         mock: () => {
           this.mockPay();
           this.mockPay();
-        }
+        },
       },
       },
       WechatMiniProgram: {
       WechatMiniProgram: {
         wechat: () => {
         wechat: () => {
@@ -49,7 +49,7 @@ export default class SheepPay {
         },
         },
         mock: () => {
         mock: () => {
           this.mockPay();
           this.mockPay();
-        }
+        },
       },
       },
       App: {
       App: {
         wechat: () => {
         wechat: () => {
@@ -63,7 +63,7 @@ export default class SheepPay {
         },
         },
         mock: () => {
         mock: () => {
           this.mockPay();
           this.mockPay();
-        }
+        },
       },
       },
       H5: {
       H5: {
         wechat: () => {
         wechat: () => {
@@ -77,7 +77,7 @@ export default class SheepPay {
         },
         },
         mock: () => {
         mock: () => {
           this.mockPay();
           this.mockPay();
-        }
+        },
       },
       },
     };
     };
     return payAction[sheep.$platform.name][this.payment]();
     return payAction[sheep.$platform.name][this.payment]();
@@ -89,7 +89,7 @@ export default class SheepPay {
       let data = {
       let data = {
         id: this.id,
         id: this.id,
         channelCode: channel,
         channelCode: channel,
-        channelExtras: {}
+        channelExtras: {},
       };
       };
       // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
       // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
       if (['wx_pub', 'wx_lite'].includes(channel)) {
       if (['wx_pub', 'wx_lite'].includes(channel)) {
@@ -108,8 +108,11 @@ export default class SheepPay {
         // 失败时
         // 失败时
         if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
         if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
           // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
           // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
-          if (res.msg.indexOf('无效的openid') >= 0 // 获取的 openid 不正确时,或者随便输入了个 openid
-            || res.msg.indexOf('下单账号与支付账号不一致') >= 0) { // https://developers.weixin.qq.com/community/develop/doc/00008c53c347804beec82aed051c00
+          if (
+            res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
+            res.msg.indexOf('下单账号与支付账号不一致') >= 0
+          ) {
+            // https://developers.weixin.qq.com/community/develop/doc/00008c53c347804beec82aed051c00
             this.bindWeixin();
             this.bindWeixin();
           }
           }
         }
         }
@@ -133,30 +136,34 @@ export default class SheepPay {
       },
       },
       fail: (error) => {
       fail: (error) => {
         if (error.errMsg.indexOf('chooseWXPay:没有此SDK或暂不支持此SDK模拟') >= 0) {
         if (error.errMsg.indexOf('chooseWXPay:没有此SDK或暂不支持此SDK模拟') >= 0) {
-          sheep.$helper.toast('发起微信支付失败,原因:可能是微信开发者工具不支持,建议使用微信打开网页后支付');
-          return
+          sheep.$helper.toast(
+            '发起微信支付失败,原因:可能是微信开发者工具不支持,建议使用微信打开网页后支付',
+          );
+          return;
         }
         }
         this.payResult('fail');
         this.payResult('fail');
       },
       },
     });
     });
   }
   }
 
 
-  // 浏览器微信 H5 支付 TODO 芋艿:待接入
+  // 浏览器微信 H5 支付 TODO 芋艿:待接入(注意:H5 支付是给普通浏览器,不是微信公众号的支付,绝大多数人用不到,可以忽略)
   async wechatWapPay() {
   async wechatWapPay() {
     const { error, data } = await this.prepay();
     const { error, data } = await this.prepay();
     if (error === 0) {
     if (error === 0) {
-      const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`;
+      const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${
+        this.payment
+      }&orderType=${this.orderType}`;
       location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
       location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
     }
     }
   }
   }
 
 
-  // 支付链接  TODO 芋艿:待接入
+  // 支付链接(支付宝 wap 支付)
   async redirectPay() {
   async redirectPay() {
-    let { error, data } = await this.prepay();
-    if (error === 0) {
-      const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`;
-      location.href = data.pay_data + encodeURIComponent(redirect_url);
+    let { code, data } = await this.prepay('alipay_wap');
+    if (code !== 0) {
+      return;
     }
     }
+    location.href = data.displayContent;
   }
   }
 
 
   // #endif
   // #endif
@@ -202,26 +209,26 @@ export default class SheepPay {
     code === 0 && this.payResult('success');
     code === 0 && this.payResult('success');
   }
   }
 
 
-  // 支付宝复制链接支付  TODO 芋艿:待接入
+  // 支付宝复制链接支付(通过支付宝 wap 支付实现)
   async copyPayLink() {
   async copyPayLink() {
-    let that = this;
-    let { error, data } = await this.prepay();
-    if (error === 0) {
-      // 引入showModal 点击确认 复制链接;
-      uni.showModal({
-        title: '支付宝支付',
-        content: '复制链接到外部浏览器',
-        confirmText: '复制链接',
-        success: (res) => {
-          if (res.confirm) {
-            sheep.$helper.copyText(data.pay_data);
-          }
-        },
-      });
+    let { code, data } = await this.prepay('alipay_wap');
+    if (code !== 0) {
+      return;
     }
     }
+    // 引入 showModal 点击确认:复制链接;
+    uni.showModal({
+      title: '支付宝支付',
+      content: '复制链接到外部浏览器',
+      confirmText: '复制链接',
+      success: (res) => {
+        if (res.confirm) {
+          sheep.$helper.copyText(data.displayContent);
+        }
+      },
+    });
   }
   }
 
 
-  // 支付宝支付  TODO 芋艿:待接入
+  // 支付宝支付(App) TODO 芋艿:待接入【暂时没打包 app,所以没接入,一般人用不到】
   async alipay() {
   async alipay() {
     let that = this;
     let that = this;
     const { error, data } = await this.prepay();
     const { error, data } = await this.prepay();
@@ -243,7 +250,7 @@ export default class SheepPay {
     }
     }
   }
   }
 
 
-  // 微信支付  TODO 芋艿:待接入
+  // 微信支付(App)  TODO 芋艿:待接入:待接入【暂时没打包 app,所以没接入,一般人用不到】
   async wechatAppPay() {
   async wechatAppPay() {
     let that = this;
     let that = this;
     let { error, data } = await this.prepay();
     let { error, data } = await this.prepay();
@@ -266,7 +273,7 @@ export default class SheepPay {
     sheep.$router.redirect('/pages/pay/result', {
     sheep.$router.redirect('/pages/pay/result', {
       id: this.id,
       id: this.id,
       orderType: this.orderType,
       orderType: this.orderType,
-      payState: resultType
+      payState: resultType,
     });
     });
   }
   }
 
 
@@ -282,7 +289,6 @@ export default class SheepPay {
       },
       },
     });
     });
   }
   }
-
 }
 }
 
 
 export function getPayMethods(channels) {
 export function getPayMethods(channels) {
@@ -316,23 +322,28 @@ export function getPayMethods(channels) {
       title: '模拟支付',
       title: '模拟支付',
       value: 'mock',
       value: 'mock',
       disabled: true,
       disabled: true,
-    }
+    },
   ];
   ];
-  const platform = sheep.$platform.name
+  const platform = sheep.$platform.name;
 
 
   // 1. 处理【微信支付】
   // 1. 处理【微信支付】
   const wechatMethod = payMethods[0];
   const wechatMethod = payMethods[0];
-  if ((platform === 'WechatOfficialAccount' && channels.includes('wx_pub'))
-    || (platform === 'WechatMiniProgram' && channels.includes('wx_lite'))
-    || (platform === 'App' && channels.includes('wx_app'))) {
+  if (
+    (platform === 'WechatOfficialAccount' && channels.includes('wx_pub')) ||
+    (platform === 'WechatMiniProgram' && channels.includes('wx_lite')) ||
+    (platform === 'App' && channels.includes('wx_app'))
+  ) {
     wechatMethod.disabled = false;
     wechatMethod.disabled = false;
   }
   }
 
 
   // 2. 处理【支付宝支付】
   // 2. 处理【支付宝支付】
   const alipayMethod = payMethods[1];
   const alipayMethod = payMethods[1];
-  if ((platform === 'WechatOfficialAccount' && channels.includes('alipay_wap'))
-    || platform === 'WechatMiniProgram' && channels.includes('alipay_wap')
-    || platform === 'App' && channels.includes('alipay_app')) {
+  if (
+    (platform === 'H5' && channels.includes('alipay_wap')) ||
+    (platform === 'WechatOfficialAccount' && channels.includes('alipay_wap')) ||
+    (platform === 'WechatMiniProgram' && channels.includes('alipay_wap')) ||
+    (platform === 'App' && channels.includes('alipay_app'))
+  ) {
     alipayMethod.disabled = false;
     alipayMethod.disabled = false;
   }
   }
   // 3. 处理【余额支付】
   // 3. 处理【余额支付】
@@ -347,4 +358,4 @@ export function getPayMethods(channels) {
     mockMethod.disabled = false;
     mockMethod.disabled = false;
   }
   }
   return payMethods;
   return payMethods;
-}
+}