Skip to content

Commit

Permalink
Merge pull request #855 from recurly/amazon-follow-up
Browse files Browse the repository at this point in the history
Amazon follow up
  • Loading branch information
isaacvance1027 authored Oct 10, 2023
2 parents 4f3aa85 + 8f1d5e5 commit 76d6560
Showing 1 changed file with 83 additions and 7 deletions.
90 changes: 83 additions & 7 deletions lib/recurly/amazon/amazon-pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,97 @@ class AmazonPay extends Emitter {
super();
this.recurly = recurly;
this.options = options;

this.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);
}
}

obtainMerchantId () {
this.recurly.request.get({ route: '/amazon_pay/button_render' }).then((data) => {
this.options.merchantId = data.merchant_id;
});
}

scriptUrl () {
switch(this.options.region) {
case 'uk':
case 'eu':
return 'https://static-eu.payments-amazon.com/checkout.js';
case 'us':
return 'https://static-na.payments-amazon.com/checkout.js';
}
}

attach (element) {
this.parent = element;
this.region = this.options?.region || 'us';
loadExternalLibraries () {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = this.scriptUrl();
script.onload = () => {
resolve;
this.emit('ready');
};
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);
});
}

attach () {
const defaultEventName = 'amazon-pay';

this.frame = this.recurly.Frame({
path: `/amazon_pay/start?region=${this.region}`,
type: Frame.TYPES.WINDOW,
defaultEventName
}).on('error', cause => console.log(cause))
.on('done', results => {
this.emit('token', results);
});
}).on('error', cause => this.emit('error', cause)) // Emitted by js/v1/amazon_pay/cancel
.on('close', () => this.emit('error', 'closed')) // Emitted by RJS Frame when the window is manually closed
.on('done', results => this.emit('token', results)); // Emitted by js/v1/amazon_pay/finish
}

setLocaleAndCurrency () {
switch (this.options.region) {
case 'uk':
this.options.currency = 'GBP';
this.options.locale = 'en_GB';
break;
case 'eu':
this.options.currency = 'EUR';
this.options.locale = 'en_GB';
break;
case 'us':
this.options.currency = 'USD';
this.options.locale = 'en_US';
break;
}
}
}

Expand Down

0 comments on commit 76d6560

Please sign in to comment.