Skip to content

Commit

Permalink
Allows a PricingPromise to be provided to the ApplePay constructor
Browse files Browse the repository at this point in the history
- Coerces the underlying pricing instance from the PricingPromise
  • Loading branch information
chrissrogers committed Jan 21, 2021
1 parent c846c5a commit 9e93579
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 70 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
BROWSER,
BROWSER = 'Chrome',
VIRTUALBOX_EDGE_UUID,
VIRTUALBOX_IE11_UUID,
ANDROID_HOME,
Expand Down
17 changes: 11 additions & 6 deletions lib/recurly/apple-pay.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Emitter from 'component-emitter';
import errors from './errors';
import { Pricing } from './pricing';
import PricingPromise from './pricing/promise';
import { normalize } from '../util/normalize';
import decimalize from '../util/decimalize';
import { FIELDS } from './token';
Expand Down Expand Up @@ -172,18 +173,22 @@ class ApplePay extends Emitter {

if ('i18n' in options) Object.assign(this.config.i18n, options.i18n);

// Listen for pricing changes to update totals and currency
const { pricing } = options;
if (pricing instanceof Pricing) {
this.config.pricing = pricing;
pricing.on('change', () => this.onPricingChange());
if (pricing.hasPrice) this.onPricingChange();
if (options.pricing instanceof PricingPromise) {
this.config.pricing = options.pricing.pricing;
} else if (options.pricing instanceof Pricing) {
this.config.pricing = options.pricing;
} else if ('total' in options) {
this.config.total = options.total;
} else {
return this.initError = this.error('apple-pay-config-missing', { opt: 'total' });
}

// If pricing is provided, attach change listeners
if (this.config.pricing) {
this.config.pricing.on('change', () => this.onPricingChange());
if (this.config.pricing.hasPrice) this.onPricingChange();
}

this.recurly.request.get({
route: '/apple_pay/info',
data: { currency: options.currency },
Expand Down
Loading

0 comments on commit 9e93579

Please sign in to comment.