Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept Billing Address in AlternativePaymentMethods Submit #838

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class AlternativePaymentMethods extends Emitter {
}
}

async submit () {
async submit ({ billingAddress } = {}) {
this.validateBillingAddress(billingAddress);
try {
const token = await this.tokenizePaymentMethod();
const token = await this.tokenizePaymentMethod({ billingAddress });
this.emit('token', token);
} catch (err) {
this.error(err);
Expand All @@ -43,6 +44,10 @@ class AlternativePaymentMethods extends Emitter {

async handleAction (paymentResponse) {
try {
if (typeof (paymentResponse) === 'string') {
paymentResponse = JSON.parse(paymentResponse);
}

this.gatewayStrategy.handleAction(paymentResponse);
} catch (err) {
this.error(err);
Expand Down Expand Up @@ -151,7 +156,7 @@ class AlternativePaymentMethods extends Emitter {
return this.gatewayStrategy.createAndMountWebComponent(paymentMethodData);
}

async tokenizePaymentMethod () {
async tokenizePaymentMethod ({ billingAddress }) {
return this.recurly.request.post({
route: '/payment_methods/token',
data: {
Expand All @@ -162,9 +167,23 @@ class AlternativePaymentMethods extends Emitter {
channel: this.options.channel || 'Web',
paymentMethodData: this.gatewayStrategy.data,
gatewayType: this.gatewayType,
returnURL: this.options.returnURL,
billingAddress,
},
});
}

validateBillingAddress (billingAddress) {
if (!billingAddress) {
return;
}

const validProperties = ['address1', 'address2', 'city', 'state', 'postalCode', 'country'];
const invalidProperties = Object.keys(billingAddress).filter(prop => !validProperties.includes(prop));
if (invalidProperties.length) {
throw recurlyError('invalid-billing-address-fields', { field: invalidProperties.join(', ') });
}
}
}

export default AlternativePaymentMethods;
2 changes: 1 addition & 1 deletion lib/recurly/alternative-payment-methods/gateways/adyen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AdyenGateway extends Base {
constructor (options) {
super(options);

this.state = undefined;
this.state = {};
this.webComponent = undefined;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/recurly/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ const ERRORS = [
code: 'risk-preflight-timeout',
message: c => `recurly.Risk timeout out in preflight procedure for ${c.processor}. Skipping.`,
classification: 'internal'
},
{
code: 'invalid-billing-address-fields',
message: 'The billing address provided fields are invalid.',
classification: 'merchant'
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('Recurly.AlternativePaymentMethods', () => {
containerSelector: 'my-div',
adyen: {
publicKey: '123',
}
},
returnURL: 'https://merchant-website.test/completed',
};
});

Expand Down Expand Up @@ -231,6 +232,47 @@ describe('Recurly.AlternativePaymentMethods', () => {
}));
});

context('when a billingAddress is specified', () => {
beforeEach((done) => {
paymentMethods = recurly.AlternativePaymentMethods(params);
paymentMethods.start().finally(done);
});

it('sends billingAddress when tokenizing', (done) => {
sandbox.stub(recurly.request, 'post').resolves({});

const billingAddress = {
address1: '123 Main St',
address2: 'Suite 701',
city: 'San Francisco',
state: 'CA',
postalCode: '94105',
country: 'US',
};

const { onChange } = window.AdyenCheckout.getCall(0).args[0];
onChange({ data: 'payment-method-state', isValid: true });

paymentMethods.submit({ billingAddress });

nextTick(() => assertDone(done, () => {
assert.equal(recurly.request.post.called, true);
assert.deepEqual(recurly.request.post.getCall(0).args[0].route, '/payment_methods/token');
assert.deepEqual(recurly.request.post.getCall(0).args[0].data, {
currency: 'USD',
amount: 100,
countryCode: 'US',
locale: 'en-US',
channel: 'Web',
paymentMethodData: 'payment-method-state',
gatewayType: 'adyen',
returnURL: 'https://merchant-website.test/completed',
billingAddress: billingAddress,
});
}));
});
});

const validateTokenization = submit => {
it("make a POST /js/v1/payment_methods/token with the needed params", done => {
sandbox.stub(recurly.request, 'post').resolves({ });
Expand All @@ -246,7 +288,9 @@ describe('Recurly.AlternativePaymentMethods', () => {
locale: 'en-US',
channel: 'Web',
paymentMethodData: 'boleto-state',
gatewayType: 'adyen'
gatewayType: 'adyen',
returnURL: 'https://merchant-website.test/completed',
billingAddress: undefined,
});
}));
});
Expand Down
Loading