@henrylabs-interview/payment-processor 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/resources/checkout.d.ts +2 -2
- package/dist/resources/checkout.js +20 -17
- package/dist/resources/embedded.d.ts +1 -1
- package/dist/resources/embedded.js +2 -2
- package/dist/utils/crypto.d.ts +2 -2
- package/dist/utils/crypto.js +4 -6
- package/dist/utils/store.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ interface CheckoutCreateSuccessImmediate extends CheckoutCreateGeneric {
|
|
|
12
12
|
status: 'success';
|
|
13
13
|
substatus: '201-immediate';
|
|
14
14
|
data: {
|
|
15
|
-
checkoutId:
|
|
15
|
+
checkoutId: string;
|
|
16
16
|
paymentMethodOptions: string[];
|
|
17
17
|
};
|
|
18
18
|
}
|
|
@@ -54,7 +54,7 @@ interface CheckoutConfirmSuccessImmediate extends CheckoutConfirmGeneric {
|
|
|
54
54
|
status: 'success';
|
|
55
55
|
substatus: '201-immediate';
|
|
56
56
|
data: {
|
|
57
|
-
confirmationId:
|
|
57
|
+
confirmationId: string;
|
|
58
58
|
amount: number;
|
|
59
59
|
currency: string;
|
|
60
60
|
customerId?: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readHistory, writeHistory } from '../utils/store';
|
|
2
|
-
import {
|
|
2
|
+
import { generateTimeBasedID, hashToString, signPayload } from '../utils/crypto';
|
|
3
3
|
import { INTERNAL_WEBHOOKS } from './webhooks';
|
|
4
4
|
import { sleep } from '../utils/async';
|
|
5
5
|
// checkoutId -> { historyRecordId }
|
|
@@ -130,7 +130,7 @@ export class Checkout {
|
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
132
|
createCheckoutRecord(hashId) {
|
|
133
|
-
const checkoutId = generateTimeBasedID('checkout')
|
|
133
|
+
const checkoutId = `cki_${generateTimeBasedID('checkout')}`;
|
|
134
134
|
INTERNAL_CHECKOUTS[`${checkoutId}`] = {
|
|
135
135
|
historyRecordId: hashId,
|
|
136
136
|
};
|
|
@@ -170,7 +170,7 @@ export class Checkout {
|
|
|
170
170
|
}, webhookDelay);
|
|
171
171
|
}
|
|
172
172
|
buildHistoryHash(params) {
|
|
173
|
-
return
|
|
173
|
+
return hashToString(JSON.stringify({
|
|
174
174
|
type: 'HISTORY_RECORD',
|
|
175
175
|
amount: params.amount,
|
|
176
176
|
currency: params.currency,
|
|
@@ -179,7 +179,7 @@ export class Checkout {
|
|
|
179
179
|
}
|
|
180
180
|
///
|
|
181
181
|
async validateConfirm(params) {
|
|
182
|
-
if (`${params.checkoutId}`.length !==
|
|
182
|
+
if (`${params.checkoutId}`.length !== 20 || !`${params.checkoutId}`.startsWith('cki_')) {
|
|
183
183
|
return {
|
|
184
184
|
status: 'failure',
|
|
185
185
|
substatus: '500-error',
|
|
@@ -195,13 +195,15 @@ export class Checkout {
|
|
|
195
195
|
message: 'Expired checkout ID',
|
|
196
196
|
};
|
|
197
197
|
}
|
|
198
|
-
if (params.type === 'embedded'
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
if (params.type === 'embedded') {
|
|
199
|
+
if (params.data.paymentToken.length !== 20 || !params.data.paymentToken.startsWith('pmt_')) {
|
|
200
|
+
return {
|
|
201
|
+
status: 'failure',
|
|
202
|
+
substatus: '500-error',
|
|
203
|
+
code: 500,
|
|
204
|
+
message: 'Invalid payment token',
|
|
205
|
+
};
|
|
206
|
+
}
|
|
205
207
|
}
|
|
206
208
|
if (params.type === 'embedded') {
|
|
207
209
|
const paymentToken = `pmt_${generateTimeBasedID('payment-token')}`;
|
|
@@ -291,12 +293,13 @@ export class Checkout {
|
|
|
291
293
|
message: 'Missing history record',
|
|
292
294
|
};
|
|
293
295
|
}
|
|
294
|
-
const confirmationId =
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
296
|
+
const confirmationId = 'cof_' +
|
|
297
|
+
hashToString(JSON.stringify({
|
|
298
|
+
type: 'CONFIRMATION_ID',
|
|
299
|
+
amount: record.amount,
|
|
300
|
+
currency: record.currency,
|
|
301
|
+
customerId: record.customerId,
|
|
302
|
+
}));
|
|
300
303
|
return {
|
|
301
304
|
status: 'success',
|
|
302
305
|
substatus: '201-immediate',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function renderEmbeddedCheckout(containerElementId: string, checkoutId:
|
|
1
|
+
export declare function renderEmbeddedCheckout(containerElementId: string, checkoutId: string, callbackFn: (paymentToken: string) => void): boolean;
|
|
@@ -5,11 +5,11 @@ export function renderEmbeddedCheckout(containerElementId, checkoutId, callbackF
|
|
|
5
5
|
console.warn('EmbeddedCheckoutUI can only be used in a browser environment.');
|
|
6
6
|
return false;
|
|
7
7
|
}
|
|
8
|
-
if (checkoutId.toString().length !==
|
|
8
|
+
if (checkoutId.toString().length !== 20 && !checkoutId.startsWith('cki_')) {
|
|
9
9
|
console.warn(`Invalid checkout ID: "${checkoutId}".`);
|
|
10
10
|
return false;
|
|
11
11
|
}
|
|
12
|
-
if (`${checkoutId}` ===
|
|
12
|
+
if (`${checkoutId}` === `cki_${generateTimeBasedID('checkout')}`) {
|
|
13
13
|
console.warn(`Expired checkout ID: "${checkoutId}".`);
|
|
14
14
|
return false;
|
|
15
15
|
}
|
package/dist/utils/crypto.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare function generateID(length?: number): number;
|
|
2
|
-
export declare function generateTimeBasedID(extra?: string):
|
|
3
|
-
export declare function
|
|
2
|
+
export declare function generateTimeBasedID(extra?: string): string;
|
|
3
|
+
export declare function hashToString(input: string): string;
|
|
4
4
|
export declare function signPayload(payload: string, secret: string): string;
|
package/dist/utils/crypto.js
CHANGED
|
@@ -11,14 +11,12 @@ export function generateTimeBasedID(extra = '') {
|
|
|
11
11
|
const now = Date.now(); // current time in ms
|
|
12
12
|
const oneMinute = 60 * 1000;
|
|
13
13
|
const ms = Math.floor(now / oneMinute) * oneMinute;
|
|
14
|
-
return
|
|
14
|
+
return hashToString(`${ms}---${extra}`);
|
|
15
15
|
}
|
|
16
|
-
export function
|
|
17
|
-
if (length > 16) {
|
|
18
|
-
throw new Error('Length is greater than max length of 16!');
|
|
19
|
-
}
|
|
16
|
+
export function hashToString(input) {
|
|
20
17
|
const hash = crypto.createHash('sha256').update(input).digest('hex');
|
|
21
|
-
|
|
18
|
+
// 16 hex characters = 64 bits
|
|
19
|
+
return hash.slice(0, 16);
|
|
22
20
|
}
|
|
23
21
|
export function signPayload(payload, secret) {
|
|
24
22
|
return crypto.createHmac('sha256', secret).update(payload).digest('hex');
|
package/dist/utils/store.d.ts
CHANGED