@infrab4a/connect-nestjs 1.9.3 → 1.10.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +303 -0
- package/index.esm.js +298 -2
- package/package.json +4 -3
- package/src/infra/index.d.ts +1 -0
- package/src/infra/pagarme/adapters/index.d.ts +3 -0
- package/src/infra/pagarme/adapters/pagarme-bank-slip-api.adapter.d.ts +9 -0
- package/src/infra/pagarme/adapters/pagarme-card-api.adapter.d.ts +13 -0
- package/src/infra/pagarme/adapters/pagarme-pix-api.adapter.d.ts +8 -0
- package/src/infra/pagarme/index.d.ts +1 -0
package/index.cjs.js
CHANGED
|
@@ -5,10 +5,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var elasticsearch = require('@elastic/elasticsearch');
|
|
6
6
|
var connect = require('@infrab4a/connect');
|
|
7
7
|
var common = require('@nestjs/common');
|
|
8
|
+
var pagarme = require('pagarme');
|
|
8
9
|
var discoveryengine = require('@google-cloud/discoveryengine');
|
|
9
10
|
var v1beta = require('@google-cloud/discoveryengine/build/src/v1beta');
|
|
10
11
|
var nestjsFirebase = require('nestjs-firebase');
|
|
11
12
|
|
|
13
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
|
+
|
|
15
|
+
var pagarme__default = /*#__PURE__*/_interopDefaultLegacy(pagarme);
|
|
16
|
+
|
|
12
17
|
const ES_CONFIG = 'ES_CONFIG';
|
|
13
18
|
|
|
14
19
|
const HASURA_OPTIONS = 'HASURA_OPTIONS';
|
|
@@ -290,6 +295,291 @@ class ConnectFirestoreService {
|
|
|
290
295
|
}
|
|
291
296
|
}
|
|
292
297
|
|
|
298
|
+
class PagarmeBankSlipAPIAdapter {
|
|
299
|
+
constructor(credentials, paymentRepository) {
|
|
300
|
+
this.credentials = credentials;
|
|
301
|
+
this.paymentRepository = paymentRepository;
|
|
302
|
+
}
|
|
303
|
+
async pay(checkout) {
|
|
304
|
+
try {
|
|
305
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
306
|
+
const payload = this.createBoletoPayment(checkout);
|
|
307
|
+
const result = await client.transactions.create(payload);
|
|
308
|
+
console.log('[PAGARME BOLETO DATA TO SEND]', payload);
|
|
309
|
+
console.log('result PagarmeBankSlipAPIAdapter', JSON.stringify(result));
|
|
310
|
+
if (result.status !== connect.PagarmePaymentStatus['Em processamento']) {
|
|
311
|
+
return Promise.reject(new connect.PaymentError(`Houve uma falha ao gerar o boleto. Tente novamente`, {
|
|
312
|
+
checkoutId: checkout.id,
|
|
313
|
+
userEmail: checkout.user.email,
|
|
314
|
+
info: result,
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
const payment = await this.paymentRepository.create({
|
|
318
|
+
createdAt: new Date(),
|
|
319
|
+
updatedAt: new Date(),
|
|
320
|
+
userId: checkout.user.id,
|
|
321
|
+
checkoutId: checkout.id,
|
|
322
|
+
totalPrice: checkout.totalPrice,
|
|
323
|
+
paymentProvider: 'pagarMe',
|
|
324
|
+
transaction: result,
|
|
325
|
+
});
|
|
326
|
+
return payment;
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
console.error('Error PagarmeBankSlipAPIAdapter', JSON.stringify(error));
|
|
330
|
+
throw new connect.PaymentError('Houve uma falha ao gerar o boleto. Tente novamente', {
|
|
331
|
+
checkoutId: checkout.id,
|
|
332
|
+
userEmail: checkout.user.email,
|
|
333
|
+
info: error,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
async getBoletoTransaction(paymentId) {
|
|
338
|
+
try {
|
|
339
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
340
|
+
const result = client.transactions.find({ id: paymentId });
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
throw new connect.BusinessError('Houve uma falha buscar o boleto com paymentId: ' + paymentId, {
|
|
345
|
+
paymentId,
|
|
346
|
+
info: error,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
createBoletoPayment(checkout) {
|
|
351
|
+
return {
|
|
352
|
+
api_key: this.credentials.API_KEY,
|
|
353
|
+
amount: Math.floor(checkout.totalPrice * 100),
|
|
354
|
+
boleto_rules: ['strict_expiration_date'],
|
|
355
|
+
boleto_instructions: 'Sr. Caixa, NÃO aceitar o pagamento após o vencimento.',
|
|
356
|
+
boleto_expiration_date: connect.format(connect.addDays(new Date(), 3), 'yyyy-MM-dd'),
|
|
357
|
+
payment_method: 'boleto',
|
|
358
|
+
postback_url: this.credentials.URL_POSTBACK,
|
|
359
|
+
customer: {
|
|
360
|
+
external_id: checkout.user.id,
|
|
361
|
+
type: 'individual',
|
|
362
|
+
country: 'br',
|
|
363
|
+
name: checkout.user.displayName,
|
|
364
|
+
documents: [
|
|
365
|
+
{
|
|
366
|
+
type: 'cpf',
|
|
367
|
+
number: checkout.user.cpf,
|
|
368
|
+
},
|
|
369
|
+
],
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
class PagarmeCardAPIAdapter {
|
|
376
|
+
constructor(credentials, paymentRepository, orderBlockedRepository) {
|
|
377
|
+
this.credentials = credentials;
|
|
378
|
+
this.paymentRepository = paymentRepository;
|
|
379
|
+
this.orderBlockedRepository = orderBlockedRepository;
|
|
380
|
+
}
|
|
381
|
+
async pay(checkout, card) {
|
|
382
|
+
try {
|
|
383
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
384
|
+
const payload = this.createCardPayment(checkout, card);
|
|
385
|
+
const result = await client.transactions.create(payload);
|
|
386
|
+
console.log('[PAGARME CARD DATA TO SEND]', payload);
|
|
387
|
+
console.log('result PagarmeCardAPIAdapter', JSON.stringify(result));
|
|
388
|
+
if (result.status !== connect.PagarmePaymentStatus.Pago) {
|
|
389
|
+
await this.orderBlockedRepository.createBlockedOrderOrPayment(checkout, 'Card not authorized', 'Card', 'day', card);
|
|
390
|
+
return Promise.reject(new connect.PaymentError(`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`, {
|
|
391
|
+
checkoutId: checkout.id,
|
|
392
|
+
userEmail: checkout.user.email,
|
|
393
|
+
info: result,
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
const payment = await this.paymentRepository.create({
|
|
397
|
+
createdAt: new Date(),
|
|
398
|
+
updatedAt: new Date(),
|
|
399
|
+
userId: checkout.user.id,
|
|
400
|
+
checkoutId: checkout.id,
|
|
401
|
+
totalPrice: checkout.totalPrice,
|
|
402
|
+
paymentProvider: connect.PaymentProviders.PAGARME,
|
|
403
|
+
transaction: Object.assign(Object.assign({}, result), { paidAt: new Date() }),
|
|
404
|
+
});
|
|
405
|
+
return payment;
|
|
406
|
+
}
|
|
407
|
+
catch (error) {
|
|
408
|
+
console.error('Error PagarmeCardAPIAdapter', JSON.stringify(error));
|
|
409
|
+
throw new connect.PaymentError('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', {
|
|
410
|
+
checkoutId: checkout.id,
|
|
411
|
+
userEmail: checkout.user.email,
|
|
412
|
+
info: error,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
async addCard(card) {
|
|
417
|
+
try {
|
|
418
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
419
|
+
const result = client.cards.create({
|
|
420
|
+
card_number: card.number,
|
|
421
|
+
card_expiration_date: card.expirationDate.replace('/', ''),
|
|
422
|
+
card_holder_name: card.name,
|
|
423
|
+
card_cvv: card.cvv,
|
|
424
|
+
});
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
throw new connect.BusinessError('Houve uma falha adicionar o cartão', {
|
|
429
|
+
info: error,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
async createCardHash(bu, card) {
|
|
434
|
+
try {
|
|
435
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
436
|
+
const result = await client.security.encrypt({
|
|
437
|
+
card_number: card.number,
|
|
438
|
+
card_expiration_date: card.expirationDate.replace('/', ''),
|
|
439
|
+
card_holder_name: card.name,
|
|
440
|
+
card_cvv: card.cvv,
|
|
441
|
+
});
|
|
442
|
+
return result;
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
throw new connect.BusinessError('Houve uma falha ao gerar o hash do cartão', {
|
|
446
|
+
info: error,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
async getCardByToken(id) {
|
|
451
|
+
try {
|
|
452
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
453
|
+
const result = client.cards.find({
|
|
454
|
+
id,
|
|
455
|
+
});
|
|
456
|
+
return result;
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
throw new connect.BusinessError('Houve uma falha buscar o cartão com id: ' + id, {
|
|
460
|
+
info: error,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async createTransaction(data) {
|
|
465
|
+
try {
|
|
466
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
467
|
+
const result = await client.transactions.create(data);
|
|
468
|
+
return result;
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
throw new connect.BusinessError('Houve uma falha ao criar a transação', {
|
|
472
|
+
info: error,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
createCardPayment(checkout, card) {
|
|
477
|
+
var _a, _b, _c, _d, _e, _f;
|
|
478
|
+
return {
|
|
479
|
+
api_key: this.credentials.API_KEY,
|
|
480
|
+
amount: Math.floor(checkout.totalPrice * 100),
|
|
481
|
+
card_id: card.cardId,
|
|
482
|
+
payment_method: 'credit_card',
|
|
483
|
+
installments: card.installments,
|
|
484
|
+
soft_descriptor: checkout.shop === connect.Shops.GLAMSHOP ? 'Glam' : "Men's Market",
|
|
485
|
+
customer: {
|
|
486
|
+
external_id: checkout.user.id,
|
|
487
|
+
name: checkout.user.displayName,
|
|
488
|
+
type: 'individual',
|
|
489
|
+
country: 'br',
|
|
490
|
+
email: checkout.user.email,
|
|
491
|
+
phone_numbers: checkout.user.phone ? ['+55' + checkout.user.phone] : '',
|
|
492
|
+
documents: [
|
|
493
|
+
{
|
|
494
|
+
type: 'cpf',
|
|
495
|
+
number: checkout.user.cpf,
|
|
496
|
+
},
|
|
497
|
+
],
|
|
498
|
+
},
|
|
499
|
+
billing: {
|
|
500
|
+
name: checkout.user.displayName,
|
|
501
|
+
address: {
|
|
502
|
+
country: 'br',
|
|
503
|
+
state: checkout.billingAddress ? checkout.billingAddress.state : (_a = checkout.shippingAddress) === null || _a === void 0 ? void 0 : _a.state,
|
|
504
|
+
city: checkout.billingAddress ? checkout.billingAddress.city : (_b = checkout.shippingAddress) === null || _b === void 0 ? void 0 : _b.city,
|
|
505
|
+
neighborhood: checkout.billingAddress ? checkout.billingAddress.district : (_c = checkout.shippingAddress) === null || _c === void 0 ? void 0 : _c.district,
|
|
506
|
+
street: checkout.billingAddress ? checkout.billingAddress.street : (_d = checkout.shippingAddress) === null || _d === void 0 ? void 0 : _d.street,
|
|
507
|
+
street_number: checkout.billingAddress ? checkout.billingAddress.number : (_e = checkout.shippingAddress) === null || _e === void 0 ? void 0 : _e.number,
|
|
508
|
+
zipcode: checkout.billingAddress
|
|
509
|
+
? checkout.billingAddress.zip.replace('-', '')
|
|
510
|
+
: (_f = checkout.shippingAddress) === null || _f === void 0 ? void 0 : _f.zip.replace('-', ''),
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
items: checkout.lineItems.map((item) => {
|
|
514
|
+
return {
|
|
515
|
+
id: item.id,
|
|
516
|
+
title: checkout.user.isSubscriber ? `${item.name} - ASSINANTE` : item.name,
|
|
517
|
+
unit_price: Math.floor(item.pricePaid * 100),
|
|
518
|
+
quantity: item.quantity,
|
|
519
|
+
tangible: true,
|
|
520
|
+
};
|
|
521
|
+
}),
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
class PagarmePixAPIAdapter {
|
|
527
|
+
constructor(credentials, paymentRepository) {
|
|
528
|
+
this.credentials = credentials;
|
|
529
|
+
this.paymentRepository = paymentRepository;
|
|
530
|
+
}
|
|
531
|
+
async pay(checkout) {
|
|
532
|
+
try {
|
|
533
|
+
const client = await pagarme__default["default"].client.connect({ api_key: this.credentials.API_KEY });
|
|
534
|
+
const payload = this.createPixPayment(checkout);
|
|
535
|
+
const result = await client.transactions.create(payload);
|
|
536
|
+
console.log('[PAGARME PIX DATA TO SEND]', payload);
|
|
537
|
+
console.log('result PagarmePixAPIAdapter', JSON.stringify(result));
|
|
538
|
+
const payment = await this.paymentRepository.create({
|
|
539
|
+
createdAt: new Date(),
|
|
540
|
+
updatedAt: new Date(),
|
|
541
|
+
userId: checkout.user.id,
|
|
542
|
+
checkoutId: checkout.id,
|
|
543
|
+
totalPrice: checkout.totalPrice,
|
|
544
|
+
paymentProvider: 'pagarMe',
|
|
545
|
+
transaction: result,
|
|
546
|
+
});
|
|
547
|
+
return payment;
|
|
548
|
+
}
|
|
549
|
+
catch (error) {
|
|
550
|
+
console.log('Error PagarmePixAPIAdapter', JSON.stringify(error));
|
|
551
|
+
throw new connect.PaymentError('Houve uma falha ao processar pagamento com pix', {
|
|
552
|
+
checkoutId: checkout.id,
|
|
553
|
+
userEmail: checkout.user.email,
|
|
554
|
+
info: error,
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
createPixPayment(checkout) {
|
|
559
|
+
return {
|
|
560
|
+
payment_method: 'pix',
|
|
561
|
+
amount: Math.floor(checkout.totalPrice * 100),
|
|
562
|
+
api_key: this.credentials.API_KEY,
|
|
563
|
+
postback_url: this.credentials.URL_POSTBACK,
|
|
564
|
+
pix_expiration_date: connect.format(connect.addDays(new Date(), 1), 'yyyy-MM-dd'),
|
|
565
|
+
customer: {
|
|
566
|
+
external_id: checkout.user.id,
|
|
567
|
+
type: 'individual',
|
|
568
|
+
country: 'br',
|
|
569
|
+
name: checkout.user.displayName,
|
|
570
|
+
email: checkout.user.email.trim(),
|
|
571
|
+
phone_numbers: checkout.user.phone ? ['+55' + checkout.user.phone] : '',
|
|
572
|
+
documents: [
|
|
573
|
+
{
|
|
574
|
+
type: 'cpf',
|
|
575
|
+
number: checkout.user.cpf,
|
|
576
|
+
},
|
|
577
|
+
],
|
|
578
|
+
},
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
293
583
|
class ProductVertexHelper {
|
|
294
584
|
constructor() { }
|
|
295
585
|
static productMapper(product) {
|
|
@@ -709,6 +999,13 @@ exports.NestFirestoreModule = NestFirestoreModule_1 = __decorate([
|
|
|
709
999
|
},
|
|
710
1000
|
inject: ['FirestoreOptions'],
|
|
711
1001
|
},
|
|
1002
|
+
{
|
|
1003
|
+
provide: 'SequenceRepository',
|
|
1004
|
+
useFactory: (options) => {
|
|
1005
|
+
return new connect.SequenceFirestoreRepository(options);
|
|
1006
|
+
},
|
|
1007
|
+
inject: ['FirestoreOptions'],
|
|
1008
|
+
},
|
|
712
1009
|
],
|
|
713
1010
|
exports: [
|
|
714
1011
|
'BeautyProfileRepository',
|
|
@@ -725,7 +1022,10 @@ exports.NestFirestoreModule = NestFirestoreModule_1 = __decorate([
|
|
|
725
1022
|
'LegacyOrderRepository',
|
|
726
1023
|
'ShopMenuRepository',
|
|
727
1024
|
'OrderRepository',
|
|
1025
|
+
'OrderBlockedRepository',
|
|
728
1026
|
'PaymentRepository',
|
|
1027
|
+
'SequenceRepository',
|
|
1028
|
+
'LogRepository',
|
|
729
1029
|
connect.ProductFirestoreRepository,
|
|
730
1030
|
'ShopSettingsRepository',
|
|
731
1031
|
'SubscriptionPaymentRepository',
|
|
@@ -965,5 +1265,8 @@ exports.ConnectFirestoreService = ConnectFirestoreService;
|
|
|
965
1265
|
exports.ES_CONFIG = ES_CONFIG;
|
|
966
1266
|
exports.FIREBASE_STORAGE = FIREBASE_STORAGE;
|
|
967
1267
|
exports.HASURA_OPTIONS = HASURA_OPTIONS;
|
|
1268
|
+
exports.PagarmeBankSlipAPIAdapter = PagarmeBankSlipAPIAdapter;
|
|
1269
|
+
exports.PagarmeCardAPIAdapter = PagarmeCardAPIAdapter;
|
|
1270
|
+
exports.PagarmePixAPIAdapter = PagarmePixAPIAdapter;
|
|
968
1271
|
exports.ProductVertexHelper = ProductVertexHelper;
|
|
969
1272
|
exports.VERTEX_CONFIG = VERTEX_CONFIG;
|
package/index.esm.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Client } from '@elastic/elasticsearch';
|
|
2
|
-
import { DebugHelper, isEmpty, NotFoundError, ProductsIndex, UserBeautyProfileFirestoreRepository, Buy2WinFirestoreRepository, CategoryFirestoreRepository, CheckoutFirestoreRepository, CheckoutSubscriptionFirestoreRepository, CouponFirestoreRepository, CampaignHashtagFirestoreRepository, CampaignDashboardFirestoreRepository, SubscriptionEditionFirestoreRepository, HomeFirestoreRepository, LeadFirestoreRepository, LegacyOrderFirestoreRepository, ShopMenuFirestoreRepository, OrderFirestoreRepository, PaymentFirestoreRepository, ProductFirestoreRepository, ShopSettingsFirestoreRepository, SubscriptionPaymentFirestoreRepository, SubscriptionPlanFirestoreRepository, SubscriptionProductFirestoreRepository, SubscriptionFirestoreRepository, UserFirestoreRepository, UserAddressFirestoreRepository, UserPaymentMethodFirestoreRepository, SubscriptionMaterializationFirestoreRepository, SubscriptionSummaryFirestoreRepository, ProductVariantFirestoreRepository, OrderBlockedFirestoreRepository, LogFirestoreRepository, CategoryHasuraGraphQLRepository, ProductHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository, ProductReviewsHasuraGraphQLRepository, VariantHasuraGraphQLRepository, ProductStockNotificationHasuraGraphQLRepository, FilterOptionHasuraGraphQLRepository, FilterHasuraGraphQLRepository, CategoryCollectionChildrenHasuraGraphQLRepository, CategoryProductHasuraGraphQLRepository, WishlistHasuraGraphQLRepository, ProductsVertexSearch, isNil } from '@infrab4a/connect';
|
|
2
|
+
import { DebugHelper, isEmpty, NotFoundError, PagarmePaymentStatus, PaymentError, BusinessError, format, addDays, PaymentProviders, Shops, ProductsIndex, UserBeautyProfileFirestoreRepository, Buy2WinFirestoreRepository, CategoryFirestoreRepository, CheckoutFirestoreRepository, CheckoutSubscriptionFirestoreRepository, CouponFirestoreRepository, CampaignHashtagFirestoreRepository, CampaignDashboardFirestoreRepository, SubscriptionEditionFirestoreRepository, HomeFirestoreRepository, LeadFirestoreRepository, LegacyOrderFirestoreRepository, ShopMenuFirestoreRepository, OrderFirestoreRepository, PaymentFirestoreRepository, ProductFirestoreRepository, ShopSettingsFirestoreRepository, SubscriptionPaymentFirestoreRepository, SubscriptionPlanFirestoreRepository, SubscriptionProductFirestoreRepository, SubscriptionFirestoreRepository, UserFirestoreRepository, UserAddressFirestoreRepository, UserPaymentMethodFirestoreRepository, SubscriptionMaterializationFirestoreRepository, SubscriptionSummaryFirestoreRepository, ProductVariantFirestoreRepository, OrderBlockedFirestoreRepository, LogFirestoreRepository, SequenceFirestoreRepository, CategoryHasuraGraphQLRepository, ProductHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository, ProductReviewsHasuraGraphQLRepository, VariantHasuraGraphQLRepository, ProductStockNotificationHasuraGraphQLRepository, FilterOptionHasuraGraphQLRepository, FilterHasuraGraphQLRepository, CategoryCollectionChildrenHasuraGraphQLRepository, CategoryProductHasuraGraphQLRepository, WishlistHasuraGraphQLRepository, ProductsVertexSearch, isNil } from '@infrab4a/connect';
|
|
3
3
|
import { Injectable, Inject, Module } from '@nestjs/common';
|
|
4
|
+
import pagarme from 'pagarme';
|
|
4
5
|
import { DocumentServiceClient } from '@google-cloud/discoveryengine';
|
|
5
6
|
import { SearchServiceClient } from '@google-cloud/discoveryengine/build/src/v1beta';
|
|
6
7
|
import { FirebaseConstants, FirebaseModule } from 'nestjs-firebase';
|
|
@@ -286,6 +287,291 @@ class ConnectFirestoreService {
|
|
|
286
287
|
}
|
|
287
288
|
}
|
|
288
289
|
|
|
290
|
+
class PagarmeBankSlipAPIAdapter {
|
|
291
|
+
constructor(credentials, paymentRepository) {
|
|
292
|
+
this.credentials = credentials;
|
|
293
|
+
this.paymentRepository = paymentRepository;
|
|
294
|
+
}
|
|
295
|
+
async pay(checkout) {
|
|
296
|
+
try {
|
|
297
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
298
|
+
const payload = this.createBoletoPayment(checkout);
|
|
299
|
+
const result = await client.transactions.create(payload);
|
|
300
|
+
console.log('[PAGARME BOLETO DATA TO SEND]', payload);
|
|
301
|
+
console.log('result PagarmeBankSlipAPIAdapter', JSON.stringify(result));
|
|
302
|
+
if (result.status !== PagarmePaymentStatus['Em processamento']) {
|
|
303
|
+
return Promise.reject(new PaymentError(`Houve uma falha ao gerar o boleto. Tente novamente`, {
|
|
304
|
+
checkoutId: checkout.id,
|
|
305
|
+
userEmail: checkout.user.email,
|
|
306
|
+
info: result,
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
const payment = await this.paymentRepository.create({
|
|
310
|
+
createdAt: new Date(),
|
|
311
|
+
updatedAt: new Date(),
|
|
312
|
+
userId: checkout.user.id,
|
|
313
|
+
checkoutId: checkout.id,
|
|
314
|
+
totalPrice: checkout.totalPrice,
|
|
315
|
+
paymentProvider: 'pagarMe',
|
|
316
|
+
transaction: result,
|
|
317
|
+
});
|
|
318
|
+
return payment;
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
console.error('Error PagarmeBankSlipAPIAdapter', JSON.stringify(error));
|
|
322
|
+
throw new PaymentError('Houve uma falha ao gerar o boleto. Tente novamente', {
|
|
323
|
+
checkoutId: checkout.id,
|
|
324
|
+
userEmail: checkout.user.email,
|
|
325
|
+
info: error,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
async getBoletoTransaction(paymentId) {
|
|
330
|
+
try {
|
|
331
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
332
|
+
const result = client.transactions.find({ id: paymentId });
|
|
333
|
+
return result;
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
throw new BusinessError('Houve uma falha buscar o boleto com paymentId: ' + paymentId, {
|
|
337
|
+
paymentId,
|
|
338
|
+
info: error,
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
createBoletoPayment(checkout) {
|
|
343
|
+
return {
|
|
344
|
+
api_key: this.credentials.API_KEY,
|
|
345
|
+
amount: Math.floor(checkout.totalPrice * 100),
|
|
346
|
+
boleto_rules: ['strict_expiration_date'],
|
|
347
|
+
boleto_instructions: 'Sr. Caixa, NÃO aceitar o pagamento após o vencimento.',
|
|
348
|
+
boleto_expiration_date: format(addDays(new Date(), 3), 'yyyy-MM-dd'),
|
|
349
|
+
payment_method: 'boleto',
|
|
350
|
+
postback_url: this.credentials.URL_POSTBACK,
|
|
351
|
+
customer: {
|
|
352
|
+
external_id: checkout.user.id,
|
|
353
|
+
type: 'individual',
|
|
354
|
+
country: 'br',
|
|
355
|
+
name: checkout.user.displayName,
|
|
356
|
+
documents: [
|
|
357
|
+
{
|
|
358
|
+
type: 'cpf',
|
|
359
|
+
number: checkout.user.cpf,
|
|
360
|
+
},
|
|
361
|
+
],
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
class PagarmeCardAPIAdapter {
|
|
368
|
+
constructor(credentials, paymentRepository, orderBlockedRepository) {
|
|
369
|
+
this.credentials = credentials;
|
|
370
|
+
this.paymentRepository = paymentRepository;
|
|
371
|
+
this.orderBlockedRepository = orderBlockedRepository;
|
|
372
|
+
}
|
|
373
|
+
async pay(checkout, card) {
|
|
374
|
+
try {
|
|
375
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
376
|
+
const payload = this.createCardPayment(checkout, card);
|
|
377
|
+
const result = await client.transactions.create(payload);
|
|
378
|
+
console.log('[PAGARME CARD DATA TO SEND]', payload);
|
|
379
|
+
console.log('result PagarmeCardAPIAdapter', JSON.stringify(result));
|
|
380
|
+
if (result.status !== PagarmePaymentStatus.Pago) {
|
|
381
|
+
await this.orderBlockedRepository.createBlockedOrderOrPayment(checkout, 'Card not authorized', 'Card', 'day', card);
|
|
382
|
+
return Promise.reject(new PaymentError(`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`, {
|
|
383
|
+
checkoutId: checkout.id,
|
|
384
|
+
userEmail: checkout.user.email,
|
|
385
|
+
info: result,
|
|
386
|
+
}));
|
|
387
|
+
}
|
|
388
|
+
const payment = await this.paymentRepository.create({
|
|
389
|
+
createdAt: new Date(),
|
|
390
|
+
updatedAt: new Date(),
|
|
391
|
+
userId: checkout.user.id,
|
|
392
|
+
checkoutId: checkout.id,
|
|
393
|
+
totalPrice: checkout.totalPrice,
|
|
394
|
+
paymentProvider: PaymentProviders.PAGARME,
|
|
395
|
+
transaction: Object.assign(Object.assign({}, result), { paidAt: new Date() }),
|
|
396
|
+
});
|
|
397
|
+
return payment;
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
console.error('Error PagarmeCardAPIAdapter', JSON.stringify(error));
|
|
401
|
+
throw new PaymentError('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', {
|
|
402
|
+
checkoutId: checkout.id,
|
|
403
|
+
userEmail: checkout.user.email,
|
|
404
|
+
info: error,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
async addCard(card) {
|
|
409
|
+
try {
|
|
410
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
411
|
+
const result = client.cards.create({
|
|
412
|
+
card_number: card.number,
|
|
413
|
+
card_expiration_date: card.expirationDate.replace('/', ''),
|
|
414
|
+
card_holder_name: card.name,
|
|
415
|
+
card_cvv: card.cvv,
|
|
416
|
+
});
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
419
|
+
catch (error) {
|
|
420
|
+
throw new BusinessError('Houve uma falha adicionar o cartão', {
|
|
421
|
+
info: error,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
async createCardHash(bu, card) {
|
|
426
|
+
try {
|
|
427
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
428
|
+
const result = await client.security.encrypt({
|
|
429
|
+
card_number: card.number,
|
|
430
|
+
card_expiration_date: card.expirationDate.replace('/', ''),
|
|
431
|
+
card_holder_name: card.name,
|
|
432
|
+
card_cvv: card.cvv,
|
|
433
|
+
});
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
catch (error) {
|
|
437
|
+
throw new BusinessError('Houve uma falha ao gerar o hash do cartão', {
|
|
438
|
+
info: error,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async getCardByToken(id) {
|
|
443
|
+
try {
|
|
444
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
445
|
+
const result = client.cards.find({
|
|
446
|
+
id,
|
|
447
|
+
});
|
|
448
|
+
return result;
|
|
449
|
+
}
|
|
450
|
+
catch (error) {
|
|
451
|
+
throw new BusinessError('Houve uma falha buscar o cartão com id: ' + id, {
|
|
452
|
+
info: error,
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
async createTransaction(data) {
|
|
457
|
+
try {
|
|
458
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
459
|
+
const result = await client.transactions.create(data);
|
|
460
|
+
return result;
|
|
461
|
+
}
|
|
462
|
+
catch (error) {
|
|
463
|
+
throw new BusinessError('Houve uma falha ao criar a transação', {
|
|
464
|
+
info: error,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
createCardPayment(checkout, card) {
|
|
469
|
+
var _a, _b, _c, _d, _e, _f;
|
|
470
|
+
return {
|
|
471
|
+
api_key: this.credentials.API_KEY,
|
|
472
|
+
amount: Math.floor(checkout.totalPrice * 100),
|
|
473
|
+
card_id: card.cardId,
|
|
474
|
+
payment_method: 'credit_card',
|
|
475
|
+
installments: card.installments,
|
|
476
|
+
soft_descriptor: checkout.shop === Shops.GLAMSHOP ? 'Glam' : "Men's Market",
|
|
477
|
+
customer: {
|
|
478
|
+
external_id: checkout.user.id,
|
|
479
|
+
name: checkout.user.displayName,
|
|
480
|
+
type: 'individual',
|
|
481
|
+
country: 'br',
|
|
482
|
+
email: checkout.user.email,
|
|
483
|
+
phone_numbers: checkout.user.phone ? ['+55' + checkout.user.phone] : '',
|
|
484
|
+
documents: [
|
|
485
|
+
{
|
|
486
|
+
type: 'cpf',
|
|
487
|
+
number: checkout.user.cpf,
|
|
488
|
+
},
|
|
489
|
+
],
|
|
490
|
+
},
|
|
491
|
+
billing: {
|
|
492
|
+
name: checkout.user.displayName,
|
|
493
|
+
address: {
|
|
494
|
+
country: 'br',
|
|
495
|
+
state: checkout.billingAddress ? checkout.billingAddress.state : (_a = checkout.shippingAddress) === null || _a === void 0 ? void 0 : _a.state,
|
|
496
|
+
city: checkout.billingAddress ? checkout.billingAddress.city : (_b = checkout.shippingAddress) === null || _b === void 0 ? void 0 : _b.city,
|
|
497
|
+
neighborhood: checkout.billingAddress ? checkout.billingAddress.district : (_c = checkout.shippingAddress) === null || _c === void 0 ? void 0 : _c.district,
|
|
498
|
+
street: checkout.billingAddress ? checkout.billingAddress.street : (_d = checkout.shippingAddress) === null || _d === void 0 ? void 0 : _d.street,
|
|
499
|
+
street_number: checkout.billingAddress ? checkout.billingAddress.number : (_e = checkout.shippingAddress) === null || _e === void 0 ? void 0 : _e.number,
|
|
500
|
+
zipcode: checkout.billingAddress
|
|
501
|
+
? checkout.billingAddress.zip.replace('-', '')
|
|
502
|
+
: (_f = checkout.shippingAddress) === null || _f === void 0 ? void 0 : _f.zip.replace('-', ''),
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
items: checkout.lineItems.map((item) => {
|
|
506
|
+
return {
|
|
507
|
+
id: item.id,
|
|
508
|
+
title: checkout.user.isSubscriber ? `${item.name} - ASSINANTE` : item.name,
|
|
509
|
+
unit_price: Math.floor(item.pricePaid * 100),
|
|
510
|
+
quantity: item.quantity,
|
|
511
|
+
tangible: true,
|
|
512
|
+
};
|
|
513
|
+
}),
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
class PagarmePixAPIAdapter {
|
|
519
|
+
constructor(credentials, paymentRepository) {
|
|
520
|
+
this.credentials = credentials;
|
|
521
|
+
this.paymentRepository = paymentRepository;
|
|
522
|
+
}
|
|
523
|
+
async pay(checkout) {
|
|
524
|
+
try {
|
|
525
|
+
const client = await pagarme.client.connect({ api_key: this.credentials.API_KEY });
|
|
526
|
+
const payload = this.createPixPayment(checkout);
|
|
527
|
+
const result = await client.transactions.create(payload);
|
|
528
|
+
console.log('[PAGARME PIX DATA TO SEND]', payload);
|
|
529
|
+
console.log('result PagarmePixAPIAdapter', JSON.stringify(result));
|
|
530
|
+
const payment = await this.paymentRepository.create({
|
|
531
|
+
createdAt: new Date(),
|
|
532
|
+
updatedAt: new Date(),
|
|
533
|
+
userId: checkout.user.id,
|
|
534
|
+
checkoutId: checkout.id,
|
|
535
|
+
totalPrice: checkout.totalPrice,
|
|
536
|
+
paymentProvider: 'pagarMe',
|
|
537
|
+
transaction: result,
|
|
538
|
+
});
|
|
539
|
+
return payment;
|
|
540
|
+
}
|
|
541
|
+
catch (error) {
|
|
542
|
+
console.log('Error PagarmePixAPIAdapter', JSON.stringify(error));
|
|
543
|
+
throw new PaymentError('Houve uma falha ao processar pagamento com pix', {
|
|
544
|
+
checkoutId: checkout.id,
|
|
545
|
+
userEmail: checkout.user.email,
|
|
546
|
+
info: error,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
createPixPayment(checkout) {
|
|
551
|
+
return {
|
|
552
|
+
payment_method: 'pix',
|
|
553
|
+
amount: Math.floor(checkout.totalPrice * 100),
|
|
554
|
+
api_key: this.credentials.API_KEY,
|
|
555
|
+
postback_url: this.credentials.URL_POSTBACK,
|
|
556
|
+
pix_expiration_date: format(addDays(new Date(), 1), 'yyyy-MM-dd'),
|
|
557
|
+
customer: {
|
|
558
|
+
external_id: checkout.user.id,
|
|
559
|
+
type: 'individual',
|
|
560
|
+
country: 'br',
|
|
561
|
+
name: checkout.user.displayName,
|
|
562
|
+
email: checkout.user.email.trim(),
|
|
563
|
+
phone_numbers: checkout.user.phone ? ['+55' + checkout.user.phone] : '',
|
|
564
|
+
documents: [
|
|
565
|
+
{
|
|
566
|
+
type: 'cpf',
|
|
567
|
+
number: checkout.user.cpf,
|
|
568
|
+
},
|
|
569
|
+
],
|
|
570
|
+
},
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
289
575
|
class ProductVertexHelper {
|
|
290
576
|
constructor() { }
|
|
291
577
|
static productMapper(product) {
|
|
@@ -705,6 +991,13 @@ NestFirestoreModule = NestFirestoreModule_1 = __decorate([
|
|
|
705
991
|
},
|
|
706
992
|
inject: ['FirestoreOptions'],
|
|
707
993
|
},
|
|
994
|
+
{
|
|
995
|
+
provide: 'SequenceRepository',
|
|
996
|
+
useFactory: (options) => {
|
|
997
|
+
return new SequenceFirestoreRepository(options);
|
|
998
|
+
},
|
|
999
|
+
inject: ['FirestoreOptions'],
|
|
1000
|
+
},
|
|
708
1001
|
],
|
|
709
1002
|
exports: [
|
|
710
1003
|
'BeautyProfileRepository',
|
|
@@ -721,7 +1014,10 @@ NestFirestoreModule = NestFirestoreModule_1 = __decorate([
|
|
|
721
1014
|
'LegacyOrderRepository',
|
|
722
1015
|
'ShopMenuRepository',
|
|
723
1016
|
'OrderRepository',
|
|
1017
|
+
'OrderBlockedRepository',
|
|
724
1018
|
'PaymentRepository',
|
|
1019
|
+
'SequenceRepository',
|
|
1020
|
+
'LogRepository',
|
|
725
1021
|
ProductFirestoreRepository,
|
|
726
1022
|
'ShopSettingsRepository',
|
|
727
1023
|
'SubscriptionPaymentRepository',
|
|
@@ -955,4 +1251,4 @@ NestConnectModule = NestConnectModule_1 = __decorate([
|
|
|
955
1251
|
Module({})
|
|
956
1252
|
], NestConnectModule);
|
|
957
1253
|
|
|
958
|
-
export { ConnectCollectionService, ConnectDocumentService, ConnectFirestoreService, DiscoveryEngineVertexAdapter, ES_CONFIG, FIREBASE_STORAGE, HASURA_OPTIONS, NativeElasticSearchAdapter, NestConnectModule, NestElasticSeachModule, NestFirestoreModule, NestHasuraGraphQLModule, NestStorageModule, ProductVertexHelper, VERTEX_CONFIG };
|
|
1254
|
+
export { ConnectCollectionService, ConnectDocumentService, ConnectFirestoreService, DiscoveryEngineVertexAdapter, ES_CONFIG, FIREBASE_STORAGE, HASURA_OPTIONS, NativeElasticSearchAdapter, NestConnectModule, NestElasticSeachModule, NestFirestoreModule, NestHasuraGraphQLModule, NestStorageModule, PagarmeBankSlipAPIAdapter, PagarmeCardAPIAdapter, PagarmePixAPIAdapter, ProductVertexHelper, VERTEX_CONFIG };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infrab4a/connect-nestjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0-beta.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org"
|
|
6
6
|
},
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"url": "https://github.com/B4AGroup/b4a-firebase-libs"
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@infrab4a/connect": "4.
|
|
12
|
+
"@infrab4a/connect": "4.22.0-beta.1",
|
|
13
13
|
"@nestjs/common": "^10.3.3",
|
|
14
14
|
"@nestjs/core": "^10.3.3",
|
|
15
15
|
"firebase-admin": "^12.0.0"
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@elastic/elasticsearch": "^8.13.1",
|
|
19
19
|
"@google-cloud/discoveryengine": "^1.14.0",
|
|
20
|
-
"nestjs-firebase": "^10.4.0"
|
|
20
|
+
"nestjs-firebase": "^10.4.0",
|
|
21
|
+
"pagarme": "4.35.2"
|
|
21
22
|
},
|
|
22
23
|
"exports": {
|
|
23
24
|
"./package.json": "./package.json",
|
package/src/infra/index.d.ts
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Checkout, PagarmeCredentials, Payment, PaymentProviderBankSlip, PaymentRepository, PaymentTransaction } from '@infrab4a/connect';
|
|
2
|
+
export declare class PagarmeBankSlipAPIAdapter implements PaymentProviderBankSlip {
|
|
3
|
+
private credentials;
|
|
4
|
+
private paymentRepository;
|
|
5
|
+
constructor(credentials: PagarmeCredentials, paymentRepository: PaymentRepository);
|
|
6
|
+
pay(checkout: Checkout): Promise<Payment>;
|
|
7
|
+
getBoletoTransaction(paymentId: number): Promise<PaymentTransaction>;
|
|
8
|
+
private createBoletoPayment;
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BusinessUnitEnum, CardInfo, Checkout, OrderBlockedRepository, PagarMeCard, PagarmeCredentials, Payment, PaymentCardInfo, PaymentProviderCard, PaymentRepository, PaymentTransaction } from '@infrab4a/connect';
|
|
2
|
+
export declare class PagarmeCardAPIAdapter implements PaymentProviderCard<PagarMeCard> {
|
|
3
|
+
private credentials;
|
|
4
|
+
private paymentRepository;
|
|
5
|
+
private orderBlockedRepository;
|
|
6
|
+
constructor(credentials: PagarmeCredentials, paymentRepository: PaymentRepository, orderBlockedRepository: OrderBlockedRepository);
|
|
7
|
+
pay(checkout: Checkout, card: PaymentCardInfo): Promise<Payment>;
|
|
8
|
+
addCard(card: CardInfo): Promise<PagarMeCard>;
|
|
9
|
+
createCardHash(bu: BusinessUnitEnum, card: CardInfo): Promise<string>;
|
|
10
|
+
getCardByToken(id: string): Promise<PagarMeCard>;
|
|
11
|
+
createTransaction(data: any): Promise<PaymentTransaction>;
|
|
12
|
+
private createCardPayment;
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Checkout, PagarmeCredentials, Payment, PaymentProviderPix, PaymentRepository } from '@infrab4a/connect';
|
|
2
|
+
export declare class PagarmePixAPIAdapter implements PaymentProviderPix {
|
|
3
|
+
private credentials;
|
|
4
|
+
private paymentRepository;
|
|
5
|
+
constructor(credentials: PagarmeCredentials, paymentRepository: PaymentRepository);
|
|
6
|
+
pay(checkout: Checkout): Promise<Payment>;
|
|
7
|
+
private createPixPayment;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './adapters';
|