Skip to content

Commit

Permalink
feat(payments): INT-1997 Add optional target parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricio-sg committed Dec 4, 2019
1 parent d5a390a commit 74ba9c9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"webpack": "^1.13.2"
},
"dependencies": {
"@bigcommerce/form-poster": "^1.2.1",
"@bigcommerce/form-poster": "^1.4.0",
"deep-assign": "^2.0.0",
"object-assign": "^4.1.0"
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ export default class Client {
/**
* @param {PaymentRequestData} data
* @param {Function} [callback]
* @param {string} target
* @returns {void}
*/
initializeOffsitePayment(data, callback) {
this.offsitePaymentInitializer.initializeOffsitePayment(data, callback);
initializeOffsitePayment(data, callback, target) {
this.offsitePaymentInitializer.initializeOffsitePayment(data, callback, target);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/payment/offsite-payment-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ export default class OffsitePaymentInitializer {
/**
* @param {PaymentRequestData} data
* @param {Function} [callback]
* @param {string} target
* @returns {void}
* @throws {Error}
*/
initializeOffsitePayment(data, callback) {
initializeOffsitePayment(data, callback, target) {
const { paymentMethod = {} } = data;

if (paymentMethod.type !== HOSTED) {
Expand All @@ -58,6 +59,6 @@ export default class OffsitePaymentInitializer {
const payload = this.payloadMapper.mapToPayload(data);
const url = this.urlHelper.getOffsitePaymentUrl();

this.formPoster.postForm(url, payload, callback);
this.formPoster.postForm(url, payload, callback, target);
}
}
21 changes: 19 additions & 2 deletions test/client/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Client', () => {
let offsitePaymentInitializer;
let paymentSubmitter;
let storeRequestSender;
let target;

beforeEach(() => {
config = { host: 'https://bigpay.dev' };
Expand All @@ -34,6 +35,8 @@ describe('Client', () => {
deleteShopperInstrument: jasmine.createSpy('deleteShopperInstrument'),
};

target = undefined;

client = new Client(
config,
paymentSubmitter,
Expand All @@ -59,7 +62,7 @@ describe('Client', () => {
expect(instance instanceof Client).toEqual(true);
});

it('initializes the offsite payment flow', () => {
it('initializes the offsite payment flow with the default target', () => {
const callback = () => {};
const data = merge({}, paymentRequestDataMock, {
paymentMethod: {
Expand All @@ -69,7 +72,21 @@ describe('Client', () => {

client.initializeOffsitePayment(data, callback);

expect(offsitePaymentInitializer.initializeOffsitePayment).toHaveBeenCalledWith(data, callback);
expect(offsitePaymentInitializer.initializeOffsitePayment).toHaveBeenCalledWith(data, callback, target);
});

it('initializes the offsite payment flow with the provided target', () => {
target = 'target_iframe';
const callback = () => {};
const data = merge({}, paymentRequestDataMock, {
paymentMethod: {
type: HOSTED,
},
});

client.initializeOffsitePayment(data, callback, target);

expect(offsitePaymentInitializer.initializeOffsitePayment).toHaveBeenCalledWith(data, callback, target);
});

it('submits the payment data', () => {
Expand Down
17 changes: 15 additions & 2 deletions test/payment/offsite-payment-initializer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('OffsitePaymentInitializer', () => {
let formPoster;
let offsitePaymentInitializer;
let payloadMapper;
let target;
let transformedData;
let urlHelper;

Expand All @@ -32,6 +33,8 @@ describe('OffsitePaymentInitializer', () => {
mapToPayload: jasmine.createSpy('mapToPayload').and.returnValue(transformedData),
};

target = undefined;

offsitePaymentInitializer = new OffsitePaymentInitializer(urlHelper, formPoster, payloadMapper);
});

Expand All @@ -48,13 +51,23 @@ describe('OffsitePaymentInitializer', () => {
expect(payloadMapper.mapToPayload).toHaveBeenCalled();
});

it('posts the request payload containing payment information to the server using a hidden HTML form', () => {
it('posts the request payload containing payment information to the server using a hidden HTML form with the default target', () => {
const callback = () => {};
const url = urlHelper.getOffsitePaymentUrl();

offsitePaymentInitializer.initializeOffsitePayment(data, callback);

expect(formPoster.postForm).toHaveBeenCalledWith(url, transformedData, callback);
expect(formPoster.postForm).toHaveBeenCalledWith(url, transformedData, callback, target);
});

it('posts the request payload containing payment information to the server using a hidden HTML form with the provided target', () => {
target = 'target_iframe';
const callback = () => {};
const url = urlHelper.getOffsitePaymentUrl();

offsitePaymentInitializer.initializeOffsitePayment(data, callback, target);

expect(formPoster.postForm).toHaveBeenCalledWith(url, transformedData, callback, target);
});

it('throws an error if the payment method is not a hosted provider', () => {
Expand Down

0 comments on commit 74ba9c9

Please sign in to comment.