-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds payment sessions support (#372)
- Loading branch information
1 parent
7a0e735
commit 4eaa148
Showing
10 changed files
with
153 additions
and
0 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,39 @@ | ||
import { determineError } from '../../services/errors'; | ||
import { post } from '../../services/http'; | ||
import { validatePayment } from '../../services/validation'; | ||
|
||
/* | ||
* Class dealing with the /payment-sessions endpoint | ||
* | ||
* @export | ||
* @class PaymentSessions | ||
*/ | ||
export default class PaymentSessions { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Sends payment sessions requests. | ||
* | ||
* @memberof PaymentSessions | ||
* @param {object} body PaymentSessions Request body. | ||
* @return {Promise<object>} A promise to payment context response. | ||
*/ | ||
async request(body) { | ||
try { | ||
validatePayment(body); | ||
|
||
const response = await post( | ||
this.config.httpClient, | ||
`${this.config.host}/payment-sessions`, | ||
this.config, | ||
this.config.sk, | ||
body | ||
); | ||
return await response.json; | ||
} catch (error) { | ||
throw await determineError(error); | ||
} | ||
} | ||
} |
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
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,46 @@ | ||
export const commonRequest = { | ||
amount: 1000, | ||
currency: 'GBP', | ||
reference: 'ORD-123A', | ||
billing: { | ||
address: { | ||
country: 'GB' | ||
} | ||
}, | ||
customer: { | ||
name: 'John Smith', | ||
email: '[email protected]' | ||
}, | ||
success_url: 'https://example.com/payments/success', | ||
failure_url: 'https://example.com/payments/failure' | ||
}; | ||
|
||
export const commonResponse = { | ||
id: 'ps_2aOig7knIeGNYPlyuxUEPQyOmxN', | ||
amount: 1000, | ||
locale: 'en-GB', | ||
currency: 'GBP', | ||
payment_methods: [ | ||
{ | ||
type: 'card', | ||
scheme_choice_enabled: false, | ||
store_payment_details: 'disabled' | ||
}, | ||
{ | ||
type: 'applepay', | ||
display_name: 'Parser Digital', | ||
country_code: 'GB', | ||
currency_code: 'GBP', | ||
}, | ||
{ | ||
type: 'googlepay', | ||
} | ||
], | ||
feature_flags: [], | ||
risk: { enabled: true }, | ||
_links: { | ||
self: { | ||
href: 'https://api.sandbox.checkout.com/payment-sessions/ps_2aOig7knIeGNYPlyuxUEPQyOmxN' | ||
} | ||
} | ||
}; |
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,27 @@ | ||
import { expect } from "chai"; | ||
import nock from "nock"; | ||
import Checkout from '../../src/Checkout.js' | ||
import { commonRequest, commonResponse } from "./payment-sessions-common.js"; | ||
|
||
afterEach(() => { | ||
nock.cleanAll(); | ||
nock.enableNetConnect(); | ||
}); | ||
|
||
const cko = new Checkout(process.env.CHECKOUT_DEFAULT_SECRET_KEY); | ||
|
||
describe('Integration::Payment-Sessions', () => { | ||
|
||
it('should request a payment session', async () => { | ||
const response = await cko.paymentSessions.request(commonRequest); | ||
|
||
expect(response.id).not.to.be.null; | ||
expect(response.amount).to.equal(commonResponse.amount); | ||
expect(response.locale).to.equal(commonResponse.locale); | ||
expect(response.currency).to.equal(commonResponse.currency); | ||
expect(response.customer).not.to.be.null; | ||
expect(response.payment_methods).not.to.be.null; | ||
expect(response._links).not.to.be.null; | ||
}); | ||
|
||
}); |
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,28 @@ | ||
import nock from "nock"; | ||
import { expect } from "chai"; | ||
import Checkout from "../../src/Checkout.js"; | ||
import { commonRequest, commonResponse } from "./payment-sessions-common.js"; | ||
|
||
|
||
describe('Unit::Payment-Sessions', () => { | ||
const SK = 'sk_test_0b9b5db6-f223-49d0-b68f-f6643dd4f808'; | ||
|
||
it('should request a payment session', async () => { | ||
nock('https://api.sandbox.checkout.com') | ||
.post('/payment-sessions') | ||
.reply(201, commonResponse); | ||
|
||
const cko = new Checkout(SK); | ||
|
||
const paymentContextResponse = await cko.paymentSessions.request(commonRequest); | ||
|
||
expect(paymentContextResponse.id).to.equal('ps_2aOig7knIeGNYPlyuxUEPQyOmxN'); | ||
expect(paymentContextResponse.amount).to.equal(1000); | ||
expect(paymentContextResponse.locale).to.equal('en-GB'); | ||
expect(paymentContextResponse.currency).to.equal('GBP'); | ||
expect(paymentContextResponse.payment_methods[0].type).to.equal('card'); | ||
expect(paymentContextResponse.risk.enabled).to.equal(true); | ||
expect(paymentContextResponse._links.self.href).to.equal('https://api.sandbox.checkout.com/payment-sessions/ps_2aOig7knIeGNYPlyuxUEPQyOmxN'); | ||
}); | ||
|
||
}); |
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,7 @@ | ||
import { config } from '../../Checkout'; | ||
|
||
export default class PaymentSessions { | ||
constructor(config: config); | ||
|
||
request: (body: object) => Promise<object>; | ||
} |
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