@infrab4a/connect 4.1.2-beta.1 → 4.1.2-beta.11

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 (38) hide show
  1. package/index.cjs.js +574 -47
  2. package/index.esm.js +563 -48
  3. package/package.json +2 -1
  4. package/src/domain/shopping/enums/index.d.ts +2 -0
  5. package/src/domain/shopping/enums/payment-methods.enum.d.ts +5 -0
  6. package/src/domain/shopping/enums/payment-providers.enum.d.ts +4 -0
  7. package/src/domain/shopping/factories/adyen-payment-method.factory.d.ts +8 -0
  8. package/src/domain/shopping/factories/base-payment-method.factory.d.ts +7 -0
  9. package/src/domain/shopping/factories/glampoints-payment-method.factory.d.ts +8 -0
  10. package/src/domain/shopping/factories/index.d.ts +4 -0
  11. package/src/domain/shopping/factories/pagarme-payment-method.factory.d.ts +10 -0
  12. package/src/domain/shopping/factories/payment-provider.factory.d.ts +14 -0
  13. package/src/domain/shopping/index.d.ts +2 -0
  14. package/src/domain/shopping/interfaces/index.d.ts +6 -0
  15. package/src/domain/shopping/interfaces/payment-method-factory.interface.d.ts +14 -0
  16. package/src/domain/shopping/interfaces/payment-provider-bank-slip.interface.d.ts +5 -0
  17. package/src/domain/shopping/interfaces/payment-provider-card.interface.d.ts +6 -0
  18. package/src/domain/shopping/interfaces/payment-provider-glampoints.interface.d.ts +5 -0
  19. package/src/domain/shopping/interfaces/payment-provider-pix.interface.d.ts +5 -0
  20. package/src/domain/shopping/interfaces/payment-provider.interface.d.ts +6 -0
  21. package/src/domain/shopping/models/index.d.ts +2 -0
  22. package/src/domain/shopping/models/order.d.ts +2 -2
  23. package/src/domain/shopping/models/payment.d.ts +7 -61
  24. package/src/domain/shopping/models/payment.transaction.d.ts +66 -0
  25. package/src/domain/shopping/models/types/index.d.ts +4 -4
  26. package/src/domain/shopping/services/adyen-card-payment.service.d.ts +28 -0
  27. package/src/domain/shopping/services/antifraude.service.d.ts +19 -0
  28. package/src/domain/shopping/services/glampoints-payment.service.d.ts +4 -0
  29. package/src/domain/shopping/services/index.d.ts +6 -0
  30. package/src/domain/shopping/services/pagarme-bank-slip-payment.service.d.ts +8 -0
  31. package/src/domain/shopping/services/pagarme-card-payment.service.d.ts +10 -0
  32. package/src/domain/shopping/services/pagarme-pix-payment.service.d.ts +8 -0
  33. package/src/domain/shopping/types/adyen-credentials.type.d.ts +6 -0
  34. package/src/domain/shopping/types/index.d.ts +5 -0
  35. package/src/domain/shopping/types/pagarme-credentials.type.d.ts +5 -0
  36. package/src/domain/shopping/types/payment-card-info.type.d.ts +4 -0
  37. package/src/domain/shopping/types/payment-method.type.d.ts +2 -0
  38. package/src/domain/shopping/types/payment-provider.type.d.ts +2 -0
package/index.cjs.js CHANGED
@@ -13,6 +13,7 @@ var axios = require('axios');
13
13
  var firestore = require('firebase/firestore');
14
14
  var auth = require('firebase/auth');
15
15
  var gqlQueryBuilder = require('gql-query-builder');
16
+ var moment = require('moment');
16
17
 
17
18
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
19
 
