Skip to content

Commit

Permalink
mock functions, deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe committed Nov 22, 2022
1 parent a324580 commit ad62a0d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
9 changes: 2 additions & 7 deletions ios/StripeSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,8 @@ class StripeSdk: RCTEventEmitter, STPBankSelectionViewControllerDelegate, UIAdap

@objc(openApplePaySetup:rejecter:)
func openApplePaySetup(resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
let library = PKPassLibrary.init()
if (library.responds(to: #selector(PKPassLibrary.openPaymentSetup))) {
library.openPaymentSetup()
resolve([])
} else {
resolve(Errors.createError(ErrorType.Failed, "Cannot open payment setup"))
}
PKPassLibrary.init().openPaymentSetup()
resolve([])
}

@objc(confirmApplePayPayment:resolver:rejecter:)
Expand Down
44 changes: 44 additions & 0 deletions jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ const mockFunctions = {
paymentMethod: {},
error: null,
})),
isPlatformPaySupported: jest.fn(async () => true),
confirmPlatformPaySetupIntent: jest.fn(async () => ({
setupIntent: {},
error: null,
})),
confirmPlatformPayPayment: jest.fn(async () => ({
paymentIntent: {},
error: null,
})),
dismissPlatformPay: jest.fn(async () => true),
createPlatformPayPaymentMethod: jest.fn(async () => ({
paymentMethod: {},
token: {},
error: null,
})),
updatePlatformPaySheet: jest.fn(async () => ({
error: null,
})),
openPlatformPaySetup: jest.fn(async () => {
return;
}),
isApplePaySupported: jest.fn(async () => true),
presentApplePay: jest.fn(async () => ({
error: null,
Expand Down Expand Up @@ -141,6 +162,28 @@ const mockHooks = {
...mockFunctions.openApplePaySetup(),
})),
})),
usePlatformPay: jest.fn(() => ({
loading: false,
isPlatformPaySupported: true,
confirmPlatformPaySetupIntent: jest.fn(async () => ({
...mockFunctions.confirmPlatformPaySetupIntent(),
})),
confirmPlatformPayPayment: jest.fn(async () => ({
...mockFunctions.confirmPlatformPayPayment(),
})),
dismissPlatformPay: jest.fn(async () => ({
...mockFunctions.dismissPlatformPay(),
})),
createPlatformPayPaymentMethod: jest.fn(async () => ({
...mockFunctions.createPlatformPayPaymentMethod(),
})),
updatePlatformPaySheet: jest.fn(async () => ({
...mockFunctions.updatePlatformPaySheet(),
})),
openPlatformPaySetup: jest.fn(async () => ({
...mockFunctions.openPlatformPaySetup(),
})),
})),
usePaymentSheet: jest.fn(() => ({
loading: false,
initPaymentSheet: jest.fn(async () => ({
Expand Down Expand Up @@ -175,5 +218,6 @@ module.exports = {
AuBECSDebitForm: () => 'AuBECSDebitForm',
GooglePayButton: () => 'GooglePayButton',
AddToWalletButton: () => 'AddToWalletButton',
PlatformPayButton: () => 'PlatformPayButton',
useStripe: jest.fn(() => mockHooks),
};
18 changes: 18 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ export const confirmPayment = async (
}
};

/** @deprecated Use `isPlatformPaySupported` instead. */
export const isApplePaySupported = async (): Promise<boolean> => {
return isiOS && (await NativeStripeSdk.isApplePaySupported());
};

/** @deprecated Use `confirmPlatformPaySetupIntent`, `confirmPlatformPayPayment`, or `createPlatformPayPaymentMethod` instead. */
export const presentApplePay = async (
params: ApplePay.PresentParams
): Promise<ApplePayResult> => {
Expand Down Expand Up @@ -209,6 +211,7 @@ export const presentApplePay = async (
}
};

/** @deprecated Use `updatePlatformPaySheet` instead. */
export const updateApplePaySummaryItems = async (
summaryItems: ApplePay.CartSummaryItem[],
errorAddressFields: Array<{
Expand Down Expand Up @@ -239,6 +242,7 @@ export const updateApplePaySummaryItems = async (
}
};

/** @deprecated Use `confirmPlatformPaySetupIntent` or `confirmPlatformPayPayment` instead. */
export const confirmApplePayPayment = async (
clientSecret: string
): Promise<{ error?: StripeError<ApplePayError> }> => {
Expand Down Expand Up @@ -466,6 +470,7 @@ export const resetPaymentSheetCustomer = async (): Promise<null> => {
return await NativeStripeSdk.resetPaymentSheetCustomer();
};

/** @deprecated Use `isPlatformPaySupported` instead. */
export const isGooglePaySupported = async (
params?: GooglePay.IsSupportedParams
): Promise<boolean> => {
Expand All @@ -474,6 +479,7 @@ export const isGooglePaySupported = async (
);
};

/** @deprecated Use `confirmPlatformPaySetupIntent`, `confirmPlatformPayPayment`, or `createPlatformPayPaymentMethod` instead. */
export const initGooglePay = async (
params: GooglePay.InitParams
): Promise<GooglePayInitResult> => {
Expand All @@ -492,6 +498,7 @@ export const initGooglePay = async (
}
};

/** @deprecated Use `confirmPlatformPaySetupIntent`, `confirmPlatformPayPayment`, or `createPlatformPayPaymentMethod` instead. */
export const presentGooglePay = async (
params: GooglePay.PresentParams
): Promise<PayWithGooglePayResult> => {
Expand All @@ -510,6 +517,7 @@ export const presentGooglePay = async (
}
};

/** @deprecated Use `createPlatformPayPaymentMethod` instead. */
export const createGooglePayPaymentMethod = async (
params: GooglePay.CreatePaymentMethodParams
): Promise<CreateGooglePayPaymentMethodResult> => {
Expand All @@ -531,6 +539,7 @@ export const createGooglePayPaymentMethod = async (
}
};

/** @deprecated Use `openNativePaySetup` instead. */
export const openApplePaySetup = async (): Promise<OpenApplePaySetupResult> => {
try {
const { error } = await NativeStripeSdk.openApplePaySetup();
Expand Down Expand Up @@ -863,3 +872,12 @@ export const updatePlatformPaySheet = async (params: {
};
}
};

/**
* iOS only, this is a no-op on Android. Use this method to move users to the interface for adding credit cards.
* This method transfers control to the Wallet app on iPhone or to the Settings
* app on iPad. For devices that don’t support Apple Pay, this method does nothing.
*/
export const openPlatformPaySetup = async (): Promise<void> => {
await NativeStripeSdk.openApplePaySetup();
};
11 changes: 11 additions & 0 deletions src/hooks/usePlatformPay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function usePlatformPay() {
dismissPlatformPay,
updatePlatformPaySheet,
canAddCardToWallet,
openPlatformPaySetup,
} = useStripe();
const [loading, setLoading] = useState(false);

Expand Down Expand Up @@ -110,6 +111,10 @@ export function usePlatformPay() {
[canAddCardToWallet]
);

const _openPlatformPaySetup = useCallback(async (): Promise<void> => {
return openPlatformPaySetup();
}, [openPlatformPaySetup]);

return {
/** Use this boolean to present a spinner or other similar loading screen. `true` if the SDK is currently processing, `false` if it is not. */
loading,
Expand Down Expand Up @@ -159,5 +164,11 @@ export function usePlatformPay() {
* @returns A promise resolving to an object of type CanAddCardToWalletResult. Check the `canAddCard` field, if it's true, you should show the `<AddToWalletButton />`
*/
canAddCardToWallet: _canAddCardToWallet,
/**
* iOS only, this is a no-op on Android. Use this method to move users to the interface for adding credit cards.
* This method transfers control to the Wallet app on iPhone or to the Settings
* app on iPad. For devices that don’t support Apple Pay, this method does nothing.
*/
openPlatformPaySetup: _openPlatformPaySetup,
};
}
6 changes: 6 additions & 0 deletions src/hooks/useStripe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
dismissPlatformPay,
createPlatformPayPaymentMethod,
updatePlatformPaySheet,
openPlatformPaySetup,
} from '../functions';

/**
Expand Down Expand Up @@ -381,6 +382,10 @@ export function useStripe() {
[]
);

const _openPlatformPaySetup = useCallback(async (): Promise<void> => {
return openPlatformPaySetup();
}, []);

return {
retrievePaymentIntent: _retrievePaymentIntent,
retrieveSetupIntent: _retrieveSetupIntent,
Expand Down Expand Up @@ -422,5 +427,6 @@ export function useStripe() {
dismissPlatformPay: _dismissPlatformPay,
createPlatformPayPaymentMethod: _createPlatformPayPaymentMethod,
updatePlatformPaySheet: _updatePlatformPaySheet,
openPlatformPaySetup: _openPlatformPaySetup,
};
}

0 comments on commit ad62a0d

Please sign in to comment.