@cloudcommerce/app-mercadopago 0.0.113 → 0.0.115
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/.turbo/turbo-build.log +2 -2
- package/package.json +8 -8
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -3
- package/lib/index.js.map +0 -1
- package/lib/mercadopago.d.ts +0 -86
- package/lib/mercadopago.js +0 -15
- package/lib/mercadopago.js.map +0 -1
- package/lib/mp-create-transaction.d.ts +0 -85
- package/lib/mp-create-transaction.js +0 -270
- package/lib/mp-create-transaction.js.map +0 -1
- package/lib/mp-list-payments.d.ts +0 -7
- package/lib/mp-list-payments.js +0 -175
- package/lib/mp-list-payments.js.map +0 -1
- package/lib/mp-webhook.d.ts +0 -5
- package/lib/mp-webhook.js +0 -119
- package/lib/mp-webhook.js.map +0 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[35m@cloudcommerce/app-mercadopago:build[0m: cache hit, replaying output [
|
|
1
|
+
[35m@cloudcommerce/app-mercadopago:build[0m: cache hit, replaying output [2m5687d08593485f69[0m
|
|
2
2
|
[35m@cloudcommerce/app-mercadopago:build: [0m
|
|
3
|
-
[35m@cloudcommerce/app-mercadopago:build: [0m> @cloudcommerce/app-mercadopago@0.0.
|
|
3
|
+
[35m@cloudcommerce/app-mercadopago:build: [0m> @cloudcommerce/app-mercadopago@0.0.114 build /home/leo/code/ecomplus/cloud-commerce/packages/apps/mercadopago
|
|
4
4
|
[35m@cloudcommerce/app-mercadopago:build: [0m> sh scripts/build.sh
|
|
5
5
|
[35m@cloudcommerce/app-mercadopago:build: [0m
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcommerce/app-mercadopago",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.115",
|
|
5
5
|
"description": "E-Com Plus Cloud Commerce app to integrate Mercado Pago",
|
|
6
6
|
"main": "lib/mercadopago.js",
|
|
7
7
|
"exports": {
|
|
@@ -20,15 +20,15 @@
|
|
|
20
20
|
},
|
|
21
21
|
"homepage": "https://github.com/ecomplus/cloud-commerce/tree/main/packages/packages/apps/mercadopago#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@cloudcommerce/api": "0.0.
|
|
24
|
-
"@cloudcommerce/firebase": "0.0.
|
|
25
|
-
"axios": "^
|
|
26
|
-
"firebase-admin": "^11.0
|
|
27
|
-
"firebase-functions": "^3.24.
|
|
23
|
+
"@cloudcommerce/api": "0.0.115",
|
|
24
|
+
"@cloudcommerce/firebase": "0.0.115",
|
|
25
|
+
"axios": "^1.1.2",
|
|
26
|
+
"firebase-admin": "^11.1.0",
|
|
27
|
+
"firebase-functions": "^3.24.1"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@cloudcommerce/types": "0.0.
|
|
31
|
-
"@firebase/app-types": "^0.
|
|
30
|
+
"@cloudcommerce/types": "0.0.115",
|
|
31
|
+
"@firebase/app-types": "^0.8.0"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "sh scripts/build.sh"
|
package/lib/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './mercadopago';
|
package/lib/index.js
DELETED
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,cAAc,eAAe,CAAC"}
|
package/lib/mercadopago.d.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import '@cloudcommerce/firebase/lib/init';
|
|
2
|
-
import type { AppModuleBody } from '@cloudcommerce/types';
|
|
3
|
-
export declare const listPayments: (modBody: AppModuleBody) => Promise<import("@cloudcommerce/types").ListPaymentsResponse | {
|
|
4
|
-
error: string;
|
|
5
|
-
message: string;
|
|
6
|
-
}>;
|
|
7
|
-
export declare const createTransaction: (modBody: AppModuleBody) => Promise<{
|
|
8
|
-
status: number;
|
|
9
|
-
redirect_to_payment: boolean;
|
|
10
|
-
transaction: {
|
|
11
|
-
payment_link?: string | undefined;
|
|
12
|
-
payment_instructions?: string | undefined;
|
|
13
|
-
intermediator?: {
|
|
14
|
-
transaction_id?: string | undefined;
|
|
15
|
-
transaction_code?: string | undefined;
|
|
16
|
-
transaction_reference?: string | undefined;
|
|
17
|
-
payment_method?: {
|
|
18
|
-
code: string;
|
|
19
|
-
name?: string | undefined;
|
|
20
|
-
} | undefined;
|
|
21
|
-
buyer_id?: string | undefined;
|
|
22
|
-
} | undefined;
|
|
23
|
-
credit_card?: {
|
|
24
|
-
holder_name?: string | undefined;
|
|
25
|
-
avs_result_code?: string | null | undefined;
|
|
26
|
-
cvv_result_code?: string | null | undefined;
|
|
27
|
-
bin?: number | undefined;
|
|
28
|
-
company?: string | undefined;
|
|
29
|
-
last_digits?: string | undefined;
|
|
30
|
-
token?: string | undefined;
|
|
31
|
-
error_code?: "incorrect_number" | "invalid_number" | "invalid_expiry_date" | "invalid_cvc" | "expired_card" | "incorrect_cvc" | "incorrect_zip" | "incorrect_address" | "card_declined" | "processing_error" | "call_issuer" | "pick_up_card" | undefined;
|
|
32
|
-
} | undefined;
|
|
33
|
-
banking_billet?: {
|
|
34
|
-
code?: string | undefined;
|
|
35
|
-
valid_thru?: string | undefined;
|
|
36
|
-
text_lines?: string[] | undefined;
|
|
37
|
-
link?: string | undefined;
|
|
38
|
-
} | undefined;
|
|
39
|
-
loyalty_points?: {
|
|
40
|
-
name?: string | undefined;
|
|
41
|
-
program_id: string;
|
|
42
|
-
points_value: number;
|
|
43
|
-
ratio?: number | undefined;
|
|
44
|
-
} | undefined;
|
|
45
|
-
currency_id?: string | undefined;
|
|
46
|
-
currency_symbol?: string | undefined;
|
|
47
|
-
discount?: number | undefined;
|
|
48
|
-
amount: number;
|
|
49
|
-
installments?: {
|
|
50
|
-
number: number;
|
|
51
|
-
value?: number | undefined;
|
|
52
|
-
tax?: boolean | undefined;
|
|
53
|
-
total?: number | undefined;
|
|
54
|
-
} | undefined;
|
|
55
|
-
creditor_fees?: {
|
|
56
|
-
installment?: number | undefined;
|
|
57
|
-
operational?: number | undefined;
|
|
58
|
-
intermediation?: number | undefined;
|
|
59
|
-
other?: number | undefined;
|
|
60
|
-
} | undefined;
|
|
61
|
-
status?: {
|
|
62
|
-
updated_at?: string | undefined;
|
|
63
|
-
current: "voided" | "under_analysis" | "paid" | "pending" | "authorized" | "unauthorized" | "in_dispute" | "refunded" | "unknown";
|
|
64
|
-
} | undefined;
|
|
65
|
-
flags?: string[] | undefined;
|
|
66
|
-
custom_fields?: {
|
|
67
|
-
field: string;
|
|
68
|
-
value: string;
|
|
69
|
-
}[] | undefined;
|
|
70
|
-
notes?: string | undefined;
|
|
71
|
-
};
|
|
72
|
-
message?: undefined;
|
|
73
|
-
error?: undefined;
|
|
74
|
-
} | {
|
|
75
|
-
status: number;
|
|
76
|
-
message: string;
|
|
77
|
-
redirect_to_payment?: undefined;
|
|
78
|
-
transaction?: undefined;
|
|
79
|
-
error?: undefined;
|
|
80
|
-
} | {
|
|
81
|
-
status: number;
|
|
82
|
-
error: string;
|
|
83
|
-
message: any;
|
|
84
|
-
redirect_to_payment?: undefined;
|
|
85
|
-
transaction?: undefined;
|
|
86
|
-
}>;
|
package/lib/mercadopago.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import '@cloudcommerce/firebase/lib/init';
|
|
2
|
-
// eslint-disable-next-line import/no-unresolved
|
|
3
|
-
import { getFirestore } from 'firebase-admin/firestore';
|
|
4
|
-
// eslint-disable-next-line import/no-unresolved
|
|
5
|
-
import handleListPayments from './mp-list-payments.js';
|
|
6
|
-
import handleCreateTransaction from './mp-create-transaction.js';
|
|
7
|
-
|
|
8
|
-
export const listPayments = async (modBody) => {
|
|
9
|
-
return handleListPayments(modBody);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const createTransaction = async (modBody) => {
|
|
13
|
-
return handleCreateTransaction(modBody, getFirestore());
|
|
14
|
-
};
|
|
15
|
-
// # sourceMappingURL=mercadopago.js.map
|
package/lib/mercadopago.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mercadopago.js","sourceRoot":"","sources":["../src/mercadopago.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC;AAG1C,gDAAgD;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,gDAAgD;AAChD,OAAO,kBAAkB,MAAM,oBAAoB,CAAC;AACpD,OAAO,uBAAuB,MAAM,yBAAyB,CAAC;AAE9D,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAAE,OAAsB,EAAE,EAAE;IAC3D,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAC;AACrC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,OAAsB,EAAE,EAAE;IAChE,OAAO,uBAAuB,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC,CAAC;AAC1D,CAAC,CAAC"}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type { AppModuleBody } from '@cloudcommerce/types';
|
|
2
|
-
import type { Firestore } from 'firebase-admin/firestore';
|
|
3
|
-
declare const parsePaymentStatus: (status: string) => "voided" | "under_analysis" | "paid" | "pending";
|
|
4
|
-
declare const _default: (appData: AppModuleBody, firestore: Firestore) => Promise<{
|
|
5
|
-
status: number;
|
|
6
|
-
redirect_to_payment: boolean;
|
|
7
|
-
transaction: {
|
|
8
|
-
payment_link?: string | undefined;
|
|
9
|
-
payment_instructions?: string | undefined;
|
|
10
|
-
intermediator?: {
|
|
11
|
-
transaction_id?: string | undefined;
|
|
12
|
-
transaction_code?: string | undefined;
|
|
13
|
-
transaction_reference?: string | undefined;
|
|
14
|
-
payment_method?: {
|
|
15
|
-
code: string;
|
|
16
|
-
name?: string | undefined;
|
|
17
|
-
} | undefined;
|
|
18
|
-
buyer_id?: string | undefined;
|
|
19
|
-
} | undefined;
|
|
20
|
-
credit_card?: {
|
|
21
|
-
holder_name?: string | undefined;
|
|
22
|
-
avs_result_code?: string | null | undefined;
|
|
23
|
-
cvv_result_code?: string | null | undefined;
|
|
24
|
-
bin?: number | undefined;
|
|
25
|
-
company?: string | undefined;
|
|
26
|
-
last_digits?: string | undefined;
|
|
27
|
-
token?: string | undefined;
|
|
28
|
-
error_code?: "incorrect_number" | "invalid_number" | "invalid_expiry_date" | "invalid_cvc" | "expired_card" | "incorrect_cvc" | "incorrect_zip" | "incorrect_address" | "card_declined" | "processing_error" | "call_issuer" | "pick_up_card" | undefined;
|
|
29
|
-
} | undefined;
|
|
30
|
-
banking_billet?: {
|
|
31
|
-
code?: string | undefined;
|
|
32
|
-
valid_thru?: string | undefined;
|
|
33
|
-
text_lines?: string[] | undefined;
|
|
34
|
-
link?: string | undefined;
|
|
35
|
-
} | undefined;
|
|
36
|
-
loyalty_points?: {
|
|
37
|
-
name?: string | undefined;
|
|
38
|
-
program_id: string;
|
|
39
|
-
points_value: number;
|
|
40
|
-
ratio?: number | undefined;
|
|
41
|
-
} | undefined;
|
|
42
|
-
currency_id?: string | undefined;
|
|
43
|
-
currency_symbol?: string | undefined;
|
|
44
|
-
discount?: number | undefined;
|
|
45
|
-
amount: number;
|
|
46
|
-
installments?: {
|
|
47
|
-
number: number;
|
|
48
|
-
value?: number | undefined;
|
|
49
|
-
tax?: boolean | undefined;
|
|
50
|
-
total?: number | undefined;
|
|
51
|
-
} | undefined;
|
|
52
|
-
creditor_fees?: {
|
|
53
|
-
installment?: number | undefined;
|
|
54
|
-
operational?: number | undefined;
|
|
55
|
-
intermediation?: number | undefined;
|
|
56
|
-
other?: number | undefined;
|
|
57
|
-
} | undefined;
|
|
58
|
-
status?: {
|
|
59
|
-
updated_at?: string | undefined;
|
|
60
|
-
current: "voided" | "under_analysis" | "paid" | "pending" | "authorized" | "unauthorized" | "in_dispute" | "refunded" | "unknown";
|
|
61
|
-
} | undefined;
|
|
62
|
-
flags?: string[] | undefined;
|
|
63
|
-
custom_fields?: {
|
|
64
|
-
field: string;
|
|
65
|
-
value: string;
|
|
66
|
-
}[] | undefined;
|
|
67
|
-
notes?: string | undefined;
|
|
68
|
-
};
|
|
69
|
-
message?: undefined;
|
|
70
|
-
error?: undefined;
|
|
71
|
-
} | {
|
|
72
|
-
status: number;
|
|
73
|
-
message: string;
|
|
74
|
-
redirect_to_payment?: undefined;
|
|
75
|
-
transaction?: undefined;
|
|
76
|
-
error?: undefined;
|
|
77
|
-
} | {
|
|
78
|
-
status: number;
|
|
79
|
-
error: string;
|
|
80
|
-
message: any;
|
|
81
|
-
redirect_to_payment?: undefined;
|
|
82
|
-
transaction?: undefined;
|
|
83
|
-
}>;
|
|
84
|
-
export default _default;
|
|
85
|
-
export { parsePaymentStatus, };
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import { logger } from 'firebase-functions';
|
|
2
|
-
import config from '@cloudcommerce/firebase/lib/config';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
|
|
5
|
-
const parsePaymentStatus = (status) => {
|
|
6
|
-
switch (status) {
|
|
7
|
-
case 'rejected':
|
|
8
|
-
return 'voided';
|
|
9
|
-
case 'in_process':
|
|
10
|
-
return 'under_analysis';
|
|
11
|
-
case 'approved':
|
|
12
|
-
return 'paid';
|
|
13
|
-
default:
|
|
14
|
-
return 'pending';
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export default async (appData, firestore) => {
|
|
19
|
-
const locationId = config.get().httpsFunctionOptions.region;
|
|
20
|
-
const baseUri = `https://${locationId}-${process.env.GCLOUD_PROJECT}.cloudfunctions.net`;
|
|
21
|
-
// body was already pre-validated on @/bin/web.js
|
|
22
|
-
// treat module request body
|
|
23
|
-
const { application, storeId } = appData;
|
|
24
|
-
const { params } = appData;
|
|
25
|
-
// app configured options
|
|
26
|
-
const configApp = { ...application.data, ...application.hidden_data };
|
|
27
|
-
const notificationUrl = `${baseUri}/mercadopago-webhook`;
|
|
28
|
-
let token;
|
|
29
|
-
let paymentMethodId;
|
|
30
|
-
const isPix = params.payment_method.code === 'account_deposit';
|
|
31
|
-
if (params.credit_card && params.credit_card.hash) {
|
|
32
|
-
const hashParts = params.credit_card.hash.split(' // ');
|
|
33
|
-
[token] = hashParts;
|
|
34
|
-
try {
|
|
35
|
-
paymentMethodId = JSON.parse(hashParts[1]).payment_method_id;
|
|
36
|
-
} catch (e) {
|
|
37
|
-
paymentMethodId = params.credit_card.company || 'visa';
|
|
38
|
-
}
|
|
39
|
-
} else if (params.payment_method.code === 'banking_billet') {
|
|
40
|
-
paymentMethodId = 'bolbradesco';
|
|
41
|
-
} else if (params.payment_method.code === 'account_deposit') {
|
|
42
|
-
paymentMethodId = 'pix';
|
|
43
|
-
} else {
|
|
44
|
-
return {
|
|
45
|
-
status: 400,
|
|
46
|
-
error: 'NO_CARD_ERR',
|
|
47
|
-
message: 'Credit card hash is required',
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
const { buyer } = params;
|
|
51
|
-
const orderId = params.order_id;
|
|
52
|
-
logger.log('> MP Transaction #', storeId, orderId);
|
|
53
|
-
// https://www.mercadopago.com.br/developers/pt/reference/payments/_payments/post/
|
|
54
|
-
const payerPhone = {
|
|
55
|
-
area_code: buyer.phone.number.substring(0, 2),
|
|
56
|
-
number: buyer.phone.number.substring(2, 11),
|
|
57
|
-
};
|
|
58
|
-
const additionalInfo = {
|
|
59
|
-
items: [],
|
|
60
|
-
payer: {
|
|
61
|
-
first_name: buyer.fullname.replace(/\s.*/, ''),
|
|
62
|
-
last_name: buyer.fullname.replace(/[^\s]+\s/, ''),
|
|
63
|
-
phone: payerPhone,
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
if (params.to && params.to.street) {
|
|
67
|
-
additionalInfo.shipments = {
|
|
68
|
-
receiver_address: {
|
|
69
|
-
zip_code: params.to.zip,
|
|
70
|
-
street_name: params.to.street,
|
|
71
|
-
street_number: params.to.number || 0,
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
if (params.billing_address && params.billing_address.street) {
|
|
76
|
-
additionalInfo.payer.address = {
|
|
77
|
-
zip_code: params.billing_address.zip,
|
|
78
|
-
street_name: params.billing_address.street,
|
|
79
|
-
street_number: params.billing_address.number || 0,
|
|
80
|
-
};
|
|
81
|
-
} else if (additionalInfo.shipments) {
|
|
82
|
-
additionalInfo.payer.address = additionalInfo.shipments.receiver_address;
|
|
83
|
-
}
|
|
84
|
-
if (Array.isArray(params.items)) {
|
|
85
|
-
params.items.forEach((item) => {
|
|
86
|
-
additionalInfo.items.push({
|
|
87
|
-
id: item.sku || item.variation_id || item.product_id,
|
|
88
|
-
title: item.name || item.sku,
|
|
89
|
-
description: item.name || item.sku,
|
|
90
|
-
quantity: item.quantity,
|
|
91
|
-
unit_price: item.final_price || item.price,
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
const payerOrBuyer = {
|
|
96
|
-
...buyer,
|
|
97
|
-
...params.payer,
|
|
98
|
-
};
|
|
99
|
-
Object.keys(payerOrBuyer).forEach((field) => {
|
|
100
|
-
if (!payerOrBuyer[field] && buyer[field]) {
|
|
101
|
-
payerOrBuyer[field] = buyer[field];
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
const payment = {
|
|
105
|
-
payer: {
|
|
106
|
-
type: 'customer',
|
|
107
|
-
email: buyer.email,
|
|
108
|
-
first_name: payerOrBuyer.fullname.replace(/\s.*/, ''),
|
|
109
|
-
last_name: payerOrBuyer.fullname.replace(/[^\s]+\s/, ''),
|
|
110
|
-
identification: {
|
|
111
|
-
type: payerOrBuyer.registry_type === 'j' ? 'CNPJ' : 'CPF',
|
|
112
|
-
number: String(payerOrBuyer.doc_number),
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
external_reference: String(params.order_number),
|
|
116
|
-
transaction_amount: params.amount.total,
|
|
117
|
-
description: `Pedido #${params.order_number} de ${buyer.fullname}`.substring(0, 60),
|
|
118
|
-
payment_method_id: paymentMethodId,
|
|
119
|
-
token,
|
|
120
|
-
statement_descriptor: configApp.statement_descriptor || `${params.domain}_MercadoPago`,
|
|
121
|
-
installments: params.installments_number || 1,
|
|
122
|
-
notification_url: notificationUrl,
|
|
123
|
-
additional_info: additionalInfo,
|
|
124
|
-
metadata: {
|
|
125
|
-
ecom_store_id: storeId,
|
|
126
|
-
ecom_order_id: orderId,
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
logger.log('>data: ', JSON.stringify(payment));
|
|
130
|
-
try {
|
|
131
|
-
// https://www.mercadopago.com.br/developers/pt/reference/payments/_payments/post
|
|
132
|
-
const { data } = await axios({
|
|
133
|
-
url: 'https://api.mercadopago.com/v1/payments',
|
|
134
|
-
method: 'post',
|
|
135
|
-
headers: {
|
|
136
|
-
Authorization: `Bearer ${configApp.mp_access_token}`,
|
|
137
|
-
'Content-Type': 'application/json',
|
|
138
|
-
},
|
|
139
|
-
data: payment,
|
|
140
|
-
});
|
|
141
|
-
if (data) {
|
|
142
|
-
logger.log('> MP Checkout #', storeId, orderId);
|
|
143
|
-
const statusPayment = parsePaymentStatus(data.status);
|
|
144
|
-
let isSaveRetry = false;
|
|
145
|
-
const saveToDb = () => {
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
firestore.collection('mp_payments')
|
|
148
|
-
.doc(String(data.id))
|
|
149
|
-
.set({
|
|
150
|
-
transaction_code: data.id,
|
|
151
|
-
store_id: storeId,
|
|
152
|
-
order_id: orderId,
|
|
153
|
-
status: statusPayment,
|
|
154
|
-
paymentMethod: paymentMethodId,
|
|
155
|
-
notificationUrl,
|
|
156
|
-
}, {
|
|
157
|
-
merge: true,
|
|
158
|
-
})
|
|
159
|
-
.then(() => {
|
|
160
|
-
logger.log('> Payment #', String(data.id));
|
|
161
|
-
resolve(true);
|
|
162
|
-
})
|
|
163
|
-
.catch((err) => {
|
|
164
|
-
if (err.code === 13 && !isSaveRetry) {
|
|
165
|
-
isSaveRetry = true;
|
|
166
|
-
setTimeout(saveToDb, 500);
|
|
167
|
-
} else {
|
|
168
|
-
logger.error('PAYMENT_SAVE_ERR', err);
|
|
169
|
-
reject(err);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
};
|
|
174
|
-
await saveToDb();
|
|
175
|
-
const transaction = {
|
|
176
|
-
amount: data.transaction_details.total_paid_amount,
|
|
177
|
-
currency_id: data.currency_id,
|
|
178
|
-
intermediator: {
|
|
179
|
-
payment_method: {
|
|
180
|
-
code: paymentMethodId || params.payment_method.code,
|
|
181
|
-
},
|
|
182
|
-
transaction_id: String(data.id),
|
|
183
|
-
transaction_code: String(data.id),
|
|
184
|
-
transaction_reference: data.external_reference,
|
|
185
|
-
},
|
|
186
|
-
status: {
|
|
187
|
-
current: statusPayment,
|
|
188
|
-
},
|
|
189
|
-
};
|
|
190
|
-
if (params.payment_method.code === 'credit_card') {
|
|
191
|
-
if (data.card) {
|
|
192
|
-
transaction.credit_card = {
|
|
193
|
-
holder_name: data.card.cardholder.name,
|
|
194
|
-
last_digits: data.card.last_four_digits,
|
|
195
|
-
token,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
if (data.installments) {
|
|
199
|
-
transaction.installments = {
|
|
200
|
-
number: data.installments,
|
|
201
|
-
tax: (data.transaction_details.total_paid_amount > data.transaction_amount),
|
|
202
|
-
total: data.transaction_details.total_paid_amount,
|
|
203
|
-
value: data.transaction_details.installment_amount,
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
} else if (!isPix && data.transaction_details
|
|
207
|
-
&& data.transaction_details.external_resource_url) {
|
|
208
|
-
transaction.payment_link = data.transaction_details.external_resource_url;
|
|
209
|
-
transaction.banking_billet = {
|
|
210
|
-
link: transaction.payment_link,
|
|
211
|
-
};
|
|
212
|
-
if (data.date_of_expiration) {
|
|
213
|
-
const dateValidThru = new Date(data.date_of_expiration);
|
|
214
|
-
if (dateValidThru.getTime() > 0) {
|
|
215
|
-
transaction.banking_billet.valid_thru = dateValidThru.toISOString();
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
} else if (isPix && data.point_of_interaction && data.point_of_interaction.transaction_data) {
|
|
219
|
-
// https://www.mercadopago.com.br/developers/pt/docs/checkout-api/integration-configuration/integrate-with-pix#bookmark_visualiza%C3%A7%C3%A3o_de_pagamento
|
|
220
|
-
const qrCode = data.point_of_interaction.transaction_data.qr_code;
|
|
221
|
-
const qrCodeBase64 = data.point_of_interaction.transaction_data.qr_code_base64;
|
|
222
|
-
transaction.notes = '<div style="display:block;margin:0 auto"> '
|
|
223
|
-
+ `<img width="280" height="280" style="margin:5px auto" src='data:image/jpeg;base64,${qrCodeBase64}'/> `
|
|
224
|
-
+ `<lable> ${qrCode} </label></div>`;
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
status: 200,
|
|
228
|
-
redirect_to_payment: false,
|
|
229
|
-
transaction,
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
return {
|
|
233
|
-
status: 409,
|
|
234
|
-
message: `CREATE_TRANSACTION_ERR #${storeId} - ${orderId} => MP not response`,
|
|
235
|
-
};
|
|
236
|
-
} catch (error) {
|
|
237
|
-
let { message } = error;
|
|
238
|
-
//
|
|
239
|
-
const err = {
|
|
240
|
-
message: `CREATE_TRANSACTION_ERR #${storeId} - ${orderId} => ${message}`,
|
|
241
|
-
payment: '',
|
|
242
|
-
status: 0,
|
|
243
|
-
response: '',
|
|
244
|
-
};
|
|
245
|
-
if (error.response) {
|
|
246
|
-
const { status, data } = error.response;
|
|
247
|
-
if (status !== 401 && status !== 403) {
|
|
248
|
-
err.payment = JSON.stringify(payment);
|
|
249
|
-
err.status = status;
|
|
250
|
-
if (typeof data === 'object' && data) {
|
|
251
|
-
err.response = JSON.stringify(data);
|
|
252
|
-
} else {
|
|
253
|
-
err.response = data;
|
|
254
|
-
}
|
|
255
|
-
if (typeof data.message === 'string' && data.message) {
|
|
256
|
-
message = data.message;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
logger.error(err);
|
|
261
|
-
return {
|
|
262
|
-
status: 409,
|
|
263
|
-
error: 'CREATE_TRANSACTION_ERR',
|
|
264
|
-
message,
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
export { parsePaymentStatus };
|
|
270
|
-
// # sourceMappingURL=mp-create-transaction.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mp-create-transaction.js","sourceRoot":"","sources":["../src/mp-create-transaction.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,MAAM,MAAM,oCAAoC,CAAC;AACxD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,kBAAkB,GAAG,CAAC,MAAc,EAAE,EAAE;IAC5C,QAAQ,MAAM,EAAE;QACd,KAAK,UAAU;YACb,OAAO,QAAQ,CAAC;QAClB,KAAK,YAAY;YACf,OAAO,gBAAgB,CAAC;QAC1B,KAAK,UAAU;YACb,OAAO,MAAM,CAAC;QAChB;YACE,OAAO,SAAS,CAAC;KACpB;AACH,CAAC,CAAC;AAEF,eAAe,KAAK,EAAE,OAAsB,EAAE,SAAmB,EAAE,EAAE;IACnE,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,MAAM,CAAC;IAC5D,MAAM,OAAO,GAAG,WAAW,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,qBAAqB,CAAC;IACzF,iDAAiD;IACjD,4BAA4B;IAC5B,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACzC,MAAM,MAAM,GAAG,OAAO,CA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,SAAS,CAAC,IAAI,CAAC,CAAC;iBACrC;qBAAM;oBACL,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC;iBACrB;gBACD,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ,IAAI,IAAI,CAAC,OAAO,EAAE;oBACpD,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;iBACxB;aACF;SACF;QACD,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAClB,OAAO;YACL,MAAM,EAAE,GAAG;YACX,KAAK,EAAE,wBAAwB;YAC/B,OAAO;SACR,CAAC;KACH;AACH,CAAC,CAAC;AAEF,OAAO,EACL,kBAAkB,GACnB,CAAC"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { AppModuleBody } from '@cloudcommerce/types';
|
|
2
|
-
import type { ListPaymentsResponse } from '@cloudcommerce/types/modules/list_payments:response';
|
|
3
|
-
declare const _default: (data: AppModuleBody) => ListPaymentsResponse | {
|
|
4
|
-
error: string;
|
|
5
|
-
message: string;
|
|
6
|
-
};
|
|
7
|
-
export default _default;
|
package/lib/mp-list-payments.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import url from 'url';
|
|
4
|
-
|
|
5
|
-
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
|
|
6
|
-
|
|
7
|
-
export default (data) => {
|
|
8
|
-
const { application } = data;
|
|
9
|
-
const { params } = data;
|
|
10
|
-
// https://apx-mods.e-com.plus/api/v1/list_payments/schema.json?store_id=100
|
|
11
|
-
const amount = params.amount || { total: undefined };
|
|
12
|
-
// const initialTotalAmount = amount.total;
|
|
13
|
-
const config = {
|
|
14
|
-
...application.data,
|
|
15
|
-
...application.hidden_data,
|
|
16
|
-
};
|
|
17
|
-
if (!config.mp_public_key || !config.mp_access_token) {
|
|
18
|
-
// must have configured PayPal app ID and secret
|
|
19
|
-
return {
|
|
20
|
-
error: 'LIST_PAYMENTS_ERR',
|
|
21
|
-
message: 'MP Public Key or Access Token is unset on app hidden data (merchant must configure the app)',
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
// start mounting response body
|
|
25
|
-
// https://apx-mods.e-com.plus/api/v1/list_payments/response_schema.json?store_id=100
|
|
26
|
-
const response = {
|
|
27
|
-
payment_gateways: [],
|
|
28
|
-
};
|
|
29
|
-
// calculate discount value
|
|
30
|
-
const { discount } = config;
|
|
31
|
-
if (discount && discount.value) {
|
|
32
|
-
if (discount.apply_at !== 'freight') {
|
|
33
|
-
// default discount option
|
|
34
|
-
const { value } = discount;
|
|
35
|
-
response.discount_option = {
|
|
36
|
-
label: config.discount_option_label,
|
|
37
|
-
value,
|
|
38
|
-
};
|
|
39
|
-
// specify the discount type and min amount is optional
|
|
40
|
-
const discountTypeMinAmount = ['type', 'min_amount'];
|
|
41
|
-
discountTypeMinAmount.forEach((prop) => {
|
|
42
|
-
if (response.discount_option && discount[prop]) {
|
|
43
|
-
response.discount_option[prop] = discount[prop];
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
if (amount.total) {
|
|
48
|
-
// check amount value to apply discount
|
|
49
|
-
if (amount.total < discount.min_amount) {
|
|
50
|
-
discount.value = 0;
|
|
51
|
-
} else {
|
|
52
|
-
delete discount.min_amount;
|
|
53
|
-
// fix local amount object
|
|
54
|
-
const maxDiscount = amount[discount.apply_at || 'subtotal'];
|
|
55
|
-
let discountValue;
|
|
56
|
-
if (discount.type === 'percentage') {
|
|
57
|
-
discountValue = (maxDiscount * discount.value) / 100;
|
|
58
|
-
} else {
|
|
59
|
-
discountValue = discount.value;
|
|
60
|
-
if (discountValue > maxDiscount) {
|
|
61
|
-
discountValue = maxDiscount;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (discountValue) {
|
|
65
|
-
amount.discount = (amount.discount || 0) + discountValue;
|
|
66
|
-
amount.total -= discountValue;
|
|
67
|
-
if (amount.total < 0) {
|
|
68
|
-
amount.total = 0;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// setup payment gateway objects
|
|
75
|
-
const intermediator = {
|
|
76
|
-
code: 'mercadopago',
|
|
77
|
-
link: 'https://www.mercadopago.com.br',
|
|
78
|
-
name: 'Mercado Pago',
|
|
79
|
-
};
|
|
80
|
-
const listPaymentMethods = ['banking_billet', 'credit_card'];
|
|
81
|
-
if (config.account_deposit && config.account_deposit.key_pix) {
|
|
82
|
-
// pix Configured
|
|
83
|
-
listPaymentMethods.push('account_deposit');
|
|
84
|
-
}
|
|
85
|
-
listPaymentMethods.forEach((paymentMethod) => {
|
|
86
|
-
const isCreditCard = paymentMethod === 'credit_card';
|
|
87
|
-
const methodConfig = isCreditCard ? config : config[paymentMethod];
|
|
88
|
-
const minAmount = methodConfig.min_amount || 0;
|
|
89
|
-
const methodEnable = methodConfig.enable || (isCreditCard && !methodConfig.disable);
|
|
90
|
-
if (methodConfig && methodEnable && (amount.total === undefined || amount.total >= minAmount)) {
|
|
91
|
-
let label;
|
|
92
|
-
if (methodConfig.label) {
|
|
93
|
-
label = methodConfig.label;
|
|
94
|
-
} else if (isCreditCard) {
|
|
95
|
-
label = 'Cartão de crédito';
|
|
96
|
-
} else {
|
|
97
|
-
label = paymentMethod === 'account_deposit' ? 'Pix' : 'Boleto bancário';
|
|
98
|
-
}
|
|
99
|
-
const gateway = {
|
|
100
|
-
label,
|
|
101
|
-
icon: methodConfig.icon,
|
|
102
|
-
text: methodConfig.text,
|
|
103
|
-
payment_method: {
|
|
104
|
-
code: paymentMethod,
|
|
105
|
-
name: `${label} - ${intermediator.name}`,
|
|
106
|
-
},
|
|
107
|
-
intermediator,
|
|
108
|
-
type: 'payment',
|
|
109
|
-
};
|
|
110
|
-
if (isCreditCard) {
|
|
111
|
-
if (!gateway.icon) {
|
|
112
|
-
// gateway.icon = `${baseUri}/checkout-stamp.png`; // TODO: baseUri
|
|
113
|
-
gateway.icon = 'https://us-central1-mercadopago-ecom.cloudfunctions.net/app/checkout-stamp.png';
|
|
114
|
-
}
|
|
115
|
-
gateway.js_client = {
|
|
116
|
-
script_uri: 'https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js',
|
|
117
|
-
onload_expression: `window.Mercadopago.setPublishableKey("${config.mp_public_key}");${fs.readFileSync(path.join(__dirname, '../../assets/onload-expression.min.js'), 'utf8')}`,
|
|
118
|
-
cc_brand: {
|
|
119
|
-
function: '_mpBrand',
|
|
120
|
-
is_promise: true,
|
|
121
|
-
},
|
|
122
|
-
cc_hash: {
|
|
123
|
-
function: '_mpHash',
|
|
124
|
-
is_promise: true,
|
|
125
|
-
},
|
|
126
|
-
cc_installments: {
|
|
127
|
-
function: '_mpInstallments',
|
|
128
|
-
is_promise: true,
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
// default configured default installments option
|
|
132
|
-
const installmentsOption = config.installments_option;
|
|
133
|
-
if (installmentsOption && installmentsOption.max_number) {
|
|
134
|
-
response.installments_option = installmentsOption;
|
|
135
|
-
const minInstallment = installmentsOption.min_installment;
|
|
136
|
-
// optional configured installments list
|
|
137
|
-
if (amount.total && Array.isArray(config.installments) && config.installments.length) {
|
|
138
|
-
gateway.installment_options = [];
|
|
139
|
-
config.installments.forEach(({ number, interest }) => {
|
|
140
|
-
if (number >= 2) {
|
|
141
|
-
const value = amount.total / number;
|
|
142
|
-
if (gateway.installment_options && value >= minInstallment) {
|
|
143
|
-
gateway.installment_options.push({
|
|
144
|
-
number,
|
|
145
|
-
value: interest > 0 ? (value + value * interest) / 100 : value,
|
|
146
|
-
tax: Boolean(interest),
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
// check available discount by payment method
|
|
155
|
-
if (discount && discount.value && discount[paymentMethod] !== false) {
|
|
156
|
-
gateway.discount = {
|
|
157
|
-
apply_at: 'total',
|
|
158
|
-
type: 'fixed',
|
|
159
|
-
value: 0,
|
|
160
|
-
};
|
|
161
|
-
['apply_at', 'type', 'value'].forEach((field) => {
|
|
162
|
-
if (gateway.discount && discount[field]) {
|
|
163
|
-
gateway.discount[field] = discount[field];
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
if (response.discount_option && !response.discount_option.label) {
|
|
167
|
-
response.discount_option.label = label;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
response.payment_gateways.push(gateway);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
return response;
|
|
174
|
-
};
|
|
175
|
-
// # sourceMappingURL=mp-list-payments.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mp-list-payments.js","sourceRoot":"","sources":["../src/mp-list-payments.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,GAAG,MAAM,KAAK,CAAC;AAKtB,MAAM,SAAS,GAAG,GAAG,CAAC,aAAa,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAEnE,eAAe,CAAC,IAAmB,EAAE,EAAE;IACrC,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;IAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,MAA4B,CAAC;IACjD,4EAA4E;IAC5E,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACrD,2CAA2C;IAE3C,MAAM,MAAM,GAAG;QACb,GAAG,WAAW,CAAC,IAAI;QACnB,GAAG,WAAW,CAAC,WAAW;KAC3B,CAAC;IAEF,IAAI,CAAC,MAAM,CAAC,aAAa,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE;QACpD,gDAAgD;Q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AE,MAAM,EAAE,OAAO,CAAC,CAAC,OAAO,CACnC,CAAC,KAAK,EAAE,EAAE;oBACR,IAAI,OAAO,CAAC,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE;wBACvC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;qBAC3C;gBACH,CAAC,CACF,CAAC;gBACF,IAAI,QAAQ,CAAC,eAAe,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,KAAK,EAAE;oBAC/D,QAAQ,CAAC,eAAe,CAAC,KAAK,GAAG,KAAK,CAAC;iBACxC;aACF;YAED,QAAQ,CAAC,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACzC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
|
package/lib/mp-webhook.d.ts
DELETED
package/lib/mp-webhook.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import '@cloudcommerce/firebase/lib/init';
|
|
3
|
-
import { logger } from 'firebase-functions';
|
|
4
|
-
import axios from 'axios';
|
|
5
|
-
import api from '@cloudcommerce/api';
|
|
6
|
-
// eslint-disable-next-line import/no-unresolved
|
|
7
|
-
import { getFirestore } from 'firebase-admin/firestore';
|
|
8
|
-
// eslint-disable-next-line import/no-unresolved
|
|
9
|
-
import * as functions from 'firebase-functions/v1';
|
|
10
|
-
import config from '@cloudcommerce/firebase/lib/config';
|
|
11
|
-
import { parsePaymentStatus } from './mp-create-transaction.js';
|
|
12
|
-
|
|
13
|
-
const ECHO_SKIP = 'SKIP';
|
|
14
|
-
let ECHO_SUCCESS = 'OK';
|
|
15
|
-
|
|
16
|
-
export const mercadopago = {
|
|
17
|
-
webhook: functions
|
|
18
|
-
.region(config.get().httpsFunctionOptions.region)
|
|
19
|
-
.https.onRequest(async (req, res) => {
|
|
20
|
-
const { method } = req;
|
|
21
|
-
if (method === 'POST') {
|
|
22
|
-
const { body } = req;
|
|
23
|
-
logger.log('>> Webhook MP #', JSON.stringify(body), ' <<');
|
|
24
|
-
try {
|
|
25
|
-
const app = (await api.get('applications?app_id=111223&fields=hidden_data')).data.result;
|
|
26
|
-
const accessTokenMp = app[0].hidden_data?.mp_access_token;
|
|
27
|
-
if (accessTokenMp) {
|
|
28
|
-
const notification = req.body;
|
|
29
|
-
if (notification.type !== 'payment' || !notification.data || !notification.data.id) {
|
|
30
|
-
res.status(404).send(ECHO_SKIP);
|
|
31
|
-
}
|
|
32
|
-
// setTimeout(() => {
|
|
33
|
-
logger.log('> MP Notification for Payment #', notification.data.id);
|
|
34
|
-
const docRef = getFirestore().collection('mp_payments')
|
|
35
|
-
.doc(String(notification.data.id));
|
|
36
|
-
docRef.get()
|
|
37
|
-
.then(async (doc) => {
|
|
38
|
-
if (doc.exists) {
|
|
39
|
-
const data = doc.data();
|
|
40
|
-
const orderId = data?.order_id;
|
|
41
|
-
const order = (await api.get(`orders/${orderId}`)).data;
|
|
42
|
-
logger.log('>order ', JSON.stringify(order), '<');
|
|
43
|
-
if (order && order.transactions) {
|
|
44
|
-
const payment = (await axios.get(`https://api.mercadopago.com/v1/payments/${notification.data.id}`, {
|
|
45
|
-
headers: {
|
|
46
|
-
Authorization: `Bearer ${accessTokenMp}`,
|
|
47
|
-
'Content-Type': 'application/json',
|
|
48
|
-
},
|
|
49
|
-
})).data;
|
|
50
|
-
logger.log('>payment ', JSON.stringify(payment), ' <');
|
|
51
|
-
const methodPayment = payment.payment_method_id;
|
|
52
|
-
const transaction = order.transactions.find(({ intermediator }) => {
|
|
53
|
-
return intermediator
|
|
54
|
-
&& intermediator.transaction_code === notification.data.id;
|
|
55
|
-
});
|
|
56
|
-
const status = parsePaymentStatus(payment.status);
|
|
57
|
-
if (transaction) {
|
|
58
|
-
const bodyPaymentHistory = {
|
|
59
|
-
transaction_id: transaction._id,
|
|
60
|
-
date_time: new Date().toISOString(),
|
|
61
|
-
status,
|
|
62
|
-
notification_code: String(notification.id),
|
|
63
|
-
flags: [
|
|
64
|
-
'mercadopago',
|
|
65
|
-
],
|
|
66
|
-
}; // TODO: incompatible type=> amount and status
|
|
67
|
-
if (status !== order.financial_status?.current) {
|
|
68
|
-
// avoid unnecessary API request
|
|
69
|
-
await api.post(`orders/${orderId}/payments_history`, bodyPaymentHistory);
|
|
70
|
-
const updatedAt = new Date().toISOString();
|
|
71
|
-
docRef.set({ status, updatedAt }, { merge: true }).catch(logger.error);
|
|
72
|
-
}
|
|
73
|
-
if ((status === 'paid' && methodPayment === 'pix' && transaction)) {
|
|
74
|
-
let { notes } = transaction;
|
|
75
|
-
notes = notes?.replace('display:block', 'display:none'); // disable QR Code
|
|
76
|
-
notes = `${notes} # PIX Aprovado`;
|
|
77
|
-
// orders/${order._id}/transactions/${transactionId}.json { notes }
|
|
78
|
-
// Update to disable QR Code
|
|
79
|
-
try {
|
|
80
|
-
await api.patch(`orders/${order._id}/transactions/${transaction._id}`, { notes });
|
|
81
|
-
ECHO_SUCCESS = 'SUCCESS';
|
|
82
|
-
} catch (e) {
|
|
83
|
-
logger.error(e);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
res.status(200).send(ECHO_SUCCESS);
|
|
87
|
-
} else {
|
|
88
|
-
// transaction not found
|
|
89
|
-
logger.log('> Transaction not found #', notification.data.id);
|
|
90
|
-
res.sendStatus(404);
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
// order or order transaction not found
|
|
94
|
-
logger.log('> Order Not Found #', orderId);
|
|
95
|
-
res.sendStatus(404);
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
logger.log('> Payment not found in Firestore #', notification.data.id);
|
|
99
|
-
res.sendStatus(404);
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
.catch((err) => {
|
|
103
|
-
logger.error(err);
|
|
104
|
-
res.sendStatus(503);
|
|
105
|
-
});
|
|
106
|
-
// }, 3000);
|
|
107
|
-
} else {
|
|
108
|
-
res.sendStatus(406);
|
|
109
|
-
}
|
|
110
|
-
} catch (e) {
|
|
111
|
-
logger.error(e);
|
|
112
|
-
res.sendStatus(500);
|
|
113
|
-
}
|
|
114
|
-
} else {
|
|
115
|
-
res.sendStatus(405);
|
|
116
|
-
}
|
|
117
|
-
}),
|
|
118
|
-
};
|
|
119
|
-
// # sourceMappingURL=mp-webhook.js.map
|
package/lib/mp-webhook.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mp-webhook.js","sourceRoot":"","sources":["../src/mp-webhook.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,kCAAkC,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,gDAAgD;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,gDAAgD;AAChD,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AACnD,OAAO,MAAM,MAAM,oCAAoC,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,SAAS,GAAG,MAAM,CAAC;AACzB,IAAI,YAAY,GAAG,IAAI,CAAC;AAExB,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,OAAO,EAAE,SAAS;SACf,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,MAAM,CAAC;SAChD,KAAK,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QAClC,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;QACvB,IAAI,MAAM,KAAK,MAAM,EAAE;YACrB,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC;YACrB,MAAM,CAAC,GAAG,CAAC,iBAAiB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