Skip to content

Commit

Permalink
Accept billing address
Browse files Browse the repository at this point in the history
  • Loading branch information
douglaslise committed Jul 13, 2023
1 parent efb0009 commit 4f0e938
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ 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);
}
}

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

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

this.gatewayStrategy.handleAction(paymentResponse);
} catch (err) {
this.error(err);
Expand Down Expand Up @@ -155,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 @@ -166,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

0 comments on commit 4f0e938

Please sign in to comment.