diff --git a/android/src/main/java/com/reactnativestripesdk/Mappers.kt b/android/src/main/java/com/reactnativestripesdk/Mappers.kt index bc41f6e8b..0cf9f19af 100644 --- a/android/src/main/java/com/reactnativestripesdk/Mappers.kt +++ b/android/src/main/java/com/reactnativestripesdk/Mappers.kt @@ -171,6 +171,14 @@ internal fun mapFromBankAccountType(type: BankAccount.Type?): String { } } +internal fun mapToBankAccountType(type: String?): BankAccountTokenParams.Type { + return when (type) { + "Company" -> BankAccountTokenParams.Type.Company + "Individual" -> BankAccountTokenParams.Type.Individual + else -> BankAccountTokenParams.Type.Individual + } +} + internal fun mapFromBankAccountStatus(status: BankAccount.Status?): String { return when (status) { BankAccount.Status.Errored -> "Errored" diff --git a/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt b/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt index 91a579dfd..cef09eacc 100644 --- a/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt +++ b/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt @@ -23,8 +23,7 @@ import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher import com.stripe.android.model.* import com.stripe.android.paymentsheet.PaymentSheetResult import com.stripe.android.view.AddPaymentMethodActivityStarter -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.* @ReactModule(name = StripeSdkModule.NAME) class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { @@ -404,21 +403,60 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ @ReactMethod fun createToken(params: ReadableMap, promise: Promise) { - val type = getValOr(params, "type", null)?.let { - if (it != "Card") { - promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "$it type is not supported yet")) - return + val type = getValOr(params, "type", null) + if (type == null) { + promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "type parameter is required")) + return + } + + when (type) { + "BankAccount" -> { + createTokenFromBankAccount(params, promise) + } + "Card" -> { + createTokenFromCard(params, promise) + } + else -> { + promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "$type type is not supported yet")) } } - val address = getMapOrNull(params, "address") + } - val cardParamsMap = (cardFieldView?.cardParams ?: cardFormView?.cardParams)?.toParamMap() ?: run { - promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "Card details not complete")) - return + private fun createTokenFromBankAccount(params: ReadableMap, promise: Promise) { + val accountHolderName = getValOr(params, "accountHolderName") + val accountHolderType = getValOr(params, "accountHolderType") + val accountNumber = getValOr(params, "accountNumber", null) + val country = getValOr(params, "country", null) + val currency = getValOr(params, "currency", null) + val routingNumber = getValOr(params, "routingNumber") + + runCatching { + val bankAccountParams = BankAccountTokenParams( + country = country!!, + currency = currency!!, + accountNumber = accountNumber!!, + accountHolderName = accountHolderName, + routingNumber = routingNumber, + accountHolderType = mapToBankAccountType(accountHolderType) + ) + CoroutineScope(Dispatchers.IO).launch { + val token = stripe.createBankAccountToken(bankAccountParams, null, stripeAccountId) + promise.resolve(createResult("token", mapFromToken(token))) + } + }.onFailure { + promise.resolve(createError(CreateTokenErrorType.Failed.toString(), it.message)) } + } - val cardAddress = cardFieldView?.cardAddress ?: cardFormView?.cardAddress + private fun createTokenFromCard(params: ReadableMap, promise: Promise) { + val cardParamsMap = (cardFieldView?.cardParams ?: cardFormView?.cardParams)?.toParamMap() + ?: run { + promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "Card details not complete")) + return + } + val cardAddress = cardFieldView?.cardAddress ?: cardFormView?.cardAddress + val address = getMapOrNull(params, "address") val cardParams = CardParams( number = cardParamsMap["number"] as String, expMonth = cardParamsMap["exp_month"] as Int, @@ -427,7 +465,8 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ address = mapToAddress(address, cardAddress), name = getValOr(params, "name", null) ) - runBlocking { + + CoroutineScope(Dispatchers.IO).launch { try { val token = stripe.createCardToken( cardParams = cardParams, diff --git a/ios/Mappers.swift b/ios/Mappers.swift index 0da5bb9a8..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,7 +37,15 @@ class Mappers { } return nil } - + + class func mapToBankAccountHolderType(_ type: String?) -> STPBankAccountHolderType { + switch type { + case "Company": return STPBankAccountHolderType.company + case "Individual": return STPBankAccountHolderType.individual + default: return STPBankAccountHolderType.individual + } + } + class func mapFromBankAccountStatus(_ status: STPBankAccountStatus?) -> String? { if let status = status { switch status { @@ -51,7 +59,7 @@ class Mappers { } return nil } - + class func mapFromBankAccount(_ bankAccount: STPBankAccount?) -> NSDictionary? { if (bankAccount == nil) { return nil @@ -68,7 +76,7 @@ class Mappers { ] return result } - + class func mapFromCard(_ card: STPCard?) -> NSDictionary? { if (card == nil) { return nil @@ -86,7 +94,7 @@ class Mappers { ] return cardMap } - + class func mapFromAddress(address: STPAddress?) -> NSDictionary { let result: NSDictionary = [ "city": address?.city ?? NSNull(), @@ -96,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 @@ -108,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 { @@ -125,7 +133,7 @@ class Mappers { } return nil } - + class func mapFromTokenType(_ type: STPTokenType?) -> String? { if let type = type { switch type { @@ -139,7 +147,7 @@ class Mappers { } return nil } - + class func mapFromToken(token: STPToken) -> NSDictionary { let tokenMap: NSDictionary = [ "id": token.tokenId, @@ -149,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 ?? "" @@ -169,10 +177,10 @@ class Mappers { shippingMethodsList.append(pm) } } - + return shippingMethodsList } - + class func mapFromShippingMethod(shippingMethod: PKShippingMethod) -> NSDictionary { let method: NSDictionary = [ "detail": shippingMethod.detail ?? "", @@ -181,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 ?? "", @@ -209,10 +217,10 @@ class Mappers { "subLocality": shippingContact.postalAddress?.subLocality, ], ] - + return contact } - + class func mapAddressFields(_ addressFields: [String]) -> [String] { return addressFields.map { if ($0 == "street") { @@ -235,7 +243,7 @@ class Mappers { return "" } } - + class func mapIntentStatus(status: STPPaymentIntentStatus?) -> String { if let status = status { switch status { @@ -251,7 +259,7 @@ class Mappers { } return "Unknown" } - + class func mapPaymentMethodType(type: STPPaymentMethodType) -> String { switch type { case STPPaymentMethodType.card: return "Card" @@ -275,7 +283,7 @@ class Mappers { default: return "Unknown" } } - + class func mapToPaymentMethodType(type: String?) -> STPPaymentMethodType? { if let type = type { switch type { @@ -302,7 +310,7 @@ class Mappers { } return nil } - + class func mapCaptureMethod(_ captureMethod: STPPaymentIntentCaptureMethod?) -> String { if let captureMethod = captureMethod { switch captureMethod { @@ -313,7 +321,7 @@ class Mappers { } return "Unknown" } - + class func mapConfirmationMethod(_ confirmationMethod: STPPaymentIntentConfirmationMethod?) -> String { if let confirmationMethod = confirmationMethod { switch confirmationMethod { @@ -324,7 +332,7 @@ class Mappers { } return "Unknown" } - + class func mapIntentShipping(_ shipping: STPPaymentIntentShippingDetails) -> NSDictionary { var addressDetails = NSDictionary() if let address = shipping.address { @@ -345,7 +353,7 @@ class Mappers { ] return shippingDetails } - + class func mapFromPaymentIntent (paymentIntent: STPPaymentIntent) -> NSDictionary { let intent: NSMutableDictionary = [ "id": paymentIntent.stripeId, @@ -364,7 +372,7 @@ class Mappers { "shipping": NSNull(), "canceledAt": NSNull() ] - + if let lastPaymentError = paymentIntent.lastPaymentError { let paymentError: NSMutableDictionary = [ "code": lastPaymentError.code ?? NSNull(), @@ -373,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 { @@ -404,7 +412,7 @@ class Mappers { } return nil } - + class func mapFromSetupIntentLastPaymentErrorType(_ errorType: STPSetupIntentLastSetupErrorType?) -> String? { if let errorType = errorType { switch errorType { @@ -421,7 +429,7 @@ class Mappers { } return nil } - + class func mapToBillingDetails(billingDetails: NSDictionary?) -> STPPaymentMethodBillingDetails? { guard let billingDetails = billingDetails else { return nil @@ -430,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(), @@ -477,10 +485,10 @@ class Mappers { "state": billingDetails?.address?.state, ], ] - + return billing } - + class func mapCardBrand(_ brand: STPCardBrand?) -> String? { if let brand = brand { switch brand { @@ -497,7 +505,7 @@ class Mappers { } return nil } - + class func mapFromPaymentMethod(_ paymentMethod: STPPaymentMethod?) -> NSDictionary? { guard let paymentMethod = paymentMethod else { return nil @@ -553,7 +561,7 @@ class Mappers { ] return method } - + class func mapIntentStatus(status: STPSetupIntentStatus?) -> String { if let status = status { switch status { @@ -569,7 +577,7 @@ class Mappers { } return "Unknown" } - + class func mapFromSetupIntentUsage(usage: STPSetupIntentUsage?) -> String { if let usage = usage { switch usage { @@ -582,7 +590,7 @@ class Mappers { } return "Unknown" } - + class func mapToPaymentIntentFutureUsage(usage: String?) -> STPPaymentIntentSetupFutureUsage { if let usage = usage { switch usage { @@ -595,7 +603,7 @@ class Mappers { } return STPPaymentIntentSetupFutureUsage.unknown } - + class func mapFromSetupIntent(setupIntent: STPSetupIntent) -> NSDictionary { let intent: NSMutableDictionary = [ "id": setupIntent.stripeID, @@ -609,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(), @@ -628,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 { @@ -640,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 { @@ -661,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) @@ -685,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) @@ -703,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) @@ -718,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) } @@ -734,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) } @@ -753,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) } @@ -772,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) } @@ -791,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) } @@ -810,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 7863bcced..1e4c6c42a 100644 --- a/ios/StripeSdk.swift +++ b/ios/StripeSdk.swift @@ -532,19 +532,63 @@ class StripeSdk: RCTEventEmitter, STPApplePayContextDelegate, STPBankSelectionVi resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock ) -> Void { - let address = params["address"] as? NSDictionary + guard let type = params["type"] as? String else { + resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, "type parameter is required")) + return + } - if let type = params["type"] as? String { - if (type != "Card") { - resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, type + " type is not supported yet")) - } + // TODO: Consider moving this to its own class when more types are supported. + switch type { + case "BankAccount": + createTokenFromBankAccount(params: params, resolver: resolve, rejecter: reject) + case "Card": + createTokenFromCard(params: params, resolver: resolve, rejecter: reject) + default: + resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, type + " type is not supported yet")) } + } + + func createTokenFromBankAccount( + params: NSDictionary, + resolver resolve: @escaping RCTPromiseResolveBlock, + rejecter reject: @escaping RCTPromiseRejectBlock + ) -> Void { + let accountHolderName = params["accountHolderName"] as? String + let accountHolderType = params["accountHolderType"] as? String + let accountNumber = params["accountNumber"] as? String + let country = params["country"] as? String + let currency = params["currency"] as? String + let routingNumber = params["routingNumber"] as? String + + let bankAccountParams = STPBankAccountParams() + bankAccountParams.accountHolderName = accountHolderName + bankAccountParams.accountNumber = accountNumber + bankAccountParams.country = country + bankAccountParams.currency = currency + bankAccountParams.routingNumber = routingNumber + bankAccountParams.accountHolderType = Mappers.mapToBankAccountHolderType(accountHolderType) + + STPAPIClient.shared.createToken(withBankAccount: bankAccountParams) { token, error in + if let token = token { + resolve(Mappers.createResult("token", Mappers.mapFromToken(token: token))) + } else { + resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, error?.localizedDescription)) + } + } + } + + func createTokenFromCard( + params: NSDictionary, + resolver resolve: @escaping RCTPromiseResolveBlock, + rejecter reject: @escaping RCTPromiseRejectBlock + ) -> Void { guard let cardParams = cardFieldView?.cardParams ?? cardFormView?.cardParams else { resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, "Card details not complete")) return } + let address = params["address"] as? NSDictionary let cardSourceParams = STPCardParams() cardSourceParams.number = cardParams.number cardSourceParams.cvc = cardParams.cvc diff --git a/src/NativeStripeSdk.tsx b/src/NativeStripeSdk.tsx index e76abed37..3ed4e8722 100644 --- a/src/NativeStripeSdk.tsx +++ b/src/NativeStripeSdk.tsx @@ -15,7 +15,6 @@ import type { InitPaymentSheetResult, PresentPaymentSheetResult, ConfirmPaymentSheetPaymentResult, - Card, ApplePayResult, CreateTokenResult, GooglePayInitResult, @@ -23,6 +22,7 @@ import type { CreateGooglePayPaymentMethodResult, GooglePay, OpenApplePaySetupResult, + CreateTokenParams, } from './types'; type NativeStripeSdkType = { @@ -65,7 +65,7 @@ type NativeStripeSdkType = { confirmPaymentSheetPayment(): Promise; createTokenForCVCUpdate(cvc: string): Promise; handleURLCallback(url: string): Promise; - createToken(params: Card.CreateTokenParams): Promise; + createToken(params: CreateTokenParams): Promise; initGooglePay(params: GooglePay.InitParams): Promise; presentGooglePay( params: GooglePay.PresentGooglePayParams diff --git a/src/functions.ts b/src/functions.ts index 7a549baeb..d142d7ea5 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -24,8 +24,8 @@ import { GooglePay, CreateGooglePayPaymentMethodResult, OpenApplePaySetupResult, + CreateTokenParams, } from './types'; -import type { Card } from './types/Card'; const APPLE_PAY_NOT_SUPPORTED_MESSAGE = 'Apple pay is not supported on this device'; @@ -55,7 +55,7 @@ export const createPaymentMethod = async ( }; export const createToken = async ( - params: Card.CreateTokenParams + params: CreateTokenParams ): Promise => { try { const { token, error } = await NativeStripeSdk.createToken(params); diff --git a/src/hooks/useStripe.tsx b/src/hooks/useStripe.tsx index 58f242750..ccf866644 100644 --- a/src/hooks/useStripe.tsx +++ b/src/hooks/useStripe.tsx @@ -17,12 +17,12 @@ import type { ConfirmPaymentSheetPaymentResult, ConfirmSetupIntent, CreateTokenResult, - Card, PayWithGooglePayResult, GooglePayInitResult, GooglePay, CreateGooglePayPaymentMethodResult, OpenApplePaySetupResult, + CreateTokenParams, } from '../types'; import { useCallback, useEffect, useState } from 'react'; import { isiOS } from '../helpers'; @@ -78,7 +78,7 @@ export function useStripe() { ); const _createToken = useCallback( - async (params: Card.CreateTokenParams): Promise => { + async (params: CreateTokenParams): Promise => { return createToken(params); }, [] diff --git a/src/types/Card.ts b/src/types/Card.ts index d077f2067..8c5c47b49 100644 --- a/src/types/Card.ts +++ b/src/types/Card.ts @@ -57,10 +57,4 @@ export namespace Card { postalCode?: string; state?: string; } - - export interface CreateTokenParams { - type: 'Account' | 'BankAccount' | 'Card' | 'CvcUpdate' | 'Person' | 'Pii'; - address?: Address; - name?: string; - } } diff --git a/src/types/index.ts b/src/types/index.ts index a55c372da..cd3ff883f 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -215,3 +215,25 @@ export type OpenApplePaySetupResult = | { error: StripeError; }; + +export type CreateTokenParams = + | CreateTokenCardParams + | CreateTokenBankAccountParams; + +export type CreateTokenCardParams = { + type: 'Card'; + address?: Card.Address; + name?: string; +}; + +export type BankAcccountHolderType = 'Company' | 'Individual'; + +export type CreateTokenBankAccountParams = { + type: 'BankAccount'; + accountHolderName?: string; + accountHolderType?: BankAcccountHolderType; + accountNumber: string; + country: string; + currency: string; + routingNumber?: string; +};