Skip to content

feat(payment): added canMakePayments method as additional verification of payment capability #2888

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ describe('ApplePayButtonStrategy', () => {
).rejects.toThrow(MissingDataError);
});

it('throws error if canMakePayments returns false', async () => {
jest.spyOn(console, 'error').mockImplementation(jest.fn());

MockApplePaySession.canMakePayments = jest.fn().mockReturnValue(false);

await strategy.initialize(getApplePayButtonInitializationOptions());

expect(console.error).toHaveBeenCalled();

MockApplePaySession.canMakePayments = jest.fn().mockReturnValue(true);
});

it('throws error when params object is empty', async () => {
await expect(
strategy.initialize({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ export default class ApplePayButtonStrategy implements CheckoutButtonStrategy {

assertApplePayWindow(window);

if (!this._sessionFactory.canMakePayment()) {
console.error('This device is not capable of making Apple Pay payments');

return;
}

if (!methodId || !applepay) {
throw new MissingDataError(MissingDataErrorType.MissingPaymentMethod);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ describe('ApplePayCustomerStrategy', () => {
await expect(strategy.initialize({})).rejects.toThrow(MissingDataError);
});

it('throws error if canMakePayments returns false', async () => {
jest.spyOn(console, 'error').mockImplementation(jest.fn());

MockApplePaySession.canMakePayments = jest.fn().mockReturnValue(false);

await strategy.initialize(getApplePayCustomerInitializationOptions());

expect(console.error).toHaveBeenCalled();

MockApplePaySession.canMakePayments = jest.fn().mockReturnValue(true);
});

it('sets up request for digital items', async () => {
const cart = getCart();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ export default class ApplePayCustomerStrategy implements CustomerStrategy {

assertApplePayWindow(window);

if (!this._sessionFactory.canMakePayment()) {
console.error('This device is not capable of making Apple Pay payments');

return;
}

try {
this._paymentMethod = state.getPaymentMethodOrThrow(methodId);
} catch (_e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ export default class ApplePaySessionFactory {

return new ApplePaySession(1, request);
}

canMakePayment(): boolean {
assertApplePayWindow(window);

return ApplePaySession.canMakePayments();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class MockApplePaySession {
static supportsVersion: () => boolean;
static canMakePayments: () => boolean;
static canMakePayments: () => boolean = jest.fn().mockReturnValue(true);

addEventListener = jest.fn();
dispatchEvent = jest.fn();
Expand Down