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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355open ReactNative
open Style
@react.component
let make = (
~paymentMethodData: AccountPaymentMethodType.payment_method_type,
~sessionObject,
~processRequest,
) => {
let (nativeProp, _) = React.useContext(NativePropContext.nativePropContext)
let (_, setLoading) = React.useContext(LoadingContext.loadingContext)
let showAlert = AlertHook.useAlerts()
let logger = LoggerHook.useLoggerHook()
let {
paypalButonColor,
googlePayButtonColor,
applePayButtonColor,
buttonBorderRadius,
primaryButtonHeight,
payNowButtonTextColor,
} = ThemebasedStyle.useThemeBasedStyle()
let handleWalletPayments = ButtonHook.useProcessPayButtonResult()
let {getRequiredFieldsForButton} = React.useContext(DynamicFieldsContext.dynamicFieldsContext)
let processWalletData = (
walletDict,
~billingAddress=?,
~shippingAddress=?,
~useIntentData=false,
) => {
let (isFieldsMissing, initialValues) = getRequiredFieldsForButton(
paymentMethodData,
walletDict,
billingAddress,
shippingAddress,
useIntentData,
)
if !isFieldsMissing {
processRequest(
initialValues, //CommonUtils.mergeDict(initialValues, formData),
Some(walletDict),
// formData
initialValues->Dict.get("email")->Option.mapOr(None, JSON.Decode.string),
)
} else {
setLoading(FillingDetails)
}
}
// React.useEffect1(() => {
// if formData->Dict.toArray->Array.length > 0 {
// let eligibleConnectors = switch paymentMethodData.payment_method {
// | CARD =>
// paymentMethodData.card_networks
// ->Array.get(0)
// ->Option.mapOr([], network => network.eligible_connectors)
// | _ =>
// paymentMethodData.payment_experience
// ->Array.get(0)
// ->Option.mapOr([], experience => experience.eligible_connectors)
// }
// let configParams: SuperpositionTypes.superpositionBaseContext = {
// payment_method: paymentMethodData.payment_method_str,
// payment_method_type: paymentMethodData.payment_method_type,
// mandate_type: accountPaymentMethodData
// ->Option.map(accountPaymentMethods => accountPaymentMethods.payment_type)
// ->Option.getOr(NORMAL) === NORMAL
// ? "non_mandate"
// : "mandate",
// collect_billing_details_from_wallet_connector: "required",
// collect_shipping_details_from_wallet_connector: "required",
// country,
// }
// let (_requiredFields, missingRequiredFields, _) = getSuperpositionFinalFields(
// eligibleConnectors,
// configParams,
// requiredFieldsFromSource,
// )
// setWalletData(missingRequiredFields, formData, walletDict, requiredFieldsFromSource)
// }
// None
// }, [country])
let confirmPayPal = var => {
let status = handleWalletPayments(PAYPAL, var)
switch status {
| Success(walletData, billingAddress, shippingAddress) =>
processWalletData(walletData, ~billingAddress?, ~shippingAddress?)
| Cancelled | Simulated =>
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message="Payment was Cancelled")
| Failed(error_message) => showAlert(~errorType="error", ~message=error_message)
}
}
let confirmGPay = var => {
let status = handleWalletPayments(GOOGLE_PAY, var)
switch status {
| Success(walletData, billingAddress, shippingAddress) =>
processWalletData(walletData, ~billingAddress?, ~shippingAddress?)
| Cancelled | Simulated =>
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message="Payment was Cancelled")
| Failed(error_message) =>
setLoading(FillingDetails)
showAlert(~errorType="error", ~message=error_message)
}
}
let _confirmSamsungPay = (var, billingAddress, shippingAddress) => {
let status = handleWalletPayments(SAMSUNG_PAY, var)
switch status {
| Success(walletData, _, _) =>
processWalletData(walletData, ~billingAddress?, ~shippingAddress?)
| Cancelled | Simulated =>
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message=`Samsung Pay Error, Please try again.`)
| Failed(error_message) =>
setLoading(FillingDetails)
showAlert(~errorType="error", ~message=`Samsung Pay Error, Please try again ${error_message}`)
}
logger(
~logType=INFO,
~value=`SPAY result from native`,
~category=USER_EVENT,
~eventName=SAMSUNG_PAY,
(),
)
}
let confirmApplePay = (var: dict<JSON.t>) => {
logger(
~logType=DEBUG,
~value=paymentMethodData.payment_method_type,
~category=USER_EVENT,
~paymentMethod=paymentMethodData.payment_method_type,
~eventName=APPLE_PAY_CALLBACK_FROM_NATIVE,
~paymentExperience=paymentMethodData.payment_experience,
(),
)
let status = handleWalletPayments(APPLE_PAY, var)
switch status {
| Success(walletData, billingAddress, shippingAddress) =>
processWalletData(walletData, ~billingAddress?, ~shippingAddress?)
| Cancelled =>
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message="Cancelled")
| Simulated => setTimeout(() => {
setLoading(FillingDetails)
showAlert(
~errorType="warning",
~message="Apple Pay is not supported in Simulated Environment",
)
}, 2000)->ignore
| Failed(error_message) =>
setLoading(FillingDetails)
showAlert(~errorType="error", ~message=error_message)
}
}
let pressHandler = () => {
setLoading(ProcessingPayments)
logger(
~logType=INFO,
~value=paymentMethodData.payment_method_type,
~category=USER_EVENT,
~paymentMethod=paymentMethodData.payment_method_type,
~eventName=PAYMENT_METHOD_CHANGED,
~paymentExperience=paymentMethodData.payment_experience,
(),
)
switch paymentMethodData.payment_method_type_wallet {
| GOOGLE_PAY =>
HyperModule.launchGPay(
WalletType.getGpayTokenStringified(~obj=sessionObject, ~appEnv=nativeProp.env),
confirmGPay,
)
| PAYPAL =>
if (
sessionObject.session_token !== "" &&
WebKit.platform == #android &&
PaypalModule.payPalModule->Option.isSome
) {
PaypalModule.launchPayPal(sessionObject.session_token, confirmPayPal)
} else if (
paymentMethodData.payment_experience
->Array.find(exp => exp.payment_experience_type_decode == REDIRECT_TO_URL)
->Option.isSome
) {
let redirectData = []->Dict.fromArray->JSON.Encode.object
let payment_method_data = [
(
paymentMethodData.payment_method_str,
[(paymentMethodData.payment_method_type ++ "_redirect", redirectData)]
->Dict.fromArray
->JSON.Encode.object,
),
]->Dict.fromArray
processWalletData(payment_method_data)
} else {
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message="Payment Method Unavailable")
}
| APPLE_PAY =>
if (
sessionObject.session_token_data == JSON.Encode.null ||
sessionObject.payment_request_data == JSON.Encode.null
) {
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message="Waiting for Sessions API")
} else {
logger(
~logType=DEBUG,
~value=paymentMethodData.payment_method_type,
~category=USER_EVENT,
~paymentMethod=paymentMethodData.payment_method_type,
~eventName=APPLE_PAY_STARTED_FROM_JS,
~paymentExperience=paymentMethodData.payment_experience,
(),
)
let timerId = setTimeout(() => {
setLoading(FillingDetails)
showAlert(~errorType="warning", ~message="Apple Pay Error, Please try again")
logger(
~logType=DEBUG,
~value=paymentMethodData.payment_method_type,
~category=USER_EVENT,
~paymentMethod=paymentMethodData.payment_method_type,
~eventName=APPLE_PAY_PRESENT_FAIL_FROM_NATIVE,
~paymentExperience=paymentMethodData.payment_experience,
(),
)
}, 5000)
HyperModule.launchApplePay(
[
("session_token_data", sessionObject.session_token_data),
("payment_request_data", sessionObject.payment_request_data),
]
->Dict.fromArray
->JSON.Encode.object
->JSON.stringify,
confirmApplePay,
_ => {
logger(
~logType=DEBUG,
~value=paymentMethodData.payment_method_type,
~category=USER_EVENT,
~paymentMethod=paymentMethodData.payment_method_type,
~eventName=APPLE_PAY_BRIDGE_SUCCESS,
~paymentExperience=paymentMethodData.payment_experience,
(),
)
},
_ => {
clearTimeout(timerId)
},
)
}
| SAMSUNG_PAY =>
logger(
~logType=INFO,
~value="Samsung Pay Button Clicked",
~category=USER_EVENT,
~eventName=SAMSUNG_PAY,
(),
)
// SamsungPayModule.presentSamsungPayPaymentSheet(confirmSamsungPay)
| _ => {
setLoading(FillingDetails)
processWalletData(Dict.make(), ~useIntentData=true)
}
}
}
React.useEffect1(() => {
switch paymentMethodData.payment_method_type_wallet {
| APPLE_PAY => Window.registerEventListener("applePayData", confirmApplePay)
| GOOGLE_PAY => Window.registerEventListener("googlePayData", confirmGPay)
| _ => ()
}
None
}, [paymentMethodData.payment_method_type_wallet])
let buttonName = paymentMethodData.payment_method_type->CommonUtils.getDisplayName
<>
<CustomButton
text={paymentMethodData.payment_method_type->CommonUtils.getDisplayName}
borderRadius=buttonBorderRadius
leftIcon=CustomIcon(<Icon name=buttonName width=24. height=32. fill=payNowButtonTextColor />)
onPress={_ => pressHandler()}
>
{switch paymentMethodData.payment_method_type_wallet {
| SAMSUNG_PAY =>
Some(
<View
style={s({
display: #flex,
flexDirection: #row,
alignItems: #center,
justifyContent: #center,
width: 100.->pct,
height: 100.->pct,
})}
>
<Icon name=buttonName width=240. height=60. />
</View>,
)
| APPLE_PAY =>
Some(
<ApplePayButtonView
style={s({height: primaryButtonHeight->dp, width: 100.->pct})}
cornerRadius=buttonBorderRadius
buttonType=nativeProp.configuration.appearance.applePay.buttonType
buttonStyle=applePayButtonColor
/>,
)
| GOOGLE_PAY =>
Some(
<GooglePayButtonView
allowedPaymentMethods={WalletType.getAllowedPaymentMethods(~obj=sessionObject)}
style={s({height: primaryButtonHeight->dp, width: 100.->pct})}
buttonType=nativeProp.configuration.appearance.googlePay.buttonType
buttonStyle=googlePayButtonColor
borderRadius={buttonBorderRadius}
/>,
)
| PAYPAL => Some(<GenericButtonElement buttonName width=80. color=paypalButonColor />)
// | SKRILL => Some(<GenericButtonElement buttonName width=42. color="#910590" />)
// | PAY_SAFE_CARD => Some(<GenericButtonElement buttonName width=92. color="#008ac9" />)
// | KLARNA => Some(<GenericButtonElement buttonName width=92. height=32. color="#0B051D" />)
| _ => None
}}
</CustomButton>
<Space height=12. />
</>
}