ordering-ui-react-native 0.14.69 → 0.14.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ordering-ui-react-native",
3
- "version": "0.14.69",
3
+ "version": "0.14.70",
4
4
  "description": "Reusable components made in react native",
5
5
  "main": "src/index.tsx",
6
6
  "author": "ordering.inc",
@@ -529,6 +529,7 @@ const CheckoutUI = (props: any) => {
529
529
  paySelected={paymethodSelected}
530
530
  handlePaymentMethodClickCustom={handlePaymentMethodClick}
531
531
  setCardData={setCardData}
532
+ handlePlaceOrder={handlePlaceOrder}
532
533
  />
533
534
  </ChPaymethods>
534
535
  </ChSection>
@@ -61,11 +61,14 @@ const PaymentOptionsUI = (props: any) => {
61
61
  handlePaymethodDataChange,
62
62
  handlePaymentMethodClickCustom,
63
63
  isOpenMethod,
64
- setCardData
64
+ setCardData,
65
+ handlePlaceOrder
65
66
  } = props
66
67
 
67
68
  const theme = useTheme();
68
69
  const [, t] = useLanguage();
70
+ const methodsPay = ['google_pay', 'apple_pay']
71
+ const stripeDirectMethods = ['stripe_direct', ...methodsPay]
69
72
 
70
73
  const [addCardOpen, setAddCardOpen] = useState({ stripe: false, stripeConnect: false });
71
74
  let paymethodSelected = props.paySelected || props.paymethodSelected || isOpenMethod?.paymethod
@@ -119,8 +122,11 @@ const PaymentOptionsUI = (props: any) => {
119
122
  }, [props.paySelected])
120
123
 
121
124
  useEffect(() => {
122
- setCardData(paymethodData)
123
- }, [paymethodData])
125
+ setCardData && setCardData(paymethodData)
126
+ if (methodsPay.includes(paymethodSelected?.gateway) && paymethodData?.id && paymethodSelected?.data?.card) {
127
+ handlePlaceOrder()
128
+ }
129
+ }, [paymethodData, paymethodSelected])
124
130
 