@@ -36,6 +37,34 @@ function _interopNamespace(e) {
36
37
 
37
38
  var tslib_1__namespace = /*#__PURE__*/_interopNamespace(tslib_1);
38
39
  var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
40
+ var moment__namespace = /*#__PURE__*/_interopNamespace(moment);
41
+
42
+ class BasePaymentMethodFactory {
43
+ constructor(methods) {
44
+ this.methods = methods;
45
+ }
46
+ build(method) {
47
+ return this.methods[method];
48
+ }
49
+ }
50
+
51
+ class AdyenPaymentMethodFactory extends BasePaymentMethodFactory {
52
+ }
53
+
54
+ class GlampointsPaymentMethodFactory extends BasePaymentMethodFactory {
55
+ }
56
+
57
+ class PagarmePaymentMethodFactory extends BasePaymentMethodFactory {
58
+ }
59
+
60
+ class PaymentProviderFactory {
61
+ constructor(paymentProviders) {
62
+ this.paymentProviders = paymentProviders;
63
+ }
64
+ build(provider) {
65
+ return this.paymentProviders[provider];
66
+ }
67
+ }
39
68
 
40
69
  class BaseModel {
41
70
  get identifier() {
@@ -498,171 +527,178 @@ class Edition extends BaseModel {
498
527
  }
499
528
  }
500
529
 
501
- class Payment extends BaseModel {
502
- static get identifiersFields() {
503
- return ['id'];
504
- }
530
+ class PaymentTransaction extends BaseModel {
505
531
  }
506
532
  tslib_1.__decorate([
507
533
  classTransformer.Expose({ name: 'refuse_reason' }),
508
534
  tslib_1.__metadata("design:type", String)
509
- ], Payment.prototype, "refuseReason", void 0);
535
+ ], PaymentTransaction.prototype, "refuseReason", void 0);
510
536
  tslib_1.__decorate([
511
537
  classTransformer.Expose({ name: 'status_reason' }),
512
538
  tslib_1.__metadata("design:type", String)
513
- ], Payment.prototype, "statusReason", void 0);
539
+ ], PaymentTransaction.prototype, "statusReason", void 0);
514
540
  tslib_1.__decorate([
515
541
  classTransformer.Expose({ name: 'acquirer_response_code' }),
516
542
  tslib_1.__metadata("design:type", String)
517
- ], Payment.prototype, "acquirerResponseCode", void 0);
543
+ ], PaymentTransaction.prototype, "acquirerResponseCode", void 0);
518
544
  tslib_1.__decorate([
519
545
  classTransformer.Expose({ name: 'acquirer_name' }),
520
546
  tslib_1.__metadata("design:type", String)
521
- ], Payment.prototype, "acquirerName", void 0);
547
+ ], PaymentTransaction.prototype, "acquirerName", void 0);
522
548
  tslib_1.__decorate([
523
549
  classTransformer.Expose({ name: 'acquirer_id' }),
524
550
  tslib_1.__metadata("design:type", String)
525
- ], Payment.prototype, "acquirerId", void 0);
551
+ ], PaymentTransaction.prototype, "acquirerId", void 0);
526
552
  tslib_1.__decorate([
527
553
  classTransformer.Expose({ name: 'authorization_code' }),
528
554
  tslib_1.__metadata("design:type", String)
529
- ], Payment.prototype, "authorizationCode", void 0);
555
+ ], PaymentTransaction.prototype, "authorizationCode", void 0);
530
556
  tslib_1.__decorate([
531
557
  classTransformer.Expose({ name: 'soft_descriptor' }),
532
558
  tslib_1.__metadata("design:type", String)
533
- ], Payment.prototype, "softDescriptor", void 0);
559
+ ], PaymentTransaction.prototype, "softDescriptor", void 0);
534
560
  tslib_1.__decorate([
535
561
  classTransformer.Expose({ name: 'date_created' }),
536
562
  tslib_1.__metadata("design:type", String)
537
- ], Payment.prototype, "dateCreated", void 0);
563
+ ], PaymentTransaction.prototype, "dateCreated", void 0);
538
564
  tslib_1.__decorate([
539
565
  classTransformer.Expose({ name: 'date_updated' }),
540
566
  tslib_1.__metadata("design:type", String)
541
- ], Payment.prototype, "dateUpdated", void 0);
567
+ ], PaymentTransaction.prototype, "dateUpdated", void 0);
542
568
  tslib_1.__decorate([
543
569
  classTransformer.Expose({ name: 'authorized_amount' }),
544
570
  tslib_1.__metadata("design:type", Number)
545
- ], Payment.prototype, "authorizedAmount", void 0);
571
+ ], PaymentTransaction.prototype, "authorizedAmount", void 0);
546
572
  tslib_1.__decorate([
547
573
  classTransformer.Expose({ name: 'paid_amount' }),
548
574
  tslib_1.__metadata("design:type", Number)
549
- ], Payment.prototype, "paidAmount", void 0);
575
+ ], PaymentTransaction.prototype, "paidAmount", void 0);
550
576
  tslib_1.__decorate([
551
577
  classTransformer.Expose({ name: 'refunded_amount' }),
552
578
  tslib_1.__metadata("design:type", Number)
553
- ], Payment.prototype, "refundedAmount", void 0);
579
+ ], PaymentTransaction.prototype, "refundedAmount", void 0);
554
580
  tslib_1.__decorate([
555
581
  classTransformer.Expose({ name: 'card_holder_name' }),
556
582
  tslib_1.__metadata("design:type", String)
557
- ], Payment.prototype, "cardHolderName", void 0);
583
+ ], PaymentTransaction.prototype, "cardHolderName", void 0);
558
584
  tslib_1.__decorate([
559
585
  classTransformer.Expose({ name: 'card_last_digits' }),
560
586
  tslib_1.__metadata("design:type", String)
561
- ], Payment.prototype, "cardLastDigits", void 0);
587
+ ], PaymentTransaction.prototype, "cardLastDigits", void 0);
562
588
  tslib_1.__decorate([
563
589
  classTransformer.Expose({ name: 'card_first_digits' }),
564
590
  tslib_1.__metadata("design:type", String)
565
- ], Payment.prototype, "cardFirstDigits", void 0);
591
+ ], PaymentTransaction.prototype, "cardFirstDigits", void 0);
566
592
  tslib_1.__decorate([
567
593
  classTransformer.Expose({ name: 'card_brand' }),
568
594
  tslib_1.__metadata("design:type", String)
569
- ], Payment.prototype, "cardBrand", void 0);
595
+ ], PaymentTransaction.prototype, "cardBrand", void 0);
570
596
  tslib_1.__decorate([
571
597
  classTransformer.Expose({ name: 'card_pin_mode' }),
572
598
  tslib_1.__metadata("design:type", String)
573
- ], Payment.prototype, "cardPinMode", void 0);
599
+ ], PaymentTransaction.prototype, "cardPinMode", void 0);
574
600
  tslib_1.__decorate([
575
601
  classTransformer.Expose({ name: 'card_magstripe_fallback' }),
576
602
  tslib_1.__metadata("design:type", Boolean)
577
- ], Payment.prototype, "cardMagstripeFallback", void 0);
603
+ ], PaymentTransaction.prototype, "cardMagstripeFallback", void 0);
578
604
  tslib_1.__decorate([
579
605
  classTransformer.Expose({ name: 'cvm_pin' }),
580
606
  tslib_1.__metadata("design:type", Boolean)
581
- ], Payment.prototype, "cvmPin", void 0);
607
+ ], PaymentTransaction.prototype, "cvmPin", void 0);
582
608
  tslib_1.__decorate([
583
609
  classTransformer.Expose({ name: 'postback_url' }),
584
610
  tslib_1.__metadata("design:type", String)
585
- ], Payment.prototype, "postbackUrl", void 0);
611
+ ], PaymentTransaction.prototype, "postbackUrl", void 0);
586
612
  tslib_1.__decorate([
587
613
  classTransformer.Expose({ name: 'payment_method' }),
588
614
  tslib_1.__metadata("design:type", String)
589
- ], Payment.prototype, "paymentMethod", void 0);
615
+ ], PaymentTransaction.prototype, "paymentMethod", void 0);
590
616
  tslib_1.__decorate([
591
617
  classTransformer.Expose({ name: 'capture_method' }),
592
618
  tslib_1.__metadata("design:type", String)
593
- ], Payment.prototype, "captureMethod", void 0);
619
+ ], PaymentTransaction.prototype, "captureMethod", void 0);
594
620
  tslib_1.__decorate([
595
621
  classTransformer.Expose({ name: 'antifraud_score' }),
596
622
  tslib_1.__metadata("design:type", String)
597
- ], Payment.prototype, "antifraudScore", void 0);
623
+ ], PaymentTransaction.prototype, "antifraudScore", void 0);
598
624
  tslib_1.__decorate([
599
625
  classTransformer.Expose({ name: 'boleto_url' }),
600
626
  tslib_1.__metadata("design:type", String)
601
- ], Payment.prototype, "boletoUrl", void 0);
627
+ ], PaymentTransaction.prototype, "boletoUrl", void 0);
602
628
  tslib_1.__decorate([
603
629
  classTransformer.Expose({ name: 'boleto_barcode' }),
604
630
  tslib_1.__metadata("design:type", String)
605
- ], Payment.prototype, "boletoBarcode", void 0);
631
+ ], PaymentTransaction.prototype, "boletoBarcode", void 0);
606
632
  tslib_1.__decorate([
607
633
  classTransformer.Expose({ name: 'boleto_expiration_date' }),
608
634
  tslib_1.__metadata("design:type", String)
609
- ], Payment.prototype, "boletoExpirationDate", void 0);
635
+ ], PaymentTransaction.prototype, "boletoExpirationDate", void 0);
610
636
  tslib_1.__decorate([
611
637
  classTransformer.Expose({ name: 'subscription_id' }),
612
638
  tslib_1.__metadata("design:type", String)
613
- ], Payment.prototype, "subscriptionId", void 0);
639
+ ], PaymentTransaction.prototype, "subscriptionId", void 0);
614
640
  tslib_1.__decorate([
615
641
  classTransformer.Expose({ name: 'split_rules' }),
616
642
  tslib_1.__metadata("design:type", String)
617
- ], Payment.prototype, "splitRules", void 0);
643
+ ], PaymentTransaction.prototype, "splitRules", void 0);
618
644
  tslib_1.__decorate([
619
645
  classTransformer.Expose({ name: 'antifraud_metadata' }),
620
646
  tslib_1.__metadata("design:type", Object)
621
- ], Payment.prototype, "antifraudMetadata", void 0);
647
+ ], PaymentTransaction.prototype, "antifraudMetadata", void 0);
622
648
  tslib_1.__decorate([
623
649
  classTransformer.Expose({ name: 'reference_key' }),
624
650
  tslib_1.__metadata("design:type", String)
625
- ], Payment.prototype, "referenceKey", void 0);
651
+ ], PaymentTransaction.prototype, "referenceKey", void 0);
626
652
  tslib_1.__decorate([
627
653
  classTransformer.Expose({ name: 'local_transaction_id' }),
628
654
  tslib_1.__metadata("design:type", String)
629
- ], Payment.prototype, "localTransactionId", void 0);
655
+ ], PaymentTransaction.prototype, "localTransactionId", void 0);
630
656
  tslib_1.__decorate([
631
657
  classTransformer.Expose({ name: 'local_time' }),
632
658
  tslib_1.__metadata("design:type", String)
633
- ], Payment.prototype, "localTime", void 0);
659
+ ], PaymentTransaction.prototype, "localTime", void 0);
634
660
  tslib_1.__decorate([
635
661
  classTransformer.Expose({ name: 'fraud_covered' }),
636
662
  tslib_1.__metadata("design:type", Boolean)
637
- ], Payment.prototype, "fraudCovered", void 0);
663
+ ], PaymentTransaction.prototype, "fraudCovered", void 0);
638
664
  tslib_1.__decorate([
639
665
  classTransformer.Expose({ name: 'fraud_reimbursed' }),
640
666
  tslib_1.__metadata("design:type", String)
641
- ], Payment.prototype, "fraudReimbursed", void 0);
667
+ ], PaymentTransaction.prototype, "fraudReimbursed", void 0);
642
668
  tslib_1.__decorate([
643
669
  classTransformer.Expose({ name: 'order_id' }),
644
670
  tslib_1.__metadata("design:type", String)
645
- ], Payment.prototype, "orderId", void 0);
671
+ ], PaymentTransaction.prototype, "orderId", void 0);
646
672
  tslib_1.__decorate([
647
673
  classTransformer.Expose({ name: 'risk_level' }),
648
674
  tslib_1.__metadata("design:type", String)
649
- ], Payment.prototype, "riskLevel", void 0);
675
+ ], PaymentTransaction.prototype, "riskLevel", void 0);
650
676
  tslib_1.__decorate([
651
677
  classTransformer.Expose({ name: 'receipt_url' }),
652
678
  tslib_1.__metadata("design:type", String)
653
- ], Payment.prototype, "receiptUrl", void 0);
679
+ ], PaymentTransaction.prototype, "receiptUrl", void 0);
654
680
  tslib_1.__decorate([
655
681
  classTransformer.Expose({ name: 'private_label' }),
656
682
  tslib_1.__metadata("design:type", String)
657
- ], Payment.prototype, "privateLabel", void 0);
683
+ ], PaymentTransaction.prototype, "privateLabel", void 0);
658
684
  tslib_1.__decorate([
659
685
  classTransformer.Expose({ name: 'pix_qr_code' }),
660
686
  tslib_1.__metadata("design:type", String)
661
- ], Payment.prototype, "pixQrCode", void 0);
687
+ ], PaymentTransaction.prototype, "pixQrCode", void 0);
662
688
  tslib_1.__decorate([
663
689
  classTransformer.Expose({ name: 'pix_expiration_date' }),
664
690
  tslib_1.__metadata("design:type", String)
665
- ], Payment.prototype, "pixExpirationDate", void 0);
691
+ ], PaymentTransaction.prototype, "pixExpirationDate", void 0);
692
+
693
+ class Payment extends BaseModel {
694
+ static get identifiersFields() {
695
+ return ['id'];
696
+ }
697
+ }
698
+ tslib_1.__decorate([
699
+ classTransformer.Type(() => PaymentTransaction),
700
+ tslib_1.__metadata("design:type", PaymentTransaction)
701
+ ], Payment.prototype, "transaction", void 0);
666
702
 
