From 5f02e269d3cb0babecd1f1f9388d2ebcd15de7ff Mon Sep 17 00:00:00 2001 From: Nazli Yurdakul Date: Mon, 2 Oct 2023 11:21:16 -1000 Subject: [PATCH] Revert "pass payment intent object to sdk" This reverts commit 9452680815a4a7eb1c6b4d6f108dee7f5781a25a. --- .../src/screens/CollectCardPaymentScreen.tsx | 13 ++--- ios/Mappers.swift | 7 +-- ios/StripeTerminalReactNative.swift | 58 ++++++++----------- src/functions.ts | 13 ++--- src/hooks/useStripeTerminal.tsx | 5 +- src/types/PaymentIntent.ts | 1 - src/types/index.ts | 2 +- 7 files changed, 42 insertions(+), 57 deletions(-) diff --git a/dev-app/src/screens/CollectCardPaymentScreen.tsx b/dev-app/src/screens/CollectCardPaymentScreen.tsx index faf295d5..9148fd71 100644 --- a/dev-app/src/screens/CollectCardPaymentScreen.tsx +++ b/dev-app/src/screens/CollectCardPaymentScreen.tsx @@ -254,13 +254,10 @@ export default function CollectCardPaymentScreen() { ], }); - console.log('create'); - console.log(paymentIntent); - - return await _collectPaymentMethod(paymentIntent); + return await _collectPaymentMethod(paymentIntent.id); }; - const _collectPaymentMethod = async (pi: PaymentIntent.Type) => { + const _collectPaymentMethod = async (paymentIntentId: string) => { // @ts-ignore setCancel((prev) => ({ ...prev, isDisabled: false })); addLogs({ @@ -269,13 +266,13 @@ export default function CollectCardPaymentScreen() { { name: 'Collect', description: 'terminal.collectPaymentMethod', - metadata: { paymentIntentId: pi.id }, + metadata: { paymentIntentId }, onBack: cancelCollectPaymentMethod, }, ], }); const { paymentIntent, error } = await collectPaymentMethod({ - paymentIntent: pi, + paymentIntentId: paymentIntentId, skipTipping: skipTipping, tipEligibleAmount: tipEligibleAmount ? Number(tipEligibleAmount) @@ -334,7 +331,7 @@ export default function CollectCardPaymentScreen() { }); const { paymentIntent, error } = await confirmPaymentIntent( - collectedPaymentIntent + collectedPaymentIntent.id ); if (error) { diff --git a/ios/Mappers.swift b/ios/Mappers.swift index b689c3c0..3d41f8c1 100644 --- a/ios/Mappers.swift +++ b/ios/Mappers.swift @@ -80,7 +80,7 @@ class Mappers { guard let displayName = cartLineItem["displayName"] as? String else { return nil } guard let quantity = cartLineItem["quantity"] as? NSNumber else { return nil } guard let amount = cartLineItem["amount"] as? NSNumber else { return nil } - + do { let lineItem = try CartLineItemBuilder(displayName: displayName) .setQuantity(Int(truncating: quantity)) @@ -136,8 +136,8 @@ class Mappers { } } - - class func mapFromPaymentIntent(_ paymentIntent: PaymentIntent, uuid: String) -> NSDictionary { + + class func mapFromPaymentIntent(_ paymentIntent: PaymentIntent) -> NSDictionary { let result: NSDictionary = [ "amount": paymentIntent.amount, "charges": mapFromCharges(paymentIntent.charges), @@ -145,7 +145,6 @@ class Mappers { "currency": paymentIntent.currency, "status": mapFromPaymentIntentStatus(paymentIntent.status), "id": paymentIntent.stripeId, - "sdk_uuid": uuid, ] return result } diff --git a/ios/StripeTerminalReactNative.swift b/ios/StripeTerminalReactNative.swift index 8f1b196d..1721cfab 100644 --- a/ios/StripeTerminalReactNative.swift +++ b/ios/StripeTerminalReactNative.swift @@ -323,7 +323,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe let onBehalfOf: String? = params["onBehalfOf"] as? String let merchantDisplayName: String? = params["merchantDisplayName"] as? String let tosAcceptancePermitted: Bool = params["tosAcceptancePermitted"] as? Bool ?? true - + let connectionConfig: LocalMobileConnectionConfiguration do { connectionConfig = try LocalMobileConnectionConfigurationBuilder(locationId: locationId ?? selectedReader.locationId ?? "") @@ -411,7 +411,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe default: break } - + let cardPresentParams: CardPresentParameters do { cardPresentParams = try cardPresentParamsBuilder.build() @@ -436,14 +436,13 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe resolve(Errors.createError(nsError: error as NSError)) return } - + Terminal.shared.createPaymentIntent(paymentParams) { pi, error in if let error = error as NSError? { resolve(Errors.createError(nsError: error)) } else if let pi = pi { - let uuid = UUID().uuidString - let paymentIntent = Mappers.mapFromPaymentIntent(pi, uuid: uuid) - self.paymentIntents[uuid] = pi + let paymentIntent = Mappers.mapFromPaymentIntent(pi) + self.paymentIntents[pi.stripeId] = pi resolve(["paymentIntent": paymentIntent]) } } @@ -474,26 +473,22 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe @objc(collectPaymentMethod:resolver:rejecter:) func collectPaymentMethod(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { - guard let paymentIntent = params["paymentIntent"] as? NSDictionary else { - resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide paymentIntent.")) - return - } - guard let uuid = paymentIntent["sdk_uuid"] as? String else { - resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide sdk_uuid.")) + guard let id = params["paymentIntentId"] as? String else { + resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide paymentIntentId.")) return } - guard let paymentIntent = self.paymentIntents[Optional(uuid)] else { - resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "There is no associated paymentIntent with uuid \(uuid)")) + guard let paymentIntent = self.paymentIntents[Optional(id)] else { + resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "There is no associated paymentIntent with id \(id)")) return } let skipTipping = params["skipTipping"] as? Bool ?? false let updatePaymentIntent = params["updatePaymentIntent"] as? Bool ?? false - + let collectConfigBuilder = CollectConfigurationBuilder() .setSkipTipping(skipTipping) .setUpdatePaymentIntent(updatePaymentIntent) - + if let eligibleAmount = params["tipEligibleAmount"] as? Int { do { let tippingConfig = try TippingConfigurationBuilder() @@ -513,7 +508,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe resolve(Errors.createError(nsError: error as NSError)) return } - + self.collectPaymentMethodCancelable = Terminal.shared.collectPaymentMethod( paymentIntent, collectConfig: collectConfig @@ -521,7 +516,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe if let error = collectError as NSError? { resolve(Errors.createError(nsError: error)) } else if let paymentIntent = pi { - let paymentIntent = Mappers.mapFromPaymentIntent(paymentIntent, uuid: uuid) + let paymentIntent = Mappers.mapFromPaymentIntent(paymentIntent) resolve(["paymentIntent": paymentIntent]) } self.collectPaymentMethodCancelable = nil @@ -539,9 +534,8 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe if let error = error as NSError? { resolve(Errors.createError(nsError: error)) } else if let pi = pi { - let uuid = UUID().uuidString - let paymentIntent = Mappers.mapFromPaymentIntent(pi, uuid: uuid) - self.paymentIntents[uuid] = pi + let paymentIntent = Mappers.mapFromPaymentIntent(pi) + self.paymentIntents[pi.stripeId] = pi resolve(["paymentIntent": paymentIntent]) } } @@ -589,18 +583,17 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide paymentIntentId.")) return } - + guard let paymentIntent = paymentIntents[Optional(id)] else { resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "There is no associated paymentIntent with id \(id)")) return } - + Terminal.shared.confirmPaymentIntent(paymentIntent) { pi, error in if let error = error as NSError? { resolve(Errors.createError(nsError: error)) } else if let pi = pi { - let uuid = UUID().uuidString - let paymentIntent = Mappers.mapFromPaymentIntent(pi, uuid: uuid) + let paymentIntent = Mappers.mapFromPaymentIntent(pi) self.paymentIntents = [:] resolve(["paymentIntent": paymentIntent]) } @@ -616,18 +609,18 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe let result = Mappers.mapFromConnectionStatus(status) sendEvent(withName: ReactNativeConstants.CHANGE_CONNECTION_STATUS.rawValue, body: ["result": result]) } - + func reader(_ reader: Reader, didStartReconnect cancelable: Cancelable) { self.cancelReaderConnectionCancellable = cancelable let reader = Mappers.mapFromReader(reader) sendEvent(withName: ReactNativeConstants.START_READER_RECONNECT.rawValue, body: ["reader": reader]) } - + func readerDidSucceedReconnect(_ reader: Reader) { let reader = Mappers.mapFromReader(reader) sendEvent(withName: ReactNativeConstants.READER_RECONNECT_SUCCEED.rawValue, body: ["reader": reader]) } - + func readerDidFailReconnect(_ reader: Reader) { let error = Errors.createError(code: ErrorCode.unexpectedSdkError, message: "Reader reconnect fail") sendEvent(withName: ReactNativeConstants.READER_RECONNECT_FAIL.rawValue, body: error) @@ -658,8 +651,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe if let error = collectError as NSError? { resolve(Errors.createError(nsError: error)) } else if let pi = pi { - let uuid = UUID().uuidString - let paymentIntent = Mappers.mapFromPaymentIntent(pi, uuid: uuid) + let paymentIntent = Mappers.mapFromPaymentIntent(pi) self.paymentIntents[pi.stripeId] = nil resolve(["paymentIntent": paymentIntent]) } @@ -699,7 +691,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe let cartBuilder = CartBuilder(currency: currency!) .setTax(Int(truncating: tax!)) .setTotal(Int(truncating: total!)) - + let cartLineItems = Mappers.mapToCartLineItems(params["lineItems"] as? NSArray ?? NSArray()) cartBuilder.setLineItems(cartLineItems) @@ -710,7 +702,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe resolve(Errors.createError(nsError: error as NSError)) return } - + Terminal.shared.setReaderDisplay(cart) { error in if let error = error as NSError? { resolve(Errors.createError(nsError: error)) @@ -831,7 +823,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe let intAmount = UInt(truncating: amount!); let refundApplicationFee = params["refundApplicationFee"] as? NSNumber let reverseTransfer = params["reverseTransfer"] as? NSNumber - + let refundParams: RefundParameters do { refundParams = try RefundParametersBuilder(chargeId: chargeId!,amount: intAmount, currency: currency!) diff --git a/src/functions.ts b/src/functions.ts index e990756c..1e109741 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -28,7 +28,6 @@ import type { ConnectReaderResultType, ConnectHandoffParams, CollectPaymentMethodParams, - PaymentIntent, } from './types'; export async function initialize( @@ -387,12 +386,12 @@ export async function getLocations( } export async function confirmPaymentIntent( - paymentIntent: PaymentIntent.Type + paymentIntentId: string ): Promise { - return Logger.traceSdkMethod(async (innerPaymentIntent) => { + return Logger.traceSdkMethod(async (innerPaymentIntentId) => { try { - const { error, paymentIntent: confirmedPaymentIntent } = - await StripeTerminalSdk.confirmPaymentIntent(innerPaymentIntent); + const { error, paymentIntent } = + await StripeTerminalSdk.confirmPaymentIntent(innerPaymentIntentId); if (error) { return { @@ -401,7 +400,7 @@ export async function confirmPaymentIntent( }; } return { - paymentIntent: confirmedPaymentIntent!, + paymentIntent: paymentIntent!, error: undefined, }; } catch (error) { @@ -409,7 +408,7 @@ export async function confirmPaymentIntent( error: error as any, }; } - }, 'confirmPaymentIntent')(paymentIntent); + }, 'confirmPaymentIntent')(paymentIntentId); } export async function cancelPaymentIntent( diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index 6fa8d42d..f7c1189a 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -18,7 +18,6 @@ import type { PaymentStatus, UserCallbacks, EventResult, - PaymentIntent, } from '../types'; import { discoverReaders, @@ -504,14 +503,14 @@ export function useStripeTerminal(props?: Props) { ); const _confirmPaymentIntent = useCallback( - async (paymentIntent: PaymentIntent.Type) => { + async (paymentIntentId: string) => { if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } setLoading(true); - const response = await confirmPaymentIntent(paymentIntent); + const response = await confirmPaymentIntent(paymentIntentId); setLoading(false); diff --git a/src/types/PaymentIntent.ts b/src/types/PaymentIntent.ts index caea5de9..df9859a5 100644 --- a/src/types/PaymentIntent.ts +++ b/src/types/PaymentIntent.ts @@ -8,7 +8,6 @@ export namespace PaymentIntent { created: string; currency: string; status: Status; - sdk_uuid: string; } export type Status = diff --git a/src/types/index.ts b/src/types/index.ts index e7d654ca..505e720f 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -179,7 +179,7 @@ export type PaymentMethodOptions = { }; export type CollectPaymentMethodParams = { - paymentIntent: PaymentIntent.Type; + paymentIntentId: string; skipTipping?: boolean; tipEligibleAmount?: number; updatePaymentIntent?: boolean;