@cloudcommerce/app-galaxpay 2.9.0 → 2.10.0
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/lib/functions-lib/all-parses.js.map +1 -1
- package/lib/functions-lib/ecom/events-to-galaxpay.js.map +1 -1
- package/lib/functions-lib/galaxpay/auth/create-access.js.map +1 -1
- package/lib/functions-lib/galaxpay/auth/create-axios.js.map +1 -1
- package/lib/functions-lib/galaxpay/auth/gerate-token.js.map +1 -1
- package/lib/functions-lib/galaxpay/handle-plans.js.map +1 -1
- package/lib/functions-lib/galaxpay/update-subscription.js.map +1 -1
- package/lib/functions-lib/galaxpay/webhook.d.ts +1 -0
- package/lib/functions-lib/galaxpay/webhook.js.map +1 -1
- package/lib/galaxpay-create-transaction.js +2 -2
- package/lib/galaxpay-create-transaction.js.map +1 -1
- package/lib/galaxpay-events.js.map +1 -1
- package/lib/galaxpay-list-payments.js +1 -1
- package/lib/galaxpay-list-payments.js.map +1 -1
- package/package.json +12 -6
- package/.turbo/turbo-build.log +0 -5
- package/CHANGELOG.md +0 -1
- package/assets/onload-expression.js +0 -23
- package/assets/onload-expression.min.js +0 -1
- package/scripts/build.sh +0 -4
- package/src/functions-lib/all-parses.ts +0 -91
- package/src/functions-lib/ecom/events-to-galaxpay.ts +0 -104
- package/src/functions-lib/galaxpay/auth/create-access.ts +0 -80
- package/src/functions-lib/galaxpay/auth/create-axios.ts +0 -21
- package/src/functions-lib/galaxpay/auth/gerate-token.ts +0 -36
- package/src/functions-lib/galaxpay/handle-plans.ts +0 -91
- package/src/functions-lib/galaxpay/update-subscription.ts +0 -92
- package/src/functions-lib/galaxpay/webhook.ts +0 -484
- package/src/functions-lib/utils.ts +0 -23
- package/src/galaxpay-create-transaction.ts +0 -257
- package/src/galaxpay-events.ts +0 -28
- package/src/galaxpay-list-payments.ts +0 -148
- package/src/galaxpay.ts +0 -12
- package/src/index.ts +0 -2
- package/tsconfig.json +0 -6
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
// import logger from 'firebase-functions/logger';
|
|
3
|
-
|
|
4
|
-
export default (accessToken: string | undefined | null, isSandbox?: boolean) => {
|
|
5
|
-
// https://docs.galaxpay.com.br/autenticacao
|
|
6
|
-
// https://docs.galaxpay.com.br/auth/token
|
|
7
|
-
|
|
8
|
-
const headers = {
|
|
9
|
-
'Content-Type': 'application/json',
|
|
10
|
-
};
|
|
11
|
-
if (accessToken) {
|
|
12
|
-
// logger.log('>(App GalaxPay) token: ', accessToken);
|
|
13
|
-
Object.assign(headers, { Authorization: `Bearer ${accessToken}` });
|
|
14
|
-
Object.assign(headers, { 'Accept-Encoding': 'gzip,deflate,compress' });
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return axios.create({
|
|
18
|
-
baseURL: `https://api.${isSandbox ? 'sandbox.cloud.' : ''}galaxpay.com.br/v2`,
|
|
19
|
-
headers,
|
|
20
|
-
});
|
|
21
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
// import logger from 'firebase-functions/logger';
|
|
2
|
-
import Axios from './create-axios';
|
|
3
|
-
|
|
4
|
-
const gereteToken = (
|
|
5
|
-
hashLogin: string,
|
|
6
|
-
isSandbox?: boolean,
|
|
7
|
-
galaxpayPartnerHash?: string,
|
|
8
|
-
) => new Promise((resolve, reject) => {
|
|
9
|
-
// https://docs.galaxpay.com.br/autenticacao
|
|
10
|
-
// https://docs.galaxpay.com.br/auth/token
|
|
11
|
-
|
|
12
|
-
const axios = Axios(null, isSandbox);
|
|
13
|
-
const request = (isRetry?: boolean) => {
|
|
14
|
-
const headers = { Authorization: `Basic ${hashLogin}` };
|
|
15
|
-
if (!isSandbox && galaxpayPartnerHash) {
|
|
16
|
-
// logger.log('#AuthorizationPartner ');
|
|
17
|
-
Object.assign(headers, { AuthorizationPartner: galaxpayPartnerHash });
|
|
18
|
-
}
|
|
19
|
-
axios.post('/token', {
|
|
20
|
-
grant_type: 'authorization_code',
|
|
21
|
-
scope: 'customers.read customers.write plans.read plans.write transactions.read transactions.write webhooks.write cards.read cards.write card-brands.read subscriptions.read subscriptions.write charges.read charges.write boletos.read',
|
|
22
|
-
}, { headers })
|
|
23
|
-
.then(({ data }) => {
|
|
24
|
-
resolve(data.access_token);
|
|
25
|
-
})
|
|
26
|
-
.catch((err) => {
|
|
27
|
-
if (!isRetry && err.response && err.response.status >= 429) {
|
|
28
|
-
setTimeout(() => request(true), 700);
|
|
29
|
-
}
|
|
30
|
-
reject(err);
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
request();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
export default gereteToken;
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { GalaxpayApp } from '../../../types/config-app';
|
|
2
|
-
import type { ListPaymentsParams } from '@cloudcommerce/types';
|
|
3
|
-
|
|
4
|
-
// type Gateway = ListPaymentsResponse['payment_gateways'][number]
|
|
5
|
-
|
|
6
|
-
const handleGateway = (appData: GalaxpayApp) => {
|
|
7
|
-
const plans: Exclude<GalaxpayApp['plans'], undefined> = [];
|
|
8
|
-
if (appData.plans) {
|
|
9
|
-
// Newer versions of the app will have a list of plans
|
|
10
|
-
appData.plans.forEach((plan) => {
|
|
11
|
-
plans.push(plan);
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return plans;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// for create-transaction
|
|
19
|
-
const findPlanToCreateTransction = (label: string | undefined, appData: GalaxpayApp) => {
|
|
20
|
-
let sendPlan: Exclude<GalaxpayApp['plans'], undefined>[number] | undefined;
|
|
21
|
-
if (appData.plans) {
|
|
22
|
-
/*
|
|
23
|
-
More recent versions of the application will have a list of plans,
|
|
24
|
-
where it will be necessary to find the plan by name,
|
|
25
|
-
and return it since it will be necessary to use the periodicity and quantity property
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
// find plan by name (label)
|
|
29
|
-
appData.plans.forEach((plan) => {
|
|
30
|
-
// if the name of the plan is blank, on the list-payments side it is set to 'Plano'
|
|
31
|
-
let planLabel = plan.label || 'Plano';
|
|
32
|
-
planLabel = `${planLabel} ${plan.periodicity}`;
|
|
33
|
-
label = label?.trim();
|
|
34
|
-
if (label === planLabel) {
|
|
35
|
-
sendPlan = plan;
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
return sendPlan;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const discountPlan = (
|
|
43
|
-
planDiscount: Exclude<GalaxpayApp['plans'], undefined>[number]['discount'],
|
|
44
|
-
amount: Exclude<ListPaymentsParams['amount'], undefined>,
|
|
45
|
-
) => {
|
|
46
|
-
if (planDiscount && planDiscount.value > 0) {
|
|
47
|
-
// default discount option
|
|
48
|
-
const discountOption = {
|
|
49
|
-
value: planDiscount.value,
|
|
50
|
-
apply_at: (planDiscount.apply_at === 'frete' ? 'freight' : planDiscount) as 'total' | 'subtotal' | 'freight',
|
|
51
|
-
type: planDiscount.percentage ? 'percentage' : 'fixed' as 'percentage' | 'fixed' | undefined,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
if (amount.total) {
|
|
55
|
-
// check amount value to apply discount
|
|
56
|
-
if (planDiscount.min_amount && amount.total < planDiscount.min_amount) {
|
|
57
|
-
planDiscount.value = 0;
|
|
58
|
-
} else {
|
|
59
|
-
delete planDiscount.min_amount;
|
|
60
|
-
|
|
61
|
-
const maxDiscount = amount[discountOption.apply_at || 'subtotal'];
|
|
62
|
-
let discountValue: number | undefined;
|
|
63
|
-
|
|
64
|
-
if (maxDiscount && discountOption.type === 'percentage') {
|
|
65
|
-
discountValue = (maxDiscount * planDiscount.value) / 100;
|
|
66
|
-
} else {
|
|
67
|
-
discountValue = planDiscount.value;
|
|
68
|
-
if (maxDiscount && discountValue > maxDiscount) {
|
|
69
|
-
discountValue = maxDiscount;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (discountValue && discountValue > 0) {
|
|
74
|
-
amount.discount = (amount.discount || 0) + discountValue;
|
|
75
|
-
amount.total -= discountValue;
|
|
76
|
-
if (amount.total < 0) {
|
|
77
|
-
amount.total = 0;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return discountOption;
|
|
83
|
-
}
|
|
84
|
-
return null;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export {
|
|
88
|
-
handleGateway,
|
|
89
|
-
findPlanToCreateTransction,
|
|
90
|
-
discountPlan,
|
|
91
|
-
};
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import type { Orders, Applications } from '@cloudcommerce/types';
|
|
2
|
-
import logger from 'firebase-functions/logger';
|
|
3
|
-
import GalaxpayAxios from './auth/create-access';
|
|
4
|
-
|
|
5
|
-
const checkAmountItemsOrder = (
|
|
6
|
-
amount: Orders['amount'],
|
|
7
|
-
items: Exclude<Orders['items'], undefined>,
|
|
8
|
-
plan: { [x: string]: any },
|
|
9
|
-
) => {
|
|
10
|
-
let subtotal = 0;
|
|
11
|
-
let item: Exclude<Orders['items'], undefined>[number];
|
|
12
|
-
for (let i = 0; i < items.length; i++) {
|
|
13
|
-
item = items[i];
|
|
14
|
-
if (item.flags && (item.flags.includes('freebie') || item.flags.includes('discount-set-free'))) {
|
|
15
|
-
items.splice(i, 1);
|
|
16
|
-
} else {
|
|
17
|
-
subtotal += item.quantity * (item.final_price || item.price);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
amount.subtotal = subtotal;
|
|
21
|
-
amount.total = amount.subtotal + (amount.tax || 0) + (amount.freight || 0) + (amount.extra || 0);
|
|
22
|
-
let planDiscount;
|
|
23
|
-
if (plan && plan.discount) {
|
|
24
|
-
if (plan.discount.percentage) {
|
|
25
|
-
planDiscount = amount[plan.discount.apply_at];
|
|
26
|
-
planDiscount *= ((plan.discount.value) / 100);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
// if the plan doesn't exist, because it's subscription before the update
|
|
30
|
-
if (plan) {
|
|
31
|
-
amount.discount = (plan.discount && !plan.discount.percentage
|
|
32
|
-
? plan.discount.value : planDiscount) || 0;
|
|
33
|
-
}
|
|
34
|
-
if (amount.discount) {
|
|
35
|
-
amount.total -= amount.discount;
|
|
36
|
-
}
|
|
37
|
-
const total: any = amount.total - (amount.discount || 0); // BUG :(
|
|
38
|
-
return Math.floor(total.toFixed(2) * 100);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const updateValueSubscription = (
|
|
42
|
-
appData: Applications,
|
|
43
|
-
subscriptionId: string,
|
|
44
|
-
amount: Orders['amount'],
|
|
45
|
-
items: Exclude<Orders['items'], undefined>,
|
|
46
|
-
plan: { [x: string]: any },
|
|
47
|
-
// GalaxPaySubscription: { [x: string]: any },
|
|
48
|
-
) => {
|
|
49
|
-
const value = checkAmountItemsOrder({ ...amount }, [...items], { ...plan });
|
|
50
|
-
|
|
51
|
-
if (!process.env.GALAXPAY_ID) {
|
|
52
|
-
const galaxpayId = appData.hidden_data?.galaxpay_id;
|
|
53
|
-
if (typeof galaxpayId === 'string' && galaxpayId) {
|
|
54
|
-
process.env.GALAXPAY_ID = galaxpayId;
|
|
55
|
-
} else {
|
|
56
|
-
logger.warn('Missing GalaxPay ID');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!process.env.GALAXPAY_HASH) {
|
|
61
|
-
const galaxpayHash = appData.hidden_data?.galaxpay_hash;
|
|
62
|
-
if (typeof galaxpayHash === 'string' && galaxpayHash) {
|
|
63
|
-
process.env.GALAXPAY_HASH = galaxpayHash;
|
|
64
|
-
} else {
|
|
65
|
-
logger.warn('Missing GalaxPay ID');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return new Promise((resolve, reject) => {
|
|
70
|
-
const galaxpayAxios = new GalaxpayAxios({
|
|
71
|
-
galaxpayId: process.env.GALAXPAY_ID,
|
|
72
|
-
galaxpayHash: process.env.GALAXPAY_HASH,
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
galaxpayAxios.preparing
|
|
76
|
-
.then(async () => {
|
|
77
|
-
if (galaxpayAxios.axios) {
|
|
78
|
-
const { data } = await galaxpayAxios.axios.put(`subscriptions/${subscriptionId}/myId`, { value });
|
|
79
|
-
if (data.type) {
|
|
80
|
-
resolve(true);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}).catch((err) => {
|
|
84
|
-
reject(err);
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export {
|
|
90
|
-
updateValueSubscription,
|
|
91
|
-
checkAmountItemsOrder,
|
|
92
|
-
};
|