diff --git a/lib/recurly/amazon/amazon-pay.js b/lib/recurly/amazon/amazon-pay.js index c899ebff6..8670f3684 100644 --- a/lib/recurly/amazon/amazon-pay.js +++ b/lib/recurly/amazon/amazon-pay.js @@ -5,27 +5,26 @@ class AmazonPay extends Emitter { constructor (recurly, options) { super(); this.recurly = recurly; - this.options = options; - this.region = this.options.region || 'us'; + this.options = Object.assign({ region: 'us' }, options); + this.options.region = this.options.region || 'us'; this.setLocaleAndCurrency(); this.start(); } - async start () { - try { - await Promise.all([ - this.loadExternalLibraries(), - this.obtainMerchantId(this.recurly) - ]); - } catch (err) { - this.error(err); - } + start () { + Promise.all([ + this.loadExternalLibraries(), + this.obtainMerchantId(this.recurly) + ]) + .then(() => this.emit('ready')) + .catch(err => this.emit('error', err)); } obtainMerchantId () { - this.recurly.request.get({ route: '/amazon_pay/button_render' }).then((data) => { - this.options.merchantId = data.merchant_id; - }); + return this.recurly.request.get({ route: '/amazon_pay/button_render', data: { region: this.options.region } }) + .then((data) => { + this.options.merchantId = data.merchant_id; + }); } scriptUrl () { @@ -42,30 +41,21 @@ class AmazonPay extends Emitter { return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = this.scriptUrl(); - script.onload = () => { - resolve; - this.emit('ready'); - }; + script.onload = resolve; script.onerror = reject; document.head.appendChild(script); }); } renderButton (element) { - return new Promise((resolve, reject) => { - const script = document.createElement('script'); - script.innerHTML = `const button = amazon.Pay.renderButton('#${element}', { - merchantId: '${this.options.merchantId}', - ledgerCurrency: '${this.options.currency}', - checkoutLanguage: '${this.options.locale}', - productType: 'PayOnly', - placement: 'Other', - sandbox: ${this.options.sandbox}, - buttonColor: 'Gold', - });`; - script.onload = resolve; - script.onerror = reject; - document.head.appendChild(script); + window.amazon.Pay.renderButton(`#${element}`, { + merchantId: this.options.merchantId, + ledgerCurrency: this.options.currency, + checkoutLanguage: this.options.locale, + productType: 'PayOnly', + placement: 'Other', + sandbox: this.options.sandbox, + buttonColor: 'Gold', }); } @@ -73,7 +63,7 @@ class AmazonPay extends Emitter { const defaultEventName = 'amazon-pay'; let gatewayCode = this.options.gatewayCode || ''; this.frame = this.recurly.Frame({ - path: `/amazon_pay/start?region=${this.region}&gateway_code=${gatewayCode}`, + path: `/amazon_pay/start?region=${this.options.region}&gateway_code=${gatewayCode}`, type: Frame.TYPES.WINDOW, defaultEventName }).on('error', cause => this.emit('error', cause)) // Emitted by js/v1/amazon_pay/cancel diff --git a/types/index.d.ts b/types/index.d.ts index ebb311e8a..6ebf3601f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -17,6 +17,7 @@ export * from './lib/address'; export * from './lib/adyen'; export * from './lib/bank-redirect'; export * from './lib/apple-pay/index'; +export * from './lib/amazon-pay'; export * from './lib/google-pay/index'; export * from './lib/bank-account'; export * from './lib/configure'; diff --git a/types/lib/amazon-pay.d.ts b/types/lib/amazon-pay.d.ts new file mode 100644 index 000000000..f65f13360 --- /dev/null +++ b/types/lib/amazon-pay.d.ts @@ -0,0 +1,34 @@ +import { Emitter } from './emitter'; + +export type AmazonPayOptions = { + /** + * 2 Digit Country Code + */ + region: string; + + /** + * The customer's locale. This is used to set the language rendered in the UI. + */ + locale: string; + + /** + * The currency of the payment. + */ + currency: string; + + /** + * Specify which Payment Gateway in Recurly must handle the payment. + */ + gatewayCode?: string +}; + +export type AmazonPayEvent = 'token' | 'error' | 'close'; + +export interface AmazonPayInstance extends Emitter { + /** + * Invokes the Amazon Payment Modal + */ + start: (amazonPayOptions: AmazonPayOptions) => void; +} + +export type AmazonPay = (amazonPayOptions?: AmazonPayOptions) => AmazonPayInstance; diff --git a/types/lib/recurly.d.ts b/types/lib/recurly.d.ts index bdf0d3321..0fafc80c4 100644 --- a/types/lib/recurly.d.ts +++ b/types/lib/recurly.d.ts @@ -3,6 +3,7 @@ import { Adyen } from './adyen'; import { BankRedirect } from './bank-redirect'; import { AlternativePaymentMethods } from './alternative-payment-methods'; import { ApplePay } from './apple-pay/index'; +import { AmazonPay } from './amazon-pay'; import { BankAccount } from './bank-account'; import { Configure } from './configure'; import { Elements } from './elements'; @@ -89,6 +90,13 @@ export interface Recurly extends Emitter { */ PayPal: PayPal; + /** + * Use Recurly to process Amazon Pay v2 transactions + * + * @see {@link https://recurly.com/developers/reference/recurly-js/index.html#amazon-pay-v2|AmazonPay} + */ + AmazonPay: AmazonPay; + /** * Recurly automates complicated subscriptions, with many factors influencing the total price at checkout. With this * in mind, Recurly.js provides a robust `recurly.Pricing.Checkout` class designed to make determining the actual