Skip to content

Commit

Permalink
ConfirmPaymentIntent methods accept param objects (#734)
Browse files Browse the repository at this point in the history
* ConfirmPaymentIntent method accept param objects

* Improve confirmSetupIntent method to accept param object

* Modify parameter name

* Improve cancelSetupIntent method to accept param object

* Improve cancelPaymentIntent method to accept param object
  • Loading branch information
EricLin-BBpos authored Jun 30, 2024
1 parent 11d41bc commit d1dae66
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
val captureMethod = params.getString("captureMethod")
val offlineBehavior = params.getString("offlineBehavior")

val paymentMethodTypes = paymentMethods?.toArrayList()?.mapNotNull {
it as? String
val paymentMethodTypes = paymentMethods?.toArrayList()?.mapNotNull {
it as? String
}?.map{
PaymentMethodType.valueOf(it.uppercase())
}
Expand Down Expand Up @@ -535,10 +535,13 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :

@ReactMethod
@Suppress("unused")
fun confirmPaymentIntent(paymentIntent: ReadableMap, promise: Promise) = withExceptionResolver(
fun confirmPaymentIntent(params: ReadableMap, promise: Promise) = withExceptionResolver(
promise
) {
val uuid = requireParam(paymentIntent.getString("sdkUuid")) {
val paymentIntentJson = requireParam(params.getMap("paymentIntent")) {
"You must provide a paymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."
}
val uuid = requireParam(paymentIntentJson.getString("sdkUuid")) {
"The PaymentIntent is missing sdkUuid field. This method requires you to use the PaymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."
}
val paymentIntent = requireParam(paymentIntents[uuid]) {
Expand Down Expand Up @@ -595,9 +598,12 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
@OptIn(OfflineMode::class)
@ReactMethod
@Suppress("unused")
fun cancelPaymentIntent(paymentIntent: ReadableMap, promise: Promise) =
fun cancelPaymentIntent(params: ReadableMap, promise: Promise) =
withExceptionResolver(promise) {
val uuid = requireParam(paymentIntent.getString("sdkUuid")) {
val paymentIntentJson = requireParam(params.getMap("paymentIntent")) {
"You must provide paymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."
}
val uuid = requireParam(paymentIntentJson.getString("sdkUuid")) {
"The PaymentIntent is missing sdkUuid field. This method requires you to use the PaymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."
}
val paymentIntent = requireParam(paymentIntents[uuid]) {
Expand Down Expand Up @@ -682,9 +688,12 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :

@ReactMethod
@Suppress("unused")
fun cancelSetupIntent(setupIntent: ReadableMap, promise: Promise) =
fun cancelSetupIntent(params: ReadableMap, promise: Promise) =
withExceptionResolver(promise) {
val uuid = requireParam(setupIntent.getString("sdkUuid")) {
val setupIntentJson = requireParam(params.getMap("setupIntent")) {
"You must provide a setupIntent."
}
val uuid = requireParam(setupIntentJson.getString("sdkUuid")) {
"The SetupIntent is missing sdkUuid field. This method requires you to use the SetupIntent that was returned from either createPaymentIntent or retrievePaymentIntent."
}
val setupIntent = requireParam(setupIntents[uuid]) {
Expand All @@ -704,9 +713,12 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :

@ReactMethod
@Suppress("unused")
fun confirmSetupIntent(setupIntent: ReadableMap, promise: Promise) =
fun confirmSetupIntent(params: ReadableMap, promise: Promise) =
withExceptionResolver(promise) {
val uuid = requireParam(setupIntent.getString("sdkUuid")) {
val setupIntentJson = requireParam(params.getMap("setupIntent")) {
"You must provide a setupIntent."
}
val uuid = requireParam(setupIntentJson.getString("sdkUuid")) {
"The SetupIntent is missing sdkUuid field. This method requires you to use the SetupIntent that was returned from either createPaymentIntent or retrievePaymentIntent."
}
val setupIntent = requireParam(setupIntents[uuid]) {
Expand Down
6 changes: 3 additions & 3 deletions dev-app/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ export default function CollectCardPaymentScreen() {
],
});

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

if (error) {
addLogs({
Expand Down
4 changes: 3 additions & 1 deletion dev-app/src/screens/SetupIntentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export default function SetupIntentScreen() {
},
],
});
const { setupIntent, error } = await confirmSetupIntent(si);
const { setupIntent, error } = await confirmSetupIntent({
setupIntent: si,
});
if (error) {
addLogs({
name: 'Process Payment',
Expand Down
8 changes: 4 additions & 4 deletions ios/StripeTerminalReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter)
)

RCT_EXTERN_METHOD(
confirmPaymentIntent:(NSDictionary *)paymentIntentJson
confirmPaymentIntent:(NSDictionary *)params
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)
Expand All @@ -90,7 +90,7 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter)
)

RCT_EXTERN_METHOD(
cancelPaymentIntent:(NSDictionary *)paymentIntentJson
cancelPaymentIntent:(NSDictionary *)params
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)
Expand Down Expand Up @@ -129,7 +129,7 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter)
)

RCT_EXTERN_METHOD(
cancelSetupIntent:(NSDictionary *)setupIntentJson
cancelSetupIntent:(NSDictionary *)params
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)
Expand All @@ -141,7 +141,7 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter)
)

RCT_EXTERN_METHOD(
confirmSetupIntent:(NSDictionary *)setupIntentJson
confirmSetupIntent:(NSDictionary *)params
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)
Expand Down
24 changes: 20 additions & 4 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,11 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}

@objc(confirmPaymentIntent:resolver:rejecter:)
func confirmPaymentIntent(paymentIntentJson: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
func confirmPaymentIntent(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
guard let paymentIntentJson = params["paymentIntent"] as? NSDictionary else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide paymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."))
return
}
guard let uuid = paymentIntentJson["sdkUuid"] as? String else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "The PaymentIntent is missing sdkUuid field. This method requires you to use the PaymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."))
return
Expand Down Expand Up @@ -696,7 +700,11 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}

@objc(cancelPaymentIntent:resolver:rejecter:)
func cancelPaymentIntent(paymentIntentJson: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
func cancelPaymentIntent(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
guard let paymentIntentJson = params["paymentIntent"] as? NSDictionary else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide paymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."))
return
}
guard let uuid = paymentIntentJson["sdkUuid"] as? String else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "The PaymentIntent is missing sdkUuid field. This method requires you to use the PaymentIntent that was returned from either createPaymentIntent or retrievePaymentIntent."))
return
Expand Down Expand Up @@ -772,7 +780,11 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}

@objc(cancelSetupIntent:resolver:rejecter:)
func cancelSetupIntent(setupIntentJson: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
func cancelSetupIntent(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
guard let setupIntentJson = params["setupIntent"] as? NSDictionary else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide setupIntent."))
return
}
guard let uuid = setupIntentJson["sdkUuid"] as? String else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "The SetupIntent is missing sdkUuid field. This method requires you to use the SetupIntent that was returned from either createPaymentIntent or retrievePaymentIntent."))
return
Expand Down Expand Up @@ -861,7 +873,11 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}

@objc(confirmSetupIntent:resolver:rejecter:)
func confirmSetupIntent(setupIntentJson: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
func confirmSetupIntent(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
guard let setupIntentJson = params["setupIntent"] as? NSDictionary else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide setupIntent."))
return
}
guard let uuid = setupIntentJson["sdkUuid"] as? String else {
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "The SetupIntent is missing sdkUuid field. This method requires you to use the SetupIntent that was returned from either createPaymentIntent or retrievePaymentIntent."))
return
Expand Down
14 changes: 8 additions & 6 deletions src/StripeTerminalSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import type {
ConnectLocalMobileParams,
ConnectReaderResultType,
CollectPaymentMethodParams,
PaymentIntent,
SetupIntent,
OfflineStatus,
ICollectInputsParameters,
ICollectInputsResults,
PaymentStatus,
ConnectionStatus,
ConfirmPaymentMethodParams,
ConfirmSetupIntentMethodParams,
CancelSetupIntentMethodParams,
CancelPaymentMethodParams,
} from './types';

const { StripeTerminalReactNative } = NativeModules;
Expand Down Expand Up @@ -91,15 +93,15 @@ export interface StripeTerminalSdkType {
retrievePaymentIntent(clientSecret: string): Promise<PaymentIntentResultType>;
// Confirm Payment Intent
confirmPaymentIntent(
paymentIntentJson: PaymentIntent.Type
params: ConfirmPaymentMethodParams
): Promise<PaymentIntentResultType>;
// Create Setup Intent
createSetupIntent(
params: CreateSetupIntentParams
): Promise<SetupIntentResultType>;
// Cancel Payment Intent
cancelPaymentIntent(
paymentIntent: PaymentIntent.Type
params: CancelPaymentMethodParams
): Promise<PaymentIntentResultType>;
// Collect Setup Intent payment method
collectSetupIntentPaymentMethod(
Expand All @@ -118,13 +120,13 @@ export interface StripeTerminalSdkType {
retrieveSetupIntent(clientSecret: string): Promise<SetupIntentResultType>;
// Cancel Setup Intent
cancelSetupIntent(
setupIntent: SetupIntent.Type
params: CancelSetupIntentMethodParams
): Promise<SetupIntentResultType>;
// List of locations belonging to the merchant
getLocations(params: GetLocationsParams): Promise<GetLocationsResultType>;
// Confirm Setup Intent
confirmSetupIntent(
setupIntent: SetupIntent.Type
params: ConfirmSetupIntentMethodParams
): Promise<SetupIntentResultType>;
simulateReaderUpdate(update: Reader.SimulateUpdateType): Promise<void>;
collectRefundPaymentMethod(
Expand Down
40 changes: 21 additions & 19 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import type {
ConnectReaderResultType,
ConnectHandoffParams,
CollectPaymentMethodParams,
PaymentIntent,
SetupIntent,
OfflineStatus,
ICollectInputsParameters,
ICollectInputsResults,
PaymentStatus,
ConnectionStatus,
ConfirmPaymentMethodParams,
ConfirmSetupIntentMethodParams,
CancelSetupIntentMethodParams,
CancelPaymentMethodParams,
} from './types';

export async function initialize(
Expand Down Expand Up @@ -422,15 +424,15 @@ export async function getLocations(
}

export async function confirmPaymentIntent(
paymentIntent: PaymentIntent.Type
params: ConfirmPaymentMethodParams
): Promise<PaymentIntentResultType> {
return Logger.traceSdkMethod(async (innerPaymentIntent) => {
return Logger.traceSdkMethod(async (innerparams) => {
try {
const { error, paymentIntent: confirmedPaymentIntent } =
await StripeTerminalSdk.confirmPaymentIntent(innerPaymentIntent);
await StripeTerminalSdk.confirmPaymentIntent(innerparams);

if (error) {
if (paymentIntent) {
if (confirmedPaymentIntent) {
return {
error,
paymentIntent: confirmedPaymentIntent,
Expand All @@ -450,16 +452,16 @@ export async function confirmPaymentIntent(
error: error as any,
};
}
}, 'confirmPaymentIntent')(paymentIntent);
}, 'confirmPaymentIntent')(params);
}

export async function cancelPaymentIntent(
paymentIntent: PaymentIntent.Type
params: CancelPaymentMethodParams
): Promise<PaymentIntentResultType> {
return Logger.traceSdkMethod(async (innerPaymentIntent) => {
return Logger.traceSdkMethod(async (innerparams) => {
try {
const { paymentIntent: canceledPaymentIntent, error } =
await StripeTerminalSdk.cancelPaymentIntent(innerPaymentIntent);
await StripeTerminalSdk.cancelPaymentIntent(innerparams);

if (error) {
return {
Expand All @@ -476,7 +478,7 @@ export async function cancelPaymentIntent(
error: error as any,
};
}
}, 'cancelPaymentIntent')(paymentIntent);
}, 'cancelPaymentIntent')(params);
}

export async function installAvailableUpdate(): Promise<{
Expand Down Expand Up @@ -597,12 +599,12 @@ export async function clearReaderDisplay(): Promise<ClearReaderDisplayResultType
}

export async function cancelSetupIntent(
setupIntent: SetupIntent.Type
params: CancelSetupIntentMethodParams
): Promise<SetupIntentResultType> {
return Logger.traceSdkMethod(async (innerSetupIntent) => {
return Logger.traceSdkMethod(async (innerParams) => {
try {
const { setupIntent: canceledSetupIntent, error } =
await StripeTerminalSdk.cancelSetupIntent(innerSetupIntent);
await StripeTerminalSdk.cancelSetupIntent(innerParams);

if (error) {
return {
Expand All @@ -619,16 +621,16 @@ export async function cancelSetupIntent(
error: error as any,
};
}
}, 'cancelSetupIntent')(setupIntent);
}, 'cancelSetupIntent')(params);
}

export async function confirmSetupIntent(
setupIntent: SetupIntent.Type
params: ConfirmSetupIntentMethodParams
): Promise<SetupIntentResultType> {
return Logger.traceSdkMethod(async (innerSetupIntent) => {
return Logger.traceSdkMethod(async (innerparams) => {
try {
const { setupIntent: confirmedSetupIntent, error } =
await StripeTerminalSdk.confirmSetupIntent(innerSetupIntent);
await StripeTerminalSdk.confirmSetupIntent(innerparams);

if (error) {
return {
Expand All @@ -645,7 +647,7 @@ export async function confirmSetupIntent(
error: error as any,
};
}
}, 'confirmSetupIntent')(setupIntent);
}, 'confirmSetupIntent')(params);
}

export async function simulateReaderUpdate(
Expand Down
Loading

0 comments on commit d1dae66

Please sign in to comment.