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

Automatically select an ApplePay version #204

Merged
merged 1 commit into from Aug 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/ApplePay/ApplePay.test.ts
@@ -1,6 +1,10 @@
import ApplePay from '.';
import defaultProps from './defaultProps';

(global as any).ApplePaySession = {
supportsVersion: jest.fn(version => true)
};

describe('ApplePay', () => {
describe('formatProps', () => {
test('normalizes an amount in a legacy format', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/ApplePay/ApplePay.tsx
Expand Up @@ -3,9 +3,10 @@ import UIElement from '../UIElement';
import ApplePayButton from './components/ApplePayButton';
import ApplePayService from './ApplePayService';
import { preparePaymentRequest } from './payment-request';
import { normalizeAmount } from './utils';
import { normalizeAmount, resolveSupportedVersion } from './utils';
import defaultProps from './defaultProps';
import { ApplePayElementProps, ApplePayElementData } from './types';
const latestSupportedVersion = 10;

class ApplePayElement extends UIElement<ApplePayElementProps> {
protected static type = 'applepay';
Expand All @@ -22,11 +23,12 @@ class ApplePayElement extends UIElement<ApplePayElementProps> {
*/
protected formatProps(props) {
const amount = normalizeAmount(props);

const version = props.version || resolveSupportedVersion(latestSupportedVersion);
return {
onAuthorized: resolve => resolve(),
onValidateMerchant: (resolve, reject) => reject('onValidateMerchant event not implemented'),
...props,
version,
totalPriceLabel: props.totalPriceLabel || props.configuration?.merchantName,
amount,
onCancel: event => props.onError(event)
Expand Down
2 changes: 0 additions & 2 deletions src/components/ApplePay/defaultProps.ts
@@ -1,6 +1,4 @@
const defaultProps = {
version: 3,

// Transaction Information
amount: { currency: 'USD', value: 0 },

Expand Down
9 changes: 9 additions & 0 deletions src/components/ApplePay/utils.ts
Expand Up @@ -14,3 +14,12 @@ export function normalizeAmount(props: ApplePayElementProps): PaymentAmount {

return null;
}

export function resolveSupportedVersion(latestVersion) {
const versions = [];
for (let i = latestVersion; i > 0; i--) {
versions.push(i);
}

return versions.find(v => v && ApplePaySession.supportsVersion(v));
}