@stancer/stancer 0.0.0-stage → 1.0.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/.editorconfig +14 -0
- package/.gitlab-ci.yml +159 -0
- package/.prettierignore +3 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE +19 -0
- package/README.md +166 -2
- package/eslint.config.js +14 -0
- package/package.json +52 -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,80 @@
|
|
|
1
|
+
import { AbstractModel, type ModelData } from '#core/abstract-model.js';
|
|
2
|
+
import type { IdValue } from '#core/common/branded-type.js';
|
|
3
|
+
import { validateID } from '#core/common/id-validator.js';
|
|
4
|
+
|
|
5
|
+
import type { IdCustomer } from '../customer/model.js';
|
|
6
|
+
|
|
7
|
+
export type IdSepa = IdValue<`sepa_${string}`>;
|
|
8
|
+
export const IdSepa = (id: string) => validateID<IdSepa>(id, SepaModel.regex);
|
|
9
|
+
export interface SepaData extends ModelData {
|
|
10
|
+
bic: string;
|
|
11
|
+
customer: IdCustomer;
|
|
12
|
+
dateMandate: Temporal.PlainDate;
|
|
13
|
+
deleted: boolean;
|
|
14
|
+
id: IdSepa;
|
|
15
|
+
last4: string;
|
|
16
|
+
mandate: string;
|
|
17
|
+
name: string;
|
|
18
|
+
iban: string;
|
|
19
|
+
}
|
|
20
|
+
export class SepaModel extends AbstractModel<SepaData> {
|
|
21
|
+
static regex = /^sepa_[a-zA-Z0-9]{24}$/;
|
|
22
|
+
|
|
23
|
+
get bic() {
|
|
24
|
+
return this._data.bic;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get customer() {
|
|
28
|
+
return this._data.customer;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get last4() {
|
|
32
|
+
if (this._data.iban && !this._data.last4) {
|
|
33
|
+
return this._data.iban?.slice(-4);
|
|
34
|
+
}
|
|
35
|
+
return this._data.last4;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get mandate() {
|
|
39
|
+
return this._data.mandate;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get dateMandate() {
|
|
43
|
+
return this._data.dateMandate;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get name() {
|
|
47
|
+
return this._data.name;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setMandate(value: string) {
|
|
51
|
+
this._data.mandate = value;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
setName(value: string) {
|
|
56
|
+
this._data.name = value;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setDateMandate(value: Temporal.PlainDate) {
|
|
61
|
+
this._data.dateMandate = value;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setIban(value: string) {
|
|
66
|
+
this._data.iban = value;
|
|
67
|
+
this._data.last4 = value.slice(-4);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
setBic(value: string) {
|
|
72
|
+
this._data.bic = value;
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
setCustomer(value: IdCustomer) {
|
|
77
|
+
this._data.customer = value;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AbstractRepository, type Repository } from '#core/abstract-repository.js';
|
|
2
|
+
import type { EndpointInterface } from '#core/endpoint-interface.js';
|
|
3
|
+
import { SepaApiEndpoint } from '#sepa/api-endpoint.js';
|
|
4
|
+
import { type SepaData, SepaModel } from '#sepa/model.js';
|
|
5
|
+
import { sepaValidator } from '#sepa/validator.js';
|
|
6
|
+
|
|
7
|
+
export class SepaRepository extends AbstractRepository<SepaData, SepaModel> implements Repository<SepaData, SepaModel> {
|
|
8
|
+
constructor(endpoint?: EndpointInterface<SepaData>) {
|
|
9
|
+
super({ endpoint: endpoint ?? new SepaApiEndpoint(), validator: sepaValidator, model: SepaModel });
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
|
|
3
|
+
import { IdValidator } from '#core/common/id-validator.js';
|
|
4
|
+
import { type Validator, createdAtValidator, goldilocksValidator } from '#core/validator.js';
|
|
5
|
+
|
|
6
|
+
import { CustomerModel, type IdCustomer } from '../customer/model.js';
|
|
7
|
+
import { type IdSepa, type SepaData, SepaModel } from './model.js';
|
|
8
|
+
|
|
9
|
+
// IBAN validation using ISO/IEC 7064 MOD 97-10 algorithm
|
|
10
|
+
// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN
|
|
11
|
+
export const ibanValidator = {
|
|
12
|
+
validate: (arg0: string) => {
|
|
13
|
+
const iban = arg0.replaceAll(' ', '');
|
|
14
|
+
if (!/^[a-zA-Z0-9]+$/.test(iban)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const start = iban.slice(0, 4);
|
|
18
|
+
const bban = iban.slice(4);
|
|
19
|
+
const codeWithLetters = (bban + start).toUpperCase();
|
|
20
|
+
|
|
21
|
+
const letterToNumber = (character: string) => character.charCodeAt(0) - 55;
|
|
22
|
+
|
|
23
|
+
const code = codeWithLetters
|
|
24
|
+
.split('')
|
|
25
|
+
.map((character) => (Number.isNaN(Number(character)) ? letterToNumber(character) : character))
|
|
26
|
+
.join('');
|
|
27
|
+
|
|
28
|
+
const check = code.slice(0, 2);
|
|
29
|
+
const parts = code.slice(2).match(/.{1,7}/g) ?? [];
|
|
30
|
+
|
|
31
|
+
const result = parts.reduce((prev, curr) => String(Number(prev + curr) % 97), check);
|
|
32
|
+
|
|
33
|
+
if (result === '1') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
},
|
|
38
|
+
errorMessage: (value: string) => `IBAN ending in ${value.slice(-4)} is not valid`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const sepaValidator: Validator<SepaData> = {
|
|
42
|
+
id: IdValidator<IdSepa>(SepaModel.regex),
|
|
43
|
+
bic: goldilocksValidator('bic', { min: 8, max: 11 }),
|
|
44
|
+
customer: IdValidator<IdCustomer>(CustomerModel.regex),
|
|
45
|
+
createdAt: createdAtValidator,
|
|
46
|
+
deleted: true,
|
|
47
|
+
dateMandate: {
|
|
48
|
+
validate: (arg0: Temporal.PlainDate) =>
|
|
49
|
+
arg0.year > 1970 && Temporal.PlainDate.compare(Temporal.Now.plainDateISO(), arg0) === 1,
|
|
50
|
+
errorMessage: () => 'DateMandate must be a past date.',
|
|
51
|
+
},
|
|
52
|
+
iban: ibanValidator,
|
|
53
|
+
last4: true,
|
|
54
|
+
mandate: goldilocksValidator('mandate', { min: 1, max: 35 }),
|
|
55
|
+
name: goldilocksValidator('name', { min: 3, max: 64 }),
|
|
56
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IdValue } from '#core/common/branded-type.js';
|
|
2
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
3
|
+
|
|
4
|
+
const id = 'stub_' + randomString(24, 24);
|
|
5
|
+
const defaultValue = randomString(1, 50);
|
|
6
|
+
const defaultNumber = randomNumber(12, 200);
|
|
7
|
+
export const stubSerialized = {
|
|
8
|
+
id,
|
|
9
|
+
default: defaultValue,
|
|
10
|
+
default_number: defaultNumber,
|
|
11
|
+
untouchable: false,
|
|
12
|
+
all_authorized: true,
|
|
13
|
+
ungettable: false,
|
|
14
|
+
unmodifiable: false,
|
|
15
|
+
};
|
|
16
|
+
export const stubDeserialized = {
|
|
17
|
+
id: id as IdValue,
|
|
18
|
+
default: defaultValue,
|
|
19
|
+
defaultNumber: defaultNumber,
|
|
20
|
+
allAuthorized: true,
|
|
21
|
+
unmodifiable: false,
|
|
22
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
2
|
+
|
|
3
|
+
const stub = {
|
|
4
|
+
stubs: [
|
|
5
|
+
{
|
|
6
|
+
id: 'stub_' + randomString(24, 24),
|
|
7
|
+
default: randomString(1, 50),
|
|
8
|
+
defaultNumber: randomNumber(12, 263),
|
|
9
|
+
untouchable: true,
|
|
10
|
+
all_authorized: true,
|
|
11
|
+
ungettable: true,
|
|
12
|
+
unmodifiable: true,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
id: 'stub_' + randomString(24, 24),
|
|
16
|
+
default: randomString(1, 50),
|
|
17
|
+
defaultNumber: randomNumber(12, 263),
|
|
18
|
+
untouchable: true,
|
|
19
|
+
all_authorized: true,
|
|
20
|
+
ungettable: true,
|
|
21
|
+
unmodifiable: true,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
range: {
|
|
25
|
+
count: 2080,
|
|
26
|
+
end: 9,
|
|
27
|
+
has_more: false,
|
|
28
|
+
limit: 2,
|
|
29
|
+
start: 0,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default stub;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IdStub, StubData } from '#test/stub/stub-model.js';
|
|
2
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
3
|
+
|
|
4
|
+
const listModel: StubData[] = [
|
|
5
|
+
{
|
|
6
|
+
id: ('stub_' + randomString(24, 24)) as IdStub,
|
|
7
|
+
default: randomString(1, 50),
|
|
8
|
+
defaultNumber: randomNumber(12, 100),
|
|
9
|
+
untouchable: true,
|
|
10
|
+
allAuthorized: true,
|
|
11
|
+
ungettable: true,
|
|
12
|
+
unmodifiable: true,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
id: ('stub_' + randomString(24, 24)) as IdStub,
|
|
16
|
+
default: randomString(1, 50),
|
|
17
|
+
defaultNumber: randomNumber(12, 100),
|
|
18
|
+
untouchable: true,
|
|
19
|
+
allAuthorized: true,
|
|
20
|
+
ungettable: true,
|
|
21
|
+
unmodifiable: true,
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
export default listModel;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
2
|
+
|
|
3
|
+
const model = {
|
|
4
|
+
default: randomString(1, 50),
|
|
5
|
+
defaultNumber: randomNumber(12, 100),
|
|
6
|
+
untouchable: true,
|
|
7
|
+
allAuthorized: true,
|
|
8
|
+
ungettable: true,
|
|
9
|
+
unmodifiable: true,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default model;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import 'temporal-polyfill/global';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { idAddress } from '#address/model.js';
|
|
6
|
+
import { AddressRepository } from '#address/repository.js';
|
|
7
|
+
import { Configuration } from '#api/configuration.js';
|
|
8
|
+
import { StancerRouteInaccessibleError } from '#error/endpoint/route-inaccessible-error.js';
|
|
9
|
+
import { randomString } from '#test/test-helper.js';
|
|
10
|
+
|
|
11
|
+
const config = Configuration;
|
|
12
|
+
config.setKey({
|
|
13
|
+
privateLiveKey: null,
|
|
14
|
+
privateTestKey: process.env['API_KEY'] ?? null,
|
|
15
|
+
});
|
|
16
|
+
config._host = process.env['API_HOST'] ?? 'api.stancer.com';
|
|
17
|
+
|
|
18
|
+
describe('address functional', () => {
|
|
19
|
+
describe('address creation', () => {
|
|
20
|
+
const city = randomString(1, 50);
|
|
21
|
+
const country = randomString(2);
|
|
22
|
+
const zipCode = randomString(2, 16);
|
|
23
|
+
|
|
24
|
+
it('create an address with aliased value', async () => {
|
|
25
|
+
const repository = new AddressRepository();
|
|
26
|
+
const address = await repository.create({ zipCode, city, country });
|
|
27
|
+
await repository.send(address);
|
|
28
|
+
expect(address.id).toBeTruthy();
|
|
29
|
+
expect(address.id).toContain('addr_');
|
|
30
|
+
expect(address.zipCode).toBe(zipCode);
|
|
31
|
+
expect(address.city).toBe(city);
|
|
32
|
+
expect(address.country).toBe(country);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('create an address', async () => {
|
|
36
|
+
const repository = new AddressRepository();
|
|
37
|
+
const address = await repository.create({ zipCode, city, country });
|
|
38
|
+
const newAddress = await repository.send(address);
|
|
39
|
+
expect(address._data).toStrictEqual(newAddress._data);
|
|
40
|
+
expect(newAddress.id).toBeTruthy();
|
|
41
|
+
expect(newAddress.zipCode).toBe(zipCode);
|
|
42
|
+
expect(newAddress._data.zipCode).toBe(newAddress.zipCode);
|
|
43
|
+
expect(newAddress.city).toBe(city);
|
|
44
|
+
expect(newAddress.country).toBe(country);
|
|
45
|
+
});
|
|
46
|
+
it('create a complete address', async () => {
|
|
47
|
+
const repository = new AddressRepository();
|
|
48
|
+
|
|
49
|
+
const zipCode = randomString(2, 16);
|
|
50
|
+
const city = randomString(1, 50);
|
|
51
|
+
const country = randomString(2);
|
|
52
|
+
|
|
53
|
+
const line1 = randomString(1, 50);
|
|
54
|
+
const line2 = randomString(1, 50);
|
|
55
|
+
const line3 = randomString(1, 50);
|
|
56
|
+
const state = randomString(1, 3);
|
|
57
|
+
|
|
58
|
+
const address = await repository.create({ zipCode, city, line1, line2, line3, state, country });
|
|
59
|
+
await repository.send(address);
|
|
60
|
+
expect(address.id).toBeTruthy();
|
|
61
|
+
expect(address.zipCode).toBe(zipCode);
|
|
62
|
+
expect(address._data.zipCode).toBe(address.zipCode);
|
|
63
|
+
expect(address.city).toBe(city);
|
|
64
|
+
expect(address.country).toBe(country);
|
|
65
|
+
expect(address.line1).toBe(line1);
|
|
66
|
+
expect(address.line2).toBe(line2);
|
|
67
|
+
expect(address.line3).toBe(line3);
|
|
68
|
+
expect(address.state).toBe(state);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('need required values', async () => {
|
|
72
|
+
const repository = new AddressRepository();
|
|
73
|
+
const addressWithoutZipCode = await repository.create({ city: randomString(1, 50), country: randomString(2) });
|
|
74
|
+
await expect(async () => await repository.send(addressWithoutZipCode)).rejects.toThrow(
|
|
75
|
+
'zipCode is required to send AddressModel',
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const addressWithoutCity = await repository.create({ zipCode: 'Bloop', country: 'FR' });
|
|
79
|
+
await expect(async () => await repository.send(addressWithoutCity)).rejects.toThrow(
|
|
80
|
+
'city is required to send AddressModel',
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const addressWithoutCountry = await repository.create({ zipCode: 'Bloop', city: randomString(12) });
|
|
84
|
+
await expect(async () => await repository.send(addressWithoutCountry)).rejects.toThrow(
|
|
85
|
+
'country is required to send AddressModel',
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe('patch', () => {
|
|
90
|
+
it('is not available on address', async () => {
|
|
91
|
+
const repository = new AddressRepository();
|
|
92
|
+
|
|
93
|
+
const zipCode = randomString(2, 16);
|
|
94
|
+
const city = randomString(1, 50);
|
|
95
|
+
const country = randomString(2);
|
|
96
|
+
|
|
97
|
+
const address = await repository.create({ zipCode, city, country });
|
|
98
|
+
await repository.send(address);
|
|
99
|
+
address.setLine1(randomString(1, 25));
|
|
100
|
+
await expect(() => repository.send(address)).rejects.toThrow(StancerRouteInaccessibleError);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('get', () => {
|
|
104
|
+
it('get the correct object', async () => {
|
|
105
|
+
const repository = new AddressRepository();
|
|
106
|
+
const id = idAddress('addr_YHvhVplBRGET3V7Ns4B6pzD2');
|
|
107
|
+
const address = await repository.create(id);
|
|
108
|
+
expect(address.id).toBe(id);
|
|
109
|
+
expect(address.zipCode).toBe('75008');
|
|
110
|
+
expect(address.zipCode).toBe(address._data.zipCode);
|
|
111
|
+
expect(address.city).toBe('Paris');
|
|
112
|
+
expect(address.country).toBe('FR');
|
|
113
|
+
expect(address.line1).toBe('10 rue');
|
|
114
|
+
expect(address.line2).toBe('de la ville');
|
|
115
|
+
expect(address.line3).toBe('leveque');
|
|
116
|
+
expect(address.state).toBe('IDF');
|
|
117
|
+
expect(address.createdAt).toBeInstanceOf(Temporal.Instant);
|
|
118
|
+
// we are not checking the exact hour for Timecode reason (not the same in our pipeline and locally)
|
|
119
|
+
expect(address.createdAt?.toLocaleString()).toContain('8/13/2026, ');
|
|
120
|
+
expect(address.createdAt?.toLocaleString()).toContain(':36:22 PM');
|
|
121
|
+
|
|
122
|
+
expect(address._data.createdAt).toStrictEqual(address.createdAt);
|
|
123
|
+
expect(address.deleted).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import 'temporal-polyfill/global';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { Configuration } from '#api/configuration.js';
|
|
6
|
+
import { PreferredNetworkCardEnum } from '#card/enum/network.js';
|
|
7
|
+
import { IdCard } from '#card/model.js';
|
|
8
|
+
import { CardRepository } from '#card/repository.js';
|
|
9
|
+
import { idCustomer } from '#customer/model.ts';
|
|
10
|
+
import { randomCVC, randomExpYear, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
11
|
+
import { getValidCardNumber } from '#test/unit/card/card-helper.js';
|
|
12
|
+
|
|
13
|
+
const config = Configuration;
|
|
14
|
+
|
|
15
|
+
config.setKey({
|
|
16
|
+
privateLiveKey: null,
|
|
17
|
+
privateTestKey: process.env['API_KEY'] ?? null,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
config._host = process.env['API_HOST'] ?? 'api.stancer.com';
|
|
21
|
+
|
|
22
|
+
describe('card functional', () => {
|
|
23
|
+
describe('card creation', () => {
|
|
24
|
+
const expMonth = randomMonth();
|
|
25
|
+
const expYear = randomExpYear();
|
|
26
|
+
const number = getValidCardNumber();
|
|
27
|
+
|
|
28
|
+
it('create an card with minimum information', async () => {
|
|
29
|
+
const repository = new CardRepository();
|
|
30
|
+
|
|
31
|
+
const card = await repository.create({ expMonth, expYear, number });
|
|
32
|
+
await repository.send(card);
|
|
33
|
+
expect(card.id).toBeTruthy();
|
|
34
|
+
expect(card.id).toContain('card_');
|
|
35
|
+
expect(card.last4).toBe(number.slice(number.length - 4));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('create a complete flat card', async () => {
|
|
39
|
+
const repository = new CardRepository();
|
|
40
|
+
const cvc = randomCVC();
|
|
41
|
+
const expMonth = randomMonth();
|
|
42
|
+
const expYear = randomExpYear();
|
|
43
|
+
const externalId = randomString(1, 36);
|
|
44
|
+
const number = getValidCardNumber();
|
|
45
|
+
const last4 = number.slice(number.length - 4);
|
|
46
|
+
const name = randomString(3, 64);
|
|
47
|
+
const preferredNetwork = PreferredNetworkCardEnum[randomNumber(0, PreferredNetworkCardEnum.length - 1)];
|
|
48
|
+
const zipCode = randomString(2, 8);
|
|
49
|
+
|
|
50
|
+
const card = await repository.create({
|
|
51
|
+
cvc,
|
|
52
|
+
expMonth,
|
|
53
|
+
expYear,
|
|
54
|
+
externalId,
|
|
55
|
+
number,
|
|
56
|
+
last4,
|
|
57
|
+
name,
|
|
58
|
+
preferredNetwork,
|
|
59
|
+
zipCode,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await repository.send(card);
|
|
63
|
+
|
|
64
|
+
expect(card.id).toBeTruthy();
|
|
65
|
+
expect(card).toMatchObject({ expMonth, expYear, last4 });
|
|
66
|
+
});
|
|
67
|
+
it('create a complete card with customer', async () => {
|
|
68
|
+
const repository = new CardRepository();
|
|
69
|
+
const customer = idCustomer(`cust_SsySBRtGUq3pG9zbSOhJSKCa`);
|
|
70
|
+
|
|
71
|
+
const expMonth = randomMonth();
|
|
72
|
+
const expYear = randomExpYear();
|
|
73
|
+
const number = getValidCardNumber();
|
|
74
|
+
|
|
75
|
+
const card = await repository.create({ customer, expMonth, expYear, number });
|
|
76
|
+
|
|
77
|
+
await repository.send(card);
|
|
78
|
+
|
|
79
|
+
expect(card.id).toBeTruthy();
|
|
80
|
+
expect(card.expMonth).toBe(expMonth);
|
|
81
|
+
expect(card.expYear).toBe(expYear);
|
|
82
|
+
expect(card.last4).toBe(number.slice(number.length - 4));
|
|
83
|
+
const now = Temporal.Now.plainDateISO();
|
|
84
|
+
// We check that the date had been created today.
|
|
85
|
+
// If not we cannot ensure the card has the customer we sent.
|
|
86
|
+
if (
|
|
87
|
+
card.createdAt?.toZonedDateTimeISO('UTC').year === now.year &&
|
|
88
|
+
card.createdAt?.toZonedDateTimeISO('UTC').month === now.month &&
|
|
89
|
+
card.createdAt?.toZonedDateTimeISO('UTC').day === now.day
|
|
90
|
+
) {
|
|
91
|
+
expect(card.customer).toBe(customer);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('need required expMonth', async () => {
|
|
96
|
+
const expYear = randomExpYear();
|
|
97
|
+
const number = getValidCardNumber();
|
|
98
|
+
|
|
99
|
+
const repository = new CardRepository();
|
|
100
|
+
|
|
101
|
+
const cardWithoutRequiredValue = await repository.create({ expYear, number });
|
|
102
|
+
|
|
103
|
+
await expect(async () => await repository.send(cardWithoutRequiredValue)).rejects.toThrow(
|
|
104
|
+
'expMonth is required to send CardModel',
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
it('need required expYear', async () => {
|
|
108
|
+
const repository = new CardRepository();
|
|
109
|
+
|
|
110
|
+
const cardWithoutRequiredValue = await repository.create({ expMonth, number });
|
|
111
|
+
|
|
112
|
+
await expect(async () => await repository.send(cardWithoutRequiredValue)).rejects.toThrow(
|
|
113
|
+
'expYear is required to send CardModel',
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
it('need required number', async () => {
|
|
117
|
+
const repository = new CardRepository();
|
|
118
|
+
|
|
119
|
+
const cardWithoutRequiredValue = await repository.create({ expMonth, expYear });
|
|
120
|
+
|
|
121
|
+
await expect(async () => await repository.send(cardWithoutRequiredValue)).rejects.toThrow(
|
|
122
|
+
'number is required to send CardModel',
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('patch', () => {
|
|
128
|
+
it('is available on card', async () => {
|
|
129
|
+
const repository = new CardRepository();
|
|
130
|
+
|
|
131
|
+
const expMonth = randomMonth();
|
|
132
|
+
const expYear = randomExpYear();
|
|
133
|
+
const number = getValidCardNumber();
|
|
134
|
+
const card = await repository.create({ expMonth, expYear, number });
|
|
135
|
+
|
|
136
|
+
await repository.send(card);
|
|
137
|
+
const id = card.id;
|
|
138
|
+
expect(id).toBeTruthy();
|
|
139
|
+
expect(card.expMonth).toBe(expMonth);
|
|
140
|
+
expect(card.expYear).toBe(expYear);
|
|
141
|
+
|
|
142
|
+
const newExpYear = randomExpYear() + 1;
|
|
143
|
+
card.setExpYear(newExpYear);
|
|
144
|
+
await repository.send(card);
|
|
145
|
+
expect(id).toBe(card.id);
|
|
146
|
+
expect(card.expYear).toBe(newExpYear);
|
|
147
|
+
expect(card.expMonth).toBe(expMonth);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('get', () => {
|
|
152
|
+
it('get the correct object', async () => {
|
|
153
|
+
const repository = new CardRepository();
|
|
154
|
+
const id = 'card_uqY2HrovY2sPm0Ac2xhnBkfU' as IdCard;
|
|
155
|
+
const card = await repository.create(id);
|
|
156
|
+
expect(card.id).toBe(id);
|
|
157
|
+
expect(card.brand).toBe('visa');
|
|
158
|
+
expect(card.country).toBe('US');
|
|
159
|
+
expect(card.funding).toBe('credit');
|
|
160
|
+
expect(card.last4).toBe('3055');
|
|
161
|
+
expect(card.nature).toBe('personal');
|
|
162
|
+
expect(card.network).toBe('visa');
|
|
163
|
+
expect(card.zipCode).toBe('75001');
|
|
164
|
+
expect(card.createdAt.epochMilliseconds).toBe(1758551022000);
|
|
165
|
+
expect(card.deleted).toBe(false);
|
|
166
|
+
expect(card.expMonth).toBe(12);
|
|
167
|
+
expect(card.expYear).toBe(2099);
|
|
168
|
+
});
|
|
169
|
+
it('get a 404 exception if unknown', async () => {
|
|
170
|
+
const id = 'card_' + randomString(24);
|
|
171
|
+
const repository = new CardRepository();
|
|
172
|
+
await expect(() => repository.create(id)).rejects.toThrow();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
});
|