-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCharge.ts
More file actions
964 lines (874 loc) · 27.3 KB
/
Copy pathCharge.ts
File metadata and controls
964 lines (874 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/**
* @fileOverview Methods for Charges
*
* A Charge represents a single attempt to move money into a platform account.
* PaymentIntent confirmation is the preferred way to create Charges; this
* module also supports the legacy Charges API for Stripe API compatibility.
*
* @module Charge
* @see https://docs.stripe.com/api/charges
*/
import { Database } from './Database';
import { EventService } from './EventService';
import { AccountModule } from './Account';
import type { CustomerModule } from './Customer';
import { GenerateId } from '../utils/IdGenerator';
import {
Charge as ChargeType,
ChargeBillingDetails,
EventType,
QueryOperators,
} from '@zoneless/shared-types';
import { ValidateUpdate } from './Util';
import { ExtractChangedFields } from './Event';
import {
CreateChargeSchema,
CreateChargeInput,
UpdateChargeSchema,
UpdateChargeInput,
CaptureChargeSchema,
CaptureChargeInput,
ListChargesFiltersInput,
} from '@zoneless/shared-schemas';
import { ListHelper, ListOptions, ListResult } from '../utils/ListHelper';
import { Now } from '../utils/Timestamp';
import { GetAppConfig } from './AppConfig';
import { AppError } from '../utils/AppError';
import { ERRORS } from '../utils/Errors';
import { GetPlatformAccountId } from './PlatformAccess';
import { BuildChargeReceiptNumber, BuildChargeReceiptUrl } from './Receipt';
type AddressInput = NonNullable<
NonNullable<CreateChargeInput['shipping']>['address']
>;
type ShippingInput = NonNullable<CreateChargeInput['shipping']>;
/**
* Fills in missing address fields with null to satisfy the Charge shipping
* address shape (all fields nullable but required).
*/
function ToChargeAddress(input: AddressInput): {
city: string | null;
country: string | null;
line1: string | null;
line2: string | null;
postal_code: string | null;
state: string | null;
} {
return {
city: input.city ?? null,
country: input.country ?? null,
line1: input.line1 ?? null,
line2: input.line2 ?? null,
postal_code: input.postal_code ?? null,
state: input.state ?? null,
};
}
function ToChargeShipping(
input: ShippingInput
): NonNullable<ChargeType['shipping']> {
return {
address: ToChargeAddress(input.address),
carrier: input.carrier ?? null,
name: input.name,
phone: input.phone ?? null,
tracking_number: input.tracking_number ?? null,
};
}
function EmptyBillingDetails(): ChargeBillingDetails {
return {
address: {
city: null,
country: null,
line1: null,
line2: null,
postal_code: null,
state: null,
},
email: null,
name: null,
phone: null,
tax_id: null,
};
}
function EmptyRefundsList(
chargeId: string
): NonNullable<ChargeType['refunds']> {
return {
object: 'list',
data: [],
has_more: false,
url: `/v1/charges/${chargeId}/refunds`,
};
}
/**
* Internal input for Charges created from a PaymentIntent payment attempt
* (e.g. hosted checkout confirmation). Not part of the public Charges API.
*/
export interface CreateChargeFromPaymentAttemptInput {
amount: number;
currency: string;
payment_intent: string;
payment_method: string | null;
customer?: string | null;
description?: string | null;
metadata?: Record<string, string>;
receipt_email?: string | null;
application_fee_amount?: number | null;
transfer_data?: {
amount: number | null;
destination: string;
} | null;
transfer_group?: string | null;
crypto: {
buyer_address: string | null;
transaction_hash: string | null;
};
outcome: 'succeeded' | 'failed';
failure_code?: string | null;
failure_message?: string | null;
}
export class ChargeModule {
private readonly db: Database;
private readonly eventService: EventService | null;
private readonly listHelper: ListHelper<ChargeType>;
private readonly accountModule: AccountModule;
private readonly customerModule: CustomerModule | null;
constructor(
db: Database,
eventService?: EventService,
customerModule?: CustomerModule
) {
this.db = db;
this.eventService = eventService || null;
this.listHelper = new ListHelper<ChargeType>(db, {
collection: 'Charges',
orderByField: 'created',
orderDirection: 'desc',
urlPath: '/v1/charges',
accountField: 'platform_account',
});
this.accountModule = new AccountModule(db);
this.customerModule = customerModule || null;
}
/**
* Create a new Charge.
* Emits `charge.succeeded` when captured, or `charge.pending` when held.
*
* @param platformAccountId - The platform account ID
* @param input - The input data for the Charge
* @returns The created Charge
*/
async CreateCharge(
platformAccountId: string,
input: CreateChargeInput
): Promise<ChargeType> {
const validatedInput = ValidateUpdate(CreateChargeSchema, input);
this.AssertCreateConstraints(validatedInput);
if (validatedInput.customer && this.customerModule) {
await this.AssertCustomerBelongsToPlatform(
validatedInput.customer,
platformAccountId
);
}
if (validatedInput.on_behalf_of) {
await this.AssertConnectedAccount(
validatedInput.on_behalf_of,
platformAccountId
);
}
if (validatedInput.transfer_data?.destination) {
await this.AssertConnectedAccount(
validatedInput.transfer_data.destination,
platformAccountId
);
}
const charge = this.ChargeObject(platformAccountId, validatedInput);
await this.db.Set('Charges', charge.id, charge);
if (this.eventService) {
const eventType: EventType = charge.captured
? 'charge.succeeded'
: 'charge.pending';
await this.eventService.Emit(eventType, charge.platform_account, charge);
}
return charge;
}
/**
* Create a Charge from a PaymentIntent payment attempt (hosted checkout).
* Emits `charge.succeeded` or `charge.failed` based on `outcome`.
*
* Used when confirming a checkout payment on-chain — Stripe creates the
* Charge as part of PaymentIntent confirmation, not via POST /v1/charges.
*/
async CreateFromPaymentAttempt(
platformAccountId: string,
input: CreateChargeFromPaymentAttemptInput
): Promise<ChargeType> {
const id = GenerateId('ch_z');
const succeeded = input.outcome === 'succeeded';
const charge: ChargeType = {
id,
object: 'charge',
amount: input.amount,
amount_captured: succeeded ? input.amount : 0,
amount_refunded: 0,
application: null,
application_fee: null,
application_fee_amount: input.application_fee_amount ?? null,
balance_transaction: null,
billing_details: EmptyBillingDetails(),
calculated_statement_descriptor: null,
captured: succeeded,
created: Now(),
currency: input.currency,
customer: input.customer ?? null,
description: input.description ?? null,
disputed: false,
failure_balance_transaction: null,
failure_code: succeeded ? null : input.failure_code ?? null,
failure_message: succeeded ? null : input.failure_message ?? null,
fraud_details: {},
livemode: GetAppConfig().livemode,
metadata: input.metadata ?? {},
on_behalf_of: null,
outcome: succeeded
? {
advice_code: null,
network_advice_code: null,
network_decline_code: null,
network_status: 'approved_by_network',
reason: null,
risk_level: 'normal',
risk_score: null,
rule: null,
seller_message: 'Payment complete.',
type: 'authorized',
}
: {
advice_code: null,
network_advice_code: null,
network_decline_code: null,
network_status: 'declined_by_network',
reason: input.failure_code ?? null,
risk_level: null,
risk_score: null,
rule: null,
seller_message: input.failure_message ?? 'Payment failed.',
type: 'issuer_declined',
},
paid: succeeded,
payment_intent: input.payment_intent,
payment_method: input.payment_method,
payment_method_details: {
type: 'crypto',
crypto: {
buyer_address: input.crypto.buyer_address,
fingerprint: null,
network: 'solana',
token_currency: 'usdc',
transaction_hash: input.crypto.transaction_hash,
},
},
presentment_details: null,
radar_options: null,
receipt_email: input.receipt_email ?? null,
receipt_number: BuildChargeReceiptNumber(id),
receipt_url: BuildChargeReceiptUrl(id),
refunded: false,
refunds: EmptyRefundsList(id),
review: null,
shipping: null,
source_transfer: null,
statement_descriptor: null,
statement_descriptor_suffix: null,
status: succeeded ? 'succeeded' : 'failed',
transfer: null,
transfer_data: input.transfer_data ?? null,
transfer_group: input.transfer_group ?? null,
platform_account: platformAccountId,
};
await this.db.Set('Charges', charge.id, charge);
if (this.eventService) {
await this.eventService.Emit(
succeeded ? 'charge.succeeded' : 'charge.failed',
charge.platform_account,
charge
);
}
return charge;
}
/**
* Link a balance transaction to a Charge after ledger recording.
*/
async AttachBalanceTransaction(
id: string,
balanceTransactionId: string
): Promise<ChargeType> {
await this.db.Update<ChargeType>('Charges', id, {
balance_transaction: balanceTransactionId,
});
return this.RequireCharge(id);
}
/**
* Build a Charge object from create input without persisting it.
*/
ChargeObject(
platformAccountId: string,
input: CreateChargeInput
): ChargeType {
const id = GenerateId('ch_z');
const capture = input.capture !== false;
const paymentMethod = input.source ?? null;
const charge: ChargeType = {
id,
object: 'charge',
amount: input.amount,
amount_captured: capture ? input.amount : 0,
amount_refunded: 0,
application: null,
application_fee: null,
application_fee_amount: input.application_fee_amount ?? null,
balance_transaction: null,
billing_details: EmptyBillingDetails(),
calculated_statement_descriptor: input.statement_descriptor ?? null,
captured: capture,
created: Now(),
currency: input.currency,
customer: input.customer ?? null,
description: input.description ?? null,
disputed: false,
failure_balance_transaction: null,
failure_code: null,
failure_message: null,
fraud_details: {},
livemode: GetAppConfig().livemode,
metadata: input.metadata ?? {},
on_behalf_of: input.on_behalf_of ?? null,
outcome: capture
? {
advice_code: null,
network_advice_code: null,
network_decline_code: null,
network_status: 'approved_by_network',
reason: null,
risk_level: 'normal',
risk_score: null,
rule: null,
seller_message: 'Payment complete.',
type: 'authorized',
}
: {
advice_code: null,
network_advice_code: null,
network_decline_code: null,
network_status: 'approved_by_network',
reason: null,
risk_level: 'normal',
risk_score: null,
rule: null,
seller_message: 'Payment authorized.',
type: 'authorized',
},
paid: true,
payment_intent: null,
payment_method: paymentMethod,
payment_method_details: paymentMethod
? {
type: 'crypto',
crypto: {
buyer_address: null,
fingerprint: null,
network: 'solana',
token_currency: 'usdc',
transaction_hash: null,
},
}
: null,
presentment_details: null,
radar_options: input.radar_options
? { session: input.radar_options.session ?? null }
: null,
receipt_email: input.receipt_email ?? null,
receipt_number: BuildChargeReceiptNumber(id),
receipt_url: BuildChargeReceiptUrl(id),
refunded: false,
refunds: EmptyRefundsList(id),
review: null,
shipping: input.shipping ? ToChargeShipping(input.shipping) : null,
source_transfer: null,
statement_descriptor: input.statement_descriptor ?? null,
statement_descriptor_suffix: input.statement_descriptor_suffix ?? null,
status: capture ? 'succeeded' : 'pending',
transfer: null,
transfer_data: input.transfer_data
? {
amount: input.transfer_data.amount ?? null,
destination: input.transfer_data.destination,
}
: null,
transfer_group: input.transfer_group ?? null,
platform_account: platformAccountId,
};
return charge;
}
/**
* Update a Charge.
* Emits a `charge.updated` event if EventService is configured.
*
* @param id - The Charge ID
* @param input - The fields to update
* @returns The updated Charge
*/
async UpdateCharge(
id: string,
input: UpdateChargeInput
): Promise<ChargeType> {
const validatedUpdate = ValidateUpdate(UpdateChargeSchema, input);
const previousCharge = await this.GetCharge(id);
if (!previousCharge) {
throw new AppError(
ERRORS.CHARGE_NOT_FOUND.message,
ERRORS.CHARGE_NOT_FOUND.status,
ERRORS.CHARGE_NOT_FOUND.type
);
}
this.AssertUpdateConstraints(previousCharge, validatedUpdate);
if (validatedUpdate.customer && this.customerModule) {
await this.AssertCustomerBelongsToPlatform(
validatedUpdate.customer,
previousCharge.platform_account
);
}
const updatePayload = this.BuildUpdatePayload(
previousCharge,
validatedUpdate
);
if (Object.keys(updatePayload).length > 0) {
await this.db.Update<ChargeType>('Charges', id, updatePayload);
}
const charge = await this.GetCharge(id);
if (!charge) {
throw new AppError(
ERRORS.CHARGE_NOT_FOUND.message,
ERRORS.CHARGE_NOT_FOUND.status,
ERRORS.CHARGE_NOT_FOUND.type
);
}
if (this.eventService) {
const previousAttributes = ExtractChangedFields(
previousCharge as unknown as Record<string, unknown>,
updatePayload as Record<string, unknown>
);
await this.eventService.Emit(
'charge.updated',
charge.platform_account,
charge,
{ previousAttributes }
);
}
return charge;
}
/**
* Capture an uncaptured Charge.
* Emits `charge.captured` (and `charge.updated` is not emitted separately).
*
* @param id - The Charge ID
* @param input - Optional capture parameters
* @returns The captured Charge
*/
async CaptureCharge(
id: string,
input: CaptureChargeInput = {}
): Promise<ChargeType> {
const validatedInput = ValidateUpdate(CaptureChargeSchema, input);
const previousCharge = await this.RequireCharge(id);
if (previousCharge.captured) {
throw new AppError(
'Charge has already been captured.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
if (previousCharge.status === 'failed') {
throw new AppError(
'Cannot capture a failed charge.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
const captureAmount = validatedInput.amount ?? previousCharge.amount;
if (captureAmount > previousCharge.amount) {
throw new AppError(
'Capture amount cannot exceed the authorized charge amount.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
if (
validatedInput.application_fee_amount !== undefined &&
validatedInput.application_fee_amount > captureAmount
) {
throw new AppError(
'application_fee_amount cannot exceed the capture amount.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
if (
validatedInput.transfer_data?.amount !== undefined &&
validatedInput.transfer_data.amount > captureAmount
) {
throw new AppError(
'transfer_data[amount] cannot exceed the capture amount.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
if (
validatedInput.transfer_group !== undefined &&
previousCharge.transfer_group !== null
) {
throw new AppError(
'transfer_group can only be set if it has not already been set on the Charge.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
const updatePayload: Partial<ChargeType> = {
captured: true,
amount_captured: captureAmount,
paid: true,
status: 'succeeded',
outcome: {
advice_code: null,
network_advice_code: null,
network_decline_code: null,
network_status: 'approved_by_network',
reason: null,
risk_level: previousCharge.outcome?.risk_level ?? 'normal',
risk_score: previousCharge.outcome?.risk_score ?? null,
rule: previousCharge.outcome?.rule ?? null,
seller_message: 'Payment complete.',
type: 'authorized',
},
};
if (validatedInput.application_fee_amount !== undefined) {
updatePayload.application_fee_amount =
validatedInput.application_fee_amount;
}
if (validatedInput.receipt_email !== undefined) {
updatePayload.receipt_email = validatedInput.receipt_email;
}
if (validatedInput.statement_descriptor !== undefined) {
updatePayload.statement_descriptor = validatedInput.statement_descriptor;
updatePayload.calculated_statement_descriptor =
validatedInput.statement_descriptor;
}
if (validatedInput.statement_descriptor_suffix !== undefined) {
updatePayload.statement_descriptor_suffix =
validatedInput.statement_descriptor_suffix;
}
if (validatedInput.transfer_group !== undefined) {
updatePayload.transfer_group = validatedInput.transfer_group;
}
if (
validatedInput.transfer_data?.amount !== undefined &&
previousCharge.transfer_data
) {
updatePayload.transfer_data = {
...previousCharge.transfer_data,
amount: validatedInput.transfer_data.amount,
};
}
await this.db.Update<ChargeType>('Charges', id, updatePayload);
const charge = await this.RequireCharge(id);
if (this.eventService) {
await this.eventService.Emit(
'charge.captured',
charge.platform_account,
charge
);
}
return charge;
}
/**
* Get a Charge by its ID.
*/
async GetCharge(id: string): Promise<ChargeType | null> {
return this.db.Get<ChargeType>('Charges', id);
}
/**
* Retrieve a Charge by ID, throwing if not found.
*/
async RetrieveCharge(id: string): Promise<ChargeType> {
return this.RequireCharge(id);
}
/**
* List Charges with cursor-based pagination.
*/
async ListCharges(
options: ListOptions & ListChargesFiltersInput
): Promise<ListResult<ChargeType>> {
const { customer, payment_intent, transfer_group, ...listOptions } =
options;
const filters: Record<string, unknown> = {};
if (customer !== undefined) filters.customer = customer;
if (payment_intent !== undefined) filters.payment_intent = payment_intent;
if (transfer_group !== undefined) filters.transfer_group = transfer_group;
return this.listHelper.List({
...listOptions,
filters: { ...listOptions.filters, ...filters },
});
}
/**
* Batch-load Charges by id, scoped to a single platform account.
*/
async BatchGet(
ids: string[],
platformAccount: string
): Promise<Map<string, ChargeType>> {
if (ids.length === 0) return new Map();
const charges = await this.db.Query<ChargeType>({
collection: 'Charges',
method: 'READ',
parameters: [
{ key: 'id', operator: QueryOperators['in'], value: ids },
{
key: 'platform_account',
operator: QueryOperators['=='],
value: platformAccount,
},
],
});
return new Map(charges.map((charge) => [charge.id, charge]));
}
/**
* Mark a Charge as failed. Emits `charge.failed`.
*/
async MarkFailed(
id: string,
options: {
failureCode?: string | null;
failureMessage?: string | null;
} = {}
): Promise<ChargeType> {
const previous = await this.RequireCharge(id);
if (previous.status === 'failed') {
return previous;
}
const updatePayload: Partial<ChargeType> = {
status: 'failed',
paid: false,
captured: false,
amount_captured: 0,
failure_code: options.failureCode ?? null,
failure_message: options.failureMessage ?? null,
outcome: {
advice_code: null,
network_advice_code: null,
network_decline_code: null,
network_status: 'declined_by_network',
reason: options.failureCode ?? null,
risk_level: previous.outcome?.risk_level ?? null,
risk_score: previous.outcome?.risk_score ?? null,
rule: previous.outcome?.rule ?? null,
seller_message: options.failureMessage ?? 'Payment failed.',
type: 'issuer_declined',
},
};
await this.db.Update<ChargeType>('Charges', id, updatePayload);
const charge = await this.RequireCharge(id);
if (this.eventService) {
await this.eventService.Emit(
'charge.failed',
charge.platform_account,
charge
);
}
return charge;
}
/**
* Mark an uncaptured Charge as expired. Emits `charge.expired`.
*/
async MarkExpired(id: string): Promise<ChargeType> {
const previous = await this.RequireCharge(id);
if (previous.captured) {
throw new AppError(
'Cannot expire a captured charge.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
const updatePayload: Partial<ChargeType> = {
status: 'failed',
paid: false,
failure_code: 'expired_uncaptured_charge',
failure_message: 'The authorization for this charge has expired.',
};
await this.db.Update<ChargeType>('Charges', id, updatePayload);
const charge = await this.RequireCharge(id);
if (this.eventService) {
await this.eventService.Emit(
'charge.expired',
charge.platform_account,
charge
);
}
return charge;
}
/**
* Mark a Charge as refunded (full or partial). Emits `charge.refunded`.
*/
async MarkRefunded(id: string, amountRefunded: number): Promise<ChargeType> {
const previous = await this.RequireCharge(id);
if (amountRefunded < 0 || amountRefunded > previous.amount) {
throw new AppError(
'Refund amount must be between 0 and the charge amount.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
const updatePayload: Partial<ChargeType> = {
amount_refunded: amountRefunded,
refunded: amountRefunded >= previous.amount_captured,
};
await this.db.Update<ChargeType>('Charges', id, updatePayload);
const charge = await this.RequireCharge(id);
if (this.eventService) {
await this.eventService.Emit(
'charge.refunded',
charge.platform_account,
charge
);
}
return charge;
}
private async RequireCharge(id: string): Promise<ChargeType> {
const charge = await this.GetCharge(id);
if (!charge) {
throw new AppError(
ERRORS.CHARGE_NOT_FOUND.message,
ERRORS.CHARGE_NOT_FOUND.status,
ERRORS.CHARGE_NOT_FOUND.type
);
}
return charge;
}
private BuildUpdatePayload(
previous: ChargeType,
input: UpdateChargeInput
): Partial<ChargeType> {
const { shipping, fraud_details, expand: _expand, ...rest } = input;
const payload: Partial<ChargeType> = { ...rest };
if (shipping !== undefined) {
payload.shipping = ToChargeShipping(shipping);
}
if (fraud_details !== undefined) {
payload.fraud_details = {
...(previous.fraud_details ?? {}),
user_report: fraud_details.user_report,
};
}
return payload;
}
private AssertCreateConstraints(input: CreateChargeInput): void {
if (!input.source && !input.customer) {
throw new AppError(
'Must provide either a source or a customer to create a charge.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
this.AssertSupportedCurrency(input.currency);
this.AssertAmountConstraints(input.amount, input);
}
private AssertUpdateConstraints(
previous: ChargeType,
input: UpdateChargeInput
): void {
if (
input.customer !== undefined &&
previous.customer !== null &&
input.customer !== previous.customer
) {
throw new AppError(
'customer can only be set if there is no existing associated customer with this charge.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
if (
input.transfer_group !== undefined &&
previous.transfer_group !== null
) {
throw new AppError(
'transfer_group can only be set if it has not already been set on the Charge.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
}
private AssertAmountConstraints(
amount: number,
input: {
application_fee_amount?: number | null;
transfer_data?: { amount?: number | null } | null;
}
): void {
if (
input.application_fee_amount !== undefined &&
input.application_fee_amount !== null &&
input.application_fee_amount > amount
) {
throw new AppError(
'application_fee_amount cannot exceed the Charge amount.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
if (
input.transfer_data?.amount !== undefined &&
input.transfer_data.amount !== null &&
input.transfer_data.amount > amount
) {
throw new AppError(
'transfer_data[amount] cannot exceed the Charge amount.',
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
}
private AssertSupportedCurrency(currency: string): void {
if (currency !== 'usdc') {
throw new AppError(
`Currency '${currency}' is not supported. Only 'usdc' is accepted.`,
ERRORS.INVALID_REQUEST.status,
ERRORS.INVALID_REQUEST.type
);
}
}
private async AssertCustomerBelongsToPlatform(
customerId: string,
platformAccountId: string
): Promise<void> {
const customer = await this.customerModule!.GetCustomer(customerId);
if (!customer || customer.platform_account !== platformAccountId) {
throw new AppError(
ERRORS.CUSTOMER_NOT_FOUND.message,
ERRORS.CUSTOMER_NOT_FOUND.status,
ERRORS.CUSTOMER_NOT_FOUND.type
);
}
}
private async AssertConnectedAccount(
accountId: string,
platformAccountId: string
): Promise<void> {
const account = await this.accountModule.GetAccount(accountId);
if (!account || GetPlatformAccountId(account) !== platformAccountId) {
throw new AppError(
`${ERRORS.NO_SUCH_CONNECTED_ACCOUNT.message}: '${accountId}'`,
ERRORS.NO_SUCH_CONNECTED_ACCOUNT.status,
ERRORS.NO_SUCH_CONNECTED_ACCOUNT.type
);
}
}
}