Browse Source

更新支付

lex 1 year ago
parent
commit
983a021e75

+ 32 - 0
src/state.ts

@@ -87,3 +87,35 @@ const goAuth = (wxAppId: string, urlString?: string) => {
   const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`;
   window.location.replace(url);
 };
+
+/**
+ * @description 支付宝授权-会根据环境去判断
+ * @param wxAppId
+ * @param urlString 回调链接【默认当前页面地址】
+ * @returns void
+ */
+export const goAliAuth = (alipayAppId: string, urlString?: string) => {
+  // 支付宝授权
+  const urlNow = encodeURIComponent(urlString || window.location.href);
+  const appid = alipayAppId || '2021004100630808';
+  // 开发环境
+  if (import.meta.env.DEV) {
+    let url = `https://mstuonline.dayaedu.com/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}`;
+    window.location.replace(url);
+  }
+
+  // 生产环境
+  if (import.meta.env.PROD) {
+    goAuth(alipayAppId, urlString);
+  }
+};
+
+const alipayAuth = (alipayAppId: string, urlString?: string) => {
+  // 用户授权
+  const urlNow = encodeURIComponent(urlString || window.location.href);
+  const scope = 'auth_base'; //snsapi_userinfo   //静默授权 用户无感知
+  const appid = alipayAppId || '2021004100630808';
+  // 判断是否是线上
+  let url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE`;
+  window.location.replace(url);
+};

+ 66 - 16
src/views/adapay/pay-define/index.tsx

@@ -1,6 +1,6 @@
 import request from '@/helpers/request';
 import { browser, getUrlCode, moneyFormat } from '@/helpers/utils';
-import { goWechatAuth } from '@/state';
+import { goAliAuth, goWechatAuth } from '@/state';
 import numeral from 'numeral';
 import { Button, Cell, CellGroup, Icon, showToast } from 'vant';
 import { defineComponent, onMounted, reactive } from 'vue';
@@ -17,11 +17,14 @@ export default defineComponent({
       code: null as any,
       pay_channel: route.query.pay_channel as any,
       wxAppId: route.query.wxAppId as any,
+      paymentType: route.query.paymentType as any,
+      alipayAppId: route.query.alipayAppId as any,
       body: route.query.body as any,
       price: route.query.price as any,
       orderNo: route.query.orderNo as any,
       userId: route.query.userId as any,
-      payInfo: {} as any
+      payInfo: {} as any,
+      isYeePay: false // 是否为易宝支付
     });
 
     const getPayment = async () => {
@@ -39,7 +42,6 @@ export default defineComponent({
         if (state.pay_channel == 'wx_pub') {
           payMap.code = state.code;
         }
-        console.log(payMap, 'payMap');
         const { data } = await request.post(
           '/edu-app/open/userOrder/executePayment',
           {
@@ -50,7 +52,7 @@ export default defineComponent({
           }
         );
 
-        console.log(data, 'payment');
+        state.isYeePay = data.paymentVender?.indexOf('yeepay') !== -1;
         scanCodePay(data.reqParams);
       } catch (e) {
         //
@@ -61,17 +63,23 @@ export default defineComponent({
     const scanCodePay = (data: any) => {
       // 判断支付方式 如果是 test 模式 支付用测试url 否则用生产url
       if (state.pay_channel == 'alipay_qr') {
-        const url =
-          data.prod_mode === 'false'
-            ? data.expend.qrcode_url +
-              '?payment_id=' +
-              data.id +
-              '&pay_channel=' +
-              data.pay_channel
-            : data.expend.qrcode_url;
-        window.location.href = url;
+        if (state.isYeePay) {
+          tradePay(data.tradeNO);
+        } else {
+          const url =
+            data.prod_mode === 'false'
+              ? data.expend.qrcode_url +
+                '?payment_id=' +
+                data.id +
+                '&pay_channel=' +
+                data.pay_channel
+              : data.expend.qrcode_url;
+          window.location.href = url;
+        }
       } else if (state.pay_channel == 'wx_pub') {
-        const tempPayInfo = JSON.parse(data.expend.pay_info);
+        const tempPayInfo = state.isYeePay
+          ? JSON.parse(data.prePayTn)
+          : JSON.parse(data.expend.pay_info);
         state.payInfo = tempPayInfo;
         if (typeof (window as any).WeixinJSBridge == 'undefined') {
           if (document.addEventListener) {
@@ -94,6 +102,42 @@ export default defineComponent({
       }
     };
 
+    // 由于js的载入是异步的,所以可以通过该方法,当AlipayJSBridgeReady事件发生后,再执行callback方法
+    const ready = (callback: any) => {
+      if ((window as any).AlipayJSBridge) {
+        callback && callback();
+      } else {
+        document.addEventListener('AlipayJSBridgeReady', callback, false);
+      }
+    };
+    const tradePay = (tradeNO: any) => {
+      ready(function () {
+        // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
+        (window as any).AlipayJSBridge.call(
+          'tradePay',
+          {
+            tradeNO: tradeNO
+          },
+          function (data: any) {
+            if ('9000' == data.resultCode) {
+              window.location.replace(
+                location.origin +
+                  '/classroom-app/#/payment-result?orderNo=' +
+                  state.orderNo
+              );
+            } else {
+              window.location.replace(
+                location.origin +
+                  '/classroom-app/#/payment-result?orderNo=' +
+                  state.orderNo
+              );
+            }
+            //使用的支付宝内置api实现关闭H5
+            (window as any).AlipayJSBridge.call('closeWebview');
+          }
+        );
+      });
+    };
     const onBridgeReady = () => {
       const payInfo = state.payInfo;
       // let orderNo = state.orderNo
@@ -146,15 +190,14 @@ export default defineComponent({
 
     const init = () => {
       const pay_channel = state.pay_channel;
+      const isYeePay = state.paymentType.indexOf('yeepay') !== -1;
       if (browser().weixin) {
         if (pay_channel === 'wx_pub') {
           //授权
           const code = getUrlCode();
-          console.log(code);
           if (code) {
             state.code = code; // 赋值code码
           } else {
-            // goAuth()
             goWechatAuth(state.wxAppId);
           }
           state.browserStatus = true;
@@ -167,6 +210,13 @@ export default defineComponent({
           state.errorText = '请使用微信扫码';
         } else if (pay_channel == 'alipay_qr') {
           // 支付宝支付
+          //授权
+          const code = getUrlCode('auth_code');
+          if (code) {
+            state.code = code; // 赋值code码
+          } else {
+            goAliAuth(state.alipayAppId);
+          }
           state.browserStatus = true;
           document.title = '支付宝支付';
         }

+ 24 - 19
src/views/adapay/pay-result/index.tsx

@@ -1,6 +1,6 @@
 import request from '@/helpers/request';
 import { browser, getUrlCode, moneyFormat } from '@/helpers/utils';
-import { goWechatAuth } from '@/state';
+import { goAliAuth, goWechatAuth } from '@/state';
 import {
   Cell,
   CellGroup,
@@ -24,6 +24,8 @@ export default defineComponent({
       code: null as any,
       pay_channel: route.query.pay_channel as any,
       wxAppId: route.query.wxAppId as any,
+      paymentType: route.query.paymentType as any,
+      alipayAppId: route.query.alipayAppId as any,
       body: route.query.body as any,
       price: route.query.price as any,
       orderNo: route.query.orderNo as any,
@@ -34,6 +36,7 @@ export default defineComponent({
 
     const init = () => {
       const query = route.query;
+      const isYeePay = state.paymentType.indexOf('yeepay') !== -1;
       // 判断是否有支付对象
       if (!query.orderNo || !query.pay_channel) {
         showConfirmDialog({
@@ -48,8 +51,19 @@ export default defineComponent({
           state.browserStatus = true;
           getWxPay();
         } else if (browser().alipay) {
-          state.browserStatus = true;
-          getPayment();
+          if (isYeePay) {
+            let code = getUrlCode('auth_code');
+            if (code) {
+              state.code = code; // 赋值code码
+              state.browserStatus = true;
+              getPayment();
+            } else {
+              goAliAuth(state.alipayAppId);
+            }
+          } else {
+            state.browserStatus = true;
+            getPayment();
+          }
         } else {
           state.errorText = '请在微信或支付宝客户端打开';
           document.title = 'ERROR';
@@ -83,16 +97,16 @@ export default defineComponent({
         );
         state.isYeePay = data.paymentVender?.indexOf('yeepay') !== -1;
         console.log(data, 'data');
-        scanCodePay(data.reqParams);
+        // scanCodePay(data.reqParams);
       } catch (e) {
         //
-        // console.log(e);
+        console.log(e);
         // 接口报错也跳转到支付回调页
-        window.location.replace(
-          location.origin +
-            '/classroom-app/#/payment-result?orderNo=' +
-            state.orderNo
-        );
+        // window.location.replace(
+        //   location.origin +
+        //     '/classroom-app/#/payment-result?orderNo=' +
+        //     state.orderNo
+        // );
       }
     };
 
@@ -224,21 +238,12 @@ export default defineComponent({
       //授权
       const code = getUrlCode();
       if (!code) {
-        // goAuth()
         goWechatAuth(state.wxAppId);
       } else {
         state.code = code;
         getPayment();
       }
     };
-    // const goAuth = () => {
-    //   // 用户授权
-    //   const urlNow = encodeURIComponent(window.location.href)
-    //   const scope = 'snsapi_base' //snsapi_userinfo   //静默授权 用户无感知
-    //   const appid = state.wxAppId || 'wx8654c671631cfade'
-    //   const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
-    //   window.location.replace(url)
-    // }
 
     onMounted(() => {
       init();

+ 4 - 1
src/views/member-center/index.tsx

@@ -157,7 +157,10 @@ export default defineComponent({
           this.$router.push({
             path: '/order-detail',
             query: {
-              config: JSON.stringify(data.paymentConfig),
+              config: JSON.stringify({
+                ...data.paymentConfig,
+                paymentType: data.paymentType
+              }),
               orderNo: data.orderNo
             }
           });

+ 2 - 0
src/views/student-register/order-detail.tsx

@@ -114,6 +114,8 @@ export default defineComponent({
       const params = qs.stringify({
         pay_channel: val.pay_channel,
         wxAppId: config.wxAppId,
+        alipayAppId: config.alipayAppId,
+        paymentType: state.paymentType,
         body: config.body,
         price: config.price,
         orderNo: config.merOrderNo,