@medusajs/medusa 1.6.0 → 1.6.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.
Files changed (47) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +13 -0
  3. package/dist/commands/migrate.js +17 -13
  4. package/dist/commands/migrate.js.map +1 -1
  5. package/dist/commands/seed.js +15 -11
  6. package/dist/commands/seed.js.map +1 -1
  7. package/dist/commands/utils/get-migrations.js +9 -5
  8. package/dist/commands/utils/get-migrations.js.map +1 -1
  9. package/dist/interfaces/payment-service.d.ts +3 -0
  10. package/dist/interfaces/payment-service.js.map +1 -1
  11. package/dist/loaders/config.d.ts +1 -0
  12. package/dist/loaders/config.js +17 -1
  13. package/dist/loaders/config.js.map +1 -1
  14. package/dist/loaders/feature-flags/analytics.js +1 -1
  15. package/dist/loaders/feature-flags/analytics.js.map +1 -1
  16. package/dist/loaders/index.js +4 -1
  17. package/dist/loaders/index.js.map +1 -1
  18. package/dist/migrations/1664880666982-payment-collection.js +2 -2
  19. package/dist/migrations/1664880666982-payment-collection.js.map +1 -1
  20. package/dist/models/payment-collection.d.ts +1 -0
  21. package/dist/models/payment-collection.js +4 -0
  22. package/dist/models/payment-collection.js.map +1 -1
  23. package/dist/models/payment-session.d.ts +1 -0
  24. package/dist/models/payment-session.js +7 -1
  25. package/dist/models/payment-session.js.map +1 -1
  26. package/dist/models/refund.d.ts +3 -0
  27. package/dist/models/refund.js +21 -1
  28. package/dist/models/refund.js.map +1 -1
  29. package/dist/repositories/payment-collection.d.ts +4 -0
  30. package/dist/repositories/payment-collection.js +108 -0
  31. package/dist/repositories/payment-collection.js.map +1 -1
  32. package/dist/services/draft-order.d.ts +4 -1
  33. package/dist/services/draft-order.js +22 -10
  34. package/dist/services/draft-order.js.map +1 -1
  35. package/dist/services/payment-collection.d.ts +23 -4
  36. package/dist/services/payment-collection.js +602 -7
  37. package/dist/services/payment-collection.js.map +1 -1
  38. package/dist/services/payment-provider.d.ts +6 -0
  39. package/dist/services/payment-provider.js +187 -8
  40. package/dist/services/payment-provider.js.map +1 -1
  41. package/dist/types/payment-collection.d.ts +19 -1
  42. package/dist/types/payment-collection.js.map +1 -1
  43. package/dist/utils/db-aware-column.js +6 -16
  44. package/dist/utils/db-aware-column.js.map +1 -1
  45. package/dist/utils/manual-auto-increment.js +7 -12
  46. package/dist/utils/manual-auto-increment.js.map +1 -1
  47. package/package.json +2 -2
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
29
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
30
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -69,13 +80,15 @@ var interfaces_1 = require("../interfaces");
69
80
  var PaymentCollectionService = /** @class */ (function (_super) {
70
81
  __extends(PaymentCollectionService, _super);
71
82
  function PaymentCollectionService(_a) {
72
- var manager = _a.manager, paymentCollectionRepository = _a.paymentCollectionRepository, eventBusService = _a.eventBusService;
83
+ var manager = _a.manager, paymentCollectionRepository = _a.paymentCollectionRepository, paymentProviderService = _a.paymentProviderService, customerService = _a.customerService, eventBusService = _a.eventBusService;
73
84
  var _this =
74
85
  // eslint-disable-next-line prefer-rest-params
75
86
  _super.call(this, arguments[0]) || this;
76
87
  _this.manager_ = manager;
77
88
  _this.paymentCollectionRepository_ = paymentCollectionRepository;
89
+ _this.paymentProviderService_ = paymentProviderService;
78
90
  _this.eventBusService_ = eventBusService;
91
+ _this.customerService_ = customerService;
79
92
  return _this;
80
93
  }
81
94
  PaymentCollectionService.prototype.retrieve = function (paymentCollectionId, config) {
@@ -89,13 +102,13 @@ var PaymentCollectionService = /** @class */ (function (_super) {
89
102
  manager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
90
103
  paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
91
104
  query = (0, utils_1.buildQuery)({ id: paymentCollectionId }, config);
92
- return [4 /*yield*/, paymentCollectionRepository.findOne(query)];
105
+ return [4 /*yield*/, paymentCollectionRepository.find(query)];
93
106
  case 1:
94
107
  paymentCollection = _b.sent();
95
- if (!paymentCollection) {
108
+ if (!paymentCollection.length) {
96
109
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Payment collection with id ".concat(paymentCollectionId, " was not found"));
97
110
  }
98
- return [2 /*return*/, paymentCollection];
111
+ return [2 /*return*/, paymentCollection[0]];
99
112
  }
100
113
  });
101
114
  });
@@ -105,12 +118,12 @@ var PaymentCollectionService = /** @class */ (function (_super) {
105
118
  var _this = this;
106
119
  return __generator(this, function (_a) {
107
120
  switch (_a.label) {
108
- case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
121
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
109
122
  var paymentCollectionRepository, paymentCollectionToCreate, paymentCollection;
110
123
  return __generator(this, function (_a) {
111
124
  switch (_a.label) {
112
125
  case 0:
113
- paymentCollectionRepository = transactionManager.getCustomRepository(this.paymentCollectionRepository_);
126
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
114
127
  paymentCollectionToCreate = paymentCollectionRepository.create({
115
128
  region_id: data.region_id,
116
129
  type: data.type,
@@ -125,7 +138,7 @@ var PaymentCollectionService = /** @class */ (function (_super) {
125
138
  case 1:
126
139
  paymentCollection = _a.sent();
127
140
  return [4 /*yield*/, this.eventBusService_
128
- .withTransaction(transactionManager)
141
+ .withTransaction(manager)
129
142
  .emit(PaymentCollectionService.Events.CREATED, paymentCollection)];
130
143
  case 2:
131
144
  _a.sent();
@@ -228,10 +241,592 @@ var PaymentCollectionService = /** @class */ (function (_super) {
228
241
  });
229
242
  });
230
243
  };
244
+ PaymentCollectionService.prototype.isValidTotalAmount = function (total, sessionsInput) {
245
+ var sum = sessionsInput.reduce(function (cur, sess) { return cur + sess.amount; }, 0);
246
+ return total === sum;
247
+ };
248
+ PaymentCollectionService.prototype.setPaymentSessions = function (paymentCollectionId, sessions) {
249
+ return __awaiter(this, void 0, void 0, function () {
250
+ var sessionsInput;
251
+ var _this = this;
252
+ return __generator(this, function (_a) {
253
+ switch (_a.label) {
254
+ case 0:
255
+ sessionsInput = Array.isArray(sessions) ? sessions : [sessions];
256
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
257
+ var paymentCollectionRepository, payCol, customer, selectedSessionIds, paymentSessions, _loop_1, this_1, sessionsInput_1, sessionsInput_1_1, session, e_2_1, removeIds;
258
+ var e_2, _a;
259
+ var _b, _c;
260
+ return __generator(this, function (_d) {
261
+ switch (_d.label) {
262
+ case 0:
263
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
264
+ return [4 /*yield*/, this.retrieve(paymentCollectionId, {
265
+ relations: ["region", "region.payment_providers", "payment_sessions"],
266
+ })];
267
+ case 1:
268
+ payCol = _d.sent();
269
+ if (payCol.status !== models_1.PaymentCollectionStatus.NOT_PAID) {
270
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot set payment sessions for a payment collection with status ".concat(payCol.status));
271
+ }
272
+ sessionsInput = sessionsInput.filter(function (session) {
273
+ return !!payCol.region.payment_providers.find(function (_a) {
274
+ var id = _a.id;
275
+ return id === session.provider_id;
276
+ });
277
+ });
278
+ if (!this.isValidTotalAmount(payCol.amount, sessionsInput)) {
279
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "The sum of sessions is not equal to ".concat(payCol.amount, " on Payment Collection"));
280
+ }
281
+ customer = undefined;
282
+ selectedSessionIds = [];
283
+ paymentSessions = [];
284
+ _loop_1 = function (session) {
285
+ var existingSession, inputData, paymentSession, paymentSession;
286
+ return __generator(this, function (_e) {
287
+ switch (_e.label) {
288
+ case 0:
289
+ if (!!customer) return [3 /*break*/, 2];
290
+ return [4 /*yield*/, this_1.customerService_
291
+ .withTransaction(manager)
292
+ .retrieve(session.customer_id, {
293
+ select: ["id", "email", "metadata"],
294
+ })];
295
+ case 1:
296
+ customer = _e.sent();
297
+ _e.label = 2;
298
+ case 2:
299
+ existingSession = (_b = payCol.payment_sessions) === null || _b === void 0 ? void 0 : _b.find(function (sess) { return session.session_id === (sess === null || sess === void 0 ? void 0 : sess.id); });
300
+ inputData = {
301
+ resource_id: payCol.id,
302
+ currency_code: payCol.currency_code,
303
+ amount: session.amount,
304
+ provider_id: session.provider_id,
305
+ customer: customer,
306
+ metadata: {
307
+ resource_id: payCol.id,
308
+ },
309
+ };
310
+ if (!existingSession) return [3 /*break*/, 4];
311
+ return [4 /*yield*/, this_1.paymentProviderService_
312
+ .withTransaction(manager)
313
+ .updateSessionNew(existingSession, inputData)];
314
+ case 3:
315
+ paymentSession = _e.sent();
316
+ selectedSessionIds.push(existingSession.id);
317
+ paymentSessions.push(paymentSession);
318
+ return [3 /*break*/, 6];
319
+ case 4: return [4 /*yield*/, this_1.paymentProviderService_
320
+ .withTransaction(manager)
321
+ .createSessionNew(inputData)];
322
+ case 5:
323
+ paymentSession = _e.sent();
324
+ selectedSessionIds.push(paymentSession.id);
325
+ paymentSessions.push(paymentSession);
326
+ _e.label = 6;
327
+ case 6: return [2 /*return*/];
328
+ }
329
+ });
330
+ };
331
+ this_1 = this;
332
+ _d.label = 2;
333
+ case 2:
334
+ _d.trys.push([2, 7, 8, 9]);
335
+ sessionsInput_1 = __values(sessionsInput), sessionsInput_1_1 = sessionsInput_1.next();
336
+ _d.label = 3;
337
+ case 3:
338
+ if (!!sessionsInput_1_1.done) return [3 /*break*/, 6];
339
+ session = sessionsInput_1_1.value;
340
+ return [5 /*yield**/, _loop_1(session)];
341
+ case 4:
342
+ _d.sent();
343
+ _d.label = 5;
344
+ case 5:
345
+ sessionsInput_1_1 = sessionsInput_1.next();
346
+ return [3 /*break*/, 3];
347
+ case 6: return [3 /*break*/, 9];
348
+ case 7:
349
+ e_2_1 = _d.sent();
350
+ e_2 = { error: e_2_1 };
351
+ return [3 /*break*/, 9];
352
+ case 8:
353
+ try {
354
+ if (sessionsInput_1_1 && !sessionsInput_1_1.done && (_a = sessionsInput_1.return)) _a.call(sessionsInput_1);
355
+ }
356
+ finally { if (e_2) throw e_2.error; }
357
+ return [7 /*endfinally*/];
358
+ case 9:
359
+ if (!((_c = payCol.payment_sessions) === null || _c === void 0 ? void 0 : _c.length)) return [3 /*break*/, 11];
360
+ removeIds = payCol.payment_sessions
361
+ .map(function (sess) { return sess.id; })
362
+ .filter(function (id) { return !selectedSessionIds.includes(id); });
363
+ if (!removeIds.length) return [3 /*break*/, 11];
364
+ return [4 /*yield*/, paymentCollectionRepository.deleteMultiple(removeIds)];
365
+ case 10:
366
+ _d.sent();
367
+ _d.label = 11;
368
+ case 11:
369
+ payCol.payment_sessions = paymentSessions;
370
+ return [4 /*yield*/, paymentCollectionRepository.save(payCol)];
371
+ case 12: return [2 /*return*/, _d.sent()];
372
+ }
373
+ });
374
+ }); })];
375
+ case 1: return [2 /*return*/, _a.sent()];
376
+ }
377
+ });
378
+ });
379
+ };
380
+ PaymentCollectionService.prototype.refreshPaymentSession = function (paymentCollectionId, sessionId, sessionInput) {
381
+ return __awaiter(this, void 0, void 0, function () {
382
+ var _this = this;
383
+ return __generator(this, function (_a) {
384
+ switch (_a.label) {
385
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
386
+ var paymentCollectionRepository, payCol, session, customer, inputData, sessionRefreshed;
387
+ return __generator(this, function (_a) {
388
+ switch (_a.label) {
389
+ case 0:
390
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
391
+ return [4 /*yield*/, paymentCollectionRepository.getPaymentCollectionIdBySessionId(sessionId, {
392
+ relations: [
393
+ "region",
394
+ "region.payment_providers",
395
+ "payment_sessions",
396
+ ],
397
+ })];
398
+ case 1:
399
+ payCol = _a.sent();
400
+ if (paymentCollectionId !== payCol.id) {
401
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Payment Session ".concat(sessionId, " does not belong to Payment Collection ").concat(paymentCollectionId));
402
+ }
403
+ session = payCol.payment_sessions.find(function (sess) { return sessionId === (sess === null || sess === void 0 ? void 0 : sess.id); });
404
+ if ((session === null || session === void 0 ? void 0 : session.amount) !== sessionInput.amount) {
405
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The amount has to be the same as the existing payment session");
406
+ }
407
+ return [4 /*yield*/, this.customerService_
408
+ .withTransaction(manager)
409
+ .retrieve(sessionInput.customer_id, {
410
+ select: ["id", "email", "metadata"],
411
+ })];
412
+ case 2:
413
+ customer = _a.sent();
414
+ inputData = {
415
+ resource_id: payCol.id,
416
+ currency_code: payCol.currency_code,
417
+ amount: session.amount,
418
+ provider_id: session.provider_id,
419
+ customer: customer,
420
+ };
421
+ return [4 /*yield*/, this.paymentProviderService_
422
+ .withTransaction(manager)
423
+ .refreshSessionNew(session, inputData)];
424
+ case 3:
425
+ sessionRefreshed = _a.sent();
426
+ payCol.payment_sessions = payCol.payment_sessions.map(function (sess) {
427
+ if (sess.id === sessionId) {
428
+ return sessionRefreshed;
429
+ }
430
+ return sess;
431
+ });
432
+ if (session.payment_authorized_at) {
433
+ payCol.authorized_amount -= session.amount;
434
+ }
435
+ return [4 /*yield*/, paymentCollectionRepository.save(payCol)];
436
+ case 4:
437
+ _a.sent();
438
+ return [2 /*return*/, sessionRefreshed];
439
+ }
440
+ });
441
+ }); })];
442
+ case 1: return [2 /*return*/, _a.sent()];
443
+ }
444
+ });
445
+ });
446
+ };
447
+ PaymentCollectionService.prototype.authorize = function (paymentCollectionId, context) {
448
+ if (context === void 0) { context = {}; }
449
+ return __awaiter(this, void 0, void 0, function () {
450
+ var _this = this;
451
+ return __generator(this, function (_a) {
452
+ switch (_a.label) {
453
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
454
+ var paymentCollectionRepository, payCol, authorizedAmount, _a, _b, session, auth, inputData, _c, _d, e_3_1, payColCopy;
455
+ var e_3, _e;
456
+ var _f;
457
+ return __generator(this, function (_g) {
458
+ switch (_g.label) {
459
+ case 0:
460
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
461
+ return [4 /*yield*/, this.retrieve(paymentCollectionId, {
462
+ relations: ["payment_sessions", "payments"],
463
+ })];
464
+ case 1:
465
+ payCol = _g.sent();
466
+ if (payCol.authorized_amount === payCol.amount) {
467
+ return [2 /*return*/, payCol];
468
+ }
469
+ if (!(payCol.amount <= 0)) return [3 /*break*/, 3];
470
+ payCol.authorized_amount = 0;
471
+ return [4 /*yield*/, paymentCollectionRepository.save(payCol)];
472
+ case 2: return [2 /*return*/, _g.sent()];
473
+ case 3:
474
+ if (!((_f = payCol.payment_sessions) === null || _f === void 0 ? void 0 : _f.length)) {
475
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "You cannot complete a Payment Collection without a payment session.");
476
+ }
477
+ authorizedAmount = 0;
478
+ _g.label = 4;
479
+ case 4:
480
+ _g.trys.push([4, 10, 11, 12]);
481
+ _a = __values(payCol.payment_sessions), _b = _a.next();
482
+ _g.label = 5;
483
+ case 5:
484
+ if (!!_b.done) return [3 /*break*/, 9];
485
+ session = _b.value;
486
+ if (session.payment_authorized_at) {
487
+ authorizedAmount += session.amount;
488
+ return [3 /*break*/, 8];
489
+ }
490
+ return [4 /*yield*/, this.paymentProviderService_
491
+ .withTransaction(manager)
492
+ .authorizePayment(session, context)];
493
+ case 6:
494
+ auth = _g.sent();
495
+ if (!((auth === null || auth === void 0 ? void 0 : auth.status) === models_1.PaymentSessionStatus.AUTHORIZED)) return [3 /*break*/, 8];
496
+ authorizedAmount += session.amount;
497
+ inputData = {
498
+ amount: session.amount,
499
+ currency_code: payCol.currency_code,
500
+ provider_id: session.provider_id,
501
+ resource_id: payCol.id,
502
+ };
503
+ _d = (_c = payCol.payments).push;
504
+ return [4 /*yield*/, this.paymentProviderService_
505
+ .withTransaction(manager)
506
+ .createPaymentNew(inputData)];
507
+ case 7:
508
+ _d.apply(_c, [_g.sent()]);
509
+ _g.label = 8;
510
+ case 8:
511
+ _b = _a.next();
512
+ return [3 /*break*/, 5];
513
+ case 9: return [3 /*break*/, 12];
514
+ case 10:
515
+ e_3_1 = _g.sent();
516
+ e_3 = { error: e_3_1 };
517
+ return [3 /*break*/, 12];
518
+ case 11:
519
+ try {
520
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
521
+ }
522
+ finally { if (e_3) throw e_3.error; }
523
+ return [7 /*endfinally*/];
524
+ case 12:
525
+ if (authorizedAmount === 0) {
526
+ payCol.status = models_1.PaymentCollectionStatus.AWAITING;
527
+ }
528
+ else if (authorizedAmount < payCol.amount) {
529
+ payCol.status = models_1.PaymentCollectionStatus.PARTIALLY_AUTHORIZED;
530
+ }
531
+ else if (authorizedAmount === payCol.amount) {
532
+ payCol.status = models_1.PaymentCollectionStatus.AUTHORIZED;
533
+ }
534
+ payCol.authorized_amount = authorizedAmount;
535
+ return [4 /*yield*/, paymentCollectionRepository.save(payCol)];
536
+ case 13:
537
+ payColCopy = _g.sent();
538
+ return [4 /*yield*/, this.eventBusService_
539
+ .withTransaction(manager)
540
+ .emit(PaymentCollectionService.Events.PAYMENT_AUTHORIZED, payColCopy)];
541
+ case 14:
542
+ _g.sent();
543
+ return [2 /*return*/, payCol];
544
+ }
545
+ });
546
+ }); })];
547
+ case 1: return [2 /*return*/, _a.sent()];
548
+ }
549
+ });
550
+ });
551
+ };
552
+ PaymentCollectionService.prototype.capturePayment = function (payCol, payment) {
553
+ return __awaiter(this, void 0, void 0, function () {
554
+ var _this = this;
555
+ return __generator(this, function (_a) {
556
+ switch (_a.label) {
557
+ case 0:
558
+ if (payment === null || payment === void 0 ? void 0 : payment.captured_at) {
559
+ return [2 /*return*/, payment];
560
+ }
561
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
562
+ var allowedStatuses, captureError, capturedPayment, paymentCollectionRepository;
563
+ var _a;
564
+ return __generator(this, function (_b) {
565
+ switch (_b.label) {
566
+ case 0:
567
+ allowedStatuses = [
568
+ models_1.PaymentCollectionStatus.AUTHORIZED,
569
+ models_1.PaymentCollectionStatus.PARTIALLY_CAPTURED,
570
+ models_1.PaymentCollectionStatus.REQUIRES_ACTION,
571
+ ];
572
+ if (!allowedStatuses.includes(payCol.status)) {
573
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A Payment Collection with status ".concat(payCol.status, " cannot capture payment"));
574
+ }
575
+ captureError = null;
576
+ return [4 /*yield*/, this.paymentProviderService_
577
+ .withTransaction(manager)
578
+ .capturePayment(payment)
579
+ .catch(function (err) {
580
+ captureError = err;
581
+ })];
582
+ case 1:
583
+ capturedPayment = _b.sent();
584
+ payCol.captured_amount = (_a = payCol.captured_amount) !== null && _a !== void 0 ? _a : 0;
585
+ if (capturedPayment) {
586
+ payCol.captured_amount += payment.amount;
587
+ }
588
+ if (payCol.captured_amount === 0) {
589
+ payCol.status = models_1.PaymentCollectionStatus.REQUIRES_ACTION;
590
+ }
591
+ else if (payCol.captured_amount === payCol.amount) {
592
+ payCol.status = models_1.PaymentCollectionStatus.CAPTURED;
593
+ }
594
+ else {
595
+ payCol.status = models_1.PaymentCollectionStatus.PARTIALLY_CAPTURED;
596
+ }
597
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
598
+ return [4 /*yield*/, paymentCollectionRepository.save(payCol)];
599
+ case 2:
600
+ _b.sent();
601
+ if (!!capturedPayment) return [3 /*break*/, 4];
602
+ return [4 /*yield*/, this.eventBusService_
603
+ .withTransaction(manager)
604
+ .emit(PaymentCollectionService.Events.PAYMENT_CAPTURE_FAILED, __assign(__assign({}, payment), { error: captureError }))];
605
+ case 3:
606
+ _b.sent();
607
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Failed to capture Payment ".concat(payment.id));
608
+ case 4: return [4 /*yield*/, this.eventBusService_
609
+ .withTransaction(manager)
610
+ .emit(PaymentCollectionService.Events.PAYMENT_CAPTURED, capturedPayment)];
611
+ case 5:
612
+ _b.sent();
613
+ return [2 /*return*/, capturedPayment];
614
+ }
615
+ });
616
+ }); })];
617
+ case 1: return [2 /*return*/, _a.sent()];
618
+ }
619
+ });
620
+ });
621
+ };
622
+ PaymentCollectionService.prototype.capture = function (paymentId) {
623
+ var _a;
624
+ return __awaiter(this, void 0, void 0, function () {
625
+ var manager, paymentCollectionRepository, payCol, payment;
626
+ return __generator(this, function (_b) {
627
+ switch (_b.label) {
628
+ case 0:
629
+ manager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
630
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
631
+ return [4 /*yield*/, paymentCollectionRepository.getPaymentCollectionIdByPaymentId(paymentId, {
632
+ relations: ["payments"],
633
+ })];
634
+ case 1:
635
+ payCol = _b.sent();
636
+ payment = payCol.payments.find(function (payment) { return paymentId === (payment === null || payment === void 0 ? void 0 : payment.id); });
637
+ return [4 /*yield*/, this.capturePayment(payCol, payment)];
638
+ case 2: return [2 /*return*/, _b.sent()];
639
+ }
640
+ });
641
+ });
642
+ };
643
+ PaymentCollectionService.prototype.captureAll = function (paymentCollectionId) {
644
+ return __awaiter(this, void 0, void 0, function () {
645
+ var payCol, allPayments, _a, _b, payment, captured, e_4_1;
646
+ var e_4, _c;
647
+ return __generator(this, function (_d) {
648
+ switch (_d.label) {
649
+ case 0: return [4 /*yield*/, this.retrieve(paymentCollectionId, {
650
+ relations: ["payments"],
651
+ })];
652
+ case 1:
653
+ payCol = _d.sent();
654
+ allPayments = [];
655
+ _d.label = 2;
656
+ case 2:
657
+ _d.trys.push([2, 7, 8, 9]);
658
+ _a = __values(payCol.payments), _b = _a.next();
659
+ _d.label = 3;
660
+ case 3:
661
+ if (!!_b.done) return [3 /*break*/, 6];
662
+ payment = _b.value;
663
+ return [4 /*yield*/, this.capturePayment(payCol, payment).catch(function () { return void 0; })];
664
+ case 4:
665
+ captured = _d.sent();
666
+ if (captured) {
667
+ allPayments.push(captured);
668
+ }
669
+ _d.label = 5;
670
+ case 5:
671
+ _b = _a.next();
672
+ return [3 /*break*/, 3];
673
+ case 6: return [3 /*break*/, 9];
674
+ case 7:
675
+ e_4_1 = _d.sent();
676
+ e_4 = { error: e_4_1 };
677
+ return [3 /*break*/, 9];
678
+ case 8:
679
+ try {
680
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
681
+ }
682
+ finally { if (e_4) throw e_4.error; }
683
+ return [7 /*endfinally*/];
684
+ case 9: return [2 /*return*/, allPayments];
685
+ }
686
+ });
687
+ });
688
+ };
689
+ PaymentCollectionService.prototype.refundPayment = function (payCol, payment, amount, reason, note) {
690
+ return __awaiter(this, void 0, void 0, function () {
691
+ var _this = this;
692
+ return __generator(this, function (_a) {
693
+ switch (_a.label) {
694
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
695
+ var refundable, refundError, refund, paymentCollectionRepository;
696
+ var _a;
697
+ return __generator(this, function (_b) {
698
+ switch (_b.label) {
699
+ case 0:
700
+ if (!payment.captured_at) {
701
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Payment ".concat(payment.id, " is not captured"));
702
+ }
703
+ refundable = payment.amount - payment.amount_refunded;
704
+ if (amount > refundable) {
705
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Only ".concat(refundable, " can be refunded from Payment ").concat(payment.id));
706
+ }
707
+ refundError = null;
708
+ return [4 /*yield*/, this.paymentProviderService_
709
+ .withTransaction(manager)
710
+ .refundFromPayment(payment, amount, reason, note)
711
+ .catch(function (err) {
712
+ refundError = err;
713
+ })];
714
+ case 1:
715
+ refund = _b.sent();
716
+ payCol.refunded_amount = (_a = payCol.refunded_amount) !== null && _a !== void 0 ? _a : 0;
717
+ if (refund) {
718
+ payCol.refunded_amount += refund.amount;
719
+ }
720
+ if (payCol.refunded_amount === 0) {
721
+ payCol.status = models_1.PaymentCollectionStatus.REQUIRES_ACTION;
722
+ }
723
+ else if (payCol.refunded_amount === payCol.amount) {
724
+ payCol.status = models_1.PaymentCollectionStatus.REFUNDED;
725
+ }
726
+ else {
727
+ payCol.status = models_1.PaymentCollectionStatus.PARTIALLY_REFUNDED;
728
+ }
729
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
730
+ return [4 /*yield*/, paymentCollectionRepository.save(payCol)];
731
+ case 2:
732
+ _b.sent();
733
+ if (!!refund) return [3 /*break*/, 4];
734
+ return [4 /*yield*/, this.eventBusService_
735
+ .withTransaction(manager)
736
+ .emit(PaymentCollectionService.Events.REFUND_FAILED, __assign(__assign({}, payment), { error: refundError }))];
737
+ case 3:
738
+ _b.sent();
739
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Failed to refund Payment ".concat(payment.id));
740
+ case 4: return [4 /*yield*/, this.eventBusService_
741
+ .withTransaction(manager)
742
+ .emit(PaymentCollectionService.Events.REFUND_CREATED, refund)];
743
+ case 5:
744
+ _b.sent();
745
+ return [2 /*return*/, refund];
746
+ }
747
+ });
748
+ }); })];
749
+ case 1: return [2 /*return*/, _a.sent()];
750
+ }
751
+ });
752
+ });
753
+ };
754
+ PaymentCollectionService.prototype.refund = function (paymentId, amount, reason, note) {
755
+ var _a;
756
+ return __awaiter(this, void 0, void 0, function () {
757
+ var manager, paymentCollectionRepository, payCol, payment;
758
+ return __generator(this, function (_b) {
759
+ switch (_b.label) {
760
+ case 0:
761
+ manager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
762
+ paymentCollectionRepository = manager.getCustomRepository(this.paymentCollectionRepository_);
763
+ return [4 /*yield*/, paymentCollectionRepository.getPaymentCollectionIdByPaymentId(paymentId)];
764
+ case 1:
765
+ payCol = _b.sent();
766
+ return [4 /*yield*/, this.paymentProviderService_.retrievePayment(paymentId)];
767
+ case 2:
768
+ payment = _b.sent();
769
+ return [4 /*yield*/, this.refundPayment(payCol, payment, amount, reason, note)];
770
+ case 3: return [2 /*return*/, _b.sent()];
771
+ }
772
+ });
773
+ });
774
+ };
775
+ PaymentCollectionService.prototype.refundAll = function (paymentCollectionId, reason, note) {
776
+ return __awaiter(this, void 0, void 0, function () {
777
+ var payCol, allRefunds, _a, _b, payment, refunded, e_5_1;
778
+ var e_5, _c;
779
+ return __generator(this, function (_d) {
780
+ switch (_d.label) {
781
+ case 0: return [4 /*yield*/, this.retrieve(paymentCollectionId, {
782
+ relations: ["payments"],
783
+ })];
784
+ case 1:
785
+ payCol = _d.sent();
786
+ allRefunds = [];
787
+ _d.label = 2;
788
+ case 2:
789
+ _d.trys.push([2, 7, 8, 9]);
790
+ _a = __values(payCol.payments), _b = _a.next();
791
+ _d.label = 3;
792
+ case 3:
793
+ if (!!_b.done) return [3 /*break*/, 6];
794
+ payment = _b.value;
795
+ return [4 /*yield*/, this.refundPayment(payCol, payment, payment.amount, reason, note).catch(function () { return void 0; })];
796
+ case 4:
797
+ refunded = _d.sent();
798
+ if (refunded) {
799
+ allRefunds.push(refunded);
800
+ }
801
+ _d.label = 5;
802
+ case 5:
803
+ _b = _a.next();
804
+ return [3 /*break*/, 3];
805
+ case 6: return [3 /*break*/, 9];
806
+ case 7:
807
+ e_5_1 = _d.sent();
808
+ e_5 = { error: e_5_1 };
809
+ return [3 /*break*/, 9];
810
+ case 8:
811
+ try {
812
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
813
+ }
814
+ finally { if (e_5) throw e_5.error; }
815
+ return [7 /*endfinally*/];
816
+ case 9: return [2 /*return*/, allRefunds];
817
+ }
818
+ });
819
+ });
820
+ };
231
821
  PaymentCollectionService.Events = {
232
822
  CREATED: "payment-collection.created",
233
823
  UPDATED: "payment-collection.updated",
234
824
  DELETED: "payment-collection.deleted",
825
+ PAYMENT_AUTHORIZED: "payment-collection.payment_authorized",
826
+ PAYMENT_CAPTURED: "payment-collection.payment_captured",
827
+ PAYMENT_CAPTURE_FAILED: "payment-collection.payment_capture_failed",
828
+ REFUND_CREATED: "payment-collection.payment_refund_created",
829
+ REFUND_FAILED: "payment-collection.payment_refund_failed",
235
830
  };
236
831
  return PaymentCollectionService;
237
832
  }(interfaces_1.TransactionBaseService));