@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 CHANGED
@@ -11,7 +11,7 @@ export declare class PaymentProcessor {
11
11
  export declare class EmbeddedCheckout {
12
12
  private checkoutId;
13
13
  constructor(config: {
14
- checkoutId: number;
14
+ checkoutId: string;
15
15
  });
16
16
  /**
17
17
  * Renders the embedded checkout UI
@@ -12,7 +12,7 @@ interface CheckoutCreateSuccessImmediate extends CheckoutCreateGeneric {
12
12
  status: 'success';
13
13
  substatus: '201-immediate';
14
14
  data: {
15
- checkoutId: number;
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: number;
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 { generateID, generateTimeBasedID, hashToNumber, signPayload } from '../utils/crypto';
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 hashToNumber(JSON.stringify({
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 !== 12) {
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' && params.data.paymentToken.length !== 12) {
199
- return {
200
- status: 'failure',
201
- substatus: '500-error',
202
- code: 500,
203
- message: 'Invalid payment token',
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 = hashToNumber(JSON.stringify({
295
- type: 'CONFIRMATION_ID',
296
- amount: record.amount,
297
- currency: record.currency,
298
- customerId: record.customerId,
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: number, callbackFn: (paymentToken: string) => void): boolean;
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 !== 12) {
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}` === `${generateTimeBasedID('checkout')}`) {
12
+ if (`${checkoutId}` === `cki_${generateTimeBasedID('checkout')}`) {
13
13
  console.warn(`Expired checkout ID: "${checkoutId}".`);
14
14
  return false;
15
15
  }
@@ -1,4 +1,4 @@
1
1
  export declare function generateID(length?: number): number;
2
- export declare function generateTimeBasedID(extra?: string): number;
3
- export declare function hashToNumber(input: string, length?: number): number;
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;
@@ -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 hashToNumber(`${ms}---${extra}`);
14
+ return hashToString(`${ms}---${extra}`);
15
15
  }
16
- export function hashToNumber(input, length = 12) {
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
- return parseInt(hash.slice(0, length), 16);
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');
@@ -1,5 +1,5 @@
1
1
  export type HistoryRecord = {
2
- id: number;
2
+ id: string;
3
3
  amount: number;
4
4
  currency: 'USD' | 'EUR' | 'JPY';
5
5
  customerId: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henrylabs-interview/payment-processor",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",