Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add handleNextActionForSetup function #1440

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

**Features**

- Added the `handleNextActionForSetup` method. This can be used exactly like `handleNextAction`, except with [SetupIntents](https://stripe.com/docs/api/setup_intents). [#1440](https://github.com/stripe/stripe-react-native/pull/1440)
- `canAddCardToWallet` now returns the `fpanLastFour` and `dpanLastFour` in the `GooglePayCardToken` type. The `cardLastFour` field (which is now superseded by `fpanLastFour`) is deprecated.

**Fixes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class PaymentLauncherFragment(
private val setupIntentClientSecret: String? = null,
private val confirmSetupParams: ConfirmSetupIntentParams? = null,
// Used when handling the next action on a payment intent
private val handleNextActionClientSecret: String? = null,
private val handleNextActionPaymentIntentClientSecret: String? = null,
// Used when handling the next action on a setup intent
private val handleNextActionSetupIntentClientSecret: String? = null,
) : Fragment() {
private lateinit var paymentLauncher: PaymentLauncher

Expand Down Expand Up @@ -88,19 +90,40 @@ class PaymentLauncherFragment(
/**
* Helper-constructor used for handling the next action on a payment intent
*/
fun forNextAction(context: ReactApplicationContext,
fun forNextActionPayment(context: ReactApplicationContext,
stripe: Stripe,
publishableKey: String,
stripeAccountId: String?,
promise: Promise,
handleNextActionClientSecret: String): PaymentLauncherFragment {
handleNextActionPaymentIntentClientSecret: String): PaymentLauncherFragment {
val paymentLauncherFragment = PaymentLauncherFragment(
context,
stripe,
publishableKey,
stripeAccountId,
promise,
handleNextActionClientSecret = handleNextActionClientSecret,
handleNextActionPaymentIntentClientSecret = handleNextActionPaymentIntentClientSecret,
)
addFragment(paymentLauncherFragment, context, promise)
return paymentLauncherFragment
}

/**
* Helper-constructor used for handling the next action on a setup intent
*/
fun forNextActionSetup(context: ReactApplicationContext,
stripe: Stripe,
publishableKey: String,
stripeAccountId: String?,
promise: Promise,
handleNextActionSetupIntentClientSecret: String): PaymentLauncherFragment {
val paymentLauncherFragment = PaymentLauncherFragment(
context,
stripe,
publishableKey,
stripeAccountId,
promise,
handleNextActionSetupIntentClientSecret = handleNextActionSetupIntentClientSecret,
)
addFragment(paymentLauncherFragment, context, promise)
return paymentLauncherFragment
Expand Down Expand Up @@ -130,8 +153,10 @@ class PaymentLauncherFragment(
paymentLauncher.confirm(confirmPaymentParams)
} else if (setupIntentClientSecret != null && confirmSetupParams != null) {
paymentLauncher.confirm(confirmSetupParams)
} else if (handleNextActionClientSecret != null) {
paymentLauncher.handleNextActionForPaymentIntent(handleNextActionClientSecret)
} else if (handleNextActionPaymentIntentClientSecret != null) {
paymentLauncher.handleNextActionForPaymentIntent(handleNextActionPaymentIntentClientSecret)
} else if (handleNextActionSetupIntentClientSecret != null) {
paymentLauncher.handleNextActionForSetupIntent(handleNextActionSetupIntentClientSecret)
} else {
throw Exception("Invalid parameters provided to PaymentLauncher. Ensure that you are providing the correct client secret and setup params (if necessary).")
}
Expand All @@ -146,10 +171,12 @@ class PaymentLauncherFragment(
is PaymentResult.Completed -> {
if (paymentIntentClientSecret != null) {
retrievePaymentIntent(paymentIntentClientSecret, stripeAccountId)
} else if (handleNextActionClientSecret != null) {
retrievePaymentIntent(handleNextActionClientSecret, stripeAccountId)
} else if (handleNextActionPaymentIntentClientSecret != null) {
retrievePaymentIntent(handleNextActionPaymentIntentClientSecret, stripeAccountId)
} else if (setupIntentClientSecret != null) {
retrieveSetupIntent(setupIntentClientSecret, stripeAccountId)
} else if (handleNextActionSetupIntentClientSecret != null) {
retrieveSetupIntent(handleNextActionSetupIntentClientSecret, stripeAccountId)
} else {
throw Exception("Failed to create Payment Launcher. No client secret provided.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ

@ReactMethod
fun handleNextAction(paymentIntentClientSecret: String, promise: Promise) {
paymentLauncherFragment = PaymentLauncherFragment.forNextAction(
paymentLauncherFragment = PaymentLauncherFragment.forNextActionPayment(
context = reactApplicationContext,
stripe,
publishableKey,
Expand All @@ -416,6 +416,18 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
)
}

@ReactMethod
fun handleNextActionForSetup(setupIntentClientSecret: String, promise: Promise) {
paymentLauncherFragment = PaymentLauncherFragment.forNextActionSetup(
context = reactApplicationContext,
stripe,
publishableKey,
stripeAccountId,
promise,
setupIntentClientSecret
)
}

// TODO: Uncomment when WeChat is re-enabled in stripe-ios
// private fun payWithWeChatPay(paymentIntentClientSecret: String, appId: String) {
// val activity = currentActivity as ComponentActivity
Expand Down
7 changes: 7 additions & 0 deletions ios/StripeSdk.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ @interface RCT_EXTERN_MODULE(StripeSdk, RCTEventEmitter)
rejecter: (RCTPromiseRejectBlock)reject
)

RCT_EXTERN_METHOD(
handleNextActionForSetup:(NSString *)setupIntentClientSecret
returnURL:(NSString *)returnURL
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)

RCT_EXTERN_METHOD(
initPaymentSheet:(NSDictionary *)params
resolver: (RCTPromiseResolveBlock)resolve
Expand Down
32 changes: 32 additions & 0 deletions ios/StripeSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,38 @@ class StripeSdk: RCTEventEmitter, STPBankSelectionViewControllerDelegate, UIAdap
}
}

@objc(handleNextActionForSetup:returnURL:resolver:rejecter:)
func handleNextActionForSetup(
setupIntentClientSecret: String,
returnURL: String?,
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
){
let paymentHandler = STPPaymentHandler.shared()
paymentHandler.handleNextAction(forSetupIntent: setupIntentClientSecret, with: self, returnURL: returnURL) { status, setupIntent, handleActionError in
switch (status) {
case .failed:
resolve(Errors.createError(ErrorType.Failed, handleActionError))
break
case .canceled:
if let lastError = setupIntent?.lastSetupError {
resolve(Errors.createError(ErrorType.Canceled, lastError))
} else {
resolve(Errors.createError(ErrorType.Canceled, "The setup intent has been canceled"))
}
break
case .succeeded:
if let setupIntent = setupIntent {
resolve(Mappers.createResult("setupIntent", Mappers.mapFromSetupIntent(setupIntent: setupIntent)))
}
break
@unknown default:
resolve(Errors.createError(ErrorType.Unknown, "Cannot complete setup"))
break
}
}
}

@objc(collectBankAccount:clientSecret:params:resolver:rejecter:)
func collectBankAccount(
isPaymentIntent: Bool,
Expand Down
4 changes: 4 additions & 0 deletions jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const mockFunctions = {
paymentIntent: {},
error: null,
})),
handleNextActionForSetup: jest.fn(async () => ({
setupIntent: {},
error: null,
})),
confirmSetupIntent: jest.fn(async () => ({
setupIntent: {},
error: null,
Expand Down
5 changes: 5 additions & 0 deletions src/NativeStripeSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
RetrieveSetupIntentResult,
ConfirmPaymentResult,
HandleNextActionResult,
HandleNextActionForSetupResult,
ConfirmSetupIntentResult,
CreateTokenForCVCUpdateResult,
InitPaymentSheetResult,
Expand All @@ -36,6 +37,10 @@ type NativeStripeSdkType = {
paymentIntentClientSecret: string,
returnURL?: string | null
): Promise<HandleNextActionResult>;
handleNextActionForSetup(
setupIntentClientSecret: string,
returnURL?: string | null
): Promise<HandleNextActionForSetupResult>;
confirmPayment(
paymentIntentClientSecret: string,
params?: PaymentIntent.ConfirmParams,
Expand Down
36 changes: 36 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
CreateTokenForCVCUpdateResult,
CreateTokenResult,
HandleNextActionResult,
HandleNextActionForSetupResult,
InitPaymentSheetResult,
PaymentMethod,
PaymentSheet,
Expand Down Expand Up @@ -203,6 +204,41 @@ export const handleNextAction = async (
}
};

/** Handles any nextAction required to authenticate the SetupIntent.
* Call this method if you are confirming the SetupIntent on your backend and get a status of requires_action.
*
* @param {string} setupIntentClientSecret The client secret associated with the SetupIntent.
* @param {string=} returnURL An optional return URL so the Stripe SDK can redirect back to your app after authentication. This should match the `return_url` you specified during PaymentIntent confirmation.
* */
export const handleNextActionForSetup = async (
setupIntentClientSecret: string,
returnURL?: string
): Promise<HandleNextActionForSetupResult> => {
try {
const { setupIntent, error } =
Platform.OS === 'ios'
? await NativeStripeSdk.handleNextActionForSetup(
setupIntentClientSecret,
returnURL ?? null
)
: await NativeStripeSdk.handleNextActionForSetup(
setupIntentClientSecret
);
if (error) {
return {
error,
};
}
return {
setupIntent: setupIntent!,
};
} catch (error: any) {
return {
error: createError(error),
};
}
};

export const confirmSetupIntent = async (
paymentIntentClientSecret: string,
params: SetupIntent.ConfirmParams,
Expand Down
13 changes: 13 additions & 0 deletions src/hooks/useStripe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
RetrieveSetupIntentResult,
ConfirmPaymentResult,
HandleNextActionResult,
HandleNextActionForSetupResult,
ConfirmSetupIntentResult,
CreateTokenForCVCUpdateResult,
StripeError,
Expand Down Expand Up @@ -36,6 +37,7 @@ import {
confirmSetupIntent,
createTokenForCVCUpdate,
handleNextAction,
handleNextActionForSetup,
handleURLCallback,
initPaymentSheet,
presentPaymentSheet,
Expand Down Expand Up @@ -115,6 +117,16 @@ export function useStripe() {
[]
);

const _handleNextActionForSetup = useCallback(
async (
setupIntentClientSecret: string,
returnURL?: string
): Promise<HandleNextActionForSetupResult> => {
return handleNextActionForSetup(setupIntentClientSecret, returnURL);
},
[]
);

const _confirmSetupIntent = useCallback(
async (
paymentIntentClientSecret: string,
Expand Down Expand Up @@ -308,6 +320,7 @@ export function useStripe() {
confirmPayment: _confirmPayment,
createPaymentMethod: _createPaymentMethod,
handleNextAction: _handleNextAction,
handleNextActionForSetup: _handleNextActionForSetup,
confirmSetupIntent: _confirmSetupIntent,
createTokenForCVCUpdate: _createTokenForCVCUpdate,
handleURLCallback: _handleURLCallback,
Expand Down
10 changes: 10 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ export type HandleNextActionResult =
error: StripeError<CardActionError>;
};

export type HandleNextActionForSetupResult =
| {
setupIntent: SetupIntent.Result;
error?: undefined;
}
| {
setupIntent?: undefined;
error: StripeError<CardActionError>;
};

export type ConfirmSetupIntentResult =
| {
setupIntent: SetupIntent.Result;
Expand Down
Loading