๐Ÿ“ฆ manideepk90 / react-native-hyperswitch-sdk

๐Ÿ“„ CardElement.res ยท 437 lines
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437open ReactNative
open Style
open Validation

type cardFormType = {isZipAvailable: bool}
type viewType = PaymentSheet | CardForm(cardFormType)

module CardBrandAndScanCardIcon = {
  @react.component
  let make = (
    ~isScanCardAvailable,
    ~eligibleCardSchemes,
    ~showCardSchemeDropDown,
    ~cardNumberFilled,
    ~onScanCard,
    ~expireRef,
    ~cvvRef,
    ~cardBrand,
    ~setCardBrand,
  ) => {
    <View style={s({flexDirection: #row, alignItems: #center})}>
      <CardSchemeComponent eligibleCardSchemes showCardSchemeDropDown cardBrand setCardBrand />
      <UIUtils.RenderIf condition={isScanCardAvailable && !cardNumberFilled}>
        <ScanCardButton onScanCard expireRef cvvRef />
      </UIUtils.RenderIf>
    </View>
  }
}

@react.component
let make = (
  ~fields: array<SuperpositionTypes.fieldConfig>,
  ~createFieldValidator,
  ~formatValue,
  ~enabledCardSchemes: array<string>=[],
  ~accessible=?,
) => {
  switch (
    fields->Array.get(0),
    fields->Array.get(1),
    fields->Array.get(2),
    fields->Array.get(3),
    fields->Array.get(4),
  ) {
  | (
      Some(cardNumberConfig),
      Some(cardExpiryMonthConfig),
      Some(cardExpiryYearConfig),
      Some(cardCvcConfig),
      Some(cardNetworkConfig),
    ) => {
      let (nativeProp, _) = React.useContext(NativePropContext.nativePropContext)
      let (expireDate, setExpireDate) = React.useState(() => "")

      let {
        component,
        dangerColor,
        borderRadius,
        borderWidth,
        primaryColor,
      } = ThemebasedStyle.useThemeBasedStyle()
      let localeObject = GetLocale.useGetLocalObj()
      let cardRef = React.useRef(Nullable.null)
      let expireRef = React.useRef(Nullable.null)
      let cvvRef = React.useRef(Nullable.null)
      let nullRef = React.useRef(Nullable.null)

      let {input: cardNumberInput, meta: cardNumberMeta} = ReactFinalForm.useField(
        cardNumberConfig.outputPath,
        ~config={
          validate: createFieldValidator(CardNumber),
          format: formatValue(CardNumber),
        },
        (),
      )

      let {input: cardExpiryMonthInput, meta: _cardExpiryMonthMeta} = ReactFinalForm.useField(
        cardExpiryMonthConfig.outputPath,
        ~config={validate: createFieldValidator(CardExpiry(expireDate))},
        (),
      )

      let {input: cardExpiryYearInput, meta: cardExpiryYearMeta} = ReactFinalForm.useField(
        cardExpiryYearConfig.outputPath,
        ~config={validate: createFieldValidator(CardExpiry(expireDate))},
        (),
      )

      let {input: cardNetworkInput, meta: cardNetworkMeta} = ReactFinalForm.useField(
        cardNetworkConfig.outputPath,
        ~config={validate: createFieldValidator(CardNetwork(enabledCardSchemes))},
        (),
      )

      let {input: cardCvcInput, meta: cardCvcMeta} = ReactFinalForm.useField(
        cardCvcConfig.outputPath,
        ~config={validate: createFieldValidator(CardCVC(cardNetworkInput.value->Option.getOr("")))},
        (),
      )

      let (
        (eligibleCardSchemes, showCardSchemeDropDown),
        setCardSchemeVariables,
      ) = React.useState(_ => ([], false))

      let onChangeCardNumber = (
        text,
        expireRef: React.ref<Nullable.t<ReactNative.TextInput.element>>,
      ) => {
        let matchedCardSchemes = text->Validation.clearSpaces->Validation.getAllMatchedCardSchemes

        let isCardCoBadged = matchedCardSchemes->Array.length > 1
        let showCardSchemeDropDown =
          isCardCoBadged && text->Validation.clearSpaces->String.length >= 16

        let currentCardBrand = matchedCardSchemes->Array.get(0)->Option.getOr("")
        let num = formatCardNumber(text, cardType(currentCardBrand))

        setCardSchemeVariables(_ => (matchedCardSchemes, showCardSchemeDropDown))

        if (
          currentCardBrand !== cardNetworkInput.value->Option.getOr("") &&
            matchedCardSchemes->Array.find(v => v === currentCardBrand)->Option.isNone
        ) {
          cardExpiryMonthInput.onChange("")
          cardExpiryYearInput.onChange("")
          cardCvcInput.onChange("")
          setExpireDate(_ => "")
        }
        if num !== cardNumberInput.value->Option.getOr("") {
          cardNumberInput.onChange(num)
          cardNetworkInput.onChange(currentCardBrand)
        }

        let isthisValid = cardValid(num, currentCardBrand)
        let shouldShiftFocusToNextField = isCardNumberEqualsMax(num, currentCardBrand)

        if isthisValid && shouldShiftFocusToNextField {
          switch expireRef.current->Nullable.toOption {
          | None => ()
          | Some(ref) => ref->ReactNative.TextInputElement.focus
          }
        }
      }
      let onChangeCardExpire = (
        text,
        cvvRef: React.ref<Nullable.t<ReactNative.TextInput.element>>,
      ) => {
        let dateExpire = formatCardExpiryNumber(text)

        let (month, year) = dateExpire->splitExpiryDates

        cardExpiryMonthInput.onChange(month)
        cardExpiryYearInput.onChange(year)
        setExpireDate(_ => dateExpire)

        let isthisValid = checkCardExpiry(dateExpire)
        if isthisValid {
          switch cvvRef.current->Nullable.toOption {
          | None => ()
          | Some(ref) => ref->ReactNative.TextInputElement.focus
          }
        }
      }
      let onChangeCvv = (
        text,
        cvvOrZipRef: React.ref<Nullable.t<ReactNative.TextInput.element>>,
      ) => {
        let cvvData = formatCVCNumber(text, cardNetworkInput.value->Option.getOr(""))

        cardCvcInput.onChange(cvvData)

        let isValidCvv = checkCardCVC(cvvData, cardNetworkInput.value->Option.getOr(""))
        let shouldShiftFocusToNextField = checkMaxCardCvv(
          cvvData,
          cardNetworkInput.value->Option.getOr(""),
        )
        if isValidCvv && shouldShiftFocusToNextField {
          switch cvvOrZipRef.current->Nullable.toOption {
          | None => ()
          | Some(ref) => ref->ReactNative.TextInputElement.blur
          }
        }
      }

      let onScanCard = (
        pan,
        expiry,
        expireRef: React.ref<Nullable.t<ReactNative.TextInput.element>>,
        cvvRef: React.ref<Nullable.t<ReactNative.TextInput.element>>,
      ) => {
        let cardBrand = getCardBrand(pan)
        let cardNumber = formatCardNumber(pan, cardType(cardBrand))
        let isCardValid = cardValid(cardNumber, cardBrand)
        let expireDate = formatCardExpiryNumber(expiry)
        let isExpiryValid = checkCardExpiry(expireDate)
        cardNumberInput.onChange(cardNumber)
        cardNetworkInput.onChange(cardBrand)
        let (month, year) = expireDate->splitExpiryDates
        cardExpiryMonthInput.onChange(month)
        cardExpiryYearInput.onChange(year)
        setExpireDate(_ => expireDate)
        switch (isCardValid, isExpiryValid) {
        | (true, true) =>
          switch cvvRef.current->Nullable.toOption {
          | None => ()
          | Some(ref) => ref->ReactNative.TextInputElement.focus
          }
        | (true, false) =>
          switch expireRef.current->Nullable.toOption {
          | None => ()
          | Some(ref) => ref->ReactNative.TextInputElement.focus
          }
        | _ => ()
        }
      }

      <React.Fragment>
        <View style={s({marginBottom: 16.->dp})}>
          <View style={s({width: 100.->pct, borderRadius})}>
            <View style={s({width: 100.->pct})}>
              <CustomInput
                name={TestUtils.cardNumberInputTestId}
                reference=Some(cardRef)
                state={cardNumberInput.value->Option.getOr("")}
                setState={text => onChangeCardNumber(text, expireRef)}
                placeholder=nativeProp.configuration.placeholder.cardNumber
                keyboardType=#"number-pad"
                isValid={cardNumberMeta.error->Option.isNone ||
                !cardNumberMeta.touched ||
                cardNumberMeta.active}
                maxLength=Some(23)
                borderTopLeftRadius=borderRadius
                borderTopRightRadius=borderRadius
                borderBottomWidth=borderWidth
                borderLeftWidth=borderWidth
                borderRightWidth=borderWidth
                borderTopWidth=borderWidth
                borderBottomLeftRadius=0.
                borderBottomRightRadius=0.
                textColor={{
                  cardNumberMeta.error->Option.isNone ||
                  !cardNumberMeta.touched ||
                  cardNumberMeta.active
                }
                  ? component.color
                  : dangerColor}
                enableCrossIcon=false
                iconRight=CustomInput.CustomIcon(
                  <CardBrandAndScanCardIcon
                    isScanCardAvailable=ScanCardModule.isAvailable
                    eligibleCardSchemes
                    showCardSchemeDropDown
                    cardNumberFilled={switch cardNumberInput.value {
                    | None | Some("") => false
                    | _ => true
                    }}
                    onScanCard
                    expireRef
                    cvvRef
                    cardBrand={cardNetworkInput.value->Option.getOr("")}
                    setCardBrand=cardNetworkInput.onChange
                  />,
                )
                onFocus={() => {
                  cardNumberInput.onFocus()
                }}
                onBlur={() => {
                  cardNumberInput.onBlur()
                }}
                onKeyPress={(ev: TextInput.KeyPressEvent.t) => {
                  if (
                    ev.nativeEvent.key == "Backspace" &&
                      cardNumberInput.value->Option.getOr("") == ""
                  ) {
                    switch cardRef.current->Nullable.toOption {
                    | None => ()
                    | Some(ref) => ref->TextInputElement.blur
                    }
                  }
                }}
                animateLabel=localeObject.cardNumberLabel
                ?accessible
              />
            </View>
            <View
              style={s({
                width: 100.->pct,
                flexDirection: localeObject.localeDirection === "rtl" ? #"row-reverse" : #row,
              })}
            >
              <View style={s({width: 50.->pct})}>
                <CustomInput
                  name={TestUtils.expiryInputTestId}
                  reference={Some(expireRef)}
                  state=expireDate
                  setState={text => onChangeCardExpire(text, cvvRef)}
                  placeholder=nativeProp.configuration.placeholder.expiryDate
                  keyboardType=#"number-pad"
                  enableCrossIcon=false
                  isValid={((cardExpiryYearMeta.error->Option.isNone ||
                  !cardExpiryYearMeta.touched ||
                  cardExpiryYearMeta.active) && expireDate->String.length < 7) ||
                    (expireDate->String.length === 7 && checkCardExpiry(expireDate))}
                  maxLength=Some(7)
                  borderTopWidth=0.25
                  borderRightWidth=borderWidth
                  borderTopLeftRadius=0.
                  borderTopRightRadius=0.
                  borderBottomRightRadius=0.
                  borderBottomLeftRadius=borderRadius
                  borderBottomWidth=borderWidth
                  borderLeftWidth=borderWidth
                  textColor={((cardExpiryYearMeta.error->Option.isNone ||
                  !cardExpiryYearMeta.touched ||
                  cardExpiryYearMeta.active) && expireDate->String.length < 7) ||
                    (expireDate->String.length === 7 && checkCardExpiry(expireDate))
                    ? component.color
                    : dangerColor}
                  onFocus={() => {
                    cardExpiryYearInput.onFocus()
                  }}
                  onBlur={() => {
                    cardExpiryYearInput.onBlur()
                  }}
                  onKeyPress={(ev: TextInput.KeyPressEvent.t) => {
                    if ev.nativeEvent.key == "Backspace" && expireDate == "" {
                      switch cardRef.current->Nullable.toOption {
                      | None => ()
                      | Some(ref) => ref->TextInputElement.focus
                      }
                    }
                  }}
                  animateLabel=localeObject.validThruText
                  ?accessible
                />
              </View>
              <View style={s({width: 50.->pct})}>
                <CustomInput
                  name={TestUtils.cvcInputTestId}
                  reference={Some(cvvRef)}
                  borderTopWidth=0.25
                  borderLeftWidth=0.5
                  borderTopLeftRadius=0.
                  borderTopRightRadius=0.
                  borderBottomLeftRadius=0.
                  borderBottomRightRadius=borderRadius
                  borderBottomWidth=borderWidth
                  borderRightWidth=borderWidth
                  secureTextEntry=true
                  state={cardCvcInput.value->Option.getOr("")}
                  isValid={cardCvcMeta.error->Option.isNone ||
                  !cardCvcMeta.touched ||
                  cardCvcMeta.active}
                  maxLength=Some(4)
                  setState={text => onChangeCvv(text, nullRef)}
                  placeholder=nativeProp.configuration.placeholder.cvv
                  keyboardType=#"number-pad"
                  enableCrossIcon=false
                  onFocus={() => {
                    cardCvcInput.onFocus()
                  }}
                  onBlur={() => {
                    cardCvcInput.onBlur()
                  }}
                  textColor={cardCvcMeta.error->Option.isNone ||
                  !cardCvcMeta.touched ||
                  cardCvcMeta.active
                    ? component.color
                    : dangerColor}
                  iconRight=CustomIcon(
                    <View
                      style={s({
                        height: 46.->dp,
                        display: #flex,
                        flexDirection: #row,
                        justifyContent: #center,
                        alignItems: #center,
                      })}
                    >
                      <Icon
                        name="cvv"
                        height=32.
                        width=32.
                        fill={checkCardCVC(
                          cardCvcInput.value->Option.getOr(""),
                          cardNetworkInput.value->Option.getOr(""),
                        )
                          ? primaryColor
                          : "#858F97"}
                      />
                    </View>,
                  )
                  onKeyPress={(ev: TextInput.KeyPressEvent.t) => {
                    if (
                      ev.nativeEvent.key == "Backspace" &&
                        cardCvcInput.value->Option.getOr("") == ""
                    ) {
                      switch expireRef.current->Nullable.toOption {
                      | None => ()
                      | Some(ref) => ref->TextInputElement.focus
                      }
                    }
                  }}
                  animateLabel=localeObject.cvcTextLabel
                  ?accessible
                />
              </View>
            </View>
          </View>
          {switch (cardNumberMeta.error, cardNumberMeta.touched) {
          | (Some(error), true) => <ErrorText text={Some(error)} />
          | _ =>
            switch (
              cardExpiryYearMeta.error,
              (expireDate->String.length > 0 || !cardExpiryYearMeta.touched) &&
                (expireDate->String.length < 7 || checkCardExpiry(expireDate)),
            ) {
            | (Some(error), false) => <ErrorText text={Some(error)} />
            | _ =>
              switch (cardCvcMeta.error, cardCvcMeta.touched, cardCvcMeta.active) {
              | (Some(error), true, false) => <ErrorText text={Some(error)} />
              | _ =>
                switch (cardNetworkMeta.error, cardNetworkMeta.touched) {
                | (Some(error), true) => <ErrorText text={Some(error)} />
                | _ => React.null
                }
              }
            }
          }}
        </View>
      </React.Fragment>
    }
  | _ => React.null
  }
}