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

add openApplePaySetup method #525

Merged
merged 2 commits into from
Aug 24, 2021
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
4 changes: 4 additions & 0 deletions ios/StripeSdk.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ @interface RCT_EXTERN_MODULE(StripeSdk, RCTEventEmitter)
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(
openApplePaySetup: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(
updateApplePaySummaryItems:(NSArray *)summaryItems
errorAddressFields: (NSArray *)errorAddressFields
Expand Down
11 changes: 11 additions & 0 deletions ios/StripeSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ class StripeSdk: RCTEventEmitter, STPApplePayContextDelegate, STPBankSelectionVi
resolve([])
}

@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("Failed", "Cannot open payment setup"))
}
}


func applePayContext(_ context: STPApplePayContext, didSelect shippingMethod: PKShippingMethod, handler: @escaping (PKPaymentRequestShippingMethodUpdate) -> Void) {
self.shippingMethodUpdateHandler = handler
Expand Down
2 changes: 2 additions & 0 deletions src/NativeStripeSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
PayWithGooglePayResult,
CreateGooglePayPaymentMethodResult,
GooglePay,
OpenApplePaySetupResult,
} from './types';

type NativeStripeSdkType = {
Expand Down Expand Up @@ -72,6 +73,7 @@ type NativeStripeSdkType = {
createGooglePayPaymentMethod(
params: GooglePay.CreatePaymentMethodParams
): Promise<CreateGooglePayPaymentMethodResult>;
openApplePaySetup(): Promise<OpenApplePaySetupResult>;
};

const { StripeSdk } = NativeModules;
Expand Down
17 changes: 17 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
StripeError,
GooglePay,
CreateGooglePayPaymentMethodResult,
OpenApplePaySetupResult,
} from './types';
import type { Card } from './types/Card';

Expand Down Expand Up @@ -417,3 +418,19 @@ export const createGooglePayPaymentMethod = async (
};
}
};

export const openApplePaySetup = async (): Promise<OpenApplePaySetupResult> => {
try {
const { error } = await NativeStripeSdk.openApplePaySetup();
if (error) {
return {
error,
};
}
return {};
} catch (error) {
return {
error: createError(error),
};
}
};
21 changes: 15 additions & 6 deletions src/hooks/useApplePay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export function useApplePay({
}: Props = {}) {
const {
isApplePaySupported,
presentApplePay: presentApplePayNative,
confirmApplePayPayment: confirmApplePayPaymentNative,
presentApplePay: _presentApplePay,
confirmApplePayPayment: _confirmApplePayPayment,
updateApplePaySummaryItems,
openApplePaySetup: _openApplePaySetup,
} = useStripe();
const [items, setItems] = useState<ApplePay.CartSummaryItem[] | null>(null);
const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -129,31 +130,39 @@ export function useApplePay({
async (params: ApplePay.PresentParams) => {
setLoading(true);
setItems(params.cartItems);
const result = await presentApplePayNative(params);
const result = await _presentApplePay(params);
if (result.error) {
setItems(null);
}
setLoading(false);
return result;
},
[presentApplePayNative]
[_presentApplePay]
);

const openApplePaySetup = useCallback(async () => {
setLoading(true);
const result = await _openApplePaySetup();
setLoading(false);
return result;
}, [_openApplePaySetup]);

const confirmApplePayPayment = useCallback(
async (clientSecret: string) => {
setLoading(true);
const result = await confirmApplePayPaymentNative(clientSecret);
const result = await _confirmApplePayPayment(clientSecret);
setItems(null);
setLoading(false);
return result;
},
[confirmApplePayPaymentNative]
[_confirmApplePayPayment]
);

return {
loading,
presentApplePay,
confirmApplePayPayment,
isApplePaySupported,
openApplePaySetup,
};
}
12 changes: 10 additions & 2 deletions src/hooks/useStripe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
GooglePayInitResult,
GooglePay,
CreateGooglePayPaymentMethodResult,
OpenApplePaySetupResult,
} from '../types';
import { useCallback, useEffect, useState } from 'react';
import { isiOS } from '../helpers';
Expand All @@ -45,6 +46,7 @@ import {
initGooglePay,
createGooglePayPaymentMethod,
presentGooglePay,
openApplePaySetup,
} from '../functions';

/**
Expand Down Expand Up @@ -209,11 +211,16 @@ export function useStripe() {
async (
params: GooglePay.CreatePaymentMethodParams
): Promise<CreateGooglePayPaymentMethodResult> => {
return _createGooglePayPaymentMethod(params);
return createGooglePayPaymentMethod(params);
},
[]
);

const _openApplePaySetup =
useCallback(async (): Promise<OpenApplePaySetupResult> => {
return openApplePaySetup();
}, []);

return {
retrievePaymentIntent: _retrievePaymentIntent,
retrieveSetupIntent: _retrieveSetupIntent,
Expand All @@ -233,6 +240,7 @@ export function useStripe() {
createToken: _createToken,
initGooglePay: _initGooglePay,
presentGooglePay: _presentGooglePay,
createGooglePayPaymentMethod: createGooglePayPaymentMethod,
createGooglePayPaymentMethod: _createGooglePayPaymentMethod,
openApplePaySetup: _openApplePaySetup,
};
}
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,11 @@ export type CreateGooglePayPaymentMethodResult =
paymentMethod?: undefined;
error: StripeError<GooglePayError>;
};

export type OpenApplePaySetupResult =
| {
error?: undefined;
}
| {
error: StripeError<ApplePayError>;
};