/
StripeProvider.tsx
96 lines (88 loc) · 2.34 KB
/
StripeProvider.tsx
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
import React, { useEffect } from 'react';
import NativeStripeSdk from '../NativeStripeSdk';
import { isAndroid, shouldAttributeExpo } from '../helpers';
import type { AppInfo, InitStripeParams, InitialiseParams } from '../types';
import pjson from '../../package.json';
const EXPO_PARTNER_ID = 'pp_partner_JBN7LkABco2yUu';
/**
* Stripe Provider Component Props
*/
export type Props = InitStripeParams & {
children: React.ReactElement | React.ReactElement[];
};
const repository: any = pjson.repository;
const appInfo: AppInfo = {
name: shouldAttributeExpo() ? `${pjson.name}/expo` : pjson.name,
// package.json output installed via npm is a bit different than from yarn
// the repository field can be an object or string
// for more context: https://github.com/stripe/stripe-react-native/issues/200
url: repository.url || repository,
version: pjson.version,
partnerId: shouldAttributeExpo() ? EXPO_PARTNER_ID : undefined,
};
export const initStripe = async (params: InitStripeParams): Promise<void> => {
const extendedParams: InitialiseParams = { ...params, appInfo };
NativeStripeSdk.initialise(extendedParams);
};
/**
* StripeProvider Component
*
* @example
* ```ts
* <StripeProvider
* publishableKey="_publishableKey"
* merchantIdentifier="merchant.com.stripe.react.native"
* threeDSecureParams={{
* backgroundColor: "#FFF",
* timeout: 5,
* }}
* >
* <App />
* </StripeProvider>
* ```
* @param __namedParameters Props
* @returns JSX.Element
* @category ReactComponents
*/
export function StripeProvider({
children,
publishableKey,
merchantIdentifier,
threeDSecureParams,
stripeAccountId,
urlScheme,
setReturnUrlSchemeOnAndroid,
}: Props) {
useEffect(() => {
if (!publishableKey) {
return;
}
if (isAndroid) {
NativeStripeSdk.initialise({
publishableKey,
appInfo,
stripeAccountId,
threeDSecureParams,
urlScheme,
setReturnUrlSchemeOnAndroid,
});
} else {
NativeStripeSdk.initialise({
publishableKey,
appInfo,
stripeAccountId,
threeDSecureParams,
merchantIdentifier,
urlScheme,
});
}
}, [
publishableKey,
merchantIdentifier,
stripeAccountId,
threeDSecureParams,
urlScheme,
setReturnUrlSchemeOnAndroid,
]);
return <>{children}</>;
}