Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions example/src/components/ApplePay/useApplePay.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Alert } from 'react-native';
import type { TapPayInstance } from 'react-native-tappay';

const merchantData = {
merchantName: 'My Merchant Name',
merchantIdentifier: 'merchant.com.verybuy.verybuy',
countryCode: 'TW',
currencyCode: 'TWD',
};
const cartData = [
{
itemName: 'total',
price: 40,
},
];

function useApplePay(TapPay: TapPayInstance) {
const [isAvailable, setIsAvailable] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [prime, setPrime] = useState('');

useEffect(() => {
TapPay.isApplePayAvailable().then(_isAvailable => {
if (!_isAvailable) {
setIsAvailable(false);
console.log('nono');

throw 'ApplePay is not available';
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這樣有 Uncaught Error

Copy link
Member Author

@blackbing blackbing Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line pay 那邊沒有額外對 error 做什麼,catch 僅 log,但這邊沒 catch 會讓應用直接壞掉吧,僅是沒支援就壞掉不太合理

TapPay.webSetupApplePay(merchantData, cartData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still run setup if isApplePayAvailable is false?

}, [TapPay]);

const gerPrime = useCallback(() => {
setIsLoading(true);
setPrime('');

TapPay.webGetApplePayPrime()
.then(({ prime: _prime }) => setPrime(_prime || ''))
.catch(() => {
Alert.alert(
'Warning!',
'Please make sure you have finished ApplePay setup, \n and change your merchantIdentifier.',
);
})
.finally(() => setIsLoading(false));
}, [TapPay]);

return useMemo(
() => ({ isAvailable, isLoading, prime, gerPrime }),
[isAvailable, isLoading, prime, gerPrime],
);
}

export default useApplePay;