Forráskód Böngészése

充值-提货使用支付宝支付

zbb 3 hónapja
szülő
commit
1fdfdbfbc4
3 módosított fájl, 71 hozzáadás és 28 törlés
  1. 41 4
      app/store/components/CheckoutModal.tsx
  2. 23 24
      app/wallet/recharge.tsx
  3. 7 0
      services/wallet.ts

+ 41 - 4
app/store/components/CheckoutModal.tsx

@@ -1,4 +1,5 @@
 import { Image } from 'expo-image';
+import Alipay from 'expo-native-alipay';
 import { useRouter } from 'expo-router';
 import React, { useEffect, useState } from 'react';
 import {
@@ -96,22 +97,58 @@ export default function CheckoutModal({ visible, selectedItems, onClose, onSucce
     router.push('/address?type=1' as any);
   };
 
+  /* 
+   * Handle Submit with Payment Choice
+   */
   const handleSubmit = async () => {
     if (!address) {
       showAlert('请选择收货地址');
       return;
     }
+    
+    if (expressAmount > 0) {
+        Alert.alert(
+            '支付运费',
+            `需支付运费 ¥${expressAmount}`,
+            [
+                { text: '取消', style: 'cancel' },
+                { text: '钱包支付', onPress: () => processTakeApply('WALLET') },
+                { text: '支付宝支付', onPress: () => processTakeApply('ALIPAY_APP') }
+            ]
+        );
+    } else {
+        processTakeApply('WALLET');
+    }
+  };
+
+  const processTakeApply = async (paymentType: string) => {
     if (submitting) return;
     setSubmitting(true);
     try {
       const ids = selectedItems.map(item => item.id);
-      const res = await takeApply(ids, address.id, 'WALLET');
-      if (res) {
-        showAlert('提货成功');
-        onSuccess();
+      const res: any = await takeApply(ids, address!.id, paymentType);
+      
+      console.log('Take Apply Res:', res, paymentType);
+
+      if (paymentType === 'ALIPAY_APP' && res?.payInfo) {
+           Alipay.setAlipayScheme('alipay2021005175632205');
+           const result = await Alipay.pay(res.payInfo);
+           console.log('Alipay Result:', result);
+           if (result?.resultStatus === '9000') {
+               showAlert('提货成功');
+               onSuccess();
+           } else {
+               showAlert('支付未完成');
+           }
+      } else if (res) {
+         // Wallet payment or free success
+         showAlert('提货成功');
+         onSuccess();
       }
     } catch (e) {
       console.error('提货失败:', e);
+      // Usually axios interceptor handles error alerts, but just in case
+      // showAlert('提货失败'); 
     }
     setSubmitting(false);
   };

+ 23 - 24
app/wallet/recharge.tsx

@@ -1,3 +1,4 @@
+import Alipay from 'expo-native-alipay';
 import { Stack, useRouter } from 'expo-router';
 import React, { useState } from 'react';
 import { ActivityIndicator, Alert, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';
@@ -31,34 +32,32 @@ export default function RechargeScreen() {
             return;
         }
 
+        setLoading(true);
         setLoading(true);
         try {
-            // Uniapp logic: generatePaymentLink -> tradeNo
-            const res = await services.wallet.generatePaymentLink(amount, 'ALIPAY_H5', 'CASH');
+            // Use ALIPAY_APP for native payment with correct API
+            const res: any = await services.wallet.rechargeSubmit(amount, 'ALIPAY_APP', 'CASH');
             
-            if (res && res.data && res.data.tradeNo) {
-                 const tradeNo = res.data.tradeNo;
-                 Alert.alert(
-                    '提示', 
-                    '已生成充值订单,请完成支付',
-                    [
-                        { 
-                            text: '已支付', 
-                            onPress: async () => {
-                                const checkRes = await services.wallet.checkPaymentStatus({ tradeNo: tradeNo }); 
-                                if (checkRes && checkRes.code === 0 && checkRes.data.paySuccess) {
-                                    Alert.alert('提示', '支付成功');
-                                    router.replace('/wallet/recharge_record');
-                                } else {
-                                    Alert.alert('提示', checkRes.msg || '支付未完成或查询失败');
-                                }
-                            } 
-                        },
-                        { text: '稍后支付' }
-                    ]
-                 );
+            console.log('Recharge API Res:', res);
+
+            // Handle response which might be direct string or object with payInfo
+            const payInfo = typeof res?.data === 'string' ? res?.data : (res?.data?.payInfo || res?.data?.orderInfo || res?.data?.tradeNo);
+
+            if (payInfo && typeof payInfo === 'string' && (payInfo.startsWith('alipay_root_cert_sn') || payInfo.includes('alipay_sdk'))) {
+                 Alipay.setAlipayScheme('alipay2021005175632205');
+                 const result = await Alipay.pay(payInfo);
+                 console.log('Alipay Result:', result);
+                 
+                 if (result?.resultStatus === '9000') {
+                     Alert.alert('提示', '充值成功');
+                     router.replace('/wallet/recharge_record');
+                 } else {
+                     Alert.alert('提示', '支付未完成');
+                 }
+            } else if (res?.data?.tradeNo && !payInfo) {
+                 Alert.alert('提示', '获取支付信息失败(仅获取到订单号)');
             } else {
-                 Alert.alert('失败', '生成充值订单失败');
+                 Alert.alert('失败', '生成充值订单失败 ' + (res?.msg || ''));
             }
 
         } catch (error) {

+ 7 - 0
services/wallet.ts

@@ -10,6 +10,7 @@ const apis = {
     WITHDRAW_INFO: '/api/user/withdraw/info', // Bank info
     WITHDRAW_SAVE: '/api/user/withdraw/save',
 
+    RECHARGE_SUBMIT: '/api/wallet/recharge/submit',
     RECHARGE_GENERATE_LINK: '/api/user/recharge/generatePaymentLink',
     RECHARGE_CHECK_PAYMENT_STATUS: '/api/user/recharge/checkPaymentStatus',
     
@@ -59,6 +60,11 @@ export const generatePaymentLink = async (amount: number, payType: string, walle
     return res;
 };
 
+export const rechargeSubmit = async (amount: number, paymentType: string, walletType: string) => {
+    const res = await postL(apis.RECHARGE_SUBMIT, { amount, paymentType, walletType });
+    return res;
+};
+
 export const checkPaymentStatus = async (data: { tradeNo: string }) => {
     const res = await post(apis.RECHARGE_CHECK_PAYMENT_STATUS, data);
     return res;
@@ -78,6 +84,7 @@ export default {
     saveWithdraw,
     withdraw,
     generatePaymentLink,
+    rechargeSubmit,
     checkPaymentStatus,
     getNewUserPromotion,
     coupons: async (size = 30) => {