-
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 context endpoint (CS2) * Fix gitignore. Add d.ts files missing
- Loading branch information
1 parent
5ee01f8
commit a0f21d5
Showing
16 changed files
with
910 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
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 |
---|---|---|
|
@@ -37,14 +37,13 @@ node_modules | |
typings/ | ||
lib/*.js | ||
lib/ | ||
test/*.js | ||
*.map | ||
|
||
.DS_Store | ||
.vscode/* | ||
/.vscode/ | ||
|
||
doc | ||
out | ||
.idea/* | ||
|
||
dist | ||
/dist/ |
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,62 @@ | ||
import { determineError } from '../../services/errors'; | ||
import { get, post } from '../../services/http'; | ||
import { validatePayment } from '../../services/validation'; | ||
|
||
/* | ||
* Class dealing with the /payment-contexts endpoint | ||
* | ||
* @export | ||
* @class PaymentContexts | ||
*/ | ||
export default class PaymentContexts { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Sends payment contexts requests. | ||
* | ||
* @memberof PaymentContexts | ||
* @param {object} body PaymentContexts Request body. | ||
* @param {string} [idempotencyKey] Idempotency Key. | ||
* @return {Promise<object>} A promise to payment context response. | ||
*/ | ||
async request(body, idempotencyKey) { | ||
try { | ||
validatePayment(body); | ||
|
||
const response = await post( | ||
this.config.httpClient, | ||
`${this.config.host}/payment-contexts`, | ||
this.config, | ||
this.config.sk, | ||
body, | ||
idempotencyKey | ||
); | ||
return await response.json; | ||
} catch (error) { | ||
throw await determineError(error); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a payment-context details with the specified identifier string. | ||
* | ||
* @memberof PaymentContexts | ||
* @param {string} id /^(pay|sid)_(\w{26})$/ The payment or payment session identifier. | ||
* @return {Promise<object>} A promise to the get payment context response. | ||
*/ | ||
async get(id) { | ||
try { | ||
const response = await get( | ||
this.config.httpClient, | ||
`${this.config.host}/payment-contexts/${id}`, | ||
this.config, | ||
this.config.sk | ||
); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { expect } from "chai"; | ||
import nock from "nock"; | ||
import Checkout from '../../src/Checkout.js' | ||
import { NotFoundError } from "../../src/services/errors.js"; | ||
|
||
afterEach(() => { | ||
nock.cleanAll(); | ||
nock.enableNetConnect(); | ||
}); | ||
|
||
const cko = new Checkout(process.env.CHECKOUT_DEFAULT_SECRET_KEY); | ||
const processingChannelId = process.env.CHECKOUT_PROCESSING_CHANNEL_ID; | ||
|
||
describe('Integration::Payment-Contexts', () => { | ||
|
||
const request = { | ||
source: { | ||
type: "paypal" | ||
}, | ||
amount: 100, | ||
currency: "USD", | ||
capture: true, | ||
shipping: { | ||
first_name: "John", | ||
last_name: "Smith", | ||
address: { | ||
address_line1: "Checkout.com", | ||
address_line2: "90 Tottenham Court Road", | ||
city: "London", | ||
state: "str", | ||
zip: "W1T 4TJ", | ||
country: "GB" | ||
}, | ||
phone: { | ||
country_code: "+1", | ||
number: "415 555 2671" | ||
}, | ||
from_address_zip: "10014", | ||
type: "pickup_in_person" | ||
}, | ||
processing: { | ||
order_id: "123456789", | ||
tax_amount: 3000, | ||
discount_amount: 0, | ||
duty_amount: 0, | ||
shipping_amount: 300, | ||
shipping_tax_amount: 100, | ||
aft: true, | ||
preferred_scheme: "mastercard", | ||
merchant_initiated_reason: "Delayed_charge", | ||
campaign_id: 0, | ||
product_type: "QR Code", | ||
open_id: "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o", | ||
original_order_amount: 10, | ||
receipt_id: 10, | ||
terminal_type: "WAP", | ||
os_type: "ANDROID", | ||
invoice_id: "string", | ||
brand_name: "Super Brand", | ||
locale: "en-US", | ||
shipping_preference: "SET_PROVIDED_ADDRESS", | ||
user_action: "PAY_NOW", | ||
set_transaction_context: { | ||
key: "string", | ||
value: "string" | ||
}, | ||
purchase_country: "GB", | ||
custom_payment_method_ids: [ | ||
"string" | ||
], | ||
merchant_callback_url: "string", | ||
line_of_business: "Flights" | ||
}, | ||
processing_channel_id: processingChannelId, | ||
reference: "ORD-5023-4E89", | ||
description: "Set of 3 masks", | ||
success_url: "https://example.com/payments/success", | ||
failure_url: "https://example.com/payments/fail", | ||
items: [ | ||
{ | ||
name: "Kevlar batterang", | ||
quantity: 2, | ||
unit_price: 50, | ||
reference: "858818ac", | ||
commodity_code: "DEF123", | ||
unit_of_measure: "metres", | ||
total_amount: 29000, | ||
tax_amount: 1000, | ||
discount_amount: 1000, | ||
wxpay_goods_id: 1001, | ||
url: "https://example.com/", | ||
image_url: "https://example.com/" | ||
} | ||
] | ||
} | ||
|
||
it('should request a payment context', async () => { | ||
const response = await cko.paymentContexts.request(request); | ||
|
||
expect(response.id).not.to.be.null; | ||
expect(response.partner_metadata.order_id).not.to.be.null; | ||
}); | ||
|
||
it('should get a payment context', async () => { | ||
const responseRquest = await cko.paymentContexts.request(request); | ||
const response = await cko.paymentContexts.get(responseRquest.id); | ||
|
||
expect(response.payment_request.source.type).to.equal('paypal'); | ||
expect(response.payment_request.amount).to.equal(100); | ||
expect(response.payment_request.currency).to.equal("USD"); | ||
expect(response.payment_request.currency.shipping).to.not.be.null | ||
expect(response.payment_request.currency.processing).to.not.be.null | ||
expect(response.payment_request.currency.items).to.not.be.null | ||
expect(response.payment_request.success_url).to.equal('https://example.com/payments/success'); | ||
expect(response.payment_request.failure_url).to.equal('https://example.com/payments/fail'); | ||
}); | ||
|
||
it('should throw NotFoundError when getting an unexistant payment context', async () => { | ||
try { | ||
await cko.paymentContexts.get('not_found'); | ||
} catch (err) { | ||
expect(err).to.be.instanceOf(NotFoundError); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.