@stancer/stancer 1.1.0 → 1.1.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/CHANGELOG.md +4 -1
- package/dist/index.d.ts +60 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/payment-intent/repository.d.ts +1 -1
- package/dist/payment-intent/repository.d.ts.map +1 -1
- package/package.json +33 -24
- package/src/address/api-endpoint.ts +0 -30
- package/src/address/model.ts +0 -79
- package/src/address/repository.ts +0 -18
- package/src/address/validator.ts +0 -34
- package/src/api-endpoint/abstract-api-endpoint.ts +0 -310
- package/src/api-endpoint/configuration.ts +0 -125
- package/src/api-endpoint/http/fetch-http-client.ts +0 -36
- package/src/api-endpoint/http/http.ts +0 -75
- package/src/api-endpoint/http/index.ts +0 -2
- package/src/api-endpoint/version.ts +0 -4
- package/src/card/api-endpoint.ts +0 -39
- package/src/card/enum/funding.ts +0 -11
- package/src/card/enum/nature.ts +0 -7
- package/src/card/enum/network.ts +0 -16
- package/src/card/model.ts +0 -139
- package/src/card/repository.ts +0 -11
- package/src/card/validator.ts +0 -79
- package/src/core/abstract-model.ts +0 -126
- package/src/core/abstract-repository.ts +0 -201
- package/src/core/common/branded-type.ts +0 -15
- package/src/core/common/handler/date-handler.ts +0 -31
- package/src/core/common/handler/enum-handler.ts +0 -18
- package/src/core/common/handler/handler.ts +0 -64
- package/src/core/common/handler/id-handler.ts +0 -27
- package/src/core/common/handler/metadata-handler.ts +0 -19
- package/src/core/common/id-validator.ts +0 -23
- package/src/core/common/mutation-tracked-object.ts +0 -49
- package/src/core/endpoint-interface.ts +0 -43
- package/src/core/error/base-error.ts +0 -1
- package/src/core/error/endpoint/api/api-conflict-error.ts +0 -7
- package/src/core/error/endpoint/api/api-error.ts +0 -10
- package/src/core/error/endpoint/api/api-not-found-error.ts +0 -7
- package/src/core/error/endpoint/api/api-unauthorized-error.ts +0 -7
- package/src/core/error/endpoint/api/api-unknown-error.ts +0 -7
- package/src/core/error/endpoint/api/api-validation-error.ts +0 -13
- package/src/core/error/endpoint/api/index.ts +0 -6
- package/src/core/error/endpoint/route-inaccessible-error.ts +0 -8
- package/src/core/error/unpopulated-error.ts +0 -3
- package/src/core/error/unsendable-error.ts +0 -3
- package/src/core/error/validation-error.ts +0 -3
- package/src/core/validator.ts +0 -129
- package/src/customer/api-endpoint.ts +0 -29
- package/src/customer/model.ts +0 -113
- package/src/customer/repository.ts +0 -14
- package/src/customer/validator.ts +0 -26
- package/src/event/model.ts +0 -42
- package/src/payment/api-endpoint.ts +0 -118
- package/src/payment/enum/currency.ts +0 -14
- package/src/payment/enum/method.ts +0 -2
- package/src/payment/enum/status.ts +0 -21
- package/src/payment/model.ts +0 -378
- package/src/payment/repository.ts +0 -73
- package/src/payment/validator.ts +0 -96
- package/src/payment-intent/api-endpoint.ts +0 -124
- package/src/payment-intent/enum/status.ts +0 -21
- package/src/payment-intent/enum/threeds.ts +0 -2
- package/src/payment-intent/model.ts +0 -271
- package/src/payment-intent/repository.ts +0 -85
- package/src/payment-intent/validator.ts +0 -45
- package/src/payout/model.ts +0 -8
- package/src/refund/api-endpoint.ts +0 -33
- package/src/refund/enum/refund-status.ts +0 -11
- package/src/refund/model.ts +0 -90
- package/src/refund/repository.ts +0 -14
- package/src/refund/validator.ts +0 -44
- package/src/sepa/api-endpoint.ts +0 -26
- package/src/sepa/model.ts +0 -80
- package/src/sepa/repository.ts +0 -11
- package/src/sepa/validator.ts +0 -56
- package/src/webhook/api-endpoint.ts +0 -36
- package/src/webhook/model.ts +0 -140
- package/src/webhook/repository.ts +0 -14
- package/src/webhook/validator.ts +0 -18
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { ApiEndpoint, baseHandler } from '#api/abstract-api-endpoint.js';
|
|
2
|
-
import { Http } from '#api/http/index.js';
|
|
3
|
-
import { RFC9557Handler } from '#core/common/handler/date-handler.js';
|
|
4
|
-
import { enumHandler } from '#core/common/handler/enum-handler.js';
|
|
5
|
-
import { defaultHandler } from '#core/common/handler/handler.js';
|
|
6
|
-
import { IdHandler } from '#core/common/handler/id-handler.js';
|
|
7
|
-
import { metadataHandler } from '#core/common/handler/metadata-handler.js';
|
|
8
|
-
import { PaymentApiEndpoint, methodsAllowedHandler } from '#payment/api-endpoint.js';
|
|
9
|
-
import { CurrencyEnum } from '#payment/enum/currency.js';
|
|
10
|
-
import { type IdPayment, type PaymentAmount, PaymentModel } from '#payment/model.js';
|
|
11
|
-
import { paymentValidator } from '#payment/validator.js';
|
|
12
|
-
import { RefundApiEndpoint } from '#refund/api-endpoint.js';
|
|
13
|
-
import { type RefundAmount, RefundModel } from '#refund/model.js';
|
|
14
|
-
import { refundValidator } from '#refund/validator.js';
|
|
15
|
-
|
|
16
|
-
import { AddressModel, type IdAddress } from '../address/model.js';
|
|
17
|
-
import { CardModel, type IdCard } from '../card/model.js';
|
|
18
|
-
import { CustomerModel, type IdCustomer } from '../customer/model.js';
|
|
19
|
-
import { type IdSepa, SepaModel } from '../sepa/model.js';
|
|
20
|
-
import { PaymentIntentStatusEnum } from './enum/status.js';
|
|
21
|
-
import { ThreeDSEnum } from './enum/threeds.js';
|
|
22
|
-
import { type IdPaymentIntent, type PaymentIntentData, PaymentIntentModel } from './model.js';
|
|
23
|
-
|
|
24
|
-
export interface PaymentIntentEndpoint extends ApiEndpoint<PaymentIntentData> {
|
|
25
|
-
refunds: (id: IdPaymentIntent) => Promise<RefundModel[]>;
|
|
26
|
-
payments: (id: IdPaymentIntent) => Promise<PaymentModel[]>;
|
|
27
|
-
capture: (id: IdPaymentIntent) => Promise<Partial<PaymentIntentData>>;
|
|
28
|
-
refund: (id: IdPaymentIntent, amount: RefundAmount) => Promise<Partial<PaymentIntentData>>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class PaymentIntentApiEndpoint extends ApiEndpoint<PaymentIntentData> {
|
|
32
|
-
public _handler = {
|
|
33
|
-
...baseHandler('created_at'),
|
|
34
|
-
id: IdHandler<IdPaymentIntent>(PaymentIntentModel.regex),
|
|
35
|
-
amount: defaultHandler<PaymentAmount>('number', { postRequired: true }),
|
|
36
|
-
billingAddress: IdHandler<IdAddress>(AddressModel.regex, { postable: true }),
|
|
37
|
-
card: IdHandler<IdCard>(CardModel.regex, { postable: true }),
|
|
38
|
-
capture: defaultHandler<PaymentIntentData['capture']>('boolean', {}),
|
|
39
|
-
currency: enumHandler(CurrencyEnum, { postable: true, postRequired: true }),
|
|
40
|
-
customer: IdHandler<IdCustomer>(CustomerModel.regex, { postable: true, patchable: true }),
|
|
41
|
-
dateBank: RFC9557Handler({ postable: false, patchable: false }),
|
|
42
|
-
dateSettlement: RFC9557Handler({}),
|
|
43
|
-
description: defaultHandler<PaymentIntentData['description']>('string'),
|
|
44
|
-
metadata: metadataHandler(),
|
|
45
|
-
methodsAllowed: methodsAllowedHandler,
|
|
46
|
-
orderId: defaultHandler<PaymentIntentData['orderId']>('string'),
|
|
47
|
-
origin: defaultHandler<PaymentIntentData['origin']>('string', { postable: false, patchable: false }),
|
|
48
|
-
payment: IdHandler<IdPayment>(PaymentModel.regex, { postable: false, patchable: false }),
|
|
49
|
-
returnUrl: defaultHandler<PaymentIntentData['returnUrl']>('string'),
|
|
50
|
-
sepa: IdHandler<IdSepa>(SepaModel.regex, { postable: true }),
|
|
51
|
-
shippingAddress: IdHandler<IdAddress>(AddressModel.regex, { postable: true }),
|
|
52
|
-
status: enumHandler(PaymentIntentStatusEnum, { postable: false, patchable: false }),
|
|
53
|
-
threeds: enumHandler(ThreeDSEnum),
|
|
54
|
-
uniqueId: defaultHandler<PaymentIntentData['uniqueId']>('string'),
|
|
55
|
-
url: defaultHandler<PaymentIntentData['url']>('string'),
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
constructor() {
|
|
59
|
-
super({
|
|
60
|
-
name: 'payment_intents',
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public async capture(id: IdPaymentIntent): Promise<Partial<PaymentIntentData>> {
|
|
65
|
-
return this._httpClient
|
|
66
|
-
.makeRequest(
|
|
67
|
-
Http.createRequest({
|
|
68
|
-
url: `${this._getResourceUri(id)}/capture`,
|
|
69
|
-
method: 'POST',
|
|
70
|
-
authenticated: true,
|
|
71
|
-
}),
|
|
72
|
-
)
|
|
73
|
-
.then(Http.transformResponseToData<Record<string, unknown>>)
|
|
74
|
-
.then(this.deserializeResource);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async payments(id: IdPaymentIntent): Promise<PaymentModel[]> {
|
|
78
|
-
return this._httpClient
|
|
79
|
-
.makeRequest(
|
|
80
|
-
Http.createRequest({
|
|
81
|
-
url: `${this._getResourceUri(id)}/payments`,
|
|
82
|
-
method: 'GET',
|
|
83
|
-
authenticated: true,
|
|
84
|
-
}),
|
|
85
|
-
)
|
|
86
|
-
.then(Http.transformResponseToData<Record<string, unknown>[]>)
|
|
87
|
-
.then((payments) =>
|
|
88
|
-
Object.values(payments).map(
|
|
89
|
-
(data) => new PaymentModel(paymentValidator, new PaymentApiEndpoint().deserializeResource(data)),
|
|
90
|
-
),
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public async refund(id: IdPaymentIntent, amount: RefundAmount): Promise<Partial<PaymentIntentData>> {
|
|
95
|
-
return this._httpClient
|
|
96
|
-
.makeRequest(
|
|
97
|
-
Http.createRequest({
|
|
98
|
-
url: `${this._getResourceUri(id)}/refunds`,
|
|
99
|
-
method: 'POST',
|
|
100
|
-
authenticated: true,
|
|
101
|
-
body: JSON.stringify({ amount }),
|
|
102
|
-
}),
|
|
103
|
-
)
|
|
104
|
-
.then(Http.transformResponseToData<Record<string, unknown>>)
|
|
105
|
-
.then(this.deserializeResource);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public async refunds(id: IdPaymentIntent): Promise<RefundModel[]> {
|
|
109
|
-
return this._httpClient
|
|
110
|
-
.makeRequest(
|
|
111
|
-
Http.createRequest({
|
|
112
|
-
url: `${this._getResourceUri(id)}/refunds`,
|
|
113
|
-
method: 'GET',
|
|
114
|
-
authenticated: true,
|
|
115
|
-
}),
|
|
116
|
-
)
|
|
117
|
-
.then(Http.transformResponseToData<Record<string, unknown>[]>)
|
|
118
|
-
.then((refunds) =>
|
|
119
|
-
Object.values(refunds).map(
|
|
120
|
-
(data) => new RefundModel(refundValidator, new RefundApiEndpoint().deserializeResource(data)),
|
|
121
|
-
),
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const PaymentIntentStatusEnum = [
|
|
2
|
-
'require_payment_method',
|
|
3
|
-
'require_authentication',
|
|
4
|
-
'require_authorization',
|
|
5
|
-
'authorized',
|
|
6
|
-
'processing',
|
|
7
|
-
'captured',
|
|
8
|
-
'canceled',
|
|
9
|
-
'unpaid',
|
|
10
|
-
] as const;
|
|
11
|
-
export type PaymentIntentStatus = (typeof PaymentIntentStatusEnum)[number];
|
|
12
|
-
|
|
13
|
-
export const failedPaymentIntentStatus = [
|
|
14
|
-
'canceled',
|
|
15
|
-
'unpaid',
|
|
16
|
-
];
|
|
17
|
-
export const succeedPaymentIntentStatus = [
|
|
18
|
-
'authorized',
|
|
19
|
-
'processing',
|
|
20
|
-
'captured',
|
|
21
|
-
];
|
|
@@ -1,271 +0,0 @@
|
|
|
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
|
-
import { StancerError } from '#core/error/base-error.js';
|
|
5
|
-
import type { Currency } from '#payment/enum/currency.js';
|
|
6
|
-
import type { Method } from '#payment/enum/method.js';
|
|
7
|
-
import type { IdPayment, PaymentAmount, PaymentModel } from '#payment/model.js';
|
|
8
|
-
import type { RefundModel } from '#refund/model.js';
|
|
9
|
-
|
|
10
|
-
import type { IdAddress } from '../address/model.js';
|
|
11
|
-
import type { IdCard } from '../card/model.js';
|
|
12
|
-
import type { IdCustomer } from '../customer/model.js';
|
|
13
|
-
import type { IdSepa } from '../sepa/model.js';
|
|
14
|
-
import { type PaymentIntentStatus, failedPaymentIntentStatus, succeedPaymentIntentStatus } from './enum/status.js';
|
|
15
|
-
import type { ThreeDS } from './enum/threeds.js';
|
|
16
|
-
|
|
17
|
-
export type IdPaymentIntent = IdValue<`pi_${string}`>;
|
|
18
|
-
export const idPaymentIntent = (id: string) => validateID<IdPaymentIntent>(id, PaymentIntentModel.regex);
|
|
19
|
-
|
|
20
|
-
export interface PaymentIntentData extends ModelData {
|
|
21
|
-
id: IdPaymentIntent;
|
|
22
|
-
amount: PaymentAmount;
|
|
23
|
-
billingAddress: IdAddress;
|
|
24
|
-
card: IdCard;
|
|
25
|
-
capture: true;
|
|
26
|
-
currency: Currency;
|
|
27
|
-
customer: IdCustomer;
|
|
28
|
-
dateSettlement: Temporal.PlainDate;
|
|
29
|
-
description: string;
|
|
30
|
-
payment: IdPayment;
|
|
31
|
-
metadata: object | string;
|
|
32
|
-
methodsAllowed: Method[];
|
|
33
|
-
orderId: string;
|
|
34
|
-
origin: string;
|
|
35
|
-
returnUrl: string;
|
|
36
|
-
sepa: IdSepa;
|
|
37
|
-
shippingAddress: IdAddress;
|
|
38
|
-
status: PaymentIntentStatus;
|
|
39
|
-
threeds: ThreeDS;
|
|
40
|
-
url: string;
|
|
41
|
-
uniqueId: string;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export class PaymentIntentModel extends AbstractModel<PaymentIntentData> {
|
|
45
|
-
static regex = /^pi_[a-zA-Z0-9]{24}$/;
|
|
46
|
-
|
|
47
|
-
private _paymentPopulated = false;
|
|
48
|
-
private _refundPopulated = false;
|
|
49
|
-
private _refundedAmount = 0;
|
|
50
|
-
private _payments: PaymentModel[] = [];
|
|
51
|
-
private _refunds: RefundModel[] = [];
|
|
52
|
-
|
|
53
|
-
//#region Methods
|
|
54
|
-
|
|
55
|
-
public isFailed() {
|
|
56
|
-
return this.status && failedPaymentIntentStatus.includes(this.status);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public isNotFailed() {
|
|
60
|
-
return !this.isFailed();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public isNotSuccess() {
|
|
64
|
-
return !this.isSuccess();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public isPaymentPopulated() {
|
|
68
|
-
return this._paymentPopulated;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public isRefundPopulated() {
|
|
72
|
-
return this._refundPopulated;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public isSuccess() {
|
|
76
|
-
return this.status && succeedPaymentIntentStatus.includes(this.status);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public populatePayments(payments: PaymentModel[]) {
|
|
80
|
-
this._paymentPopulated = true;
|
|
81
|
-
this._payments = payments;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public populateRefunds(refunds: RefundModel[]) {
|
|
85
|
-
this._refundPopulated = true;
|
|
86
|
-
this._refundedAmount = refunds.reduce((prev, curr) => prev + (curr.amount ?? 0), 0);
|
|
87
|
-
this._refunds = refunds;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
//#endregion
|
|
91
|
-
|
|
92
|
-
//#region Getters
|
|
93
|
-
|
|
94
|
-
get amount() {
|
|
95
|
-
return this._data.amount;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
get billingAddress() {
|
|
99
|
-
return this._data.billingAddress;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
get card() {
|
|
103
|
-
return this._data.card;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
get currency() {
|
|
107
|
-
return this._data.currency;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
get customer() {
|
|
111
|
-
return this._data.customer;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
get dateSettlement() {
|
|
115
|
-
return this._data.dateSettlement;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
get description() {
|
|
119
|
-
return this._data.description;
|
|
120
|
-
}
|
|
121
|
-
get metadata() {
|
|
122
|
-
if (typeof this._data.metadata === 'object' && Object.hasOwn(this._data.metadata, 'SDK')) {
|
|
123
|
-
return this._data.metadata ? ['custom-metadata'] : null;
|
|
124
|
-
}
|
|
125
|
-
return this._data.metadata;
|
|
126
|
-
}
|
|
127
|
-
get methodsAllowed() {
|
|
128
|
-
return this._data.methodsAllowed;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
get orderId() {
|
|
132
|
-
return this._data.orderId;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
get origin() {
|
|
136
|
-
return this._data.origin;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
get payments() {
|
|
140
|
-
return this._payments;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
get refundableAmount() {
|
|
144
|
-
if (!this.amount || !this.id) {
|
|
145
|
-
throw new StancerError('Refunds must be populated before accessing refundableAmount.');
|
|
146
|
-
}
|
|
147
|
-
return this.amount - this.refundedAmount;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
get refundedAmount() {
|
|
151
|
-
if (!this.isRefundPopulated()) {
|
|
152
|
-
throw new StancerError('Refunds must be populated before accessing refundedAmount.');
|
|
153
|
-
}
|
|
154
|
-
return this._refundedAmount;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
get refunds() {
|
|
158
|
-
return this._refunds;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
get returnUrl() {
|
|
162
|
-
return this._data.returnUrl;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
get sepa() {
|
|
166
|
-
return this._data.sepa;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
get shippingAddress() {
|
|
170
|
-
return this._data.shippingAddress;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
get status() {
|
|
174
|
-
return this._data.status;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
get threeDS() {
|
|
178
|
-
return this._data.threeds;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
get uniqueId() {
|
|
182
|
-
return this._data.uniqueId;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
get url() {
|
|
186
|
-
return this._data.url;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
//#endregion
|
|
190
|
-
|
|
191
|
-
//#region Setters
|
|
192
|
-
|
|
193
|
-
setAmount(amount: PaymentAmount) {
|
|
194
|
-
this._data.amount = amount;
|
|
195
|
-
return this;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
setBillingAddress(address: IdAddress) {
|
|
199
|
-
this._data.billingAddress = address;
|
|
200
|
-
return this;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
setCard(card: IdCard) {
|
|
204
|
-
this._data.card = card;
|
|
205
|
-
return this;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
setCapture(capture: boolean) {
|
|
209
|
-
if (capture === true) {
|
|
210
|
-
this._data.capture = capture;
|
|
211
|
-
}
|
|
212
|
-
return this;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
setCurrency(currency: Currency) {
|
|
216
|
-
this._data.currency = currency;
|
|
217
|
-
return this;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
setCustomer(customer: IdCustomer) {
|
|
221
|
-
this._data.customer = customer;
|
|
222
|
-
return this;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
setDateSettlement(dateSettlement: Temporal.PlainDate) {
|
|
226
|
-
this._data.dateSettlement = dateSettlement;
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
setDescription(description: string) {
|
|
231
|
-
this._data.description = description;
|
|
232
|
-
return this;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
setMethodsAllowed(methodsAllowed: Method[]) {
|
|
236
|
-
this._data.methodsAllowed = methodsAllowed;
|
|
237
|
-
return this;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
setMetadata(metadata: object | string) {
|
|
241
|
-
this._data.metadata = metadata;
|
|
242
|
-
return this;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
setOrderId(orderId: string) {
|
|
246
|
-
this._data.orderId = orderId;
|
|
247
|
-
return this;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
setReturnUrl(returnUrl: string) {
|
|
251
|
-
this._data.returnUrl = returnUrl;
|
|
252
|
-
return this;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
setSepa(sepa: IdSepa) {
|
|
256
|
-
this._data.sepa = sepa;
|
|
257
|
-
return this;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
setShippingAddress(shippingAddress: IdAddress) {
|
|
261
|
-
this._data.shippingAddress = shippingAddress;
|
|
262
|
-
return this;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
setUniqueId(uniqueId: string) {
|
|
266
|
-
this._data.uniqueId = uniqueId;
|
|
267
|
-
return this;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
//#endregion
|
|
271
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { AbstractRepository, type Repository } from '#core/abstract-repository.js';
|
|
2
|
-
import { StancerValidationError } from '#core/error/validation-error.js';
|
|
3
|
-
import { type RefundAmount, refundAmount } from '#refund/model.js';
|
|
4
|
-
|
|
5
|
-
import { PaymentIntentApiEndpoint } from './api-endpoint.js';
|
|
6
|
-
import { type PaymentIntentData, PaymentIntentModel } from './model.js';
|
|
7
|
-
import { PaymentIntentValidator } from './validator.js';
|
|
8
|
-
|
|
9
|
-
export class PaymentIntentRepository
|
|
10
|
-
extends AbstractRepository<PaymentIntentData, PaymentIntentModel>
|
|
11
|
-
implements Repository<PaymentIntentData, PaymentIntentModel>
|
|
12
|
-
{
|
|
13
|
-
protected override endpoint: PaymentIntentApiEndpoint;
|
|
14
|
-
|
|
15
|
-
constructor(endpoint?: PaymentIntentApiEndpoint) {
|
|
16
|
-
const PIendpoint = endpoint ?? new PaymentIntentApiEndpoint();
|
|
17
|
-
super({
|
|
18
|
-
endpoint: PIendpoint,
|
|
19
|
-
validator: PaymentIntentValidator,
|
|
20
|
-
model: PaymentIntentModel,
|
|
21
|
-
});
|
|
22
|
-
this.endpoint = PIendpoint;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public async capture(paymentIntent: PaymentIntentModel) {
|
|
26
|
-
if (!paymentIntent.id) {
|
|
27
|
-
throw new StancerValidationError('PaymentIntent must be populated before calling capture method');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (paymentIntent.status !== 'authorized') {
|
|
31
|
-
throw new StancerValidationError('PaymentIntent must be authorize to be captured');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
await this.endpoint.capture(paymentIntent.id);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async getPayments(paymentIntent: PaymentIntentModel) {
|
|
38
|
-
if (paymentIntent.id === undefined) {
|
|
39
|
-
throw new StancerValidationError('PaymentIntent must be populated before calling getPayments method');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const payments = await this.endpoint.payments(paymentIntent.id);
|
|
43
|
-
paymentIntent.populatePayments(payments);
|
|
44
|
-
return payments;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public async getRefunds(paymentIntent: PaymentIntentModel) {
|
|
48
|
-
if (paymentIntent.id === undefined) {
|
|
49
|
-
throw new StancerValidationError('PaymentIntent must be populated before calling getRefunds method');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const refunds = await this.endpoint.refunds(paymentIntent.id);
|
|
53
|
-
paymentIntent.populateRefunds(refunds);
|
|
54
|
-
return refunds;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public async refund(paymentIntent: PaymentIntentModel, amount?: RefundAmount) {
|
|
58
|
-
if (!paymentIntent.amount || !paymentIntent.id) {
|
|
59
|
-
throw new StancerValidationError('PaymentIntent must be populated before calling refund method');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (paymentIntent.isNotSuccess()) {
|
|
63
|
-
throw new StancerValidationError('PaymentIntent must be successful to be refunded');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (!paymentIntent.isRefundPopulated()) {
|
|
67
|
-
await this.getRefunds(paymentIntent);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const refundingAmount = refundAmount(amount ?? paymentIntent.amount);
|
|
71
|
-
if (refundingAmount > paymentIntent.amount || refundingAmount > paymentIntent.refundableAmount) {
|
|
72
|
-
throw new StancerValidationError(
|
|
73
|
-
`You are trying to refund ${refundingAmount} it's ${refundingAmount - paymentIntent.refundableAmount} more than possible ${paymentIntent.refundableAmount}.`,
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (paymentIntent.status !== 'captured' && Number(refundingAmount) !== Number(paymentIntent.amount)) {
|
|
78
|
-
throw new StancerValidationError(`You cannot partially refund a payment intent that isn't captured`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const newData = await this.endpoint.refund(paymentIntent.id, refundingAmount);
|
|
82
|
-
paymentIntent.dataTrackMutations(newData);
|
|
83
|
-
return paymentIntent;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { IdValidator } from '#core/common/id-validator.js';
|
|
2
|
-
import {
|
|
3
|
-
type Validator,
|
|
4
|
-
createdAtValidator,
|
|
5
|
-
enumValidator,
|
|
6
|
-
goldilocksValidator,
|
|
7
|
-
plainDateValidator,
|
|
8
|
-
} from '#core/validator.js';
|
|
9
|
-
import { CurrencyEnum } from '#payment/enum/currency.js';
|
|
10
|
-
import { type IdPayment, PaymentModel } from '#payment/model.js';
|
|
11
|
-
import { amountValidator, captureValidator, methodsAllowedValidator } from '#payment/validator.js';
|
|
12
|
-
|
|
13
|
-
import { AddressModel, type IdAddress } from '../address/model.js';
|
|
14
|
-
import { CardModel, type IdCard } from '../card/model.js';
|
|
15
|
-
import { CustomerModel, type IdCustomer } from '../customer/model.js';
|
|
16
|
-
import { type IdSepa, SepaModel } from '../sepa/model.js';
|
|
17
|
-
import { PaymentIntentStatusEnum } from './enum/status.js';
|
|
18
|
-
import { ThreeDSEnum } from './enum/threeds.js';
|
|
19
|
-
import { type IdPaymentIntent, type PaymentIntentData, PaymentIntentModel } from './model.js';
|
|
20
|
-
|
|
21
|
-
export const PaymentIntentValidator: Validator<PaymentIntentData> = {
|
|
22
|
-
id: IdValidator<IdPaymentIntent>(PaymentIntentModel.regex),
|
|
23
|
-
amount: amountValidator,
|
|
24
|
-
billingAddress: IdValidator<IdAddress>(AddressModel.regex),
|
|
25
|
-
card: IdValidator<IdCard>(CardModel.regex),
|
|
26
|
-
capture: captureValidator,
|
|
27
|
-
createdAt: createdAtValidator,
|
|
28
|
-
currency: enumValidator(CurrencyEnum),
|
|
29
|
-
customer: IdValidator<IdCustomer>(CustomerModel.regex),
|
|
30
|
-
dateSettlement: plainDateValidator('dateSettlement'),
|
|
31
|
-
deleted: true,
|
|
32
|
-
description: goldilocksValidator('description', { min: 1, max: 64 }),
|
|
33
|
-
metadata: true,
|
|
34
|
-
methodsAllowed: methodsAllowedValidator,
|
|
35
|
-
orderId: goldilocksValidator('orderId', { min: 1, max: 36 }),
|
|
36
|
-
origin: true,
|
|
37
|
-
payment: IdValidator<IdPayment>(PaymentModel.regex),
|
|
38
|
-
returnUrl: goldilocksValidator('returnUrl', { min: 1, max: 2048 }),
|
|
39
|
-
sepa: IdValidator<IdSepa>(SepaModel.regex),
|
|
40
|
-
shippingAddress: IdValidator<IdAddress>(AddressModel.regex),
|
|
41
|
-
status: enumValidator(PaymentIntentStatusEnum, false),
|
|
42
|
-
threeds: enumValidator(ThreeDSEnum, false),
|
|
43
|
-
uniqueId: goldilocksValidator('uniqueId', { min: 1, max: 36 }),
|
|
44
|
-
url: goldilocksValidator('url', { min: 1, max: 2048 }),
|
|
45
|
-
};
|
package/src/payout/model.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { IdValue } from '#core/common/branded-type.js';
|
|
2
|
-
import { validateID } from '#core/common/id-validator.js';
|
|
3
|
-
|
|
4
|
-
export type IdPayout = IdValue<`pout_${string}`>;
|
|
5
|
-
export const IdPayout = (id: string) => validateID<IdPayout>(id, PayoutModel.regex);
|
|
6
|
-
export class PayoutModel {
|
|
7
|
-
static regex = /^pout_[a-zA-Z0-9]{24}$/;
|
|
8
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { ApiEndpoint, baseHandler } from '#api/abstract-api-endpoint.js';
|
|
2
|
-
import { RFC9557Handler } from '#core/common/handler/date-handler.js';
|
|
3
|
-
import { enumHandler } from '#core/common/handler/enum-handler.js';
|
|
4
|
-
import { defaultHandler } from '#core/common/handler/handler.js';
|
|
5
|
-
import { IdHandler } from '#core/common/handler/id-handler.js';
|
|
6
|
-
import { CurrencyEnum } from '#payment/enum/currency.js';
|
|
7
|
-
import { type IdPayment, PaymentModel } from '#payment/model.js';
|
|
8
|
-
|
|
9
|
-
import { type IdPaymentIntent, PaymentIntentModel } from '../payment-intent/model.js';
|
|
10
|
-
import { type IdPayout, PayoutModel } from '../payout/model.js';
|
|
11
|
-
import { RefundStatusEnum } from './enum/refund-status.js';
|
|
12
|
-
import { type IdRefund, type RefundAmount, type RefundData, RefundModel } from './model.js';
|
|
13
|
-
|
|
14
|
-
export class RefundApiEndpoint extends ApiEndpoint<RefundData> {
|
|
15
|
-
public _handler = {
|
|
16
|
-
...baseHandler('created'),
|
|
17
|
-
id: IdHandler<IdRefund>(RefundModel.regex),
|
|
18
|
-
amount: defaultHandler<RefundAmount>('number', { postRequired: true }),
|
|
19
|
-
currency: enumHandler(CurrencyEnum, { postable: false }),
|
|
20
|
-
dateBank: RFC9557Handler({ postable: false, patchable: false }),
|
|
21
|
-
dateRefund: RFC9557Handler({ postable: false, patchable: false }),
|
|
22
|
-
payment: IdHandler<IdPayment>(PaymentModel.regex, { postRequired: true }),
|
|
23
|
-
paymentIntent: IdHandler<IdPaymentIntent>(PaymentIntentModel.regex, { postable: false, patchable: false }),
|
|
24
|
-
payout: IdHandler<IdPayout>(PayoutModel.regex, { postable: false, patchable: false }),
|
|
25
|
-
status: enumHandler(RefundStatusEnum, { postable: false }),
|
|
26
|
-
};
|
|
27
|
-
constructor() {
|
|
28
|
-
super({
|
|
29
|
-
name: 'refunds',
|
|
30
|
-
patchable: false,
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
package/src/refund/model.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
|
|
3
|
-
import { AbstractModel, type ModelType } from '#core/abstract-model.js';
|
|
4
|
-
import type { Branded, IdValue } from '#core/common/branded-type.js';
|
|
5
|
-
import { validateID } from '#core/common/id-validator.js';
|
|
6
|
-
import { StancerValidationError } from '#core/error/validation-error.js';
|
|
7
|
-
import type { Currency } from '#payment/enum/currency.js';
|
|
8
|
-
import type { IdPayment } from '#payment/model.js';
|
|
9
|
-
|
|
10
|
-
import type { IdPaymentIntent } from '../payment-intent/model.js';
|
|
11
|
-
import type { IdPayout } from '../payout/model.js';
|
|
12
|
-
import type { RefundStatus } from './enum/refund-status.js';
|
|
13
|
-
|
|
14
|
-
export type IdRefund = IdValue<`refd_${string}`>;
|
|
15
|
-
export const idRefund = (id: string) => validateID<IdRefund>(id, RefundModel.regex);
|
|
16
|
-
|
|
17
|
-
export type RefundAmount = Branded<number, 'refundAmount'>;
|
|
18
|
-
export const isRefundAmount = (value: number): value is RefundAmount => value >= 0;
|
|
19
|
-
export const refundAmount = (value: number): RefundAmount => {
|
|
20
|
-
if (!isRefundAmount(value)) {
|
|
21
|
-
throw new StancerValidationError(`${value} is not a valid Refund amount`);
|
|
22
|
-
}
|
|
23
|
-
return value;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export interface RefundData extends ModelType {
|
|
27
|
-
id: IdRefund;
|
|
28
|
-
amount: RefundAmount;
|
|
29
|
-
currency: Currency;
|
|
30
|
-
dateBank: Temporal.PlainDate;
|
|
31
|
-
dateRefund: Temporal.PlainDate;
|
|
32
|
-
payment: IdPayment;
|
|
33
|
-
paymentIntent: IdPaymentIntent;
|
|
34
|
-
payout: IdPayout;
|
|
35
|
-
status: RefundStatus;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class RefundModel extends AbstractModel<RefundData> {
|
|
39
|
-
static regex = /^refd_[a-zA-Z0-9]{24}$/;
|
|
40
|
-
|
|
41
|
-
//#region Getters
|
|
42
|
-
|
|
43
|
-
get amount() {
|
|
44
|
-
return this._data.amount;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get currency() {
|
|
48
|
-
return this._data.currency;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get dateBank() {
|
|
52
|
-
return this._data.dateBank;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get dateRefund() {
|
|
56
|
-
return this._data.dateRefund;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
get payment() {
|
|
60
|
-
return this._data.payment;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get paymentIntent() {
|
|
64
|
-
return this._data.paymentIntent;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
get payout() {
|
|
68
|
-
return this._data.payout;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
get status() {
|
|
72
|
-
return this._data.status;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
//#endregion
|
|
76
|
-
|
|
77
|
-
//#region Setters
|
|
78
|
-
|
|
79
|
-
setAmount(value: RefundAmount) {
|
|
80
|
-
this._data.amount = value;
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
setPayment(value: IdPayment) {
|
|
85
|
-
this._data.payment = value;
|
|
86
|
-
return this;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
//#endregion
|
|
90
|
-
}
|