From bf363b8b89e2771617bcaec3a4985e3125e042d7 Mon Sep 17 00:00:00 2001 From: Charles Cruzan Date: Tue, 8 Feb 2022 09:54:57 -0500 Subject: [PATCH] fix: default to 'Individual' bank account type --- .../java/com/reactnativestripesdk/Mappers.kt | 4 +- ios/Mappers.swift | 181 +++++++++--------- ios/StripeSdk.swift | 4 +- 3 files changed, 92 insertions(+), 97 deletions(-) diff --git a/android/src/main/java/com/reactnativestripesdk/Mappers.kt b/android/src/main/java/com/reactnativestripesdk/Mappers.kt index 4412fb2c0..0cf9f19af 100644 --- a/android/src/main/java/com/reactnativestripesdk/Mappers.kt +++ b/android/src/main/java/com/reactnativestripesdk/Mappers.kt @@ -171,11 +171,11 @@ internal fun mapFromBankAccountType(type: BankAccount.Type?): String { } } -internal fun mapToBankAccountType(type: String?): BankAccountTokenParams.Type? { +internal fun mapToBankAccountType(type: String?): BankAccountTokenParams.Type { return when (type) { "Company" -> BankAccountTokenParams.Type.Company "Individual" -> BankAccountTokenParams.Type.Individual - else -> null + else -> BankAccountTokenParams.Type.Individual } } diff --git a/ios/Mappers.swift b/ios/Mappers.swift index 09addb94c..458429971 100644 --- a/ios/Mappers.swift +++ b/ios/Mappers.swift @@ -4,7 +4,7 @@ class Mappers { class func createResult(_ key: String, _ value: NSDictionary?) -> NSDictionary { return [key: value ?? NSNull()] } - + class func mapToPKContactField(field: String) -> PKContactField { switch field { case "emailAddress": return PKContactField.emailAddress @@ -15,7 +15,7 @@ class Mappers { default: return PKContactField.name } } - + class func mapToPaymentSummaryItemType(type: String?) -> PKPaymentSummaryItemType { if let type = type { switch type { @@ -26,7 +26,7 @@ class Mappers { } return PKPaymentSummaryItemType.final } - + class func mapFromBankAccountHolderType(_ type: STPBankAccountHolderType?) -> String? { if let type = type { switch type { @@ -37,18 +37,15 @@ class Mappers { } return nil } - - class func mapToBankAccountHolderType(_ type: String?) -> STPBankAccountHolderType? { - if let type = type { - switch type { - case "Company": return STPBankAccountHolderType.company - case "Individual": return STPBankAccountHolderType.individual - default: return nil - } + + class func mapToBankAccountHolderType(_ type: String?) -> STPBankAccountHolderType { + switch type { + case "Company": return STPBankAccountHolderType.company + case "Individual": return STPBankAccountHolderType.individual + default: return STPBankAccountHolderType.individual } - return nil } - + class func mapFromBankAccountStatus(_ status: STPBankAccountStatus?) -> String? { if let status = status { switch status { @@ -62,7 +59,7 @@ class Mappers { } return nil } - + class func mapFromBankAccount(_ bankAccount: STPBankAccount?) -> NSDictionary? { if (bankAccount == nil) { return nil @@ -79,7 +76,7 @@ class Mappers { ] return result } - + class func mapFromCard(_ card: STPCard?) -> NSDictionary? { if (card == nil) { return nil @@ -97,7 +94,7 @@ class Mappers { ] return cardMap } - + class func mapFromAddress(address: STPAddress?) -> NSDictionary { let result: NSDictionary = [ "city": address?.city ?? NSNull(), @@ -107,10 +104,10 @@ class Mappers { "line2": address?.line2 ?? NSNull(), "state": address?.state ?? NSNull(), ] - + return result } - + class func mapToAddress(address: NSDictionary?) -> STPAddress { let result = STPAddress() result.city = address?["city"] as? String @@ -119,10 +116,10 @@ class Mappers { result.line2 = address?["line2"] as? String result.postalCode = address?["postalCode"] as? String result.state = address?["state"] as? String - + return result } - + class func mapFromFunding(_ funding: STPCardFundingType?) -> String? { if let funding = funding { switch funding { @@ -136,7 +133,7 @@ class Mappers { } return nil } - + class func mapFromTokenType(_ type: STPTokenType?) -> String? { if let type = type { switch type { @@ -150,7 +147,7 @@ class Mappers { } return nil } - + class func mapFromToken(token: STPToken) -> NSDictionary { let tokenMap: NSDictionary = [ "id": token.tokenId, @@ -160,13 +157,13 @@ class Mappers { "livemode": token.livemode, "type": mapFromTokenType(token.type) ?? NSNull(), ] - + return tokenMap } - + class func mapToShippingMethods(shippingMethods: NSArray?) -> [PKShippingMethod] { var shippingMethodsList: [PKShippingMethod] = [] - + if let methods = shippingMethods as? [[String : Any]] { for method in methods { let label = method["label"] as? String ?? "" @@ -180,10 +177,10 @@ class Mappers { shippingMethodsList.append(pm) } } - + return shippingMethodsList } - + class func mapFromShippingMethod(shippingMethod: PKShippingMethod) -> NSDictionary { let method: NSDictionary = [ "detail": shippingMethod.detail ?? "", @@ -192,10 +189,10 @@ class Mappers { "type": shippingMethod.type, "label": shippingMethod.label ] - + return method } - + class func mapFromShippingContact(shippingContact: PKContact) -> NSDictionary { let name: NSDictionary = [ "familyName": shippingContact.name?.familyName ?? "", @@ -220,10 +217,10 @@ class Mappers { "subLocality": shippingContact.postalAddress?.subLocality, ], ] - + return contact } - + class func mapAddressFields(_ addressFields: [String]) -> [String] { return addressFields.map { if ($0 == "street") { @@ -246,7 +243,7 @@ class Mappers { return "" } } - + class func mapIntentStatus(status: STPPaymentIntentStatus?) -> String { if let status = status { switch status { @@ -262,7 +259,7 @@ class Mappers { } return "Unknown" } - + class func mapPaymentMethodType(type: STPPaymentMethodType) -> String { switch type { case STPPaymentMethodType.card: return "Card" @@ -286,7 +283,7 @@ class Mappers { default: return "Unknown" } } - + class func mapToPaymentMethodType(type: String?) -> STPPaymentMethodType? { if let type = type { switch type { @@ -313,7 +310,7 @@ class Mappers { } return nil } - + class func mapCaptureMethod(_ captureMethod: STPPaymentIntentCaptureMethod?) -> String { if let captureMethod = captureMethod { switch captureMethod { @@ -324,7 +321,7 @@ class Mappers { } return "Unknown" } - + class func mapConfirmationMethod(_ confirmationMethod: STPPaymentIntentConfirmationMethod?) -> String { if let confirmationMethod = confirmationMethod { switch confirmationMethod { @@ -335,7 +332,7 @@ class Mappers { } return "Unknown" } - + class func mapIntentShipping(_ shipping: STPPaymentIntentShippingDetails) -> NSDictionary { var addressDetails = NSDictionary() if let address = shipping.address { @@ -356,7 +353,7 @@ class Mappers { ] return shippingDetails } - + class func mapFromPaymentIntent (paymentIntent: STPPaymentIntent) -> NSDictionary { let intent: NSMutableDictionary = [ "id": paymentIntent.stripeId, @@ -375,7 +372,7 @@ class Mappers { "shipping": NSNull(), "canceledAt": NSNull() ] - + if let lastPaymentError = paymentIntent.lastPaymentError { let paymentError: NSMutableDictionary = [ "code": lastPaymentError.code ?? NSNull(), @@ -384,21 +381,21 @@ class Mappers { "declineCode": lastPaymentError.declineCode ?? NSNull(), "paymentMethod": mapFromPaymentMethod(lastPaymentError.paymentMethod) ?? NSNull() ] - + intent.setValue(paymentError, forKey: "lastPaymentError") } - + if let shipping = paymentIntent.shipping { intent.setValue(mapIntentShipping(shipping), forKey: "shipping") } - + if let canceledAt = paymentIntent.canceledAt { intent.setValue(convertDateToUnixTimestamp(date: canceledAt), forKey: "canceledAt") } - + return intent; } - + class func mapFromPaymentIntentLastPaymentErrorType(_ errorType: STPPaymentIntentLastPaymentErrorType?) -> String? { if let errorType = errorType { switch errorType { @@ -415,7 +412,7 @@ class Mappers { } return nil } - + class func mapFromSetupIntentLastPaymentErrorType(_ errorType: STPSetupIntentLastSetupErrorType?) -> String? { if let errorType = errorType { switch errorType { @@ -432,7 +429,7 @@ class Mappers { } return nil } - + class func mapToBillingDetails(billingDetails: NSDictionary?) -> STPPaymentMethodBillingDetails? { guard let billingDetails = billingDetails else { return nil @@ -441,39 +438,39 @@ class Mappers { billing.email = RCTConvert.nsString(billingDetails["email"]) billing.phone = RCTConvert.nsString(billingDetails["phone"]) billing.name = RCTConvert.nsString(billingDetails["name"]) - + let billingAddres = STPPaymentMethodAddress() - + billingAddres.city = RCTConvert.nsString(billingDetails["addressCity"]) billingAddres.postalCode = RCTConvert.nsString(billingDetails["addressPostalCode"]) billingAddres.country = RCTConvert.nsString(billingDetails["addressCountry"]) billingAddres.line1 = RCTConvert.nsString(billingDetails["addressLine1"]) billingAddres.line2 = RCTConvert.nsString(billingDetails["addressLine2"]) billingAddres.state = RCTConvert.nsString(billingDetails["addressState"]) - + billing.address = billingAddres - + return billing } - + class func mapToShippingDetails(shippingDetails: NSDictionary?) -> STPPaymentIntentShippingDetailsParams? { guard let shippingDetails = shippingDetails else { return nil } let shippingAddress = STPPaymentIntentShippingDetailsAddressParams(line1: shippingDetails["addressLine1"] as? String ?? "") - + shippingAddress.city = shippingDetails["addressCity"] as? String shippingAddress.postalCode = shippingDetails["addressPostalCode"] as? String shippingAddress.country = shippingDetails["addressCountry"] as? String shippingAddress.line1 = shippingDetails["addressLine1"] as? String ?? "" shippingAddress.line2 = shippingDetails["addressLine2"] as? String shippingAddress.state = shippingDetails["addressState"] as? String - + let shipping = STPPaymentIntentShippingDetailsParams(address: shippingAddress, name: shippingDetails["name"] as? String ?? "") - + return shipping } - + class func mapFromBillingDetails(billingDetails: STPPaymentMethodBillingDetails?) -> NSDictionary { let billing: NSDictionary = [ "email": billingDetails?.email ?? NSNull(), @@ -488,10 +485,10 @@ class Mappers { "state": billingDetails?.address?.state, ], ] - + return billing } - + class func mapCardBrand(_ brand: STPCardBrand?) -> String? { if let brand = brand { switch brand { @@ -508,7 +505,7 @@ class Mappers { } return nil } - + class func mapFromPaymentMethod(_ paymentMethod: STPPaymentMethod?) -> NSDictionary? { guard let paymentMethod = paymentMethod else { return nil @@ -564,7 +561,7 @@ class Mappers { ] return method } - + class func mapIntentStatus(status: STPSetupIntentStatus?) -> String { if let status = status { switch status { @@ -580,7 +577,7 @@ class Mappers { } return "Unknown" } - + class func mapFromSetupIntentUsage(usage: STPSetupIntentUsage?) -> String { if let usage = usage { switch usage { @@ -593,7 +590,7 @@ class Mappers { } return "Unknown" } - + class func mapToPaymentIntentFutureUsage(usage: String?) -> STPPaymentIntentSetupFutureUsage { if let usage = usage { switch usage { @@ -606,7 +603,7 @@ class Mappers { } return STPPaymentIntentSetupFutureUsage.unknown } - + class func mapFromSetupIntent(setupIntent: STPSetupIntent) -> NSDictionary { let intent: NSMutableDictionary = [ "id": setupIntent.stripeID, @@ -620,15 +617,15 @@ class Mappers { "created": NSNull(), "lastSetupError": NSNull() ] - - + + let types = setupIntent.paymentMethodTypes.map { mapPaymentMethodType(type: STPPaymentMethodType.init(rawValue: Int(truncating: $0))!) } - + intent.setValue(types, forKey: "paymentMethodTypes") intent.setValue(convertDateToUnixTimestamp(date: setupIntent.created), forKey: "created") - + if let lastSetupError = setupIntent.lastSetupError { let setupError: NSMutableDictionary = [ "code": lastSetupError.code ?? NSNull(), @@ -639,10 +636,10 @@ class Mappers { ] intent.setValue(setupError, forKey: "lastSetupError") } - + return intent } - + @available(iOS 13.0, *) class func mapToUserInterfaceStyle(_ style: String) -> PaymentSheet.UserInterfaceStyle { switch style { @@ -651,11 +648,11 @@ class Mappers { default: return PaymentSheet.UserInterfaceStyle.automatic } } - + class func mapToReturnURL(urlScheme: String) -> String { return urlScheme + "://safepay" } - + class func mapUICustomization(_ params: NSDictionary) -> STPThreeDSUICustomization { let uiCustomization = STPThreeDSUICustomization() if let labelSettings = params["label"] as? Dictionary { @@ -672,7 +669,7 @@ class Mappers { uiCustomization.labelCustomization.font = UIFont.systemFont(ofSize: CGFloat(textFontSize)) } } - + if let navigationBarSettings = params["navigationBar"] as? Dictionary { if let barTintColor = navigationBarSettings["barTintColor"] as? String { uiCustomization.navigationBarCustomization.barTintColor = UIColor(hexString: barTintColor) @@ -696,7 +693,7 @@ class Mappers { uiCustomization.navigationBarCustomization.translucent = translucent } } - + if let textFieldSettings = params["textField"] as? Dictionary { if let borderColor = textFieldSettings["borderColor"] as? String { uiCustomization.textFieldCustomization.borderColor = UIColor(hexString: borderColor) @@ -714,7 +711,7 @@ class Mappers { uiCustomization.textFieldCustomization.font = UIFont.systemFont(ofSize: CGFloat(textFontSize)) } } - + if let footerSettings = params["footer"] as? Dictionary { if let backgroundColor = footerSettings["backgroundColor"] as? String { uiCustomization.footerCustomization.backgroundColor = UIColor(hexString: backgroundColor) @@ -729,10 +726,10 @@ class Mappers { uiCustomization.footerCustomization.textColor = UIColor(hexString: textColor) } } - + if let submitButtonSettings = params["submitButton"] as? Dictionary { let buttonCustomization = uiCustomization.buttonCustomization(for: STPThreeDSCustomizationButtonType.submit) - + if let backgroundColor = submitButtonSettings["backgroundColor"] as? String { buttonCustomization.backgroundColor = UIColor(hexString: backgroundColor) } @@ -745,13 +742,13 @@ class Mappers { if let textColor = submitButtonSettings["textColor"] as? String { buttonCustomization.textColor = UIColor(hexString: textColor) } - + uiCustomization.setButtonCustomization(buttonCustomization, for: STPThreeDSCustomizationButtonType.submit) } - + if let submitButtonSettings = params["cancelButton"] as? Dictionary { let buttonCustomization = uiCustomization.buttonCustomization(for: STPThreeDSCustomizationButtonType.cancel) - + if let backgroundColor = submitButtonSettings["backgroundColor"] as? String { buttonCustomization.backgroundColor = UIColor(hexString: backgroundColor) } @@ -764,13 +761,13 @@ class Mappers { if let textColor = submitButtonSettings["textColor"] as? String { buttonCustomization.textColor = UIColor(hexString: textColor) } - + uiCustomization.setButtonCustomization(buttonCustomization, for: STPThreeDSCustomizationButtonType.cancel) } - + if let submitButtonSettings = params["continueButton"] as? Dictionary { let buttonCustomization = uiCustomization.buttonCustomization(for: STPThreeDSCustomizationButtonType.continue) - + if let backgroundColor = submitButtonSettings["backgroundColor"] as? String { buttonCustomization.backgroundColor = UIColor(hexString: backgroundColor) } @@ -783,13 +780,13 @@ class Mappers { if let textColor = submitButtonSettings["textColor"] as? String { buttonCustomization.textColor = UIColor(hexString: textColor) } - + uiCustomization.setButtonCustomization(buttonCustomization, for: STPThreeDSCustomizationButtonType.continue) } - + if let submitButtonSettings = params["nextButton"] as? Dictionary { let buttonCustomization = uiCustomization.buttonCustomization(for: STPThreeDSCustomizationButtonType.next) - + if let backgroundColor = submitButtonSettings["backgroundColor"] as? String { buttonCustomization.backgroundColor = UIColor(hexString: backgroundColor) } @@ -802,13 +799,13 @@ class Mappers { if let textColor = submitButtonSettings["textColor"] as? String { buttonCustomization.textColor = UIColor(hexString: textColor) } - + uiCustomization.setButtonCustomization(buttonCustomization, for: STPThreeDSCustomizationButtonType.next) } - + if let submitButtonSettings = params["resendButton"] as? Dictionary { let buttonCustomization = uiCustomization.buttonCustomization(for: STPThreeDSCustomizationButtonType.resend) - + if let backgroundColor = submitButtonSettings["backgroundColor"] as? String { buttonCustomization.backgroundColor = UIColor(hexString: backgroundColor) } @@ -821,18 +818,18 @@ class Mappers { if let textColor = submitButtonSettings["textColor"] as? String { buttonCustomization.textColor = UIColor(hexString: textColor) } - + uiCustomization.setButtonCustomization(buttonCustomization, for: STPThreeDSCustomizationButtonType.resend) } - + if let backgroundColor = params["backgroundColor"] as? String { uiCustomization.backgroundColor = UIColor(hexString: backgroundColor) } - - + + return uiCustomization } - + class func convertDateToUnixTimestamp(date: Date?) -> String? { if let date = date { let value = date.timeIntervalSince1970 * 1000.0 diff --git a/ios/StripeSdk.swift b/ios/StripeSdk.swift index 8b3b8ca10..1e4c6c42a 100644 --- a/ios/StripeSdk.swift +++ b/ios/StripeSdk.swift @@ -566,10 +566,8 @@ class StripeSdk: RCTEventEmitter, STPApplePayContextDelegate, STPBankSelectionVi bankAccountParams.country = country bankAccountParams.currency = currency bankAccountParams.routingNumber = routingNumber + bankAccountParams.accountHolderType = Mappers.mapToBankAccountHolderType(accountHolderType) - if let holderType = Mappers.mapToBankAccountHolderType(accountHolderType) { - bankAccountParams.accountHolderType = holderType - } STPAPIClient.shared.createToken(withBankAccount: bankAccountParams) { token, error in if let token = token {