667
703
  class SubscriptionPayment extends BaseModel {
668
704
  static get identifiersFields() {
@@ -2228,8 +2264,8 @@ exports.OrderStatus = void 0;
2228
2264
  class Order extends Checkout {
2229
2265
  }
2230
2266
  tslib_1.__decorate([
2231
- classTransformer.Type(() => Payment),
2232
- tslib_1.__metadata("design:type", Payment)
2267
+ classTransformer.Type(() => PaymentTransaction),
2268
+ tslib_1.__metadata("design:type", PaymentTransaction)
2233
2269
  ], Order.prototype, "payment", void 0);
2234
2270
 
2235
2271
  class OrderBlocked extends BaseModel {
@@ -2260,6 +2296,486 @@ tslib_1.__decorate([
2260
2296
  tslib_1.__metadata("design:type", Coupon)
2261
2297
  ], CheckoutSubscription.prototype, "coupon", void 0);
2262
2298
 
2299
+ class AdyenCardService {
2300
+ constructor(credentials) {
2301
+ this.credentials = credentials;
2302
+ }
2303
+ async pay(checkout, card) {
2304
+ try {
2305
+ const result = await axios__default["default"]({
2306
+ method: 'POST',
2307
+ url: this.credentials.URL_TRANSACTION,
2308
+ headers: {
2309
+ 'x-api-key': this.credentials.API_KEY,
2310
+ 'content-type': 'application/json',
2311
+ },
2312
+ data: this.createCardPayment(checkout, card),
2313
+ });
2314
+ if (result.data.resultCode !== 'Authorised') {
2315
+ this.checkoutAntiFraudService.createBlockedOrderOrPayment(checkout, 'Card not authorized', 'Card', 'day', card);
2316
+ return Promise.reject(new Error(`Seu pagamento com cartão não foi autorizado. Para não perder seus produtos, pague com PIX ou outro cartão de crédito`));
2317
+ }
2318
+ const payment = {
2319
+ createdAt: new Date(),
2320
+ updatedAt: new Date(),
2321
+ userId: checkout.user.id,
2322
+ checkoutId: checkout.id,
2323
+ totalPrice: checkout.totalPrice,
2324
+ transaction: Object.assign(Object.assign({}, result.data), { status: 'paid' }),
2325
+ };
2326
+ return payment;
2327
+ }
2328
+ catch (error) { }
2329
+ }
2330
+ createCardPayment(checkout, card) {
2331
+ return {
2332
+ amount: {
2333
+ currency: 'BRL',
2334
+ value: ((checkout === null || checkout === void 0 ? void 0 : checkout.totalPrice) || 0) * 100,
2335
+ },
2336
+ paymentMethod: {
2337
+ type: 'scheme',
2338
+ storedPaymentMethodId: card.cardId,
2339
+ },
2340
+ reference: checkout.id,
2341
+ shopperInteraction: 'Ecommerce',
2342
+ merchantAccount: this.credentials.MERCHANT_ACCOUNT,
2343
+ shopperReference: checkout.user.id,
2344
+ recurringProcessingModel: 'CardOnFile',
2345
+ returnUrl: this.credentials.URL_POSTBACK,
2346
+ installments: {
2347
+ value: card.installments,
2348
+ },
2349
+ };
2350
+ }
2351
+ addCard() {
2352
+ throw new Error('Method not implemented.');
2353
+ }
2354
+ }
2355
+
2356
+ class CheckoutAntiFraudService {
2357
+ constructor(orderRepository, orderBlockedRepository) {
2358
+ this.orderRepository = orderRepository;
2359
+ this.orderBlockedRepository = orderBlockedRepository;
2360
+ this.LIMIT_ORDERS_DAY = 2;
2361
+ this.LIMIT_ORDERS_WEEK = 7;
2362
+ this.LIMIT_BLOCKED_ORDERS_DAY = 5;
2363
+ }
2364
+ async validAntiFraud(checkout, boleto, pix, card) {
2365
+ if (this.couponValidation(checkout))
2366
+ return false;
2367
+ if (pix)
2368
+ return true;
2369
+ if (boleto && !this.verifyBoletoOrder(checkout))
2370
+ return false;
2371
+ if (card && !(await this.verifyBlockedOrderAttempts(checkout, card)))
2372
+ return false;
2373
+ if (card && !(await this.verifyDayAndWeekOrders(checkout, card)))
2374
+ return false;
2375
+ return true;
2376
+ }
2377
+ couponValidation(checkout) {
2378
+ var _a, _b;
2379
+ if (((_a = checkout.coupon) === null || _a === void 0 ? void 0 : _a.nickname) === 'FALHADEPAGAMENTO') {
2380
+ console.error(`Falha de pagamento com cupom. CheckoutId: ${JSON.stringify({
2381
+ checkoutId: checkout.id,
2382
+ user: checkout.user.id,
2383
+ coupon: (_b = checkout.coupon) === null || _b === void 0 ? void 0 : _b.nickname,
2384
+ })}`);
2385
+ return false;
2386
+ }
2387
+ return true;
2388
+ }
2389
+ async verifyDayAndWeekOrders(checkout, card) {
2390
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2391
+ const ordersPerDay = await this.validateOrdersByRange((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.cpf, (_b = checkout.user) === null || _b === void 0 ? void 0 : _b.email, (_c = checkout.user) === null || _c === void 0 ? void 0 : _c.phone, (_d = checkout.shippingAddress) === null || _d === void 0 ? void 0 : _d.zip, card, this.getDateRange('day'));
2392
+ for (const key in ordersPerDay) {
2393
+ if (ordersPerDay[key] > this.LIMIT_ORDERS_DAY) {
2394
+ await this.createBlockedOrderOrPayment(checkout, 'Order limit', key, 'day');
2395
+ return false;
2396
+ }
2397
+ }
2398
+ const ordersPerWeek = await this.validateOrdersByRange((_e = checkout.user) === null || _e === void 0 ? void 0 : _e.cpf, (_f = checkout.user) === null || _f === void 0 ? void 0 : _f.email, (_g = checkout.user) === null || _g === void 0 ? void 0 : _g.phone, (_h = checkout.shippingAddress) === null || _h === void 0 ? void 0 : _h.zip, card, this.getDateRange('week'));
2399
+ for (const key in ordersPerWeek) {
2400
+ if (ordersPerDay[key] > this.LIMIT_ORDERS_WEEK) {
2401
+ await this.createBlockedOrderOrPayment(checkout, 'Order limit', key, 'week');
2402
+ return false;
2403
+ }
2404
+ }
2405
+ return true;
2406
+ }
2407
+ async validateOrdersByRange(cpf, email, phone, zip, card, range) {
2408
+ const ordersCpf = await this.countOrdersByField('user', 'cpf', cpf, range);
2409
+ const ordersByEmail = await this.countOrdersByField('user', 'email', email, range);
2410
+ const ordersByPhone = await this.countOrdersByField('user', 'phone', phone, range);
2411
+ const ordersByZip = await this.countOrdersByField('shippingAddress', 'zip', zip, range);
2412
+ return {
2413
+ cpf: ordersCpf,
2414
+ email: ordersByEmail,
2415
+ phone: ordersByPhone,
2416
+ zip: ordersByZip,
2417
+ };
2418
+ }
2419
+ async countOrdersByField(property, field, value, range) {
2420
+ const filters = {
2421
+ [property]: {
2422
+ [field]: value,
2423
+ },
2424
+ ['createdAt']: [
2425
+ { operator: exports.Where.GTE, value: range.firstDate },
2426
+ { operator: exports.Where.LTE, value: range.lastDate },
2427
+ ],
2428
+ };
2429
+ const docs = await (await this.orderRepository.find({ filters })).count;
2430
+ return docs;
2431
+ }
2432
+ async verifyBoletoOrder(checkout) {
2433
+ var _a;
2434
+ const maxOrderValue = 5000;
2435
+ if (checkout.totalPrice && checkout.totalPrice > maxOrderValue && !((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber)) {
2436
+ await this.createBlockedOrderOrPayment(checkout, 'Boleto not authorized', 'Boleto', 'day');
2437
+ return false;
2438
+ }
2439
+ return true;
2440
+ }
2441
+ async verifyBlockedOrderAttempts(checkout, card) {
2442
+ var _a, _b, _c, _d;
2443
+ const day = `${moment__namespace().format('YYYY-MM-DD')}T00:00:00`;
2444
+ const endOfDay = `${moment__namespace().format('YYYY-MM-DD')}T23:59:59`;
2445
+ const ordersBlockedWithCpf = await this.orderBlockedRepository
2446
+ .find({
2447
+ filters: {
2448
+ customer: { cpf: { operator: exports.Where.EQUALS, value: (_a = checkout.user) === null || _a === void 0 ? void 0 : _a.cpf } },
2449
+ date: [
2450
+ { operator: exports.Where.GTE, value: new Date(day) },
2451
+ { operator: exports.Where.LTE, value: new Date(endOfDay) },
2452
+ ],
2453
+ },
2454
+ })
2455
+ .then((data) => data.data);
2456
+ const ordersBlockedWithEmail = await this.orderBlockedRepository
2457
+ .find({
2458
+ filters: {
2459
+ customer: { email: { operator: exports.Where.EQUALS, value: (_b = checkout.user) === null || _b === void 0 ? void 0 : _b.email } },
2460
+ date: [
2461
+ { operator: exports.Where.GTE, value: new Date(day) },
2462
+ { operator: exports.Where.LTE, value: new Date(endOfDay) },
2463
+ ],
2464
+ },
2465
+ })
2466
+ .then((data) => data.data);
2467
+ const ordersBlockedWithCep = await this.orderBlockedRepository
2468
+ .find({
2469
+ filters: {
2470
+ customer: { shippingAddress: { zip: { operator: exports.Where.EQUALS, value: (_c = checkout.shippingAddress) === null || _c === void 0 ? void 0 : _c.zip } } },
2471
+ date: [
2472
+ { operator: exports.Where.GTE, value: new Date(day) },
2473
+ { operator: exports.Where.LTE, value: new Date(endOfDay) },
2474
+ ],
2475
+ },
2476
+ })
2477
+ .then((data) => data.data);
2478
+ const ordersBlockedWithPhone = await this.orderBlockedRepository
2479
+ .find({
2480
+ filters: {
2481
+ customer: { phoneNumber: { operator: exports.Where.EQUALS, value: (_d = checkout.user) === null || _d === void 0 ? void 0 : _d.phone } },
2482
+ date: [
2483
+ { operator: exports.Where.GTE, value: new Date(day) },
2484
+ { operator: exports.Where.LTE, value: new Date(endOfDay) },
2485
+ ],
2486
+ },
2487
+ })
2488
+ .then((data) => data.data);
2489
+ const blockedUniqueEmails = ordersBlockedWithEmail.filter((e) => {
2490
+ var _a;
2491
+ return e.customer.cpf !== ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.cpf);
2492
+ });
2493
+ const blockedUniqueCeps = ordersBlockedWithCep.filter((e) => {
2494
+ var _a, _b;
2495
+ return e.customer.cpf !== ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.cpf) && e.customer.email !== ((_b = checkout.user) === null || _b === void 0 ? void 0 : _b.email);
2496
+ });
2497
+ const blockedUniquePhone = ordersBlockedWithPhone.filter((e) => {
2498
+ var _a, _b, _c, _d, _e, _f;
2499
+ return (e.customer.cpf !== ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.cpf) &&
2500
+ e.customer.email !== ((_b = checkout.user) === null || _b === void 0 ? void 0 : _b.email) &&
2501
+ ((_d = (_c = e.customer.shippingAddress) === null || _c === void 0 ? void 0 : _c.zip) === null || _d === void 0 ? void 0 : _d.toString()) !== ((_f = (_e = checkout.shippingAddress) === null || _e === void 0 ? void 0 : _e.zip) === null || _f === void 0 ? void 0 : _f.toString()));
2502
+ });
2503
+ const blockedAttempts = ordersBlockedWithCpf
2504
+ .concat(blockedUniqueEmails)
2505
+ .concat(blockedUniqueCeps)
2506
+ .concat(blockedUniquePhone);
2507
+ if (blockedAttempts.length >= this.LIMIT_BLOCKED_ORDERS_DAY) {
2508
+ await this.createBlockedOrderOrPayment(checkout, 'More than 5 attempts have failed', 'Failed attempts', 'day', card || null);
2509
+ return false;
2510
+ }
2511
+ return true;
2512
+ }
2513
+ async createBlockedOrderOrPayment(checkout, blockType, type, limiteRange, card = null) {
2514
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2515
+ const paymentBlocked = {
2516
+ customer: {
2517
+ name: ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.displayName) || '',
2518
+ cpf: ((_b = checkout.user) === null || _b === void 0 ? void 0 : _b.cpf) || '',
2519
+ id: (_c = checkout.user) === null || _c === void 0 ? void 0 : _c.id,
2520
+ email: ((_d = checkout.user) === null || _d === void 0 ? void 0 : _d.email) || '',
2521
+ phoneNumber: '+55' + ((_e = checkout.user) === null || _e === void 0 ? void 0 : _e.phone),
2522
+ isSubscriber: (_f = checkout.user) === null || _f === void 0 ? void 0 : _f.isSubscriber,
2523
+ subscriptionPlan: ((_g = checkout.user) === null || _g === void 0 ? void 0 : _g.subscriptionPlan) || '',
2524
+ shippingAddress: Object.assign(Object.assign({}, checkout.shippingAddress), { zip: this.formatZip((_h = checkout.shippingAddress) === null || _h === void 0 ? void 0 : _h.zip) }),
2525
+ },
2526
+ blockType,
2527
+ limiteRange,
2528
+ type,
2529
+ card,
2530
+ checkout: {
2531
+ id: checkout.id,
2532
+ shop: checkout.shop,
2533
+ total: checkout.totalPrice,
2534
+ },
2535
+ date: new Date(),
2536
+ };
2537
+ await this.orderBlockedRepository.create(paymentBlocked);
2538
+ }
2539
+ getDateRange(range = 'day') {
2540
+ switch (range) {
2541
+ case 'day':
2542
+ return {
2543
+ firstDate: moment__namespace().startOf('D').valueOf(),
2544
+ lastDate: moment__namespace().endOf('D').valueOf(),
2545
+ };
2546
+ case 'week':
2547
+ return {
2548
+ firstDate: moment__namespace().subtract(7, 'd').startOf('D').valueOf(),
2549
+ lastDate: moment__namespace().endOf('D').valueOf(),
2550
+ };
2551
+ default:
2552
+ return {
2553
+ firstDate: moment__namespace().startOf('D').valueOf(),
2554
+ lastDate: moment__namespace().endOf('D').valueOf(),
2555
+ };
2556
+ }
2557
+ }
2558
+ formatZip(zip) {
2559
+ if (zip.length === 8)
2560
+ return zip.substring(0, 5) + '-' + zip.substring(5, 8);
2561
+ return zip;
2562
+ }
2563
+ }
2564
+
2565
+ class GlampointsPaymentService {
2566
+ pay(checkout) {
2567
+ const payment = {
2568
+ createdAt: new Date(),
2569
+ updatedAt: new Date(),
2570
+ userId: checkout.user.id,
2571
+ checkoutId: checkout.id,
2572
+ totalPrice: checkout.totalPrice,
2573
+ transaction: {
2574
+ amount: 0,
2575
+ acquirerResponseCode: '0000',
2576
+ acquirerName: 'glampoints',
2577
+ authorizedAmount: 0,
2578
+ captureMethod: 'ecommerce',
2579
+ installments: 1,
2580
+ cost: 0,
2581
+ paidAmount: 0,
2582
+ paymentMethod: 'glampoints',
2583
+ referer: 'api_key',
2584
+ refundedAmount: 0,
2585
+ status: 'paid',
2586
+ statusReason: 'acquirer',
2587
+ },
2588
+ };
2589
+ return Promise.resolve(payment);
2590
+ }
2591
+ }
2592
+
2593
+ class PagarmeBankSlipService {
2594
+ constructor(credentials) {
2595
+ this.credentials = credentials;
2596
+ }
2597
+ async pay(checkout) {
2598
+ try {
2599
+ const result = await axios__default["default"]({
2600
+ method: 'POST',
2601
+ url: this.credentials.URL_TRANSACTION,
2602
+ data: this.createBoletoPayment(checkout),
2603
+ });
2604
+ if (result.data.status !== 'processing') {
2605
+ console.log('Erro ao gerar o boleto para o checkout: ');
2606
+ return Promise.reject(new Error(`Erro ao gerar o boleto para o checkout: ${checkout.id}`));
2607
+ }
2608
+ const payment = {
2609
+ createdAt: new Date(),
2610
+ updatedAt: new Date(),
2611
+ userId: checkout.user.id,
2612
+ checkoutId: checkout.id,
2613
+ totalPrice: checkout.totalPrice,
2614
+ transaction: result.data,
2615
+ };
2616
+ return payment;
2617
+ }
2618
+ catch (error) {
2619
+ console.log(error);
2620
+ }
2621
+ }
2622
+ createBoletoPayment(checkout) {
2623
+ return {
2624
+ api_key: this.credentials.API_KEY,
2625
+ amount: Math.floor(checkout.totalPrice * 100),
2626
+ boleto_rules: ['strict_expiration_date'],
2627
+ boleto_instructions: 'Sr. Caixa, NÃO aceitar o pagamento após o vencimento.',
2628
+ boleto_expiration_date: moment__namespace().add(3, 'day').format('YYYY-MM-DD'),
2629
+ payment_method: 'boleto',
2630
+ postback_url: this.credentials.URL_POSTBACK,
2631
+ customer: {
2632
+ external_id: checkout.user.id,
2633
+ type: 'individual',
2634
+ country: 'br',
2635
+ name: checkout.user.displayName,
2636
+ documents: [
2637
+ {
2638
+ type: 'cpf',
2639
+ number: checkout.user.cpf,
2640
+ },
2641
+ ],
2642
+ },
2643
+ };
2644
+ }
2645
+ }
2646
+
2647
+ class PagarmeCardService {
2648
+ constructor(credentials) {
2649
+ this.credentials = credentials;
2650
+ }
2651
+ async pay(checkout, card) {
2652
+ try {
2653
+ const result = await axios__default["default"]({
2654
+ method: 'POST',
2655
+ url: this.credentials.URL_TRANSACTION,
2656
+ data: this.createCardPayment(checkout, card),
2657
+ });
2658
+ if (result.data.status !== 'paid') {
2659
+ this.checkoutAntiFraudService.createBlockedOrderOrPayment(checkout, 'Card not authorized', 'Card', 'day', card);
2660
+ return Promise.reject(new Error(`Seu pagamento com cartão não foi autorizado. Para não perder seus produtos, pague com PIX ou outro cartão de crédito`));
2661
+ }
2662
+ const payment = {
2663
+ createdAt: new Date(),
2664
+ updatedAt: new Date(),
2665
+ userId: checkout.user.id,
2666
+ checkoutId: checkout.id,
2667
+ totalPrice: checkout.totalPrice,
2668
+ transaction: result.data,
2669
+ };
2670
+ return payment;
2671
+ }
2672
+ catch (error) {
2673
+ console.log(error);
2674
+ }
2675
+ }
2676
+ addCard() {
2677
+ throw new Error('Method not implemented.');
2678
+ }
2679
+ createCardPayment(checkout, card) {
2680
+ var _a, _b, _c, _d, _e, _f;
2681
+ return {
2682
+ api_key: this.credentials.API_KEY,
2683
+ amount: Math.floor(checkout.totalPrice * 100),
2684
+ card_id: card.cardId,
2685
+ payment_method: 'credit_card',
2686
+ installments: card.installments,
2687
+ soft_descriptor: checkout.shop,
2688
+ customer: {
2689
+ external_id: checkout.user.id,
2690
+ name: checkout.user.displayName,
2691
+ type: 'individual',
2692
+ country: 'br',
2693
+ email: checkout.user.email,
2694
+ phone_numbers: checkout.user.phone ? ['+55' + checkout.user.phone] : '',
2695
+ documents: [
2696
+ {
2697
+ type: 'cpf',
2698
+ number: checkout.user.cpf,
2699
+ },
2700
+ ],
2701
+ },
2702
+ billing: {
2703
+ name: checkout.user.displayName,
2704
+ address: {
2705
+ country: 'br',
2706
+ state: checkout.billingAddress ? checkout.billingAddress.state : (_a = checkout.shippingAddress) === null || _a === void 0 ? void 0 : _a.state,
2707
+ city: checkout.billingAddress ? checkout.billingAddress.city : (_b = checkout.shippingAddress) === null || _b === void 0 ? void 0 : _b.city,
2708
+ neighborhood: checkout.billingAddress ? checkout.billingAddress.district : (_c = checkout.shippingAddress) === null || _c === void 0 ? void 0 : _c.district,
2709
+ street: checkout.billingAddress ? checkout.billingAddress.street : (_d = checkout.shippingAddress) === null || _d === void 0 ? void 0 : _d.street,
2710
+ street_number: checkout.billingAddress ? checkout.billingAddress.number : (_e = checkout.shippingAddress) === null || _e === void 0 ? void 0 : _e.number,
2711
+ zipcode: checkout.billingAddress
2712
+ ? checkout.billingAddress.zip.replace('-', '')
2713
+ : (_f = checkout.shippingAddress) === null || _f === void 0 ? void 0 : _f.zip.replace('-', ''),
2714
+ },
2715
+ },
2716
+ items: checkout.lineItems.map((item) => {
2717
+ return {
2718
+ id: item.id,
2719
+ title: checkout.user.isSubscriber ? `${item.name} - ASSINANTE` : item.name,
2720
+ unit_price: Math.floor(item.pricePaid * 100),
2721
+ quantity: item.quantity,
2722
+ tangible: true,
2723
+ };
2724
+ }),
2725
+ };
2726
+ }
2727
+ }
2728
+
2729
+ class PagarmePixService {
2730
+ constructor(credentials) {
2731
+ this.credentials = credentials;
2732
+ }
2733
+ async pay(checkout) {
2734
+ try {
2735
+ const result = await axios__default["default"]({
2736
+ method: 'POST',
2737
+ url: this.credentials.URL_TRANSACTION,
2738
+ data: this.createPixPayment(checkout),
2739
+ });
2740
+ const payment = {
2741
+ createdAt: new Date(),
2742
+ updatedAt: new Date(),
2743
+ userId: checkout.user.id,
2744
+ checkoutId: checkout.id,
2745
+ totalPrice: checkout.totalPrice,
2746
+ transaction: result.data,
2747
+ };
2748
+ return payment;
2749
+ }
2750
+ catch (error) {
2751
+ console.log(error);
2752
+ }
2753
+ }
2754
+ createPixPayment(checkout) {
2755
+ return {
2756
+ payment_method: 'pix',
2757
+ amount: Math.floor(checkout.totalPrice * 100),
2758
+ api_key: this.credentials.API_KEY,
2759
+ postback_url: this.credentials.URL_POSTBACK,
2760
+ pix_expiration_date: moment__namespace().add(1, 'day').format('YYYY-MM-DD'),
2761
+ customer: {
2762
+ external_id: checkout.user.id,
2763
+ type: 'individual',
2764
+ country: 'br',
2765
+ name: checkout.user.displayName,
2766
+ email: checkout.user.email.trim(),
2767
+ phone_numbers: checkout.user.phone ? ['+55' + checkout.user.phone] : '',
2768
+ documents: [
2769
+ {
2770
+ type: 'cpf',
2771
+ number: checkout.user.cpf,
2772
+ },
2773
+ ],
2774
+ },
2775
+ };
2776
+ }
2777
+ }
2778
+
2263
2779
  class RoundProductPricesHelper {
2264
2780
  static roundProductPrices(product) {
2265
2781
  product.price.price = Number(product.price.price.toFixed(2));
@@ -2794,7 +3310,7 @@ const withFindFirestore = (MixinBase) => {
2794
3310
  const collection = this.collection(this.buildCollectionPathForFind(find.filters));
2795
3311
  const enableCount = (_b = (_a = find === null || find === void 0 ? void 0 : find.options) === null || _a === void 0 ? void 0 : _a.enableCount) !== null && _b !== void 0 ? _b : true;
2796
3312
  const intercepted = await ((_d = (_c = this.interceptors) === null || _c === void 0 ? void 0 : _c.request) === null || _d === void 0 ? void 0 : _d.call(_c, { find }));
2797
- const { filters, limits, orderBy } = intercepted.find || find;
3313
+ const { filters, limits, orderBy } = (intercepted === null || intercepted === void 0 ? void 0 : intercepted.find) || find;
2798
3314
  const queries = this.makeFirestoreWhere(filters || {});
2799
3315
  const ordination = this.makeFirestoreOrderBy(filters, orderBy);
2800
3316
  const offsets = await this.defineLimits(filters, limits);
@@ -5681,6 +6197,8 @@ Object.defineProperty(exports, 'unset', {
5681
6197
  get: function () { return lodash.unset; }
5682
6198
  });
5683
6199
  exports.Address = Address;
6200
+ exports.AdyenCardService = AdyenCardService;
6201
+ exports.AdyenPaymentMethodFactory = AdyenPaymentMethodFactory;
5684
6202
  exports.Authentication = Authentication;
5685
6203
  exports.AuthenticationFirebaseAuthService = AuthenticationFirebaseAuthService;
5686
6204
  exports.AxiosAdapter = AxiosAdapter;
@@ -5705,6 +6223,7 @@ exports.CategoryFirestoreRepository = CategoryFirestoreRepository;
5705
6223
  exports.CategoryHasuraGraphQL = CategoryHasuraGraphQL;
5706
6224
  exports.CategoryHasuraGraphQLRepository = CategoryHasuraGraphQLRepository;
5707
6225
  exports.Checkout = Checkout;
6226
+ exports.CheckoutAntiFraudService = CheckoutAntiFraudService;
5708
6227
  exports.CheckoutFirestoreRepository = CheckoutFirestoreRepository;
5709
6228
  exports.CheckoutSubscription = CheckoutSubscription;
5710
6229
  exports.CheckoutSubscriptionFirestoreRepository = CheckoutSubscriptionFirestoreRepository;
@@ -5720,6 +6239,8 @@ exports.Filter = Filter;
5720
6239
  exports.FilterHasuraGraphQLRepository = FilterHasuraGraphQLRepository;
5721
6240
  exports.FilterOption = FilterOption;
5722
6241
  exports.FilterOptionHasuraGraphQLRepository = FilterOptionHasuraGraphQLRepository;
6242
+ exports.GlampointsPaymentMethodFactory = GlampointsPaymentMethodFactory;
6243
+ exports.GlampointsPaymentService = GlampointsPaymentService;
5723
6244
  exports.Home = Home;
5724
6245
  exports.HomeFirestoreRepository = HomeFirestoreRepository;
5725
6246
  exports.InvalidArgumentError = InvalidArgumentError;
@@ -5736,8 +6257,14 @@ exports.Order = Order;
5736
6257
  exports.OrderBlocked = OrderBlocked;
5737
6258
  exports.OrderBlockedFirestoreRepository = OrderBlockedFirestoreRepository;
5738
6259
  exports.OrderFirestoreRepository = OrderFirestoreRepository;
6260
+ exports.PagarmeBankSlipService = PagarmeBankSlipService;
6261
+ exports.PagarmeCardService = PagarmeCardService;
6262
+ exports.PagarmePaymentMethodFactory = PagarmePaymentMethodFactory;
6263
+ exports.PagarmePixService = PagarmePixService;
5739
6264
  exports.Payment = Payment;
5740
6265
  exports.PaymentFirestoreRepository = PaymentFirestoreRepository;
6266
+ exports.PaymentProviderFactory = PaymentProviderFactory;
6267
+ exports.PaymentTransaction = PaymentTransaction;
5741
6268
  exports.Product = Product;
5742
6269
  exports.ProductFirestoreRepository = ProductFirestoreRepository;
5743
6270
  exports.ProductHasuraGraphQL = ProductHasuraGraphQL;