Compare commits
No commits in common. "c51168ec563ec68304c95b1fb1218b71910559ac" and "26e3b4d0c98a2e80084c5ef132d3520ccb79f05a" have entirely different histories.
c51168ec56
...
26e3b4d0c9
@ -8,25 +8,12 @@ import ProRights from '@/components/owner/rights/proRights';
|
|||||||
import { createOrder, createPayment, getPAy, isOrderExpired, payFailure, payProcessing, paySuccess } from '@/components/owner/rights/utils';
|
import { createOrder, createPayment, getPAy, isOrderExpired, payFailure, payProcessing, paySuccess } from '@/components/owner/rights/utils';
|
||||||
import { ThemedText } from '@/components/ThemedText';
|
import { ThemedText } from '@/components/ThemedText';
|
||||||
import { CreateOrder } from '@/types/personal-info';
|
import { CreateOrder } from '@/types/personal-info';
|
||||||
|
import { ErrorCode, getAvailablePurchases, getPurchaseHistories, ProductPurchase, useIAP } from 'expo-iap';
|
||||||
import { useLocalSearchParams, useRouter } from "expo-router";
|
import { useLocalSearchParams, useRouter } from "expo-router";
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { ActivityIndicator, Image, Platform, ScrollView, StyleSheet, TouchableOpacity, View } from 'react-native';
|
import { ActivityIndicator, Image, ScrollView, StyleSheet, TouchableOpacity, View } from 'react-native';
|
||||||
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||||
|
|
||||||
// 根据平台动态导入 expo-iap
|
|
||||||
let useIAP: any, requestPurchase: any, getPurchaseHistories: any;
|
|
||||||
if (Platform.OS !== 'web') {
|
|
||||||
const iap = require('expo-iap');
|
|
||||||
useIAP = iap.useIAP;
|
|
||||||
requestPurchase = iap.requestPurchase;
|
|
||||||
getPurchaseHistories = iap.getPurchaseHistories;
|
|
||||||
} else {
|
|
||||||
// 为 Web 端提供 mock 实现
|
|
||||||
useIAP = () => ({ connected: false });
|
|
||||||
requestPurchase = async () => { console.log('IAP is not available on web.'); };
|
|
||||||
getPurchaseHistories = async () => [];
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Rights() {
|
export default function Rights() {
|
||||||
const insets = useSafeAreaInsets();
|
const insets = useSafeAreaInsets();
|
||||||
@ -34,19 +21,26 @@ export default function Rights() {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const {
|
const {
|
||||||
connected,
|
connected,
|
||||||
|
products,
|
||||||
|
subscriptions,
|
||||||
|
currentPurchase,
|
||||||
|
currentPurchaseError,
|
||||||
requestProducts,
|
requestProducts,
|
||||||
ErrorCode
|
requestPurchase,
|
||||||
|
finishTransaction,
|
||||||
|
validateReceipt,
|
||||||
} = useIAP();
|
} = useIAP();
|
||||||
const { pro } = useLocalSearchParams<{
|
|
||||||
credit: string;
|
|
||||||
pro: string;
|
|
||||||
}>();
|
|
||||||
// 用户勾选协议
|
// 用户勾选协议
|
||||||
const [agree, setAgree] = useState<boolean>(false);
|
const [agree, setAgree] = useState<boolean>(false);
|
||||||
// 用户选择购买的loading
|
// 用户选择购买的loading
|
||||||
const [confirmLoading, setConfirmLoading] = useState<boolean>(false);
|
const [confirmLoading, setConfirmLoading] = useState<boolean>(false);
|
||||||
// 选择购买方式
|
// 选择购买方式
|
||||||
const [payChoice, setPayChoice] = useState<'ApplePay'>('ApplePay');
|
const [payChoice, setPayChoice] = useState<'ApplePay'>('ApplePay');
|
||||||
|
// 获取路由参数
|
||||||
|
const { credit, pro } = useLocalSearchParams<{
|
||||||
|
credit: string;
|
||||||
|
pro: string;
|
||||||
|
}>();
|
||||||
// 普通用户,会员
|
// 普通用户,会员
|
||||||
const [userType, setUserType] = useState<'normal' | 'premium'>('normal');
|
const [userType, setUserType] = useState<'normal' | 'premium'>('normal');
|
||||||
|
|
||||||
@ -72,19 +66,19 @@ export default function Rights() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 恢复购买
|
// 恢复购买
|
||||||
// const restorePurchases = async () => {
|
const restorePurchases = async () => {
|
||||||
// try {
|
try {
|
||||||
// const purchases = await getAvailablePurchases();
|
const purchases = await getAvailablePurchases();
|
||||||
// console.log('Available purchases:', purchases);
|
console.log('Available purchases:', purchases);
|
||||||
// // Process and validate restored purchases
|
// Process and validate restored purchases
|
||||||
// for (const purchase of purchases) {
|
for (const purchase of purchases) {
|
||||||
// await validateAndGrantPurchase(purchase);
|
await validateAndGrantPurchase(purchase);
|
||||||
// }
|
}
|
||||||
// alert(t('personal:rights.restoreSuccess'));
|
alert(t('personal:rights.restoreSuccess'));
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// console.error('Restore failed:', error);
|
console.error('Restore failed:', error);
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
|
|
||||||
// 处理购买
|
// 处理购买
|
||||||
const handlePurchase = async (sku: string, transaction_id: string) => {
|
const handlePurchase = async (sku: string, transaction_id: string) => {
|
||||||
@ -94,13 +88,15 @@ export default function Rights() {
|
|||||||
const res = await requestPurchase({
|
const res = await requestPurchase({
|
||||||
request: {
|
request: {
|
||||||
ios: {
|
ios: {
|
||||||
sku: sku,
|
sku: payType,
|
||||||
andDangerouslyFinishTransactionAutomaticallyIOS: false,
|
andDangerouslyFinishTransactionAutomaticallyIOS: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
console.log('Purchase success:', res);
|
||||||
|
|
||||||
// 支付成功
|
// 支付成功
|
||||||
await paySuccess(transaction_id, res?.transaction_id || "")
|
await paySuccess(transaction_id, res?.transactionId || "")
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log('Purchase failed:', error);
|
console.log('Purchase failed:', error);
|
||||||
// 支付失败
|
// 支付失败
|
||||||
@ -226,7 +222,7 @@ export default function Rights() {
|
|||||||
</ThemedText>
|
</ThemedText>
|
||||||
<View style={styles.cardPoints}>
|
<View style={styles.cardPoints}>
|
||||||
<StarSvg />
|
<StarSvg />
|
||||||
<ThemedText style={styles.cardPointsText}>{pro}</ThemedText>
|
<ThemedText style={styles.cardPointsText}>{credit}</ThemedText>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
@ -252,7 +248,7 @@ export default function Rights() {
|
|||||||
{/* 普通权益 */}
|
{/* 普通权益 */}
|
||||||
<Normal setUserType={setUserType} style={{ display: userType === 'normal' ? "flex" : "none" }} />
|
<Normal setUserType={setUserType} style={{ display: userType === 'normal' ? "flex" : "none" }} />
|
||||||
{/* 会员权益 */}
|
{/* 会员权益 */}
|
||||||
<Premium setPayType={setPayType} setShowTerms={setShowTerms} payType={payType} premiumPay={premiumPay} loading={loading} style={{ display: userType === 'normal' ? "none" : "flex" }} />
|
<Premium restorePurchases={restorePurchases} setPayType={setPayType} setShowTerms={setShowTerms} payType={payType} premiumPay={premiumPay} loading={loading} style={{ display: userType === 'normal' ? "none" : "flex" }} />
|
||||||
</View>
|
</View>
|
||||||
{/* 支付方式 */}
|
{/* 支付方式 */}
|
||||||
{/* <PayTypeModal setConfirmPay={setConfirmPay} modalVisible={showPayType} setModalVisible={setShowPayType} payChoice={payChoice} setPayChoice={setPayChoice} /> */}
|
{/* <PayTypeModal setConfirmPay={setConfirmPay} modalVisible={showPayType} setModalVisible={setShowPayType} payChoice={payChoice} setPayChoice={setPayChoice} /> */}
|
||||||
@ -310,7 +306,7 @@ export default function Rights() {
|
|||||||
|
|
||||||
</View>
|
</View>
|
||||||
{/* 协议弹窗 */}
|
{/* 协议弹窗 */}
|
||||||
<PrivacyModal modalVisible={showTerms} setModalVisible={setShowTerms} type={"membership"} />
|
<PrivacyModal modalVisible={showTerms} setModalVisible={setShowTerms} type={"member"} />
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -459,3 +455,6 @@ const styles = StyleSheet.create({
|
|||||||
lineHeight: 32
|
lineHeight: 32
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
function validateAndGrantPurchase(purchase: ProductPurchase) {
|
||||||
|
throw new Error('Function not implemented.');
|
||||||
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ interface Props {
|
|||||||
premiumPay: any;
|
premiumPay: any;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
setShowTerms: (visible: boolean) => void;
|
setShowTerms: (visible: boolean) => void;
|
||||||
|
restorePurchases: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PayItem {
|
export interface PayItem {
|
||||||
@ -30,7 +31,7 @@ export interface PayItem {
|
|||||||
|
|
||||||
}
|
}
|
||||||
const Premium = (props: Props) => {
|
const Premium = (props: Props) => {
|
||||||
const { style, payType, setPayType, premiumPay, loading } = props;
|
const { style, payType, setPayType, premiumPay, loading, setShowTerms, restorePurchases } = props;
|
||||||
const bestValue = maxDiscountProduct(premiumPay)?.product_code
|
const bestValue = maxDiscountProduct(premiumPay)?.product_code
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
@ -79,7 +80,7 @@ const Premium = (props: Props) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
{/* <View style={{ flexDirection: 'row', gap: 8, marginLeft: 4, marginTop: 8 }}>
|
<View style={{ flexDirection: 'row', gap: 8, marginLeft: 4, marginTop: 8 }}>
|
||||||
<ThemedText style={{ color: '#AC7E35', fontSize: 10 }}>
|
<ThemedText style={{ color: '#AC7E35', fontSize: 10 }}>
|
||||||
{t('personal:rights.restorePurchase')}
|
{t('personal:rights.restorePurchase')}
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
@ -88,7 +89,7 @@ const Premium = (props: Props) => {
|
|||||||
{t('personal:rights.restore')}
|
{t('personal:rights.restore')}
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View> */}
|
</View>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user