From d1d68e7452babdc7a57212b3d184e4e2ec6fab04 Mon Sep 17 00:00:00 2001 From: Fluccioni Date: Tue, 10 Apr 2018 01:22:03 +0200 Subject: [PATCH] Add google pay web api definitions (#24860) --- types/googlepay/googlepay-tests.ts | 68 ++++++++++++ types/googlepay/index.d.ts | 160 +++++++++++++++++++++++++++++ types/googlepay/tsconfig.json | 24 +++++ types/googlepay/tslint.json | 3 + 4 files changed, 255 insertions(+) create mode 100644 types/googlepay/googlepay-tests.ts create mode 100644 types/googlepay/index.d.ts create mode 100644 types/googlepay/tsconfig.json create mode 100644 types/googlepay/tslint.json diff --git a/types/googlepay/googlepay-tests.ts b/types/googlepay/googlepay-tests.ts new file mode 100644 index 0000000000..ea97c2a586 --- /dev/null +++ b/types/googlepay/googlepay-tests.ts @@ -0,0 +1,68 @@ +const allowedPaymentMethods = new Array('CARD', 'TOKENIZED_CARD'); + +const allowedCardNetworks = new Array('AMEX', 'DISCOVER', 'JCB', 'MASTERCARD', 'VISA'); + +const tokenizationParameters: google.payments.api.PaymentMethodTokenizationParameters = { + tokenizationType: 'PAYMENT_GATEWAY', + parameters: { + gateway: 'example', + gatewayMerchantId: 'abc123' + } +}; + +const getGooglePaymentsClient = (env?: google.payments.api.EnvironmentType) => new google.payments.api.PaymentsClient({environment: env}); + +function onGooglePayLoaded() { + const client = getGooglePaymentsClient(); + + client.isReadyToPay({allowedPaymentMethods}).then(response => { + if (response.result) { + addGooglePayButton(); + prefetchGooglePaymentData(); + } + }).catch(err => { + console.error(err); + }); +} + +function addGooglePayButton() { + const button = document.createElement('button'); + button.className = 'google-pay'; + button.appendChild(document.createTextNode('Google Pay')); + button.addEventListener('click', onGooglePaymentButtonClick); + document.appendChild(document.createElement('div').appendChild(button)); +} + +function getGooglePaymentDataConfiguration(): google.payments.api.PaymentDataRequest { + return { + merchantId: '01234567890123456789', + transactionInfo: { + totalPriceStatus: 'FINAL', + totalPrice: '123.45', + currencyCode: 'USD' + }, + paymentMethodTokenizationParameters: tokenizationParameters, + allowedPaymentMethods, + cardRequirements: { + allowedCardNetworks, + billingAddressRequired: true, + billingAddressFormat: 'FULL' + }, + phoneNumberRequired: false, + shippingAddressRequired: true + }; +} + +function prefetchGooglePaymentData() { + const client = getGooglePaymentsClient(); + client.prefetchPaymentData(getGooglePaymentDataConfiguration()); +} + +function onGooglePaymentButtonClick() { + const request = getGooglePaymentDataConfiguration(); + const client = getGooglePaymentsClient(); + + client.loadPaymentData(request) + .then(data => console.log(data)) + .catch(err => console.error(err)); +} diff --git a/types/googlepay/index.d.ts b/types/googlepay/index.d.ts new file mode 100644 index 0000000000..524c277691 --- /dev/null +++ b/types/googlepay/index.d.ts @@ -0,0 +1,160 @@ +// Type definitions for Google Pay API 0.0 +// Project: https://developers.google.com/pay/api/web/setup/ +// Definitions by: Florian Luccioni +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace google.payments.api { + type AddressFormat = 'FULL' | 'MIN'; + type AllowedCardNetwork = 'AMEX' | 'DISCOVER' | 'JCB' | 'MASTERCARD' | 'VISA'; + type AllowedPaymentMethod = 'CARD' | 'TOKENIZED_CARD'; + type CardClass = 'CREDIT' | 'DEBIT'; + type CardInfo = CardInfoMin | CardInfoFull; + type CardRequirements = CardRequirementsMin | CardRequirementsFull; + type EnvironmentType = 'PRODUCTION' | 'TEST'; + type ErrorStatusCode = 'BUYER_ACCOUNT_ERROR' | 'CANCELED' | 'DEVELOPER_ERROR' | 'INTERNAL_ERROR'; + type PaymentMethodTokenizationParameters = PaymentMethodDirectTokenizationParameters | PaymentMethodGatewayTokenizationParameters; + type TokenizationType = 'DIRECT' | 'PAYMENT_GATEWAY'; + type TotalPriceStatus = 'ESTIMATED' | 'FINAL' | 'NOT_CURRENTLY_KNOWN'; + type UserAddress = UserAddressFull | UserAddressMin; + type PaymentDataRequest = PaymentDataRequestMin | PaymentDataRequestFull; + type PaymentData = PaymentDataMin | PaymentDataFull; + + interface PaymentOptions { + environment?: EnvironmentType; + } + + interface IsReadyToPayRequest { + allowedPaymentMethods: AllowedPaymentMethod[]; + } + + interface BasePaymentDataRequest { + merchantId: string; + transactionInfo: TransactionInfo; + cardRequirements: CardRequirements; + paymentMethodTokenizationParameters: PaymentMethodTokenizationParameters; + allowedPaymentMethods: AllowedPaymentMethod[]; + phoneNumberRequired?: boolean; + emailRequired?: boolean; + shippingAddressRequired?: boolean; + shippingAddressRequirements?: ShippingAddressRequirements; + } + + interface PaymentDataRequestMin extends BasePaymentDataRequest { + cardRequirements: CardRequirementsMin; + } + + interface PaymentDataRequestFull extends BasePaymentDataRequest { + cardRequirements: CardRequirementsFull; + } + + interface BasePaymentMethodTokenizationParameters { + tokenizationType: TokenizationType; + } + + interface PaymentMethodGatewayTokenizationParameters extends BasePaymentMethodTokenizationParameters { + tokenizationType: 'PAYMENT_GATEWAY'; + parameters: { + [parameter: string]: string; + }; + } + + interface PaymentMethodDirectTokenizationParameters extends BasePaymentMethodTokenizationParameters { + tokenizationType: 'DIRECT'; + parameters: { + publicKey: string; + }; + } + + interface BaseCardRequirements { + allowedCardNetworks: AllowedCardNetwork[]; + billingAddressRequired?: boolean; + billingAddressFormat?: AddressFormat; + } + + interface CardRequirementsMin extends BaseCardRequirements { + billingAddressFormat?: 'MIN'; + } + + interface CardRequirementsFull extends BaseCardRequirements { + billingAddressFormat?: 'FULL'; + } + + interface ShippingAddressRequirements { + allowedCountryCodes?: string[]; + } + + interface TransactionInfo { + totalPriceStatus: TotalPriceStatus; + totalPrice?: string; + currencyCode?: string; + } + + interface BasePaymentData { + cardInfo: CardInfo; + paymentMethodToken: PaymentMethodToken; + shippingAddress?: UserAddressFull; + email?: string; + } + + interface PaymentDataMin extends BasePaymentData { + cardInfo: CardInfoMin; + } + + interface PaymentDataFull extends BasePaymentData { + cardInfo: CardInfoFull; + } + + interface BaseCardInfo { + cardDescription: string; + cardClass: CardClass; + cardDetails: string; + cardNetwork: AllowedCardNetwork; + billingAddress?: UserAddress; + } + + interface CardInfoMin extends BaseCardInfo { + billingAddress?: UserAddressMin; + } + + interface CardInfoFull extends BaseCardInfo { + billingAddress?: UserAddressFull; + } + + interface UserAddressMin { + name: string; + postalCode: string; + countryCode: string; + phoneNumber?: string; + } + + interface UserAddressFull extends UserAddressMin { + companyName: string; + address1: string; + address2: string; + address3: string; + address4: string; + address5: string; + locality: string; + administrativeArea: string; + sortingCode: string; + } + + interface PaymentMethodToken { + tokenizationType: TokenizationType; + token: string; + } + + interface PaymentsError { + statusCode: ErrorStatusCode; + statusMessage: string; + } + + class PaymentsClient { + constructor(paymentOptions: PaymentOptions); + isReadyToPay(request: IsReadyToPayRequest): Promise<{result: boolean}>; + loadPaymentData(request: PaymentDataRequestMin): Promise; + loadPaymentData(request: PaymentDataRequestFull): Promise; + loadPaymentData(request: PaymentDataRequest): Promise; + prefetchPaymentData(request: PaymentDataRequest): void; + } +} diff --git a/types/googlepay/tsconfig.json b/types/googlepay/tsconfig.json new file mode 100644 index 0000000000..38fba1d6c8 --- /dev/null +++ b/types/googlepay/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "googlepay-tests.ts" + ] +} diff --git a/types/googlepay/tslint.json b/types/googlepay/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/googlepay/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}