Skip to content

Commit

Permalink
ios
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe committed Aug 16, 2022
1 parent a3efb46 commit 35e9481
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions ios/CardFieldManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions ios/CardFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions ios/CardFormManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 18 additions & 2 deletions ios/CardFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -29,14 +30,15 @@ class CardFormView: UIView, STPCardFormViewDelegate {
self.cardForm = _cardForm
self.addSubview(_cardForm)
setStyles()
setDefaultValues()
}

@objc var cardStyle: NSDictionary = NSDictionary() {
didSet {
setStyles()
}
}

func cardFormView(_ form: STPCardFormView, didChangeToStateComplete complete: Bool) {
if onFormComplete != nil {
let brand = STPCardValidator.brand(forNumber: cardForm?.cardParams?.card?.number ?? "")
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 13 additions & 0 deletions ios/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 35e9481

Please sign in to comment.