diff --git a/ios/CardFieldManager.m b/ios/CardFieldManager.m index 6ea66092b..25c62d4c7 100644 --- a/ios/CardFieldManager.m +++ b/ios/CardFieldManager.m @@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(CardFieldManager, RCTViewManager) RCT_EXPORT_VIEW_PROPERTY(postalCodeEnabled, BOOL) RCT_EXPORT_VIEW_PROPERTY(countryCode, NSString) +RCT_EXPORT_VIEW_PROPERTY(defaultValues, NSDictionary) RCT_EXPORT_VIEW_PROPERTY(onCardChange, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onFocusChange, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(cardStyle, NSDictionary) diff --git a/ios/CardFieldView.swift b/ios/CardFieldView.swift index 0f9284381..b553ace65 100644 --- a/ios/CardFieldView.swift +++ b/ios/CardFieldView.swift @@ -24,6 +24,13 @@ class CardFieldView: UIView, STPPaymentCardTextFieldDelegate { } } + @objc var defaultValues: NSDictionary? { + didSet { + cardField.cardParams = Mappers.mapToCardParams(defaultValues) + cardField.postalCode = defaultValues?["postalCode"] as? String + } + } + @objc var placeholders: NSDictionary = NSDictionary() { didSet { if let numberPlaceholder = placeholders["number"] as? String { diff --git a/ios/CardFormManager.m b/ios/CardFormManager.m index 22ad6b356..369fc1c5e 100644 --- a/ios/CardFormManager.m +++ b/ios/CardFormManager.m @@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(CardFormManager, RCTViewManager) RCT_EXPORT_VIEW_PROPERTY(onFormComplete, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(dangerouslyGetFullCardDetails, BOOL) +RCT_EXPORT_VIEW_PROPERTY(defaultValues, NSDictionary) RCT_EXPORT_VIEW_PROPERTY(autofocus, BOOL) RCT_EXPORT_VIEW_PROPERTY(isUserInteractionEnabledValue, BOOL) RCT_EXTERN_METHOD(focus:(nonnull NSNumber*) reactTag) diff --git a/ios/CardFormView.swift b/ios/CardFormView.swift index f0bc39b2a..62e3b6dd2 100644 --- a/ios/CardFormView.swift +++ b/ios/CardFormView.swift @@ -11,7 +11,8 @@ class CardFormView: UIView, STPCardFormViewDelegate { @objc var onFormComplete: RCTDirectEventBlock? @objc var autofocus: Bool = false @objc var isUserInteractionEnabledValue: Bool = true - + @objc var defaultValues: NSDictionary? + override func didSetProps(_ changedProps: [String]!) { if let cardForm = self.cardForm { cardForm.removeFromSuperview() @@ -29,6 +30,7 @@ class CardFormView: UIView, STPCardFormViewDelegate { self.cardForm = _cardForm self.addSubview(_cardForm) setStyles() + setDefaultValues() } @objc var cardStyle: NSDictionary = NSDictionary() { @@ -36,7 +38,7 @@ class CardFormView: UIView, STPCardFormViewDelegate { setStyles() } } - + func cardFormView(_ form: STPCardFormView, didChangeToStateComplete complete: Bool) { if onFormComplete != nil { let brand = STPCardValidator.brand(forNumber: cardForm?.cardParams?.card?.number ?? "") @@ -100,6 +102,20 @@ class CardFormView: UIView, STPCardFormViewDelegate { // } } + func setDefaultValues() { + guard let cardForm = cardForm else { return } + + let card = Mappers.mapToCardParams(defaultValues) + + let address = STPPaymentMethodAddress() + address.postalCode = defaultValues?["postalCode"] as? String + + let billingDetails = STPPaymentMethodBillingDetails() + billingDetails.address = address + + cardForm.cardParams = STPPaymentMethodParams.init(card: card, billingDetails: billingDetails, metadata: nil) + } + override init(frame: CGRect) { super.init(frame: frame) } diff --git a/ios/Mappers.swift b/ios/Mappers.swift index 25af05059..a92fd0e6a 100644 --- a/ios/Mappers.swift +++ b/ios/Mappers.swift @@ -986,4 +986,17 @@ class Mappers { default: return STPPaymentMethodUSBankAccountType.checking } } + + class func mapToCardParams(_ input: NSDictionary?) -> STPPaymentMethodCardParams { + let params = STPPaymentMethodCardParams() + params.cvc = input?["cvc"] as? String + params.number = input?["number"] as? String + if let month = input?["expiryMonth"] as? String { + params.expMonth = NSNumber(value: Int(month) ?? 0) + } + if let year = input?["expiryYear"] as? String { + params.expYear = NSNumber(value: Int(year) ?? 0) + } + return params + } }