diff --git a/types/applepayjs/applepayjs-tests.ts b/types/applepayjs/applepayjs-tests.ts new file mode 100644 index 0000000000..26c61396ae --- /dev/null +++ b/types/applepayjs/applepayjs-tests.ts @@ -0,0 +1,273 @@ +// Copyright (c) Martin Costello, 2017. All rights reserved. +// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. + +declare function describe(desc: string, fn: () => void): void; +declare function it(desc: string, fn: () => void): void; + +describe("ApplePaySession", () => { + it("the constants are defined", () => { + let status = 0; + switch (status) { + case ApplePaySession.STATUS_FAILURE: + case ApplePaySession.STATUS_INVALID_BILLING_POSTAL_ADDRESS: + case ApplePaySession.STATUS_INVALID_SHIPPING_CONTACT: + case ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS: + case ApplePaySession.STATUS_PIN_INCORRECT: + case ApplePaySession.STATUS_PIN_LOCKOUT: + case ApplePaySession.STATUS_PIN_REQUIRED: + case ApplePaySession.STATUS_SUCCESS: + default: + break; + } + }); + it("can create a new instance", () => { + + const version = 1; + const paymentRequest = { + countryCode: "US", + currencyCode: "USD", + supportedNetworks: [ + "masterCard", + "visa" + ], + merchantCapabilities: [ + "supports3DS" + ], + total: { + label: "My Store", + amount: "9.99" + } + }; + + const session = new ApplePaySession(version, paymentRequest); + }); + it("can call static methods", () => { + + const merchantIdentifier = "MyMerchantId"; + + let canMakePayments: boolean = ApplePaySession.canMakePayments(); + let supported: boolean = ApplePaySession.supportsVersion(2); + + ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier) + .then((status: boolean) => { + console.log(`Can make payments with active card: ${status}.`); + }); + + ApplePaySession.openPaymentSetup(merchantIdentifier) + .then((success) => { + console.log(`Apple Pay setup: ${success}.`); + }); + }); + it("can call instance methods", () => { + + const version = 1; + const paymentRequest = { + countryCode: "US", + currencyCode: "USD", + supportedNetworks: [ + "masterCard", + "visa" + ], + merchantCapabilities: [ + "supports3DS" + ], + total: { + label: "My Store", + amount: "9.99" + } + }; + + const session = new ApplePaySession(version, paymentRequest); + + session.abort(); + session.completeMerchantValidation({ + foo: "bar" + }); + session.completePayment(ApplePaySession.STATUS_SUCCESS); + + const total = { + label: "Subtotal", + type: "final", + amount: "35.00" + }; + + const lineItems = [ + { + label: "Subtotal", + type: "final", + amount: "35.00" + }, + { + label: "Free Shipping", + amount: "0.00", + type: "pending" + }, + { + label: "Estimated Tax", + amount: "3.06" + } + ]; + + const shippingMethods = [ + { + label: "Free Shipping", + detail: "Arrives in 5 to 7 days", + amount: "0.00", + identifier: "FreeShipping" + }, + { + label: "2-hour Shipping", + amount: "5.00" + } + ]; + + session.completePaymentMethodSelection(total, lineItems); + + session.completeShippingContactSelection( + ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, + shippingMethods, + total, + lineItems); + + session.completeShippingMethodSelection( + ApplePaySession.STATUS_SUCCESS, + total, + lineItems); + + session.oncancel = (event: ApplePayJS.Event): void => { + event.cancelBubble = true; + }; + + session.onpaymentauthorized = (event: ApplePayJS.ApplePayPaymentAuthorizedEvent) => { + if (event.payment) { + console.log("Payment data:", JSON.stringify(event.payment)); + } + }; + + session.onpaymentmethodselected = (event: ApplePayJS.ApplePayPaymentMethodSelectedEvent) => { + if (event.paymentMethod) { + console.log("Payment method:", JSON.stringify(event.paymentMethod)); + } + }; + + session.onshippingcontactselected = (event: ApplePayJS.ApplePayShippingContactSelectedEvent) => { + if (event.shippingContact) { + console.log("Shipping contact:", JSON.stringify(event.shippingContact)); + } + }; + + session.onshippingmethodselected = (event: ApplePayJS.ApplePayShippingMethodSelectedEvent) => { + if (event.shippingMethod) { + console.log("Shipping method:", JSON.stringify(event.shippingMethod)); + } + }; + + session.onvalidatemerchant = (event: ApplePayJS.ApplePayValidateMerchantEvent) => { + if (event.validationURL) { + console.log(`The validation URL is '${event.validationURL}'.`); + } + }; + }); +}); +describe("ApplePayPaymentRequest", () => { + it("can create a new instance", () => { + + let paymentRequest: ApplePayJS.ApplePayPaymentRequest = { + applicationData: "ApplicationData", + countryCode: "GB", + currencyCode: "GBP", + merchantCapabilities: [ + "supports3DS", + "supportsCredit", + "supportsDebit" + ], + supportedNetworks: [ + "amex", + "discover", + "jcb", + "master​Card", + "private​Label", + "visa" + ], + total: { + label: "Apple", + type: "final", + amount: "9.99" + } + }; + + paymentRequest.billingContact = { + emailAddress: "ravipatel@example.com", + familyName: "Patel", + givenName: "Ravi", + phoneNumber: "(408) 555-5555", + addressLines: [ + "1 Infinite Loop" + ], + locality: "Cupertino", + administrativeArea: "CA", + postalCode: "95014", + country: "United States", + countryCode: "US" + }; + + paymentRequest.lineItems = [ + { + label: "Subtotal", + type: "final", + amount: "35.00" + }, + { + label: "Free Shipping", + amount: "0.00", + type: "pending" + }, + { + label: "Estimated Tax", + amount: "3.06" + } + ]; + + paymentRequest.requiredBillingContactFields = [ + "postalAddress", + "name" + ]; + + paymentRequest.requiredShippingContactFields = [ + "postalAddress", + "name", + "phone", + "email" + ]; + + paymentRequest.shippingContact = { + emailAddress: "ravipatel@example.com", + familyName: "Patel", + givenName: "Ravi", + phoneNumber: "(408) 555-5555", + addressLines: [ + "1 Infinite Loop" + ], + locality: "Cupertino", + administrativeArea: "CA", + postalCode: "95014", + country: "United States", + countryCode: "US" + }; + + paymentRequest.shippingMethods = [ + { + label: "Free Shipping", + detail: "Arrives in 5 to 7 days", + amount: "0.00", + identifier: "FreeShipping" + }, + { + label: "2-hour Shipping", + amount: "5.00" + } + ]; + + paymentRequest.shippingType = "storePickup"; + }); +}); diff --git a/types/applepayjs/index.d.ts b/types/applepayjs/index.d.ts new file mode 100644 index 0000000000..52af393ebd --- /dev/null +++ b/types/applepayjs/index.d.ts @@ -0,0 +1,576 @@ +// Type definitions for Apple Pay JS 1.0 +// Project: https://developer.apple.com/reference/applepayjs +// Definitions by: Martin Costello +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * A session object for managing the payment process on the web. + */ +declare class ApplePaySession extends EventTarget { + + /** + * Creates a new instance of the ApplePaySession class. + * @param version - The version of the ApplePay JS API you are using. + * @param paymentRequest - An Apple​Pay​Payment​Request object that contains the information that is displayed on the Apple Pay payment sheet. + */ + constructor(version: number, paymentRequest: ApplePayJS.ApplePayPaymentRequest); + + /** + * A callback function that is automatically called when the payment UI is dismissed with an error. + */ + oncancel: (event: ApplePayJS.Event) => void; + + /** + * A callback function that is automatically called when the user has authorized the Apple Pay payment, typically via TouchID. + */ + onpaymentauthorized: (event: ApplePayJS.ApplePayPaymentAuthorizedEvent) => void; + + /** + * A callback function that is automatically called when a new payment method is selected. + */ + onpaymentmethodselected: (event: ApplePayJS.ApplePayPaymentMethodSelectedEvent) => void; + + /** + * A callback function that is called when a shipping contact is selected in the payment sheet. + */ + onshippingcontactselected: (event: ApplePayJS.ApplePayShippingContactSelectedEvent) => void; + + /** + * A callback function that is automatically called when a shipping method is selected. + */ + onshippingmethodselected: (event: ApplePayJS.ApplePayShippingMethodSelectedEvent) => void; + + /** + * A callback function that is automatically called when the payment sheet is displayed. + */ + onvalidatemerchant: (event: ApplePayJS.ApplePayValidateMerchantEvent) => void; + + /** + * Indicates whether or not the device supports Apple Pay. + * @returns true if the device supports making payments with Apple Pay; otherwise, false. + */ + static canMakePayments(): boolean; + + /** + * Indicates whether or not the device supports Apple Pay and if the user has an active card in Wallet. + * @param merchantIdentifier - The merchant ID received when the merchant enrolled in Apple Pay. + * @returns true if the device supports Apple Pay and there is at least one active card in Wallet; otherwise, false. + */ + static canMakePaymentsWithActiveCard(merchantIdentifier: string): Promise; + + /** + * Displays the Set up Apple Pay button. + * @param merchantIdentifier - The merchant ID received when the merchant enrolled in Apple Pay. + * @returns A boolean value indicating whether setup was successful. + */ + static openPaymentSetup(merchantIdentifier: string): Promise; + + /** + * Verifies if a web browser supports a given Apple Pay JS API version. + * @param version - A number representing the Apple Pay JS API version being checked. The initial version is 1. + * @returns A boolean value indicating whether the web browser supports the given API version. Returns false if the web browser does not support the specified version. + */ + static supportsVersion(version: number): boolean; + + /** + * Aborts the current Apple Pay session. + */ + abort(): void; + + /** + * Begins the merchant validation process. + */ + begin(): void; + + /** + * Call after the merchant has been validated. + * @param merchantSession - An opaque message session object. + */ + completeMerchantValidation(merchantSession: any): void; + + /** + * Call when a payment has been authorized. + * @param status - The status of the payment. + */ + completePayment(status: number): void; + + /** + * Call after a payment method has been selected. + * @param newTotal - An Apple​Pay​Line​Item dictionary representing the total price for the purchase. + * @param newLineItems - A sequence of Apple​Pay​Line​Item dictionaries. + */ + completePaymentMethodSelection(newTotal: ApplePayJS.ApplePayLineItem, newLineItems: ApplePayJS.ApplePayLineItem[]): void; + + /** + * Call after a shipping contact has been selected. + * @param status - The status of the shipping contact update. + * @param newShippingMethods - A sequence of ApplePayShippingMethod dictionaries. + * @param newTotal - An Apple​Pay​Line​Item dictionary representing the total price for the purchase. + * @param newLineItems - A sequence of Apple​Pay​Line​Item dictionaries. + */ + completeShippingContactSelection( + status: number, + newShippingMethods: ApplePayJS.ApplePayShippingMethod[], + newTotal: ApplePayJS.ApplePayLineItem, + newLineItems: ApplePayJS.ApplePayLineItem[]): void; + + /** + * Call after the shipping method has been selected. + * @param status - The status of the shipping method update. + * @param newTotal - An Apple​Pay​Line​Item dictionary representing the total price for the purchase. + * @param newLineItems - A sequence of Apple​Pay​Line​Item dictionaries. + */ + completeShippingMethodSelection(status: number, newTotal: ApplePayJS.ApplePayLineItem, newLineItems: ApplePayJS.ApplePayLineItem[]): void; + + /** + * The requested action succeeded. + */ + static readonly STATUS_SUCCESS: number; + + /** + * The requested action failed. + */ + static readonly STATUS_FAILURE: number; + + /** + * The billing address is not valid. + */ + static readonly STATUS_INVALID_BILLING_POSTAL_ADDRESS: number; + + /** + * The shipping address is not valid. + */ + static readonly STATUS_INVALID_SHIPPING_POSTAL_ADDRESS: number; + + /** + * The shipping contact information is not valid. + */ + static readonly STATUS_INVALID_SHIPPING_CONTACT: number; + + /** + * The PIN information is not valid. Cards on the China Union Pay network may require a PIN. + */ + static readonly STATUS_PIN_INCORRECT: number; + + /** + * The maximum number of tries for a PIN has been reached and the user has been locked out. Cards on the China Union Pay network may require a PIN. + */ + static readonly STATUS_PIN_LOCKOUT: number; + + /** + * The required PIN information was not provided. Cards on the China Union Pay payment network may require a PIN to authenticate the transaction. + */ + static readonly STATUS_PIN_REQUIRED: number; +} + +declare namespace ApplePayJS { + + /** + * Defines a line item in a payment request - for example, total, tax, discount, or grand total. + */ + interface ApplePayLineItem { + + /** + * A short, localized description of the line item. + */ + label: string; + + /** + * The line item's amount. + */ + amount: string; + + /** + * A value that indicates if the line item is final or pending. + */ + type?: string; + } + + /** + * Represents the result of authorizing a payment request and contains encrypted payment information. + */ + interface ApplePayPayment { + + /** + * The encrypted token for an authorized payment. + */ + token: ApplePayPaymentToken; + + /** + * The billing contact selected by the user for this transaction. + */ + billingContact?: ApplePayPaymentContact; + + /** + * The shipping contact selected by the user for this transaction. + */ + shippingContact?: ApplePayPaymentContact; + } + + /** + * The Apple​Pay​Payment​Authorized​Event class defines the attributes contained by the ApplePaySession.onpaymentauthorized callback function. + */ + abstract class ApplePayPaymentAuthorizedEvent extends Event { + + /** + * The payment token used to authorize a payment. + */ + readonly payment: ApplePayPayment; + } + + /** + * Encapsulates contact information needed for billing and shipping. + */ + interface ApplePayPaymentContact { + + /** + * An email address for the contact. + */ + emailAddress: string; + + /** + * The contact's family name. + */ + familyName: string; + + /** + * The contact's given name. + */ + givenName: string; + + /** + * A phone number for the contact. + */ + phoneNumber: string; + + /** + * The address for the contact. + */ + addressLines: string[]; + + /** + * The city for the contact. + */ + locality: string; + + /** + * The state for the contact. + */ + administrativeArea: string; + + /** + * The zip code, where applicable, for the contact. + */ + postalCode: string; + + /** + * The colloquial country name for the contact. + */ + country: string; + + /** + * The contact's ISO country code. + */ + countryCode: string; + } + + /** + * Contains information about an Apple Pay payment card. + */ + interface ApplePayPaymentMethod { + + /** + * A string, suitable for display, that describes the card. + */ + displayName: string; + + /** + * A string, suitable for display, that is the name of the payment network backing the card. + * The value is one of the supported networks specified in the supported​Networks property of the Apple​Pay​Payment​Request. + */ + network: string; + + /** + * A value representing the card's type of payment. + */ + type: string; + + /** + * The payment pass object associated with the payment. + */ + paymentPass: ApplePayPaymentPass; + } + + /** + * The Apple​Pay​Payment​Method​Selected​Event class defines the attributes contained by the ApplePaySession.onpaymentmethodselected callback function. + */ + abstract class ApplePayPaymentMethodSelectedEvent extends Event { + + /** + * The card used to complete a payment. + */ + readonly paymentMethod: ApplePayPaymentMethod; + } + + /** + * Represents a provisioned payment card for Apple Pay payments. + */ + interface ApplePayPaymentPass { + + /** + * The unique identifier for the primary account number for the payment card. + */ + primaryAccountIdentifier: string; + + /** + * A version of the primary account number suitable for display in your UI. + */ + primaryAccountNumberSuffix: string; + + /** + * The unique identifier for the device-specific account number. + */ + deviceAccountIdentifier?: string; + + /** + * A version of the device account number suitable for display in your UI. + */ + deviceAccountNumberSuffix?: string; + + /** + * The activation state of the pass. + */ + activationState: string; + } + + /** + * Encapsulates a request for payment, including information about payment processing capabilities, the payment amount, and shipping information. + */ + interface ApplePayPaymentRequest { + + /** + * The merchant's two-letter ISO 3166 country code. + */ + countryCode: string; + + /** + * The three-letter ISO 4217 currency code for the payment. + */ + currencyCode: string; + + /** + * A set of line items that explain recurring payments and/or additional charges. + */ + lineItems?: ApplePayLineItem[]; + + /** + * The payment capabilities supported by the merchant. + * The value must at least contain ApplePayMerchantCapability.supports3DS. + */ + merchantCapabilities: string[]; + + /** + * The payment networks supported by the merchant. + */ + supportedNetworks: string[]; + + /** + * A line item representing the total for the payment. + */ + total: ApplePayLineItem; + + /** + * Billing contact information for the user. + */ + billingContact?: ApplePayPaymentContact; + + /** + * The billing information that you require from the user in order to process the transaction. + */ + requiredBillingContactFields?: string[]; + + /** + * The shipping information that you require from the user in order to fulfill the order. + */ + requiredShippingContactFields?: string[]; + + /** + * Shipping contact information for the user. + */ + shippingContact?: ApplePayPaymentContact; + + /** + * A set of shipping method objects that describe the available shipping methods. + */ + shippingMethods?: ApplePayShippingMethod[] | string[]; + + /** + * How the items are to be shipped. + */ + shippingType?: string; + + /** + * Optional user-defined data. + */ + applicationData?: string; + } + + /** + * Contains the user's payment credentials. + */ + interface ApplePayPaymentToken { + + /** + * An object containing the encrypted payment data. + */ + paymentData: any; + + /** + * Information about the card used in the transaction. + */ + paymentMethod: ApplePayPaymentMethod; + + /** + * A unique identifier for this payment. + */ + transactionIdentifier: string; + } + + /** + * The Apple​Pay​Shipping​Contact​Selected​Event class defines the attributes contained by the ApplePaySession.onshippingcontactselected callback function. + */ + abstract class ApplePayShippingContactSelectedEvent extends Event { + + /** + * The shipping address selected by the user. + */ + readonly shippingContact: ApplePayPaymentContact; + } + + /** + * Defines a shipping method for delivering physical goods. + */ + interface ApplePayShippingMethod { + + /** + * A short description of the shipping method. + */ + label: string; + + /** + * A further description of the shipping method. + */ + detail?: string; + + /** + * The amount associated with this shipping method. + */ + amount: string; + + /** + * A client-defined identifier. + */ + identifier?: string; + } + + /** + * The Apple​Pay​Shipping​Method​Selected​Event class defines the attribute contained by the ApplePaySession.onshippingmethodselected callback function. + */ + abstract class ApplePayShippingMethodSelectedEvent extends Event { + + /** + * The shipping method selected by the user. + */ + readonly shippingMethod: ApplePayShippingMethod; + } + + /** + * The Apple​Pay​Validate​Merchant​Event class defines the attributes contained by the ApplePaySession.onvalidatemerchant callback function. + */ + abstract class ApplePayValidateMerchantEvent extends Event { + + /** + * The URL used to validate the merchant server. + */ + readonly validationURL: string; + } + + abstract class Event { + + readonly bubbles: boolean; + + cancelBubble: boolean; + + readonly cancelable: boolean; + + readonly composed: boolean; + + readonly currentTarget: EventTarget; + + readonly defaultPrevented: boolean; + + readonly eventPhase: number; + + readonly isTrusted: boolean; + + returnValue: boolean; + + readonly srcElement: EventTarget; + + readonly target: EventTarget; + + readonly timeStamp: string; + + readonly type: string; + + composedPath(): Node[]; + + initEvent(type?: string, bubbles?: boolean, cancelable?: boolean): void; + + preventDefault(): void; + + stopImmediatePropagation(): void; + + stopPropagation(): void; + + static readonly AT_TARGET: number; + + static readonly BLUR: number; + + static readonly BUBBLING_PHASE: number; + + static readonly CAPTURING_PHASE: number; + + static readonly CHANGE: number; + + static readonly CLICK: number; + + static readonly DBLCLICK: number; + + static readonly DRAGDROP: number; + + static readonly FOCUS: number; + + static readonly KEYDOWN: number; + + static readonly KEYPRESS: number; + + static readonly KEYUP: number; + + static readonly MOUSEDOWN: number; + + static readonly MOUSEDRAG: number; + + static readonly MOUSEMOVE: number; + + static readonly MOUSEOUT: number; + + static readonly MOUSEOVER: number; + + static readonly MOUSEUP: number; + + static readonly NONE: number; + + static readonly SELECT: number; + } +} diff --git a/types/applepayjs/tsconfig.json b/types/applepayjs/tsconfig.json new file mode 100644 index 0000000000..0bb26b97ac --- /dev/null +++ b/types/applepayjs/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "baseUrl": "../", + "forceConsistentCasingInFileNames": true, + "lib": [ + "dom", + "es6" + ], + "module": "commonjs", + "noImplicitAny": true, + "noImplicitThis": true, + "noEmit": true, + "strictNullChecks": true, + "typeRoots": [ + "../" + ], + "types": [] + }, + "files": [ + "index.d.ts", + "applepayjs-tests.ts" + ] +} diff --git a/types/applepayjs/tslint.json b/types/applepayjs/tslint.json new file mode 100644 index 0000000000..0344a1a3fa --- /dev/null +++ b/types/applepayjs/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +}