-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payments): INT-1577 Add support for paymentIntent creations endp…
…oint
- Loading branch information
Carlos Lopez
committed
Jul 25, 2019
1 parent
34002fc
commit fdc3c5d
Showing
8 changed files
with
242 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import RequestSender from '../common/http-request/request-sender'; | ||
import UrlHelper from './url-helper'; | ||
import PaymentIntentMapper from './v2/payment-mappers/payment-intent-mapper'; | ||
|
||
export default class PaymentIntentGenerator { | ||
/** | ||
* @param {Object} config | ||
* @returns {PaymentSubmitter} | ||
*/ | ||
static create(config) { | ||
const urlHelper = UrlHelper.create(config); | ||
const requestSender = RequestSender.create(); | ||
const paymentIntentMapper = PaymentIntentMapper.create(); | ||
|
||
return new PaymentIntentGenerator(urlHelper, requestSender, paymentIntentMapper); | ||
} | ||
|
||
/** | ||
* @param {UrlHelper} urlHelper | ||
* @param {RequestSender} requestSender | ||
* @param {PaymentIntentMapper} paymentIntentMapper | ||
* @returns {void} | ||
*/ | ||
constructor(urlHelper, requestSender, paymentIntentMapper) { | ||
/** | ||
* @private | ||
* @type {UrlHelper} | ||
*/ | ||
this.urlHelper = urlHelper; | ||
|
||
/** | ||
* @private | ||
* @type {RequestSender} | ||
*/ | ||
this.requestSender = requestSender; | ||
|
||
/** | ||
* @private | ||
* @type {PaymentIntentMapper} | ||
*/ | ||
this.paymentIntentMapper = paymentIntentMapper; | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @param {Function} [callback] | ||
* @returns {void} | ||
*/ | ||
generatePaymentIntent(data, callback) { | ||
const url = this.urlHelper.getGeneratePaymentIntentUrl(); | ||
const payload = this.paymentIntentMapper.mapToPaymentIntent(data); | ||
|
||
this.requestSender.postRequest(url, payload, {}, callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { omitNil } from '../../../common/utils'; | ||
import GatewayMapper from './gateway-mapper'; | ||
|
||
export default class PaymentIntentMapper { | ||
/** | ||
* @returns {PaymentIntentMapper} | ||
*/ | ||
static create() { | ||
const gatewayMapper = GatewayMapper.create(); | ||
|
||
return new PaymentIntentMapper(gatewayMapper); | ||
} | ||
|
||
/** | ||
* @param {GatewayMapper} gatewayMapper | ||
*/ | ||
constructor(gatewayMapper) { | ||
/** | ||
* @private | ||
* @type {GatewayMapper} | ||
*/ | ||
this.gatewayMapper = gatewayMapper; | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToPaymentIntent(data) { | ||
return omitNil({ | ||
stripe_data: this.mapStripeData(data), | ||
gateway: this.gatewayMapper.mapToGateway(data), | ||
}); | ||
} | ||
|
||
mapStripeData(data) { | ||
const { | ||
shouldSavePaymentInstrument = false, | ||
customer = {}, | ||
cart = {}, | ||
store = {}, | ||
} = data; | ||
|
||
return omitNil({ | ||
should_save_payment_instrument: shouldSavePaymentInstrument, | ||
customer_id: customer.customerId, | ||
customer_name: customer.name, | ||
customer_email: customer.email, | ||
grand_total: cart.grandTotal.integerAmount, | ||
currency_code: cart.currency, | ||
store_id: store.storeId, | ||
store_name: store.storeName, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import paymentRequestDataMock from '../mocks/payment-request-data'; | ||
import PaymentIntentGenerator from '../../src/payment/payment-intent-generator'; | ||
|
||
describe('PaymentIntentGenerator', () => { | ||
let data; | ||
let urlHelper; | ||
let requestSender; | ||
let paymentIntentMapper; | ||
let paymentIntentGenerator; | ||
|
||
beforeEach(() => { | ||
data = paymentRequestDataMock; | ||
|
||
urlHelper = { | ||
getGeneratePaymentIntentUrl: | ||
jasmine.createSpy('getPaymentUrl').and.returnValue('/api/v2/public/payments/payment_intents'), | ||
}; | ||
|
||
requestSender = { | ||
postRequest: jasmine.createSpy('postRequest'), | ||
}; | ||
|
||
paymentIntentMapper = { | ||
mapToPaymentIntent: jasmine.createSpy('mapToPaymentIntent'), | ||
}; | ||
|
||
paymentIntentGenerator = new PaymentIntentGenerator(urlHelper, requestSender, paymentIntentMapper); | ||
}); | ||
|
||
it('Create an instance of PaymentIntentGenerator', () => { | ||
const config = { host: 'https://bigpay.dev' }; | ||
const instance = PaymentIntentGenerator.create(config); | ||
|
||
expect(instance instanceof PaymentIntentGenerator).toBeTruthy(); | ||
}); | ||
|
||
it('Maps the input data into a payment intent object required to create a payment intent', () => { | ||
paymentIntentGenerator.generatePaymentIntent(data, () => {}); | ||
|
||
expect(paymentIntentMapper.mapToPaymentIntent).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/payment/v2/payment-mappers/payment-intent-mapper.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import GatewayMapper from '../../../../src/payment/v2/payment-mappers/gateway-mapper'; | ||
import paymentRequestDataMock from '../../../mocks/payment-request-data'; | ||
import PaymentIntentMapper from '../../../../src/payment/v2/payment-mappers/payment-intent-mapper'; | ||
|
||
describe('PaymentIntentMapper', () => { | ||
let data; | ||
let gatewayMapper; | ||
let paymentIntentMapper; | ||
let mappedData; | ||
let paymentMethodIdMapper; | ||
|
||
beforeEach(() => { | ||
data = paymentRequestDataMock; | ||
mappedData = { | ||
name: 'id', | ||
}; | ||
|
||
paymentMethodIdMapper = { | ||
mapToId: jasmine.createSpy('mapToId').and.returnValue(mappedData.name), | ||
}; | ||
|
||
gatewayMapper = new GatewayMapper(paymentMethodIdMapper); | ||
|
||
paymentIntentMapper = new PaymentIntentMapper(gatewayMapper); | ||
}); | ||
|
||
it('create a instance of paymentIntentMapper', () => { | ||
const instance = PaymentIntentMapper.create(); | ||
|
||
expect(instance instanceof PaymentIntentMapper).toBeTruthy(); | ||
}); | ||
|
||
it('map the data to the payment intent', () => { | ||
const output = paymentIntentMapper.mapToPaymentIntent(data); | ||
|
||
expect(output).toEqual({ | ||
stripe_data: paymentIntentMapper.mapStripeData(data), | ||
gateway: gatewayMapper.mapToGateway(data), | ||
}); | ||
}); | ||
}); |