Skip to content

Commit

Permalink
Merge pull request #881 from recurly/support_cash_app
Browse files Browse the repository at this point in the history
Support Cash App
  • Loading branch information
ajkelso authored May 30, 2024
2 parents 6c2c76c + 3b44a58 commit 7e3be2d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class AlternativePaymentMethods extends Emitter {

async submit ({ billingAddress } = {}) {
this.validateBillingAddress(billingAddress);
if (this.gatewayStrategy.data?.paymentMethod?.type == 'cashapp') {
return await this.gatewayStrategy.submitWebComponent();
}
try {
const token = await this.tokenizePaymentMethod({ billingAddress });
this.emit('token', token);
Expand Down Expand Up @@ -124,7 +127,7 @@ class AlternativePaymentMethods extends Emitter {
throw recurlyError('payment-methods-not-available');
}

return new gatewayClass(this.options);
return new gatewayClass(this.options, this.recurly);
}

async setupGatewayStrategyListeners () {
Expand All @@ -139,6 +142,10 @@ class AlternativePaymentMethods extends Emitter {
this.gatewayStrategy.on('error', err => {
this.error(err);
});

this.gatewayStrategy.on('token', token => {
this.emit('token', token);
});
}

async loadExternalLibraries () {
Expand Down
51 changes: 42 additions & 9 deletions lib/recurly/alternative-payment-methods/gateways/adyen.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import Base from './base';

class AdyenGateway extends Base {
constructor (options) {
constructor (options, recurly) {
super(options);

this.recurly = recurly;
this.state = {};
this.gatewayType = 'adyen';
this.webComponent = undefined;
}

scripts () {
return [
{
url: 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.31.1/adyen.js',
urlintegrity: 'sha384-d6l5Qqod+Ks601U/jqsLz7QkW0LL6T5pfEsSHypuTSnDUYVGRLNV1ZdITbEwb1yL',
url: 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.44.0/adyen.js',
urlintegrity: 'sha384-EedHbKYY/Ev3kVMABmp+l25jEaNxkVg45aee29kCwCpd4DAQaNsVd3pgArwZX3JJ',
crossorigin: 'anonymous',
},
];
Expand All @@ -21,8 +22,8 @@ class AdyenGateway extends Base {
styles () {
return [
{
url: 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.31.1/adyen.css',
integrity: 'sha384-u0ZzEn9TjQx9ID0fkB21aOi32DxL9+b2ngTVz2x3q5wTi8sMfW3l49Dpe+TmBhb2',
url: 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.44.0/adyen.css',
integrity: 'sha384-ncGh5IPNy7Xx9N0SGgkQn36wdbE16y1E/iHvYdCiJqdNldS7gusM2QlbIIN77h1X',
crossorigin: 'anonymous',
},
];
Expand All @@ -42,6 +43,12 @@ class AdyenGateway extends Base {
paymentMethodsResponse: paymentMethodData,
environment: adyenOpts.env || 'test',
showPayButton: adyenOpts.showPayButton || false,
paymentMethodsConfiguration: {
cashapp: {
storePaymentMethod: true,
}
},
setStatusAutomatically: adyenOpts.setStatusAutomatically || false,
amount: adyenOpts.showPayButton
? {
value: this.options.amount,
Expand All @@ -54,11 +61,15 @@ class AdyenGateway extends Base {
},
onSubmit: state => {
this.state = state;
this.emit('submit');
this.tokenizePaymentMethod({}).then(token => {
this.emit('token', token);
}).catch(err => {
this.emit('error', err);
});
},
onError: err => {
this.emit('error', err);
}
},
});

this.webComponent = checkout
Expand All @@ -71,13 +82,35 @@ class AdyenGateway extends Base {
get data () {
const methodState = this.state.data;
const componentState = this.webComponent?.activePaymentMethod?.data;
const paymentMethodState = this.webComponent?.activePaymentMethod?.state;

return { ...methodState, ...componentState };
return { ...methodState, ...componentState, ...paymentMethodState };
}

async handleAction (action) {
return this.webComponent.handleAction(action);
}

async submitWebComponent () {
return this.webComponent.submit();
}

async tokenizePaymentMethod ({ billingAddress }) {
return this.recurly.request.post({
route: '/payment_methods/token',
data: {
currency: this.options.currency,
amount: this.options.amount,
countryCode: this.options.countryCode,
locale: this.options.locale || 'en-US',
channel: this.options.channel || 'Web',
paymentMethodData: this.data,
gatewayType: this.gatewayType,
returnURL: this.options.returnURL,
billingAddress,
},
});
}
}

export default AdyenGateway;
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ describe('Recurly.AlternativePaymentMethods', () => {

paymentMethods.start()
.finally(() => assertDone(done, () => {
assert.equal(dom.loadScript.getCall(0).args[0], 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.31.1/adyen.js');
assert.equal(dom.loadStyle.getCall(0).args[0], 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.31.1/adyen.css');
assert.equal(dom.loadScript.getCall(0).args[0], 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.44.0/adyen.js');
assert.equal(dom.loadStyle.getCall(0).args[0], 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/5.44.0/adyen.css');
}));
});

Expand Down

0 comments on commit 7e3be2d

Please sign in to comment.