125
131
  const renderPaymethods = ({ item }: any) => {
126
132
  return (
@@ -283,7 +289,7 @@ const PaymentOptionsUI = (props: any) => {
283
289
  <OModal
284
290
  entireModal
285
291
  title={t('ADD_CREDIT_OR_DEBIT_CARD', 'Add credit or debit card')}
286
- open={isOpenMethod?.paymethod?.gateway === 'stripe_direct' && !paymethodData?.id}
292
+ open={stripeDirectMethods?.includes(isOpenMethod?.paymethod?.gateway) && !paymethodData.id}
287
293
  onClose={() => handlePaymethodClick(null)}
288
294
  >
289
295
  <KeyboardAvoidingView
@@ -292,10 +298,13 @@ const PaymentOptionsUI = (props: any) => {
292
298
  enabled={Platform.OS === 'ios' ? true : false}
293
299
  >
294
300
  <StripeElementsForm
301
+ cart={cart}
302
+ paymethod={isOpenMethod?.paymethod?.gateway}
303
+ methodsPay={methodsPay}
295
304
  businessId={props.businessId}
296
- publicKey={isOpenMethod?.paymethod?.credentials?.publishable}
305
+ publicKey={isOpenMethod?.paymethod?.credentials?.publishable || isOpenMethod?.paymethod?.credentials?.publishable_key}
297
306
  handleSource={handlePaymethodDataChange}
298
- onCancel={() => handlePaymethodClick(false)}
307
+ onCancel={() => handlePaymethodClick(null)}
299
308
  />
300
309
  </KeyboardAvoidingView>
301
310
  </OModal>
@@ -7,10 +7,11 @@ import {
7
7
  useConfirmSetupIntent,
8
8
  createPaymentMethod
9
9
  } from '@stripe/stripe-react-native';
10
-
10
+ import configs from '../../config.json'
11
11
  import { ErrorMessage } from './styles';
12
12
 
13
13
  import { StripeElementsForm as StripeFormController } from './naked';
14
+ import { StripeMethodForm } from '../StripeMethodForm';
14
15
  import { OButton, OText } from '../shared';
15
16
  import { useTheme } from 'styled-components/native';
16
17
 
@@ -22,6 +23,10 @@ const StripeElementsFormUI = (props: any) => {
22
23
  businessId,
23
24
  requirements,
24
25
  stripeTokenHandler,
26
+ methodsPay,
27
+ paymethod,
28
+ onCancel,
29
+ cart
25
30
  } = props;
26
31
 
27
32
  const theme = useTheme();
@@ -121,33 +126,49 @@ const StripeElementsFormUI = (props: any) => {
121
126
  <View style={styles.container}>
122
127
  {publicKey ? (
123
128
  <View style={{ flex: 1 }}>
124
- <StripeProvider publishableKey={publicKey}>
125
- <CardField
126
- postalCodeEnabled={true}
127
- cardStyle={{
128
- backgroundColor: '#FFFFFF',
129
- textColor: '#000000',
130
- }}
131
- style={{
132
- width: '100%',
133
- height: 50,
134
- marginVertical: 30,
135
- zIndex: 9999,
136
- }}
137
- onCardChange={(cardDetails: any) => setCard(cardDetails)}
138
- />
129
+ <StripeProvider
130
+ publishableKey={publicKey}
131
+ merchantIdentifier={`merchant.${configs.apple_app_id}`}
132
+ >
133
+ {methodsPay.includes(paymethod) ? (
134
+ <StripeMethodForm
135
+ handleSource={handleSource}
136
+ onCancel={onCancel}
137
+ cart={cart}
138
+ setErrors={setErrors}
139
+ paymethod={paymethod}
140
+ devMode={publicKey?.includes('test')}
141
+ />
142
+ ) : (
143
+ <CardField
144
+ postalCodeEnabled={true}
145
+ cardStyle={{
146
+ backgroundColor: '#FFFFFF',
147
+ textColor: '#000000',
148
+ }}
149
+ style={{
150
+ width: '100%',
151
+ height: 50,
152
+ marginVertical: 30,
153
+ zIndex: 9999,
154
+ }}
155
+ onCardChange={(cardDetails: any) => setCard(cardDetails)}
156
+ />
157
+ )}
139
158
  </StripeProvider>
140
- <OButton
141
- text={t('SAVE_CARD', 'Save card')}
142
- bgColor={isCompleted ? theme.colors.primary : theme.colors.backgroundGray}
143
- borderColor={isCompleted ? theme.colors.primary :theme.colors.backgroundGray}
144
- style={styles.btnAddStyle}
145
- textStyle={{color: 'white'}}
146
- imgRightSrc={null}
147
- onClick={() => handleSaveCard()}
148
- isDisabled={!isCompleted}
149
- isLoading={confirmSetupLoading || values.loadingAdd || createPmLoading}
150
- />
159
+ {!methodsPay?.includes(paymethod) && (
160
+ <OButton
161
+ text={t('SAVE_CARD', 'Save card')}
162
+ bgColor={isCompleted ? theme.colors.primary : theme.colors.backgroundGray}
163
+ borderColor={isCompleted ? theme.colors.primary :theme.colors.backgroundGray}
164
+ style={styles.btnAddStyle}
165
+ textStyle={{color: 'white'}}
166
+ imgRightSrc={null}
167
+ onClick={() => handleSaveCard()}
168
+ isDisabled={!isCompleted}
169
+ isLoading={confirmSetupLoading || values.loadingAdd || createPmLoading}
170
+ />
171
+ )}
151
172
  {!!errors && (
152
173
  <ErrorMessage>
153
174
  <OText
@@ -0,0 +1,163 @@
1
+ import React, { useState, useEffect } from 'react'
2
+ import { useLanguage } from 'ordering-components/native'
3
+ import { GooglePayButton, useGooglePay, ApplePayButton, useApplePay } from '@stripe/stripe-react-native'
4
+ import { OButton } from '../shared';
5
+ import { Platform, View } from 'react-native';
6
+ import { StripeMethodFormParams } from '../../types';
7
+
8
+ export const StripeMethodForm = (props: StripeMethodFormParams) => {
9
+ const {
10
+ cart,
11
+ handleSource,
12
+ onCancel,
13
+ setErrors,
14
+ paymethod,
15
+ devMode
16
+ } = props
17
+ const { initGooglePay, createGooglePayPaymentMethod, loading } = useGooglePay();
18
+ const { presentApplePay, isApplePaySupported } = useApplePay();
19
+ const [initialized, setInitialized] = useState(false);
20
+ const [, t] = useLanguage()
21
+
22
+ useEffect(() => {
23
+ if (paymethod !== 'google_pay') return
24
+ if (Platform.OS === 'ios') {
25
+ setErrors(t('GOOGLE_PAY_NOT_SUPPORTED', 'Google pay not supported'))
26
+ return
27
+ }
28
+ const initialize = async () => {
29
+ try {
30
+ const { error } = await initGooglePay({
31
+ testEnv: devMode,
32
+ merchantName: 'Widget Store',
33
+ countryCode: 'US',
34
+ billingAddressConfig: {
35
+ format: 'FULL',
36
+ isPhoneNumberRequired: true,
37
+ isRequired: false,
38
+ },
39
+ existingPaymentMethodRequired: false,
40
+ isEmailRequired: true,
41
+ });
42
+
43
+ if (error) {
44
+ setErrors(error.code + ' - ' + error.message);
45
+ return;
46
+ }
47
+ setInitialized(true);
48
+ } catch (err: any) {
49
+ setErrors('Catch ' + err?.message)
50
+ }
51
+ }
52
+ initialize();
53
+ }, [initGooglePay]);
54
+
55
+ useEffect(() => {
56
+ if (paymethod !== 'apple_pay') return
57
+ if (Platform.OS === 'android') {
58
+ setErrors(t('APPLE_PAY_NOT_SUPPORTED', 'Apple pay not supported'))
59
+ return
60
+ }
61
+ }, [])
62
+
63
+ const createPaymentMethod = async () => {
64
+
65
+ const { error, paymentMethod } = await createGooglePayPaymentMethod({
66
+ amount: cart?.balance ?? cart?.total,
67
+ currencyCode: 'USD',
68
+ });
69
+
70
+ if (error) {
71
+ setErrors(error.code + ' - ' + error.message);
72
+ return;
73
+ } else if (paymentMethod) {
74
+ handleSource({
75
+ ...paymentMethod?.Card,
76
+ id: paymentMethod.id,
77
+ type: paymentMethod.type,
78
+ source_id: paymentMethod?.id,
79
+ card: {
80
+ brand: paymentMethod.Card.brand,
81
+ last4: paymentMethod.Card.last4
82
+ }
83
+ })
84
+ onCancel()
85
+ }
86
+ setInitialized(false);
87
+ };
88
+
89
+ const pay = async () => {
90
+ if (!isApplePaySupported) {
91
+ setErrors(t('APPLE_PAY_NOT_SUPPORTED', 'Apple pay not supported'))
92
+ return
93
+ }
94
+
95
+ const { error, paymentMethod } = await presentApplePay({
96
+ cartItems: cart?.products?.map((product: any) => ({ label: product?.name, amount: product?.price?.toString?.() })),
97
+ country: 'US',
98
+ currency: 'USD',
99
+ shippingMethods: [
100
+ {
101
+ amount: cart?.balance?.toString() ?? cart?.total?.toString?.(),
102
+ identifier: 'DPS',
103
+ label: 'Courier',
104
+ detail: 'Delivery',
105
+ type: 'final',
106
+ },
107
+ ],
108
+
109
+ requiredShippingAddressFields: ['emailAddress', 'phoneNumber'],
110
+ requiredBillingContactFields: ['phoneNumber', 'name'],
111
+ });
112
+ if (error) {
113
+ setErrors(error.code + ' - ' + error.message);
114
+ } else if (paymentMethod) {
115
+ handleSource({
116
+ ...paymentMethod?.Card,
117
+ id: paymentMethod.id,
118
+ type: paymentMethod.type,
119
+ source_id: paymentMethod?.id,
120
+ card: {
121
+ brand: paymentMethod.Card.brand,
122
+ last4: paymentMethod.Card.last4
123
+ }
124
+ })
125
+ onCancel()
126
+ }
127
+ }
128
+
129
+ return (
130
+ <>
131
+ {paymethod === 'google_pay' ? (
132
+ <View>
133
+ {!loading && initialized && (
134
+ <OButton
135
+ textStyle={{
136
+ color: '#fff'
137
+ }}
138
+ imgRightSrc={null}
139
+ onClick={createPaymentMethod}
140
+ isDisabled={!initialized}
141
+ text={t('PAY_WITH_GOOGLE_PAY', 'Pay with Google Pay')}
142
+ />
143
+ )}
144
+ </View>
145
+ ) : (
146
+ <View>
147
+ {isApplePaySupported && (
148
+ <ApplePayButton
149
+ onPress={pay}
150
+ type="plain"
151
+ buttonStyle="black"
152
+ borderRadius={4}
153
+ style={{
154
+ width: '100%',
155
+ height: 50,
156
+ }}
157
+ />
158
+ )}
159
+ </View>
160
+ )}
161
+ </>
162
+ )
163
+ }
package/src/config.json CHANGED
@@ -3,6 +3,8 @@
3
3
  "notification_app": "orderingapp",
4
4
  "app_name": "Ordering",
5
5
  "project": "reactdemo",
6
+ "apple_app_id": "com.delivery",
7
+ "android_app_id": "com.delivery",
6
8
  "api": {
7
9
  "url": "https://apiv4.ordering.co",
8
10
  "language": "en",
@@ -473,3 +473,12 @@ export interface HelpGuideParams {
473
473
  export interface HelpAccountAndPaymentParams {
474
474
  navigation: any;
475
475
  }
476
+
477
+ export interface StripeMethodFormParams {
478
+ cart: any;
479
+ handleSource: ({id, card} : {id : string, card : any}) => void;
480
+ onCancel: () => void;
481
+ setErrors: (error: string) => void;
482
+ paymethod: string;
483
+ devMode?: boolean;
484
+ }