@stancer/stancer 0.0.0-stage → 1.0.1
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/.editorconfig +14 -0
- package/.gitlab-ci.yml +159 -0
- package/.prettierignore +3 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE +19 -0
- package/README.md +166 -2
- package/eslint.config.js +14 -0
- package/package.json +58 -3
- package/pnpm-workspace.yaml +19 -0
- package/prettier.config.js +3 -0
- package/src/address/api-endpoint.ts +30 -0
- package/src/address/model.ts +79 -0
- package/src/address/repository.ts +18 -0
- package/src/address/validator.ts +34 -0
- package/src/api-endpoint/abstract-api-endpoint.ts +310 -0
- package/src/api-endpoint/configuration.ts +125 -0
- package/src/api-endpoint/http/fetch-http-client.ts +29 -0
- package/src/api-endpoint/http/http.ts +75 -0
- package/src/api-endpoint/http/index.ts +2 -0
- package/src/api-endpoint/version.ts +4 -0
- package/src/card/api-endpoint.ts +39 -0
- package/src/card/enum/funding.ts +11 -0
- package/src/card/enum/nature.ts +7 -0
- package/src/card/enum/network.ts +16 -0
- package/src/card/model.ts +139 -0
- package/src/card/repository.ts +11 -0
- package/src/card/validator.ts +79 -0
- package/src/core/abstract-model.ts +126 -0
- package/src/core/abstract-repository.ts +201 -0
- package/src/core/common/branded-type.ts +15 -0
- package/src/core/common/handler/date-handler.ts +31 -0
- package/src/core/common/handler/enum-handler.ts +18 -0
- package/src/core/common/handler/handler.ts +64 -0
- package/src/core/common/handler/id-handler.ts +27 -0
- package/src/core/common/id-validator.ts +23 -0
- package/src/core/common/mutation-tracked-object.ts +49 -0
- package/src/core/endpoint-interface.ts +43 -0
- package/src/core/error/base-error.ts +1 -0
- package/src/core/error/endpoint/api/api-conflict-error.ts +7 -0
- package/src/core/error/endpoint/api/api-error.ts +10 -0
- package/src/core/error/endpoint/api/api-not-found-error.ts +7 -0
- package/src/core/error/endpoint/api/api-unauthorized-error.ts +7 -0
- package/src/core/error/endpoint/api/api-unknown-error.ts +7 -0
- package/src/core/error/endpoint/api/api-validation-error.ts +13 -0
- package/src/core/error/endpoint/api/index.ts +6 -0
- package/src/core/error/endpoint/route-inaccessible-error.ts +8 -0
- package/src/core/error/unpopulated-error.ts +3 -0
- package/src/core/error/unsendable-error.ts +3 -0
- package/src/core/error/validation-error.ts +3 -0
- package/src/core/validator.ts +129 -0
- package/src/customer/api-endpoint.ts +29 -0
- package/src/customer/model.ts +113 -0
- package/src/customer/repository.ts +14 -0
- package/src/customer/validator.ts +26 -0
- package/src/payment/api-endpoint.ts +118 -0
- package/src/payment/enum/currency.ts +14 -0
- package/src/payment/enum/method.ts +2 -0
- package/src/payment/enum/status.ts +21 -0
- package/src/payment/model.ts +378 -0
- package/src/payment/repository.ts +73 -0
- package/src/payment/validator.ts +96 -0
- package/src/payment-intent/api-endpoint.ts +123 -0
- package/src/payment-intent/enum/status.ts +21 -0
- package/src/payment-intent/enum/threeds.ts +2 -0
- package/src/payment-intent/model.ts +266 -0
- package/src/payment-intent/repository.ts +85 -0
- package/src/payment-intent/validator.ts +45 -0
- package/src/payout/model.ts +8 -0
- package/src/refund/api-endpoint.ts +33 -0
- package/src/refund/enum/refund-status.ts +11 -0
- package/src/refund/model.ts +90 -0
- package/src/refund/repository.ts +14 -0
- package/src/refund/validator.ts +44 -0
- package/src/sepa/api-endpoint.ts +26 -0
- package/src/sepa/model.ts +80 -0
- package/src/sepa/repository.ts +11 -0
- package/src/sepa/validator.ts +56 -0
- package/test/fixtures/endpoint.stub.ts +22 -0
- package/test/fixtures/list-endpoint.stub.ts +33 -0
- package/test/fixtures/list-model.stub.ts +24 -0
- package/test/fixtures/model.stub.ts +12 -0
- package/test/functional/address.test.ts +126 -0
- package/test/functional/card.test.ts +175 -0
- package/test/functional/customer.test.ts +174 -0
- package/test/functional/payment-intent.test.ts +215 -0
- package/test/functional/payment.test.ts +206 -0
- package/test/functional/refund.test.ts +112 -0
- package/test/functional/sepa.test.ts +136 -0
- package/test/integration/stub-integration.test.ts +88 -0
- package/test/stub/stub-endpoint.ts +42 -0
- package/test/stub/stub-model.ts +82 -0
- package/test/stub/stub-repository.ts +11 -0
- package/test/test-helper.ts +133 -0
- package/test/unit/address/api-endpoint.test.ts +134 -0
- package/test/unit/address/fixtures.ts +60 -0
- package/test/unit/address/model.test.ts +75 -0
- package/test/unit/address/repository.test.ts +148 -0
- package/test/unit/address/validator.test.ts +49 -0
- package/test/unit/api-endpoint/http/fetch-http-client.test.ts +44 -0
- package/test/unit/api-endpoint/http/http.test.ts +152 -0
- package/test/unit/card/api-endpoint.test.ts +170 -0
- package/test/unit/card/card-helper.ts +116 -0
- package/test/unit/card/fixtures.ts +87 -0
- package/test/unit/card/model.test.ts +167 -0
- package/test/unit/card/repository.test.ts +146 -0
- package/test/unit/card/validator.test.ts +96 -0
- package/test/unit/core/common/mutation-tracked-object.test.ts +24 -0
- package/test/unit/customer/api-endpoint.test.ts +143 -0
- package/test/unit/customer/fixtures.ts +73 -0
- package/test/unit/customer/model.test.ts +90 -0
- package/test/unit/customer/repository.test.ts +148 -0
- package/test/unit/customer/validator.test.ts +62 -0
- package/test/unit/payment/api-endpoint.test.ts +188 -0
- package/test/unit/payment/fixtures.ts +116 -0
- package/test/unit/payment/model.test.ts +240 -0
- package/test/unit/payment/repository.test.ts +270 -0
- package/test/unit/payment/validator.test.ts +137 -0
- package/test/unit/payment-intent/api-endpoint.test.ts +142 -0
- package/test/unit/payment-intent/fixtures.ts +94 -0
- package/test/unit/payment-intent/model.test.ts +157 -0
- package/test/unit/payment-intent/repository.test.ts +263 -0
- package/test/unit/payment-intent/validator.test.ts +136 -0
- package/test/unit/refund/api-endpoint.test.ts +129 -0
- package/test/unit/refund/fixtures.ts +58 -0
- package/test/unit/refund/model.test.ts +89 -0
- package/test/unit/refund/repository.test.ts +157 -0
- package/test/unit/refund/validator.test.ts +89 -0
- package/test/unit/sepa/api-endpoint.test.ts +170 -0
- package/test/unit/sepa/fixture.ts +62 -0
- package/test/unit/sepa/model.test.ts +86 -0
- package/test/unit/sepa/repository.test.ts +149 -0
- package/test/unit/sepa/sepa-helper.ts +71 -0
- package/test/unit/sepa/validator.test.ts +58 -0
- package/test/unit/stub/stub-endpoint.test.ts +236 -0
- package/test/unit/stub/stub-list-endpoint.test.ts +86 -0
- package/test/unit/stub/stub-model.test.ts +111 -0
- package/test/unit/stub/stub-repository.test.ts +144 -0
- package/tsconfig.json +50 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { AbstractRepository } from '#core/abstract-repository.js';
|
|
4
|
+
import { StancerError } from '#error/base-error.js';
|
|
5
|
+
import { StancerRouteInaccessibleError } from '#error/endpoint/route-inaccessible-error.js';
|
|
6
|
+
import { PaymentApiEndpoint } from '#payment/api-endpoint.js';
|
|
7
|
+
import { IdPayment, PaymentModel, idPayment, paymentAmount } from '#payment/model.js';
|
|
8
|
+
import { PaymentRepository } from '#payment/repository.js';
|
|
9
|
+
import { paymentValidator } from '#payment/validator.js';
|
|
10
|
+
import { RefundModel, refundAmount } from '#refund/model.js';
|
|
11
|
+
import { RefundRepository } from '#refund/repository.js';
|
|
12
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
13
|
+
|
|
14
|
+
import { listResource as listRefund, endpointResourceDeserialized as refundDeserialized } from '../refund/fixtures.js';
|
|
15
|
+
import { creationResource, endpointResource, listResource } from './fixtures.js';
|
|
16
|
+
|
|
17
|
+
const idPrefix = 'paym';
|
|
18
|
+
const id = idPayment(`${idPrefix}_${randomString(24)}`);
|
|
19
|
+
const stancerErrorID = idPayment(`${idPrefix}_unidentifiedError${randomString(7)}`);
|
|
20
|
+
const stancerRouteInaccessibleId = idPayment(`${idPrefix}_StancerRouteInnacessible`);
|
|
21
|
+
const dataHasBeenSentId = idPayment(`${idPrefix}_DataHasBeenSent${randomString(9)}`);
|
|
22
|
+
const dataUnmodified = idPayment(`${idPrefix}_unmodifiedData${randomString(10)}`);
|
|
23
|
+
const emptyRefund = idPayment(`${idPrefix}_emptyRefund${randomString(13)}`);
|
|
24
|
+
const refundId = idPayment(`${idPrefix}_refundedPayment${randomString(9)}`);
|
|
25
|
+
let refundingAmount = refundAmount(0);
|
|
26
|
+
|
|
27
|
+
class MockApiEndpoint {
|
|
28
|
+
constructor() {}
|
|
29
|
+
|
|
30
|
+
list() {
|
|
31
|
+
async function* listGenerator() {
|
|
32
|
+
yield* listResource;
|
|
33
|
+
}
|
|
34
|
+
return listGenerator();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
delete() {
|
|
38
|
+
return creationResource;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
send(id: string, data: Record<string, unknown>) {
|
|
42
|
+
if (id) {
|
|
43
|
+
if (id === stancerErrorID) {
|
|
44
|
+
throw new StancerError();
|
|
45
|
+
}
|
|
46
|
+
if (id === stancerRouteInaccessibleId) {
|
|
47
|
+
throw new StancerRouteInaccessibleError('PATCH', 'stub');
|
|
48
|
+
}
|
|
49
|
+
if (id === dataUnmodified) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { ...data, id: dataHasBeenSentId };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get() {
|
|
57
|
+
return { ...creationResource, id: id };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
refunds(id: IdPayment) {
|
|
61
|
+
if (id === emptyRefund) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
if (id === refundId) {
|
|
65
|
+
listRefund.push({ ...refundDeserialized, amount: refundingAmount });
|
|
66
|
+
}
|
|
67
|
+
return listRefund.map((value) => new RefundModel({}, value));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
it('can be instanciated', () => {
|
|
72
|
+
const instance = new PaymentRepository();
|
|
73
|
+
expect(instance).toBeInstanceOf(PaymentRepository);
|
|
74
|
+
expect(instance).toBeInstanceOf(AbstractRepository);
|
|
75
|
+
//accessing private method & property
|
|
76
|
+
expect(instance.endpoint).toBeInstanceOf(PaymentApiEndpoint);
|
|
77
|
+
expect(instance.validator).toBe(paymentValidator);
|
|
78
|
+
expect(instance.staticModel).toBe(PaymentModel);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('repository', () => {
|
|
82
|
+
const mockRepo = new MockApiEndpoint() as unknown as PaymentApiEndpoint; // I lie, for convenience sake
|
|
83
|
+
const repository = new PaymentRepository(mockRepo);
|
|
84
|
+
|
|
85
|
+
describe('create', () => {
|
|
86
|
+
it("doesn't accept bad data", async () => {
|
|
87
|
+
const badID = randomString(30, 56);
|
|
88
|
+
// using puposefuly bad ID
|
|
89
|
+
await expect(async () => await repository.create(badID)).rejects.toThrow();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('accept good data', async () => {
|
|
93
|
+
const model = await repository.create(id);
|
|
94
|
+
expect(model).toBeInstanceOf(PaymentModel);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('delete', async () => {
|
|
99
|
+
const modelWithoutId = await repository.create();
|
|
100
|
+
await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
|
|
101
|
+
|
|
102
|
+
const model = await repository.create({ ...creationResource, id: idPayment(endpointResource.id) });
|
|
103
|
+
await expect((await repository.delete(model))._data).toStrictEqual(creationResource);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('send', () => {
|
|
107
|
+
it('should return model', async () => {
|
|
108
|
+
const model = await repository.create(creationResource);
|
|
109
|
+
const sentModel = await repository.send(model);
|
|
110
|
+
expect(sentModel).toStrictEqual(model);
|
|
111
|
+
expect(sentModel._data.id).toEqual(dataHasBeenSentId);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should throw on inprecise error', async () => {
|
|
115
|
+
const model = await repository.create({ ...creationResource, id: stancerErrorID as IdPayment });
|
|
116
|
+
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should return error on StancerRouteInacessibleError', async () => {
|
|
120
|
+
const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdPayment });
|
|
121
|
+
await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return unmodified model when no new Data', async () => {
|
|
125
|
+
const model = await repository.create({ ...creationResource, id: dataUnmodified as IdPayment });
|
|
126
|
+
const sentModel = await repository.send(model);
|
|
127
|
+
expect(model).toStrictEqual(sentModel);
|
|
128
|
+
expect(sentModel._data.id).toEqual(dataUnmodified);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('populate', () => {
|
|
133
|
+
it('should not modify model if no Id', async () => {
|
|
134
|
+
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
135
|
+
const model = await repository.create(creationResource);
|
|
136
|
+
expect(model).toBeInstanceOf(PaymentModel);
|
|
137
|
+
expect(getEndpoint).toHaveBeenCalledTimes(0);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should get model if only id', async () => {
|
|
141
|
+
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
142
|
+
const model = await repository.create(id);
|
|
143
|
+
expect(model).toBeInstanceOf(PaymentModel);
|
|
144
|
+
expect(getEndpoint).toHaveBeenCalledTimes(1);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('list', () => {
|
|
149
|
+
it('return correct model one by one', async () => {
|
|
150
|
+
const listStub = repository.list();
|
|
151
|
+
|
|
152
|
+
const item1 = await listStub.next();
|
|
153
|
+
expect(item1.done).toBeFalsy();
|
|
154
|
+
expect(item1.value).toHaveProperty('id');
|
|
155
|
+
expect(item1.value).toBeInstanceOf(PaymentModel);
|
|
156
|
+
expect((item1.value as PaymentModel).uniqueId).toBe('firstResource');
|
|
157
|
+
|
|
158
|
+
const item2 = await listStub.next();
|
|
159
|
+
expect(item2.done).toBeFalsy();
|
|
160
|
+
expect(item2.value).toHaveProperty('id');
|
|
161
|
+
expect(item2.value).toBeInstanceOf(PaymentModel);
|
|
162
|
+
expect((item2.value as PaymentModel).uniqueId).toBe('secondResource');
|
|
163
|
+
|
|
164
|
+
const item3 = await listStub.next();
|
|
165
|
+
expect(item3.done).toBeFalsy();
|
|
166
|
+
expect(item3.value).toHaveProperty('id');
|
|
167
|
+
expect(item3.value).toBeInstanceOf(PaymentModel);
|
|
168
|
+
expect((item3.value as PaymentModel).uniqueId).toBe('thirdResource');
|
|
169
|
+
|
|
170
|
+
const item4 = await listStub.next();
|
|
171
|
+
expect(item4.done).toBeTruthy();
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('list payment refunds', () => {
|
|
176
|
+
it('not populated payment cannot get refund', async () => {
|
|
177
|
+
const payment = await repository.create({ amount: paymentAmount(100), currency: 'eur' });
|
|
178
|
+
await expect(() => repository.populateRefunds(payment)).rejects.toThrow(
|
|
179
|
+
'Payment must be populated before calling populateRefund method',
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('populate empty array when no refund', async () => {
|
|
184
|
+
const payment = await repository.create(emptyRefund);
|
|
185
|
+
payment._id = emptyRefund;
|
|
186
|
+
await expect(repository.populateRefunds(payment)).resolves.toStrictEqual(payment);
|
|
187
|
+
expect(payment.refunds).toEqual([]);
|
|
188
|
+
expect(payment.isRefundPopulated()).toEqual(true);
|
|
189
|
+
expect(payment.refundedAmount).toEqual(0);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('populate refund array', async () => {
|
|
193
|
+
const payment = await repository.create(id);
|
|
194
|
+
await expect(repository.populateRefunds(payment)).resolves.toStrictEqual(payment);
|
|
195
|
+
expect(payment.refunds).toHaveLength(3);
|
|
196
|
+
expect(payment.isRefundPopulated()).toEqual(true);
|
|
197
|
+
expect(payment.refundedAmount).toEqual(150);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('refund the payment', () => {
|
|
202
|
+
it('cannot refund if not populated', async () => {
|
|
203
|
+
const payment = await repository.create({ amount: paymentAmount(100), currency: 'eur' });
|
|
204
|
+
await expect(() => repository.refund(payment, refundAmount(50))).rejects.toThrow(
|
|
205
|
+
'Payment must be populated before calling refund method',
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('cannot do partial refund if not captured', async () => {
|
|
210
|
+
const payment = await repository.create(id);
|
|
211
|
+
payment._data.status = 'to_capture';
|
|
212
|
+
await expect(() => repository.refund(payment, refundAmount(50))).rejects.toThrow(
|
|
213
|
+
`Partial refund is not allowed when payment is not captured.`,
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('cannot refund more than paid', async () => {
|
|
218
|
+
const payment = await repository.create(id);
|
|
219
|
+
expect(payment.amount).toBeDefined();
|
|
220
|
+
expect(payment.id).toBeDefined();
|
|
221
|
+
payment._data.status = 'captured';
|
|
222
|
+
await repository.populateRefunds(payment);
|
|
223
|
+
expect(payment.amount).toBeDefined();
|
|
224
|
+
if (payment.amount === undefined) {
|
|
225
|
+
throw Error('unacessible error');
|
|
226
|
+
}
|
|
227
|
+
const refundingAmount = payment.amount + randomNumber(1, 100);
|
|
228
|
+
await expect(() => repository.refund(payment, refundAmount(refundingAmount))).rejects.toThrow(
|
|
229
|
+
`You are trying to refund ${refundingAmount}, which is ${refundingAmount - payment.refundableAmount} more than the refundable amount of ${payment.refundableAmount}.`,
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('cannot refund more than paid minus already refunded', async () => {
|
|
234
|
+
const payment = await repository.create(id);
|
|
235
|
+
payment._data.status = 'captured';
|
|
236
|
+
expect(payment.amount).toBeDefined();
|
|
237
|
+
expect(payment.id).toBeDefined();
|
|
238
|
+
expect(await repository.populateRefunds(payment)).toStrictEqual(payment);
|
|
239
|
+
const refundingAmount = payment.refundableAmount + randomNumber(1, 100);
|
|
240
|
+
await expect(() => repository.refund(payment, refundAmount(refundingAmount))).rejects.toThrow(
|
|
241
|
+
`You are trying to refund ${refundingAmount}, which is ${refundingAmount - payment.refundableAmount} more than the refundable amount of ${payment.refundableAmount}.`,
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('refund when number is correct', async () => {
|
|
246
|
+
class MockApiRefundEndpoint {
|
|
247
|
+
constructor() {}
|
|
248
|
+
send(id: string, data: Record<string, unknown>) {
|
|
249
|
+
return { ...data, id: `refd_${randomString(24)}` };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const mockRefundRepository = new RefundRepository(new MockApiRefundEndpoint());
|
|
254
|
+
const paymentRepository = new PaymentRepository(new MockApiEndpoint(), mockRefundRepository);
|
|
255
|
+
const payment = await paymentRepository.create(id);
|
|
256
|
+
expect(payment.amount).toBeDefined();
|
|
257
|
+
expect(payment.id).toBeDefined();
|
|
258
|
+
expect(await paymentRepository.populateRefunds(payment)).toStrictEqual(payment);
|
|
259
|
+
payment._id = idPayment(refundId);
|
|
260
|
+
payment._data.status = 'captured';
|
|
261
|
+
|
|
262
|
+
const refundableAmount = payment.refundableAmount;
|
|
263
|
+
const refundedAmount = payment.refundedAmount;
|
|
264
|
+
refundingAmount = refundAmount(randomNumber(1, refundableAmount));
|
|
265
|
+
await expect(paymentRepository.refund(payment, refundAmount(refundingAmount))).resolves.toStrictEqual(payment);
|
|
266
|
+
expect(refundedAmount).toBe(payment.refundedAmount - refundingAmount);
|
|
267
|
+
expect(refundableAmount).toBe(payment.refundableAmount + refundingAmount);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { Currency, CurrencyEnum } from '#payment/enum/currency.ts';
|
|
5
|
+
import { Method, MethodEnum } from '#payment/enum/method.ts';
|
|
6
|
+
import { PaymentStatus, StatusEnum } from '#payment/enum/status.ts';
|
|
7
|
+
import { Device, PaymentModel } from '#payment/model.js';
|
|
8
|
+
import { paymentValidator } from '#payment/validator.js';
|
|
9
|
+
import { checkType, goldilocks, goldilocksId, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
10
|
+
|
|
11
|
+
import { IdAddress } from '../../../src/address/model';
|
|
12
|
+
import { IdCard } from '../../../src/card/model';
|
|
13
|
+
import { IdCustomer } from '../../../src/customer/model';
|
|
14
|
+
import { IdPaymentIntent } from '../../../src/payment-intent/model';
|
|
15
|
+
import { IdSepa } from '../../../src/sepa/model';
|
|
16
|
+
|
|
17
|
+
describe('Goldilocks & type', () => {
|
|
18
|
+
const payment = new PaymentModel(paymentValidator);
|
|
19
|
+
it('id', () => {
|
|
20
|
+
goldilocksId((value: string) => new PaymentModel(paymentValidator, value), 'paym_');
|
|
21
|
+
});
|
|
22
|
+
it('amount', () => {
|
|
23
|
+
goldilocks((value: number) => payment.setAmount(value), 'number', { min: 0 });
|
|
24
|
+
checkType((value: number) => payment.setAmount(value), ['number'], randomNumber(0, 20000000));
|
|
25
|
+
});
|
|
26
|
+
it('auth', () => {
|
|
27
|
+
checkType((value: boolean) => (payment._data.auth = value), ['boolean'], true);
|
|
28
|
+
});
|
|
29
|
+
it('billing address', () => {
|
|
30
|
+
goldilocksId((value: IdAddress) => payment.setBillingAddress(value), 'addr_');
|
|
31
|
+
checkType((value: IdAddress) => payment.setBillingAddress(value), ['object', 'string'], `addr_${randomString(24)}`);
|
|
32
|
+
});
|
|
33
|
+
it('capture', () => {
|
|
34
|
+
checkType((value: boolean) => payment.setCapture(value), ['boolean'], true);
|
|
35
|
+
});
|
|
36
|
+
it('card', () => {
|
|
37
|
+
goldilocksId((value: IdCard) => payment.setCard(value), 'card_');
|
|
38
|
+
checkType((value: IdCard) => payment.setCard(value), ['string', 'object'], `card_${randomString(24)}`);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it.each(CurrencyEnum.map((n) => [n]))('currency %s', (currency) => {
|
|
42
|
+
const falseCurrency = randomString(3);
|
|
43
|
+
expect(() => payment.setCurrency(falseCurrency)).toThrow();
|
|
44
|
+
payment.setCurrency(currency);
|
|
45
|
+
expect(payment.currency).toBe(currency);
|
|
46
|
+
checkType((value: Currency) => payment.setCurrency(value), [], currency);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('Customer', () => {
|
|
50
|
+
const falseCustomer = randomString(24);
|
|
51
|
+
const trueCustomer = `cust_${randomString(24)}`;
|
|
52
|
+
expect(() => payment.setCustomer(falseCustomer)).toThrow();
|
|
53
|
+
goldilocksId((value: IdCustomer) => payment.setCustomer(value), 'cust_');
|
|
54
|
+
checkType((value: IdCustomer) => payment.setCustomer(value), ['string'], trueCustomer);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('DateSettlement', () => {
|
|
58
|
+
const falseDateSettlement = new Temporal.PlainDate(9999, randomMonth(), randomNumber(1, 30));
|
|
59
|
+
|
|
60
|
+
const DateSettlement = new Temporal.PlainDate(
|
|
61
|
+
randomNumber(1971, Temporal.PlainDate.from(Temporal.Now.plainDateISO()).year),
|
|
62
|
+
randomMonth(),
|
|
63
|
+
randomNumber(1, 30),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(() => payment.setDateSettlement(falseDateSettlement)).toThrow();
|
|
67
|
+
|
|
68
|
+
payment.setDateSettlement(DateSettlement);
|
|
69
|
+
|
|
70
|
+
expect(payment.dateSettlement).toBe(DateSettlement);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('Description', () => {
|
|
74
|
+
goldilocks((value: string) => payment.setDescription(value), 'string', { min: 1, max: 64 });
|
|
75
|
+
checkType((value: string) => payment.setDescription(value), ['string'], randomString(64));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('device', () => {
|
|
79
|
+
checkType((value: Device | boolean) => payment.setDevice(value), ['object', 'boolean'], { city: 'city' });
|
|
80
|
+
checkType((value: Device | boolean) => payment.setDevice(value), ['object', 'boolean'], true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it.each(MethodEnum.map((n) => [[n]]))('method allowed: %s', (methodsAllowed) => {
|
|
84
|
+
const falseMethodsAllowed = randomString(100, 200);
|
|
85
|
+
expect(() => payment.setMethodsAllowed(falseMethodsAllowed)).toThrow();
|
|
86
|
+
payment.setMethodsAllowed(methodsAllowed);
|
|
87
|
+
expect(payment.methodsAllowed).toBe(methodsAllowed);
|
|
88
|
+
checkType((value: Method[]) => payment.setMethodsAllowed(value), [], methodsAllowed);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('orderId', () => {
|
|
92
|
+
goldilocks((value: string) => payment.setOrderId(value), 'string', { min: 1, max: 36 });
|
|
93
|
+
checkType((value: string) => payment.setOrderId(value), ['string'], randomString(24));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('uniqueID', () => {
|
|
97
|
+
goldilocks((value: string) => payment.setUniqueId(value), 'string', { min: 1, max: 36 });
|
|
98
|
+
checkType((value: string) => payment.setUniqueId(value), ['string'], randomString(24));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('returnUrl', () => {
|
|
102
|
+
goldilocks((value: string) => payment.setReturnUrl(value), 'string', { min: 1, max: 2048 });
|
|
103
|
+
checkType((value: string) => payment.setReturnUrl(value), ['string'], randomString(24));
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('Sepa', () => {
|
|
107
|
+
payment._data.currency = 'eur';
|
|
108
|
+
goldilocksId((value: IdSepa) => payment.setSepa(value), 'sepa_');
|
|
109
|
+
checkType((value: IdSepa) => payment.setSepa(value), ['string', 'object'], `sepa_${randomString(24)}`);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('shipping address', () => {
|
|
113
|
+
goldilocksId((value: IdAddress) => payment.setShippingAddress(value), 'addr_');
|
|
114
|
+
checkType(
|
|
115
|
+
(value: IdAddress) => payment.setShippingAddress(value),
|
|
116
|
+
['object', 'string'],
|
|
117
|
+
`addr_${randomString(24)}`,
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('Payment intent', () => {
|
|
122
|
+
goldilocksId((value: IdPaymentIntent) => (payment._data.paymentIntent = value), 'pi_');
|
|
123
|
+
checkType(
|
|
124
|
+
(value: IdPaymentIntent) => (payment._data.paymentIntent = value),
|
|
125
|
+
['string', 'object'],
|
|
126
|
+
`pi_${randomString(24)}`,
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it.each(StatusEnum.map((n) => [n]))('payment status %s', (status) => {
|
|
131
|
+
const falseStatus = randomString(100, 200);
|
|
132
|
+
expect(() => (payment._data.status = falseStatus)).toThrow();
|
|
133
|
+
payment._data.status = status;
|
|
134
|
+
expect(payment.status).toBe(status);
|
|
135
|
+
checkType((value: PaymentStatus) => (payment._data.status = value), [], status);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { PaymentIntentApiEndpoint } from '#payment-intent/api-endpoint.js';
|
|
4
|
+
import type { IdPaymentIntent } from '#payment-intent/model.js';
|
|
5
|
+
import { randomString } from '#test/test-helper.js';
|
|
6
|
+
|
|
7
|
+
import { endpointResource, endpointResourceDeserialized } from './fixtures';
|
|
8
|
+
|
|
9
|
+
vi.mock('#api/http/http.js', async (importHTTP) => {
|
|
10
|
+
const original: Record<string, Promise<unknown>> = await importHTTP();
|
|
11
|
+
return {
|
|
12
|
+
...original,
|
|
13
|
+
transformResponseToData: vi.fn((value) => value.json()),
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
vi.mock('#api/http/fetch-http-client.js', () => {
|
|
18
|
+
return {
|
|
19
|
+
httpClient: {
|
|
20
|
+
makeRequest: vi.fn(() => {
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
status: 200,
|
|
23
|
+
json: () => {
|
|
24
|
+
return {
|
|
25
|
+
...endpointResource,
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}),
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('paymentintent api endpoint', () => {
|
|
35
|
+
describe('Serialize/Deserialize resources', () => {
|
|
36
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
37
|
+
|
|
38
|
+
it('test deserialize resource', async () => {
|
|
39
|
+
const deserializedResources = await endpoint.deserializeResource(endpointResource);
|
|
40
|
+
expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
|
|
41
|
+
expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('test post serialize resource', () => {
|
|
45
|
+
const serializedResources = endpoint.postObjectSerializer(
|
|
46
|
+
endpointResourceDeserialized,
|
|
47
|
+
) as typeof endpointResource;
|
|
48
|
+
// We extract the API generated Data and the non postable data from our endpoint resources.
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
50
|
+
const { origin, payment, status, id, created_at, ...postObject } = endpointResource;
|
|
51
|
+
expect(serializedResources).toMatchObject(postObject);
|
|
52
|
+
expect(postObject).toMatchObject(serializedResources);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('test patch serialize resource', () => {
|
|
56
|
+
const serializedResources = endpoint.patchObjectSerializer(
|
|
57
|
+
endpointResourceDeserialized,
|
|
58
|
+
) as typeof endpointResource;
|
|
59
|
+
// We extract the API generated Data and the non patchable data from our endpoint resources.
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
61
|
+
const { origin, payment, status, id, created_at, ...patchObject } = endpointResource;
|
|
62
|
+
expect(serializedResources).toMatchObject(patchObject);
|
|
63
|
+
expect(patchObject).toMatchObject(serializedResources);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('endpoint path', () => {
|
|
68
|
+
it('endpoint url', () => {
|
|
69
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
70
|
+
const id = ('paym_' + randomString(24)) as IdPaymentIntent;
|
|
71
|
+
expect(endpoint._getResourceUri(id)).toStrictEqual(`https://api.stancer.com/v2/payment_intents/${id}`);
|
|
72
|
+
expect(endpoint._getResourceUri()).toStrictEqual('https://api.stancer.com/v2/payment_intents/');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('verbs', () => {
|
|
77
|
+
const paymentintentEndpoint = new PaymentIntentApiEndpoint();
|
|
78
|
+
|
|
79
|
+
it('get', async () => {
|
|
80
|
+
const responseWithId = await paymentintentEndpoint.get(('paym_' + randomString(24)) as IdPaymentIntent);
|
|
81
|
+
expect(await responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('post', async () => {
|
|
85
|
+
const responseWithId = await paymentintentEndpoint.post(endpointResource);
|
|
86
|
+
expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('patch', async () => {
|
|
90
|
+
const patchResponse = await paymentintentEndpoint.patch(
|
|
91
|
+
('paym_' + randomString(24)) as IdPaymentIntent,
|
|
92
|
+
endpointResource,
|
|
93
|
+
);
|
|
94
|
+
expect(patchResponse).toStrictEqual(endpointResourceDeserialized);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('delete', async () => {
|
|
98
|
+
expect(await paymentintentEndpoint.delete(('paym_' + randomString(24)) as IdPaymentIntent)).toStrictEqual(
|
|
99
|
+
endpointResourceDeserialized,
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('send', () => {
|
|
105
|
+
const { amount, currency } = endpointResourceDeserialized;
|
|
106
|
+
|
|
107
|
+
it('send with required data', async () => {
|
|
108
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
109
|
+
await expect(endpoint.send(undefined, { amount, currency })).resolves.toStrictEqual(endpointResourceDeserialized);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('does not post with missing amount', async () => {
|
|
113
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
114
|
+
expect(() => endpoint.send(undefined, { currency })).toThrow();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('does not post with missing currency', async () => {
|
|
118
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
119
|
+
expect(() => endpoint.send(undefined, { amount })).toThrow();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('Patch if id', async () => {
|
|
123
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
124
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
125
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
126
|
+
await expect(endpoint.send('pi_' + randomString(24), { currency, amount })).resolves.toStrictEqual(
|
|
127
|
+
endpointResourceDeserialized,
|
|
128
|
+
);
|
|
129
|
+
expect(spyonPatch).toHaveBeenCalledTimes(1);
|
|
130
|
+
expect(spyonPost).toHaveBeenCalledTimes(0);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('post if no id', () => {
|
|
134
|
+
const endpoint = new PaymentIntentApiEndpoint();
|
|
135
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
136
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
137
|
+
endpoint.send(undefined, { currency, amount });
|
|
138
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
139
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
|
|
3
|
+
import { PaymentIntentStatusEnum } from '#payment-intent/enum/status.js';
|
|
4
|
+
import { ThreeDSEnum } from '#payment-intent/enum/threeds.js';
|
|
5
|
+
import { idPaymentIntent } from '#payment-intent/model.js';
|
|
6
|
+
import { Currency } from '#payment/enum/currency.js';
|
|
7
|
+
import { MethodEnum } from '#payment/enum/method.js';
|
|
8
|
+
import { idPayment, paymentAmount } from '#payment/model.js';
|
|
9
|
+
import { randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
10
|
+
|
|
11
|
+
import { IdAddress } from '../../../src/address/model';
|
|
12
|
+
import { IdCard } from '../../../src/card/model';
|
|
13
|
+
import { IdCustomer } from '../../../src/customer/model';
|
|
14
|
+
import { IdSepa } from '../../../src/sepa/model';
|
|
15
|
+
|
|
16
|
+
const id = `pi_${randomString(24)}`;
|
|
17
|
+
const amount = randomNumber(1, 50000);
|
|
18
|
+
const billing_address = `addr_${randomString(24)}`;
|
|
19
|
+
const capture = true;
|
|
20
|
+
const card = `card_${randomString(24)}`;
|
|
21
|
+
const currency: Currency = 'eur';
|
|
22
|
+
const customer = `cust_${randomString(24)}`;
|
|
23
|
+
const created_at = randomNumber(786460000, 1000000000);
|
|
24
|
+
const date_settlement = `${randomNumber(1971, 2025)}-${randomMonth().toString().padStart(2, '0')}-${randomNumber(1, 28).toString().padStart(2, '0')}`;
|
|
25
|
+
const description = randomString(1, 64);
|
|
26
|
+
const metadata = randomString(24);
|
|
27
|
+
const methods_allowed = [MethodEnum[randomNumber(0, MethodEnum.length - 1)]];
|
|
28
|
+
const order_id = randomString(1, 36);
|
|
29
|
+
const origin = 'api';
|
|
30
|
+
const payment = `paym_${randomString(24)}`;
|
|
31
|
+
const return_url = 'https://' + randomString(24);
|
|
32
|
+
const sepa = `sepa_${randomString(24)}`;
|
|
33
|
+
const shipping_address = `addr_${randomString(24)}`;
|
|
34
|
+
const status = PaymentIntentStatusEnum[randomNumber(0, PaymentIntentStatusEnum.length - 1)];
|
|
35
|
+
const threeds = ThreeDSEnum[randomNumber(0, ThreeDSEnum.length - 1)];
|
|
36
|
+
const url = 'https://' + randomString(1, 800);
|
|
37
|
+
|
|
38
|
+
export const endpointResource = {
|
|
39
|
+
id,
|
|
40
|
+
amount,
|
|
41
|
+
billing_address,
|
|
42
|
+
capture,
|
|
43
|
+
card,
|
|
44
|
+
currency,
|
|
45
|
+
customer,
|
|
46
|
+
created_at,
|
|
47
|
+
date_settlement,
|
|
48
|
+
description,
|
|
49
|
+
metadata,
|
|
50
|
+
methods_allowed,
|
|
51
|
+
order_id,
|
|
52
|
+
origin,
|
|
53
|
+
payment,
|
|
54
|
+
return_url,
|
|
55
|
+
sepa,
|
|
56
|
+
shipping_address,
|
|
57
|
+
status,
|
|
58
|
+
threeds,
|
|
59
|
+
url,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const creationResource = {
|
|
63
|
+
amount: paymentAmount(amount),
|
|
64
|
+
billingAddress: billing_address as IdAddress,
|
|
65
|
+
capture,
|
|
66
|
+
card: card as IdCard,
|
|
67
|
+
currency,
|
|
68
|
+
customer: customer as IdCustomer,
|
|
69
|
+
dateSettlement: Temporal.PlainDate.from(date_settlement),
|
|
70
|
+
description,
|
|
71
|
+
metadata,
|
|
72
|
+
methodsAllowed: methods_allowed,
|
|
73
|
+
orderId: order_id,
|
|
74
|
+
origin,
|
|
75
|
+
payment: idPayment(payment),
|
|
76
|
+
returnUrl: return_url,
|
|
77
|
+
sepa: sepa as IdSepa,
|
|
78
|
+
shippingAddress: shipping_address as IdAddress,
|
|
79
|
+
status,
|
|
80
|
+
threeds,
|
|
81
|
+
url,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const endpointResourceDeserialized = {
|
|
85
|
+
id: idPaymentIntent(id),
|
|
86
|
+
createdAt: Temporal.Instant.fromEpochMilliseconds(created_at * 1000),
|
|
87
|
+
...creationResource,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const listResource = [
|
|
91
|
+
{ ...creationResource, orderId: 'firstResource' },
|
|
92
|
+
{ ...creationResource, orderId: 'secondResource' },
|
|
93
|
+
{ ...creationResource, orderId: 'thirdResource' },
|
|
94
|
+
];
|