-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayments.js
More file actions
120 lines (105 loc) · 3.3 KB
/
Copy pathPayments.js
File metadata and controls
120 lines (105 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, {createContext, useContext, useState, useEffect} from 'react'
import useNativePay from './use_native_pay'
import useInAppPay from './use_in_app_pay'
const PaymentContext = createContext({})
export default function Payments(props) {
const onProgress = props.onProgress ? props.onProgress : () => {
}
const stripe = props.stripe ? props.stripe : () => {
}
const {
npReady,
npSetup,
npStart,
npProcessing,
npFinish,
npReset,
npFellback // Unable to use Google/Apple Pay, so asking directly for CC
} = useNativePay(props)
const {
iapReady,
iapSetup,
iapStart,
iapProcessing,
iapFinish,
iapReset,
iapProducts, // pricing in local currency available here
iapSubscriptions, // pricing in local currency available here
iapPurchase, // The last purchase can be used in case the payment timed out initially
iapError // The last error for displaying to the user
} = useInAppPay(props)
/////////////////////////////////////////////////////
// Generic purchasing (in-app or native purchases) //
/////////////////////////////////////////////////////
const [paymentReady, setPaymentReady] = useState(false)
const [paymentProcessing, setPaymentProcessing] = useState(false)
useEffect(() => {
console.log({npReady, iapReady})
setPaymentReady(npReady || iapReady)
}, [npReady, iapReady])
useEffect(() => {
console.log({npProcessing, iapProcessing})
setPaymentProcessing(npProcessing || iapProcessing)
}, [npProcessing, iapProcessing])
const paymentSetup = async (props) => {
const {inApp, native} = props
if (inApp) iapSetup(inApp)
if (native) npSetup(native)
}
const paymentStart = async (props) => {
const {productId, description, amount, isSubscription = false} = props
onProgress({event: 'payment_start', productId, amount, description, isSubscription})
if (npReady) {
return await npStart(props)
} else if (iapReady) {
return await iapStart(props)
} else {
onProgress({event: 'payment_not_ready', productId, amount, description, isSubscription})
}
}
const paymentFinish = (purchase) => {
if (iapProcessing) {
iapFinish(purchase)
iapReset()
} else if (npProcessing) npReset()
}
const paymentReset = () => {
if (iapProcessing) iapReset()
else if (npProcessing) npReset()
}
return (
<Elements stripe={stripe}>
<PaymentContext.Provider value={{
paymentReady,
paymentSetup,
paymentStart,
paymentProcessing,
paymentFinish,
paymentReset,
npReady,
npSetup,
npStart,
npProcessing,
npFinish,
npReset,
npFellback, // Unable to use Google/Apple Pay, so asking directly for CC
iapReady,
iapSetup,
iapStart,
iapProcessing,
iapFinish,
iapReset,
iapProducts, // pricing in local currency available here
iapSubscriptions, // pricing in local currency available here
iapPurchase, // The last purchase can be used in case the payment timed out initially
iapError // The last error for displaying to the user
}}
>
{props.children}
</PaymentContext.Provider>
</Elements>
)
}
export function usePayments() {
return useContext(PaymentContext)
}