Add google pay web api definitions (#24860)

This commit is contained in:
Fluccioni
2018-04-10 01:22:03 +02:00
committed by Mohamed Hegazy
parent c0d45f6810
commit d1d68e7452
4 changed files with 255 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
const allowedPaymentMethods = new Array<google.payments.api.AllowedPaymentMethod>('CARD', 'TOKENIZED_CARD');
const allowedCardNetworks = new Array<google.payments.api.AllowedCardNetwork>('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));
}

160
types/googlepay/index.d.ts vendored Normal file
View File

@@ -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 <https://github.com/Fluccioni>
// 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<PaymentDataMin>;
loadPaymentData(request: PaymentDataRequestFull): Promise<PaymentDataFull>;
loadPaymentData(request: PaymentDataRequest): Promise<PaymentData>;
prefetchPaymentData(request: PaymentDataRequest): void;
}
}

View File

@@ -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"
]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}