@stancer/stancer 1.0.2 → 1.1.0
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/CHANGELOG.md +8 -0
- package/dist/api-endpoint/http/fetch-http-client.d.ts.map +1 -1
- package/dist/api-endpoint/http/fetch-http-client.js +7 -1
- package/dist/api-endpoint/http/fetch-http-client.js.map +1 -1
- package/dist/core/common/handler/metadata-handler.d.ts +18 -0
- package/dist/core/common/handler/metadata-handler.d.ts.map +1 -0
- package/dist/core/common/handler/metadata-handler.js +19 -0
- package/dist/core/common/handler/metadata-handler.js.map +1 -0
- package/dist/event/model.d.ts +20 -0
- package/dist/event/model.d.ts.map +1 -0
- package/dist/event/model.js +30 -0
- package/dist/event/model.js.map +1 -0
- package/dist/payment-intent/api-endpoint.d.ts +16 -1
- package/dist/payment-intent/api-endpoint.d.ts.map +1 -1
- package/dist/payment-intent/api-endpoint.js +2 -1
- package/dist/payment-intent/api-endpoint.js.map +1 -1
- package/dist/payment-intent/model.d.ts +3 -2
- package/dist/payment-intent/model.d.ts.map +1 -1
- package/dist/payment-intent/model.js +6 -0
- package/dist/payment-intent/model.js.map +1 -1
- package/dist/webhook/api-endpoint.d.ts +16 -0
- package/dist/webhook/api-endpoint.d.ts.map +1 -0
- package/dist/webhook/api-endpoint.js +33 -0
- package/dist/webhook/api-endpoint.js.map +1 -0
- package/dist/webhook/model.d.ts +35 -0
- package/dist/webhook/model.d.ts.map +1 -0
- package/dist/webhook/model.js +110 -0
- package/dist/webhook/model.js.map +1 -0
- package/dist/webhook/repository.d.ts +7 -0
- package/dist/webhook/repository.d.ts.map +1 -0
- package/dist/webhook/repository.js +10 -0
- package/dist/webhook/repository.js.map +1 -0
- package/dist/webhook/validator.d.ts +4 -0
- package/dist/webhook/validator.d.ts.map +1 -0
- package/dist/webhook/validator.js +16 -0
- package/dist/webhook/validator.js.map +1 -0
- package/package.json +4 -2
- package/src/api-endpoint/http/fetch-http-client.ts +8 -1
- package/src/core/common/handler/metadata-handler.ts +19 -0
- package/src/event/model.ts +42 -0
- package/src/payment-intent/api-endpoint.ts +2 -1
- package/src/payment-intent/model.ts +8 -3
- package/src/webhook/api-endpoint.ts +36 -0
- package/src/webhook/model.ts +140 -0
- package/src/webhook/repository.ts +14 -0
- package/src/webhook/validator.ts +18 -0
- package/dist/main.d.ts +0 -2
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -2
- package/dist/main.js.map +0 -1
- package/reports/base.css +0 -362
- package/reports/block-navigation.js +0 -75
- package/reports/cobertura-coverage.xml +0 -18
- package/reports/favicon.png +0 -0
- package/reports/index.html +0 -177
- package/reports/junit.xml +0 -25
- package/reports/prettify.css +0 -101
- package/reports/prettify.js +0 -955
- package/reports/sort-arrow-sprite.png +0 -0
- package/reports/sorter.js +0 -205
- package/test/fixtures/endpoint.stub.ts +0 -22
- package/test/fixtures/list-endpoint.stub.ts +0 -33
- package/test/fixtures/list-model.stub.ts +0 -24
- package/test/fixtures/model.stub.ts +0 -12
- package/test/functional/address.test.ts +0 -126
- package/test/functional/card.test.ts +0 -175
- package/test/functional/customer.test.ts +0 -174
- package/test/functional/payment-intent.test.ts +0 -215
- package/test/functional/payment.test.ts +0 -206
- package/test/functional/refund.test.ts +0 -112
- package/test/functional/sepa.test.ts +0 -136
- package/test/integration/stub-integration.test.ts +0 -88
- package/test/stub/stub-endpoint.ts +0 -42
- package/test/stub/stub-model.ts +0 -82
- package/test/stub/stub-repository.ts +0 -11
- package/test/test-helper.ts +0 -133
- package/test/unit/address/api-endpoint.test.ts +0 -134
- package/test/unit/address/fixtures.ts +0 -60
- package/test/unit/address/model.test.ts +0 -75
- package/test/unit/address/repository.test.ts +0 -148
- package/test/unit/address/validator.test.ts +0 -49
- package/test/unit/api-endpoint/http/fetch-http-client.test.ts +0 -44
- package/test/unit/api-endpoint/http/http.test.ts +0 -152
- package/test/unit/card/api-endpoint.test.ts +0 -170
- package/test/unit/card/card-helper.ts +0 -116
- package/test/unit/card/fixtures.ts +0 -87
- package/test/unit/card/model.test.ts +0 -167
- package/test/unit/card/repository.test.ts +0 -146
- package/test/unit/card/validator.test.ts +0 -96
- package/test/unit/core/common/mutation-tracked-object.test.ts +0 -24
- package/test/unit/customer/api-endpoint.test.ts +0 -143
- package/test/unit/customer/fixtures.ts +0 -73
- package/test/unit/customer/model.test.ts +0 -90
- package/test/unit/customer/repository.test.ts +0 -148
- package/test/unit/customer/validator.test.ts +0 -62
- package/test/unit/payment/api-endpoint.test.ts +0 -188
- package/test/unit/payment/fixtures.ts +0 -116
- package/test/unit/payment/model.test.ts +0 -240
- package/test/unit/payment/repository.test.ts +0 -270
- package/test/unit/payment/validator.test.ts +0 -137
- package/test/unit/payment-intent/api-endpoint.test.ts +0 -142
- package/test/unit/payment-intent/fixtures.ts +0 -94
- package/test/unit/payment-intent/model.test.ts +0 -157
- package/test/unit/payment-intent/repository.test.ts +0 -263
- package/test/unit/payment-intent/validator.test.ts +0 -136
- package/test/unit/refund/api-endpoint.test.ts +0 -129
- package/test/unit/refund/fixtures.ts +0 -58
- package/test/unit/refund/model.test.ts +0 -89
- package/test/unit/refund/repository.test.ts +0 -157
- package/test/unit/refund/validator.test.ts +0 -89
- package/test/unit/sepa/api-endpoint.test.ts +0 -170
- package/test/unit/sepa/fixture.ts +0 -62
- package/test/unit/sepa/model.test.ts +0 -86
- package/test/unit/sepa/repository.test.ts +0 -149
- package/test/unit/sepa/sepa-helper.ts +0 -71
- package/test/unit/sepa/validator.test.ts +0 -58
- package/test/unit/stub/stub-endpoint.test.ts +0 -236
- package/test/unit/stub/stub-list-endpoint.test.ts +0 -86
- package/test/unit/stub/stub-model.test.ts +0 -111
- package/test/unit/stub/stub-repository.test.ts +0 -144
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
import { describe, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { type PaymentIntentStatus, PaymentIntentStatusEnum } from '#payment-intent/enum/status.js';
|
|
5
|
-
import { IdPaymentIntent, PaymentIntentModel } from '#payment-intent/model.js';
|
|
6
|
-
import { Currency, CurrencyEnum } from '#payment/enum/currency.ts';
|
|
7
|
-
import { Method, MethodEnum } from '#payment/enum/method.ts';
|
|
8
|
-
import { PaymentAmount, paymentAmount } from '#payment/model.js';
|
|
9
|
-
import { SetterGetter, SetterGetterId, randomNumber, randomPastDate, 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
|
-
|
|
15
|
-
describe('Setter & Getter', () => {
|
|
16
|
-
const paymentIntent = new PaymentIntentModel({});
|
|
17
|
-
|
|
18
|
-
it('id', () => {
|
|
19
|
-
SetterGetterId((value: IdPaymentIntent) => new PaymentIntentModel({}, value), 'paym_' + randomString(24));
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('amount', () => {
|
|
23
|
-
SetterGetter(
|
|
24
|
-
(value: PaymentAmount) => {
|
|
25
|
-
paymentIntent.setAmount(value);
|
|
26
|
-
},
|
|
27
|
-
() => paymentIntent.amount,
|
|
28
|
-
paymentAmount(randomNumber(50, 3000)),
|
|
29
|
-
);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('billing address', () => {
|
|
33
|
-
SetterGetter(
|
|
34
|
-
(value: IdAddress) => paymentIntent.setBillingAddress(value),
|
|
35
|
-
() => paymentIntent.billingAddress,
|
|
36
|
-
'addr_' + randomString(24),
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('card', () => {
|
|
41
|
-
SetterGetter(
|
|
42
|
-
(value: IdCard) => paymentIntent.setCard(value),
|
|
43
|
-
() => paymentIntent.card,
|
|
44
|
-
'card_' + randomString(24),
|
|
45
|
-
);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it.each(CurrencyEnum.map((n) => [n]))('currency %s', (currency: Currency) => {
|
|
49
|
-
paymentIntent._data.currency = null;
|
|
50
|
-
SetterGetter(
|
|
51
|
-
(value: Currency) => paymentIntent.setCurrency(value),
|
|
52
|
-
() => paymentIntent.currency,
|
|
53
|
-
currency,
|
|
54
|
-
);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('customer', () => {
|
|
58
|
-
SetterGetter(
|
|
59
|
-
(value: IdCustomer) => paymentIntent.setCustomer(value),
|
|
60
|
-
() => paymentIntent.customer,
|
|
61
|
-
'cust_' + randomString(24),
|
|
62
|
-
);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('dateSettlement', () => {
|
|
66
|
-
SetterGetter(
|
|
67
|
-
(value: Temporal.PlainDate) => (paymentIntent._data.dateSettlement = value),
|
|
68
|
-
() => paymentIntent.dateSettlement,
|
|
69
|
-
randomPastDate(),
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('description', () => {
|
|
74
|
-
SetterGetter(
|
|
75
|
-
(value: string) => paymentIntent.setDescription(value),
|
|
76
|
-
() => paymentIntent.description,
|
|
77
|
-
randomString(24),
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it.each(MethodEnum.map((n) => [[n]]))('methodsAllowed %s', (method) => {
|
|
82
|
-
paymentIntent._data.methodsAllowed = null;
|
|
83
|
-
SetterGetter(
|
|
84
|
-
(value: Method[]) => paymentIntent.setMethodsAllowed(value),
|
|
85
|
-
() => paymentIntent.methodsAllowed,
|
|
86
|
-
method,
|
|
87
|
-
);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('orderId', () => {
|
|
91
|
-
SetterGetter(
|
|
92
|
-
(value: string) => paymentIntent.setOrderId(value),
|
|
93
|
-
() => paymentIntent.orderId,
|
|
94
|
-
randomString(24),
|
|
95
|
-
);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('origin', () => {
|
|
99
|
-
SetterGetter(
|
|
100
|
-
(value: number) => (paymentIntent._data.origin = value),
|
|
101
|
-
() => paymentIntent.origin,
|
|
102
|
-
randomNumber(0, 8),
|
|
103
|
-
);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('returnUrl', () => {
|
|
107
|
-
paymentIntent._data.returnUrl = null;
|
|
108
|
-
SetterGetter(
|
|
109
|
-
(value: string) => paymentIntent.setReturnUrl(value),
|
|
110
|
-
() => paymentIntent.returnUrl,
|
|
111
|
-
randomString(1, 1000),
|
|
112
|
-
);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('Sepa', () => {
|
|
116
|
-
paymentIntent._data.sepa = null;
|
|
117
|
-
SetterGetter(
|
|
118
|
-
(value: string) => paymentIntent.setSepa(value),
|
|
119
|
-
() => paymentIntent.sepa,
|
|
120
|
-
'sepa_' + randomString(24),
|
|
121
|
-
);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('Shipping address', () => {
|
|
125
|
-
SetterGetter(
|
|
126
|
-
(value: IdAddress) => paymentIntent.setShippingAddress(value),
|
|
127
|
-
() => paymentIntent.shippingAddress,
|
|
128
|
-
'addr_' + randomString(24),
|
|
129
|
-
);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it.each(PaymentIntentStatusEnum.map((n) => [n]))('status %s', (status) => {
|
|
133
|
-
paymentIntent._data.status = null;
|
|
134
|
-
SetterGetter(
|
|
135
|
-
(value: PaymentIntentStatus) => (paymentIntent._data.status = value),
|
|
136
|
-
() => paymentIntent.status,
|
|
137
|
-
status,
|
|
138
|
-
);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('url', () => {
|
|
142
|
-
paymentIntent._data.url = null;
|
|
143
|
-
SetterGetter(
|
|
144
|
-
(value: string) => (paymentIntent._data.url = value),
|
|
145
|
-
() => paymentIntent.url,
|
|
146
|
-
randomString(1, 2048),
|
|
147
|
-
);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('uniqueId', () => {
|
|
151
|
-
SetterGetter(
|
|
152
|
-
(value: string) => paymentIntent.setUniqueId(value),
|
|
153
|
-
() => paymentIntent.uniqueId,
|
|
154
|
-
randomString(24),
|
|
155
|
-
);
|
|
156
|
-
});
|
|
157
|
-
});
|
|
@@ -1,263 +0,0 @@
|
|
|
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 { PaymentIntentApiEndpoint } from '#payment-intent/api-endpoint.js';
|
|
7
|
-
import { IdPaymentIntent, PaymentIntentModel, idPaymentIntent } from '#payment-intent/model.js';
|
|
8
|
-
import { PaymentIntentRepository } from '#payment-intent/repository.js';
|
|
9
|
-
import { PaymentIntentValidator } from '#payment-intent/validator.js';
|
|
10
|
-
import { paymentAmount } from '#payment/model.js';
|
|
11
|
-
import { RefundModel, refundAmount } from '#refund/model.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 = 'pi';
|
|
18
|
-
const id = idPaymentIntent(`${idPrefix}_${randomString(24)}`);
|
|
19
|
-
const stancerErrorID = idPaymentIntent(`${idPrefix}_unidentifiedError${randomString(7)}`);
|
|
20
|
-
const stancerRouteInaccessibleId = idPaymentIntent(`${idPrefix}_StancerRouteInnacessible`);
|
|
21
|
-
const dataHasBeenSentId = idPaymentIntent(`${idPrefix}_DataHasBeenSent${randomString(9)}`);
|
|
22
|
-
const dataUnmodified = idPaymentIntent(`${idPrefix}_unmodifiedData${randomString(10)}`);
|
|
23
|
-
const emptyRefund = idPaymentIntent(`${idPrefix}_emptyRefund${randomString(13)}`);
|
|
24
|
-
const refundId = idPaymentIntent(`${idPrefix}_refundedPaymentIntent${randomString(3)}`);
|
|
25
|
-
|
|
26
|
-
class MockApiEndpoint {
|
|
27
|
-
constructor() {}
|
|
28
|
-
|
|
29
|
-
list() {
|
|
30
|
-
async function* listGenerator() {
|
|
31
|
-
yield* listResource;
|
|
32
|
-
}
|
|
33
|
-
return listGenerator();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
delete() {
|
|
37
|
-
return creationResource;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
send(id: string, data: Record<string, unknown>) {
|
|
41
|
-
if (id) {
|
|
42
|
-
if (id === stancerErrorID) {
|
|
43
|
-
throw new StancerError();
|
|
44
|
-
}
|
|
45
|
-
if (id === stancerRouteInaccessibleId) {
|
|
46
|
-
throw new StancerRouteInaccessibleError('PATCH', 'stub');
|
|
47
|
-
}
|
|
48
|
-
if (id === dataUnmodified) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return { ...data, id: dataHasBeenSentId };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get() {
|
|
56
|
-
return { ...creationResource, id: id };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
refunds(id: IdPaymentIntent) {
|
|
60
|
-
if (id === emptyRefund) {
|
|
61
|
-
return [];
|
|
62
|
-
}
|
|
63
|
-
if (id === refundId) {
|
|
64
|
-
listRefund.push({ ...refundDeserialized, amount: refundingAmount });
|
|
65
|
-
}
|
|
66
|
-
return listRefund.map((value) => new RefundModel({}, value));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
refund() {
|
|
70
|
-
return { ...creationResource, id: id };
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
it('can be instanciated', () => {
|
|
75
|
-
const instance = new PaymentIntentRepository();
|
|
76
|
-
expect(instance).toBeInstanceOf(PaymentIntentRepository);
|
|
77
|
-
expect(instance).toBeInstanceOf(AbstractRepository);
|
|
78
|
-
//accessing private method & property
|
|
79
|
-
expect(instance.endpoint).toBeInstanceOf(PaymentIntentApiEndpoint);
|
|
80
|
-
expect(instance.validator).toBe(PaymentIntentValidator);
|
|
81
|
-
expect(instance.staticModel).toBe(PaymentIntentModel);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe('repository', () => {
|
|
85
|
-
const mockRepo = new MockApiEndpoint() as unknown as PaymentIntentApiEndpoint; // I lie, for convenience sake
|
|
86
|
-
const repository = new PaymentIntentRepository(mockRepo);
|
|
87
|
-
|
|
88
|
-
describe('create', () => {
|
|
89
|
-
it("doesn't accept bad data", async () => {
|
|
90
|
-
const badID = randomString(30, 56);
|
|
91
|
-
// using puposefuly bad ID
|
|
92
|
-
await expect(async () => await repository.create(badID)).rejects.toThrow();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('accept good data', async () => {
|
|
96
|
-
const model = await repository.create(id);
|
|
97
|
-
expect(model).toBeInstanceOf(PaymentIntentModel);
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('delete', async () => {
|
|
102
|
-
const modelWithoutId = await repository.create();
|
|
103
|
-
await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
|
|
104
|
-
const model = await repository.create({ ...creationResource, id: idPaymentIntent(endpointResource.id) });
|
|
105
|
-
await expect((await repository.delete(model))._data).toStrictEqual(creationResource);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe('send', () => {
|
|
109
|
-
it('should return model', async () => {
|
|
110
|
-
const model = await repository.create(creationResource);
|
|
111
|
-
const sentModel = await repository.send(model);
|
|
112
|
-
expect(sentModel).toStrictEqual(model);
|
|
113
|
-
expect(sentModel._data.id).toEqual(dataHasBeenSentId);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('should throw on inprecise error', async () => {
|
|
117
|
-
const model = await repository.create({ ...creationResource, id: stancerErrorID as IdPaymentIntent });
|
|
118
|
-
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should return error on StancerRouteInacessibleError', async () => {
|
|
122
|
-
const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdPaymentIntent });
|
|
123
|
-
await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('should return unmodified model when no new Data', async () => {
|
|
127
|
-
const model = await repository.create({ ...creationResource, id: dataUnmodified as IdPaymentIntent });
|
|
128
|
-
const sentModel = await repository.send(model);
|
|
129
|
-
expect(model).toStrictEqual(sentModel);
|
|
130
|
-
expect(sentModel._data.id).toEqual(dataUnmodified);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
describe('populate', () => {
|
|
135
|
-
it('should not modify model if no Id', async () => {
|
|
136
|
-
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
137
|
-
const model = await repository.create(creationResource);
|
|
138
|
-
expect(model).toBeInstanceOf(PaymentIntentModel);
|
|
139
|
-
expect(getEndpoint).toHaveBeenCalledTimes(0);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should get model if only id', async () => {
|
|
143
|
-
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
144
|
-
const model = await repository.create(id);
|
|
145
|
-
expect(model).toBeInstanceOf(PaymentIntentModel);
|
|
146
|
-
expect(getEndpoint).toHaveBeenCalledTimes(1);
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
describe('list', () => {
|
|
151
|
-
it('return correct model one by one', async () => {
|
|
152
|
-
const listStub = repository.list();
|
|
153
|
-
const item1 = await listStub.next();
|
|
154
|
-
expect(item1.done).toBeFalsy();
|
|
155
|
-
expect(item1.value).toHaveProperty('id');
|
|
156
|
-
expect(item1.value).toBeInstanceOf(PaymentIntentModel);
|
|
157
|
-
expect((item1.value as PaymentIntentModel).orderId).toBe('firstResource');
|
|
158
|
-
|
|
159
|
-
const item2 = await listStub.next();
|
|
160
|
-
expect(item2.done).toBeFalsy();
|
|
161
|
-
expect(item2.value).toHaveProperty('id');
|
|
162
|
-
expect(item2.value).toBeInstanceOf(PaymentIntentModel);
|
|
163
|
-
|
|
164
|
-
expect((item2.value as PaymentIntentModel).orderId).toBe('secondResource');
|
|
165
|
-
|
|
166
|
-
const item3 = await listStub.next();
|
|
167
|
-
expect(item3.done).toBeFalsy();
|
|
168
|
-
expect(item3.value).toHaveProperty('id');
|
|
169
|
-
expect(item3.value).toBeInstanceOf(PaymentIntentModel);
|
|
170
|
-
expect((item3.value as PaymentIntentModel).orderId).toBe('thirdResource');
|
|
171
|
-
|
|
172
|
-
const item4 = await listStub.next();
|
|
173
|
-
expect(item4.done).toBeTruthy();
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
describe('list paymentintent refunds', () => {
|
|
178
|
-
it('not populated paymentintent cannot get refund', async () => {
|
|
179
|
-
const paymentintent = await repository.create({ amount: paymentAmount(100), currency: 'eur' });
|
|
180
|
-
await expect(() => repository.getRefunds(paymentintent)).rejects.toThrow(
|
|
181
|
-
'PaymentIntent must be populated before calling getRefunds method',
|
|
182
|
-
);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
it('populate empty array when no refund', async () => {
|
|
186
|
-
const paymentintent = await repository.create(emptyRefund);
|
|
187
|
-
paymentintent._id = emptyRefund;
|
|
188
|
-
|
|
189
|
-
await expect(repository.getRefunds(paymentintent)).resolves.toStrictEqual([]);
|
|
190
|
-
expect(paymentintent.refunds).toEqual([]);
|
|
191
|
-
expect(paymentintent.isRefundPopulated()).toEqual(true);
|
|
192
|
-
expect(paymentintent.refundedAmount).toEqual(0);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it('populate refund array', async () => {
|
|
196
|
-
const paymentintent = await repository.create(id);
|
|
197
|
-
|
|
198
|
-
await expect(repository.getRefunds(paymentintent));
|
|
199
|
-
expect(paymentintent.refunds).toHaveLength(3);
|
|
200
|
-
expect(paymentintent.isRefundPopulated()).toEqual(true);
|
|
201
|
-
expect(paymentintent.refundedAmount).toEqual(150);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
describe('refund the paymentintent', () => {
|
|
206
|
-
it('cannot refund if not populated', async () => {
|
|
207
|
-
const paymentintent = await repository.create({ amount: paymentAmount(100), currency: 'eur' });
|
|
208
|
-
await expect(() => repository.refund(paymentintent, refundAmount(50))).rejects.toThrow(
|
|
209
|
-
'PaymentIntent must be populated before calling refund method',
|
|
210
|
-
);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('cannot do partial refund if not captured', async () => {
|
|
214
|
-
const paymentintent = await repository.create(id);
|
|
215
|
-
paymentintent._data.status = 'processing';
|
|
216
|
-
await expect(() => repository.refund(paymentintent, refundAmount(50))).rejects.toThrow(
|
|
217
|
-
"You cannot partially refund a payment intent that isn't captured",
|
|
218
|
-
);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it('cannot refund more than paid', async () => {
|
|
222
|
-
const paymentintent = await repository.create(id);
|
|
223
|
-
expect(paymentintent.amount).toBeDefined();
|
|
224
|
-
expect(paymentintent.id).toBeDefined();
|
|
225
|
-
paymentintent._data.status = 'captured';
|
|
226
|
-
await repository.getRefunds(paymentintent);
|
|
227
|
-
expect(paymentintent.amount).toBeDefined();
|
|
228
|
-
if (paymentintent.amount === undefined) {
|
|
229
|
-
throw Error('unacessible error');
|
|
230
|
-
}
|
|
231
|
-
const refundingAmount = paymentintent.amount + randomNumber(1, 100);
|
|
232
|
-
await expect(() => repository.refund(paymentintent, refundAmount(refundingAmount))).rejects.toThrow(
|
|
233
|
-
`You are trying to refund ${refundingAmount} it's ${refundingAmount - paymentintent.refundableAmount} more than possible ${paymentintent.refundableAmount}.`,
|
|
234
|
-
);
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it('cannot refund more than paid minus already refunded', async () => {
|
|
238
|
-
const paymentintent = await repository.create(id);
|
|
239
|
-
paymentintent._data.status = 'captured';
|
|
240
|
-
expect(paymentintent.amount).toBeDefined();
|
|
241
|
-
expect(paymentintent.id).toBeDefined();
|
|
242
|
-
expect(await repository.getRefunds(paymentintent));
|
|
243
|
-
const refundingAmount = paymentintent.refundableAmount + randomNumber(1, 100);
|
|
244
|
-
await expect(() => repository.refund(paymentintent, refundAmount(refundingAmount))).rejects.toThrow(
|
|
245
|
-
`You are trying to refund ${refundingAmount} it's ${refundingAmount - paymentintent.refundableAmount} more than possible ${paymentintent.refundableAmount}.`,
|
|
246
|
-
);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
//with the mocker it is really hard to add a new refund to the refund list
|
|
250
|
-
it('refund when number is correct', async () => {
|
|
251
|
-
const paymentintent = await repository.create(id);
|
|
252
|
-
expect(paymentintent.amount).toBeDefined();
|
|
253
|
-
expect(paymentintent.id).toBeDefined();
|
|
254
|
-
expect(await repository.getRefunds(paymentintent));
|
|
255
|
-
paymentintent._id = idPaymentIntent(refundId);
|
|
256
|
-
paymentintent._data.status = 'captured';
|
|
257
|
-
expect(paymentintent.amount).toBeGreaterThan(paymentintent.refundableAmount);
|
|
258
|
-
const refundableAmount = paymentintent.refundableAmount;
|
|
259
|
-
const refundingAmount = refundAmount(randomNumber(1, refundableAmount));
|
|
260
|
-
await expect(repository.refund(paymentintent, refundAmount(refundingAmount))).resolves.toEqual(paymentintent);
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
});
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { PaymentIntentStatus, PaymentIntentStatusEnum } from '#payment-intent/enum/status.js';
|
|
5
|
-
import { type ThreeDS, ThreeDSEnum } from '#payment-intent/enum/threeds.js';
|
|
6
|
-
import { PaymentIntentModel } from '#payment-intent/model.js';
|
|
7
|
-
import { PaymentIntentValidator } from '#payment-intent/validator.js';
|
|
8
|
-
import { Currency, CurrencyEnum } from '#payment/enum/currency.js';
|
|
9
|
-
import { PaymentAmount, PaymentModel, paymentAmount } from '#payment/model.js';
|
|
10
|
-
import { IdSepa } from '#sepa/model.js';
|
|
11
|
-
import { checkType, goldilocks, goldilocksId, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
12
|
-
|
|
13
|
-
import { IdAddress } from '../../../src/address/model';
|
|
14
|
-
import { IdCard } from '../../../src/card/model';
|
|
15
|
-
import { IdCustomer } from '../../../src/customer/model';
|
|
16
|
-
|
|
17
|
-
describe('Goldilocks & type', () => {
|
|
18
|
-
const paymentIntent = new PaymentIntentModel(PaymentIntentValidator);
|
|
19
|
-
|
|
20
|
-
it('amount', () => {
|
|
21
|
-
goldilocks((value: PaymentAmount) => paymentIntent.setAmount(value), 'number', { min: 0 });
|
|
22
|
-
checkType((value: PaymentAmount) => paymentIntent.setAmount(value), ['number'], paymentAmount(randomNumber(50)));
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('billing address', () => {
|
|
26
|
-
goldilocksId((value: IdAddress) => paymentIntent.setBillingAddress(value), 'addr_');
|
|
27
|
-
checkType(
|
|
28
|
-
(value: IdAddress) => paymentIntent.setBillingAddress(value),
|
|
29
|
-
['object', 'string'],
|
|
30
|
-
`addr_${randomString(24)}`,
|
|
31
|
-
);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('capture', () => {
|
|
35
|
-
checkType((value: boolean) => (paymentIntent._data.capture = value), ['boolean'], true);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('card', () => {
|
|
39
|
-
goldilocksId((value: IdCard) => paymentIntent.setCard(value), 'card_');
|
|
40
|
-
checkType((value: IdCard) => paymentIntent.setCard(value), ['string', 'object'], `card_${randomString(24)}`);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it.each(CurrencyEnum.map((n) => [n]))('currency %s', (currency) => {
|
|
44
|
-
const falseCurrency = randomString(3);
|
|
45
|
-
expect(() => paymentIntent.setCurrency(falseCurrency)).toThrow();
|
|
46
|
-
paymentIntent.setCurrency(currency);
|
|
47
|
-
expect(paymentIntent.currency).toBe(currency);
|
|
48
|
-
checkType((value: Currency) => paymentIntent.setCurrency(value), [], currency);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('Customer', () => {
|
|
52
|
-
const falseCustomer = randomString(24);
|
|
53
|
-
const trueCustomer = `cust_${randomString(24)}`;
|
|
54
|
-
expect(() => paymentIntent.setCustomer(falseCustomer)).toThrow();
|
|
55
|
-
goldilocksId((value: IdCustomer) => paymentIntent.setCustomer(value), 'cust_');
|
|
56
|
-
checkType((value: IdCustomer) => paymentIntent.setCustomer(value), ['string'], trueCustomer);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('DateSettlement', () => {
|
|
60
|
-
const falseDateSettlement = new Temporal.PlainDate(9999, randomMonth(), randomNumber(1, 28));
|
|
61
|
-
|
|
62
|
-
const DateSettlement = new Temporal.PlainDate(
|
|
63
|
-
randomNumber(1971, Temporal.PlainDate.from(Temporal.Now.plainDateISO()).year),
|
|
64
|
-
randomMonth(),
|
|
65
|
-
randomNumber(1, 30),
|
|
66
|
-
);
|
|
67
|
-
expect(() => paymentIntent.setDateSettlement(falseDateSettlement)).toThrow();
|
|
68
|
-
paymentIntent.setDateSettlement(DateSettlement);
|
|
69
|
-
expect(paymentIntent.dateSettlement).toBe(DateSettlement);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('Description', () => {
|
|
73
|
-
goldilocks((value: string) => paymentIntent.setDescription(value), 'string', { min: 1, max: 64 });
|
|
74
|
-
checkType((value: string) => paymentIntent.setDescription(value), ['string'], randomString(64));
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('orderId', () => {
|
|
78
|
-
goldilocks((value: string) => paymentIntent.setOrderId(value), 'string', { min: 1, max: 36 });
|
|
79
|
-
checkType((value: string) => paymentIntent.setOrderId(value), ['string'], randomString(24));
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('payment', () => {
|
|
83
|
-
checkType(
|
|
84
|
-
(value: PaymentModel) => (paymentIntent._data.payment = value),
|
|
85
|
-
['string', 'object'],
|
|
86
|
-
`paym_${randomString(24)}`,
|
|
87
|
-
);
|
|
88
|
-
const newPaymentIntent = new PaymentIntentModel(PaymentIntentValidator);
|
|
89
|
-
expect(() => newPaymentIntent._data.payment(IdPayment(`paym_${randomString(24)}`)));
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('returnUrl', () => {
|
|
93
|
-
goldilocks((value: string) => paymentIntent.setReturnUrl(value), 'string', { min: 1, max: 2048 });
|
|
94
|
-
checkType((value: string) => paymentIntent.setReturnUrl(value), ['string'], randomString(24));
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('Sepa', () => {
|
|
98
|
-
goldilocksId((value: IdSepa) => paymentIntent.setSepa(value), 'sepa_');
|
|
99
|
-
checkType((value: IdSepa) => paymentIntent.setSepa(value), ['string', 'object'], `sepa_${randomString(24)}`);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('shipping address', () => {
|
|
103
|
-
goldilocksId((value: IdAddress) => paymentIntent.setShippingAddress(value), 'addr_');
|
|
104
|
-
checkType(
|
|
105
|
-
(value: IdAddress) => paymentIntent.setShippingAddress(value),
|
|
106
|
-
['object', 'string'],
|
|
107
|
-
`addr_${randomString(24)}`,
|
|
108
|
-
);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it.each(PaymentIntentStatusEnum.map((n) => [n]))('paymentIntent status %s', (status) => {
|
|
112
|
-
const falseStatus = randomString(100, 200);
|
|
113
|
-
expect(() => (paymentIntent._data.status = falseStatus)).toThrow();
|
|
114
|
-
paymentIntent._data.status = status;
|
|
115
|
-
expect(paymentIntent.status).toBe(status);
|
|
116
|
-
checkType((value: PaymentIntentStatus) => (paymentIntent._data.status = value), [], status);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it.each(ThreeDSEnum.map((n) => [n]))('paymentIntent status %s', (threeds) => {
|
|
120
|
-
const falsethreeds = randomString(100, 200);
|
|
121
|
-
expect(() => (paymentIntent._data.threeds = falsethreeds)).toThrow();
|
|
122
|
-
paymentIntent._data.threeds = threeds;
|
|
123
|
-
expect(paymentIntent.threeDS).toBe(threeds);
|
|
124
|
-
checkType((value: ThreeDS) => (paymentIntent._data.threeds = value), [], threeds);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('url', () => {
|
|
128
|
-
goldilocks((value: string) => (paymentIntent._data.url = value), 'string', { min: 1, max: 2048 });
|
|
129
|
-
checkType((value: string) => (paymentIntent._data.url = value), ['string'], randomString(24));
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('uniqueId', () => {
|
|
133
|
-
goldilocks((value: string) => paymentIntent.setUniqueId(value), 'string', { min: 1, max: 36 });
|
|
134
|
-
checkType((value: string) => paymentIntent.setUniqueId(value), ['string'], randomString(24));
|
|
135
|
-
});
|
|
136
|
-
});
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { RefundApiEndpoint } from '#refund/api-endpoint.js';
|
|
4
|
-
import type { IdRefund } from '#refund/model.js';
|
|
5
|
-
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
6
|
-
|
|
7
|
-
import { endpointResource, endpointResourceDeserialized } from './fixtures.js';
|
|
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('refund api endpoint', () => {
|
|
35
|
-
describe('Serialize/Deserialize resources', () => {
|
|
36
|
-
const endpoint = new RefundApiEndpoint();
|
|
37
|
-
|
|
38
|
-
it('test deserialize resource', async () => {
|
|
39
|
-
const deserializedResources = await endpoint.deserializeResource(endpointResource);
|
|
40
|
-
expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
|
|
41
|
-
expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('test post serialize resource', () => {
|
|
45
|
-
const serializedResources = endpoint.postObjectSerializer(
|
|
46
|
-
endpointResourceDeserialized,
|
|
47
|
-
) as typeof endpointResource;
|
|
48
|
-
const postObject = {
|
|
49
|
-
amount: endpointResource.amount,
|
|
50
|
-
payment: endpointResource.payment,
|
|
51
|
-
};
|
|
52
|
-
expect(serializedResources).toMatchObject(postObject);
|
|
53
|
-
expect(postObject).toMatchObject(serializedResources);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe('endpoint path', () => {
|
|
58
|
-
it('endpoint url', () => {
|
|
59
|
-
const endpoint = new RefundApiEndpoint();
|
|
60
|
-
const id = ('refd_' + randomString(24)) as IdRefund;
|
|
61
|
-
expect(endpoint._getResourceUri(id)).toStrictEqual(`https://api.stancer.com/v2/refunds/${id}`);
|
|
62
|
-
expect(endpoint._getResourceUri()).toStrictEqual('https://api.stancer.com/v2/refunds/');
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe('verbs', () => {
|
|
67
|
-
const refundEndpoint = new RefundApiEndpoint();
|
|
68
|
-
|
|
69
|
-
it('get', async () => {
|
|
70
|
-
const responseWithId = await refundEndpoint.get(('refd_' + randomString(24)) as IdRefund);
|
|
71
|
-
expect(await responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('post no deserialization', async () => {
|
|
75
|
-
const responseWithId = await refundEndpoint.post({
|
|
76
|
-
amount: randomNumber(0, 100000),
|
|
77
|
-
payment: `paym_${randomString(24)}`,
|
|
78
|
-
});
|
|
79
|
-
expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('does not patch', async () => {
|
|
83
|
-
expect(() => refundEndpoint.patch(('refd_' + randomString(24)) as IdRefund, endpointResource)).toThrow();
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('delete', async () => {
|
|
87
|
-
expect(await refundEndpoint.delete(('refd_' + randomString(24)) as IdRefund)).toStrictEqual(
|
|
88
|
-
endpointResourceDeserialized,
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
describe('send', () => {
|
|
94
|
-
const { amount, payment } = endpointResourceDeserialized;
|
|
95
|
-
|
|
96
|
-
it('send with required data', async () => {
|
|
97
|
-
const endpoint = new RefundApiEndpoint();
|
|
98
|
-
await expect(endpoint.send(undefined, { amount, payment })).resolves.toStrictEqual(endpointResourceDeserialized);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('does not post with missing amount', async () => {
|
|
102
|
-
const endpoint = new RefundApiEndpoint();
|
|
103
|
-
expect(() => endpoint.send(undefined, { payment })).toThrow();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('does not post with missing payment', async () => {
|
|
107
|
-
const endpoint = new RefundApiEndpoint();
|
|
108
|
-
expect(() => endpoint.send(undefined, { amount })).toThrow();
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('Refund does not Patch if id', async () => {
|
|
112
|
-
const endpoint = new RefundApiEndpoint();
|
|
113
|
-
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
114
|
-
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
115
|
-
expect(() => endpoint.send('refd_' + randomString(24), { payment, amount })).toThrow();
|
|
116
|
-
expect(spyonPatch).toHaveBeenCalledTimes(1);
|
|
117
|
-
expect(spyonPost).toHaveBeenCalledTimes(0);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('post if no id', () => {
|
|
121
|
-
const endpoint = new RefundApiEndpoint();
|
|
122
|
-
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
123
|
-
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
124
|
-
endpoint.send(undefined, { payment, amount });
|
|
125
|
-
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
126
|
-
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
});
|