Skip to content

Commit

Permalink
Revert "pass payment intent object to sdk"
Browse files Browse the repository at this point in the history
This reverts commit 9452680.
  • Loading branch information
nazli-stripe committed Oct 2, 2023
1 parent 9452680 commit 5f02e26
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 57 deletions.
13 changes: 5 additions & 8 deletions dev-app/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
Expand Down Expand Up @@ -334,7 +331,7 @@ export default function CollectCardPaymentScreen() {
});

const { paymentIntent, error } = await confirmPaymentIntent(
collectedPaymentIntent
collectedPaymentIntent.id
);

if (error) {
Expand Down
7 changes: 3 additions & 4 deletions ios/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -136,16 +136,15 @@ 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),
"created": convertDateToUnixTimestamp(date: paymentIntent.created) ?? NSNull(),
"currency": paymentIntent.currency,
"status": mapFromPaymentIntentStatus(paymentIntent.status),
"id": paymentIntent.stripeId,
"sdk_uuid": uuid,
]
return result
}
Expand Down
58 changes: 25 additions & 33 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "")
Expand Down Expand Up @@ -411,7 +411,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
default:
break
}

let cardPresentParams: CardPresentParameters
do {
cardPresentParams = try cardPresentParamsBuilder.build()
Expand All @@ -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])
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -513,15 +508,15 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
resolve(Errors.createError(nsError: error as NSError))
return
}

self.collectPaymentMethodCancelable = Terminal.shared.collectPaymentMethod(
paymentIntent,
collectConfig: collectConfig
) { pi, collectError in
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
Expand All @@ -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])
}
}
Expand Down Expand Up @@ -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])
}
Expand All @@ -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)
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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)

Expand All @@ -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))
Expand Down Expand Up @@ -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!)
Expand Down
13 changes: 6 additions & 7 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
ConnectReaderResultType,
ConnectHandoffParams,
CollectPaymentMethodParams,
PaymentIntent,
} from './types';

export async function initialize(
Expand Down Expand Up @@ -387,12 +386,12 @@ export async function getLocations(
}

export async function confirmPaymentIntent(
paymentIntent: PaymentIntent.Type
paymentIntentId: string
): Promise<PaymentIntentResultType> {
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 {
Expand All @@ -401,15 +400,15 @@ export async function confirmPaymentIntent(
};
}
return {
paymentIntent: confirmedPaymentIntent!,
paymentIntent: paymentIntent!,
error: undefined,
};
} catch (error) {
return {
error: error as any,
};
}
}, 'confirmPaymentIntent')(paymentIntent);
}, 'confirmPaymentIntent')(paymentIntentId);
}

export async function cancelPaymentIntent(
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useStripeTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type {
PaymentStatus,
UserCallbacks,
EventResult,
PaymentIntent,
} from '../types';
import {
discoverReaders,
Expand Down Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/types/PaymentIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export namespace PaymentIntent {
created: string;
currency: string;
status: Status;
sdk_uuid: string;
}

export type Status =
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export type PaymentMethodOptions = {
};

export type CollectPaymentMethodParams = {
paymentIntent: PaymentIntent.Type;
paymentIntentId: string;
skipTipping?: boolean;
tipEligibleAmount?: number;
updatePaymentIntent?: boolean;
Expand Down

0 comments on commit 5f02e26

Please sign in to comment.