@opengis/pay 2.0.4 → 2.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/pay",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "type": "module",
5
5
  "description": "pay plugins for billing",
6
6
  "main": "plugin.js",
@@ -17,7 +17,7 @@ export default async function createTransaction({
17
17
  }, pg = pgClients.client) {
18
18
  if (!service) throw new Error('not enough params: service');
19
19
  if (!controller[service]) { throw new Error('invalid params: service'); }
20
- if (amount && !uid) throw new Error('not enough params: uid');
20
+ if (!amount || !uid || !referer) throw new Error('not enough params: uid / amount / referer');
21
21
 
22
22
  if (currency && !['uah', 'usd', 'eur'].includes(currency)) {
23
23
  throw new Error('invalid params: currency');
@@ -8,7 +8,7 @@ import dataUpdate from '../crud/dataUpdate.js';
8
8
  import createPayment from './createPayment.js';
9
9
 
10
10
  const { prefix = '/api' } = config;
11
- const { table = 'pay.transactions', backUrl: bUrl } = config.integrations?.pay || {};
11
+ const { table = 'pay.transactions', backUrl } = config.integrations?.pay || {};
12
12
 
13
13
  const {
14
14
  host = 'https://www.liqpay.ua',
@@ -20,7 +20,11 @@ const liqpay = new LiqPay(publicKey, privateKey);
20
20
  export default async function liqpayCreateTransaction({
21
21
  id, orderId, amount, referer, uid, debug, expiredDate, description, paytypes, action = 'pay', currency = 'uah', language = 'uk', deliveryEmails,
22
22
  }, pg = pgClients.client) {
23
- const backUrl = bUrl || referer?.slice?.(0, referer.indexOf('/', 7));
23
+ if (!referer) {
24
+ throw new Error('not enough params: referer required');
25
+ }
26
+
27
+ const rootUrl = referer.slice(0, referer.indexOf('/', 7));
24
28
 
25
29
  if (!publicKey || !privateKey) {
26
30
  throw new Error('invalid liqpay integration settings');
@@ -78,6 +82,8 @@ export default async function liqpayCreateTransaction({
78
82
  return paymentData.redirect_url;
79
83
  }
80
84
 
85
+ const resultUrl = `${rootUrl}${backUrl ? backUrl.replace(/{{service}}/, 'liqpay').replace(/{{id}}/, paymentData.id) : `${prefix}/liqpay/${paymentData.id}/success`}`;
86
+
81
87
  const paymentOptions = {
82
88
  action,
83
89
  amount: paymentAmount,
@@ -88,8 +94,8 @@ export default async function liqpayCreateTransaction({
88
94
  language,
89
95
  paytypes,
90
96
  expired_date: expiredDate && new Date(expiredDate) ? new Date(expiredDate).toISOString().split('.')[0].replace('T', ' ') : undefined,
91
- // server_url: `${backUrl}${prefix}/liqpay/${paymentData.id}/success`, // update payment info - api url, does not work on sdk package out of the box
92
- result_url: `${backUrl}${prefix}/liqpay/${paymentData.id}/success`, // redirect user after payment - page url
97
+ // server_url: resultUrl, // update payment info - api url, does not work on sdk package out of the box
98
+ result_url: resultUrl, // redirect user after payment - page url
93
99
  rro_info: Array.isArray(deliveryEmails) && deliveryEmails.length ? {
94
100
  delivery_emails: deliveryEmails,
95
101
  } : undefined,
@@ -5,7 +5,7 @@ import dataUpdate from '../crud/dataUpdate.js';
5
5
  import createPayment from './createPayment.js';
6
6
 
7
7
  const { prefix = '/api' } = config;
8
- const { table = 'pay.transactions', backUrl: bUrl } = config.integrations?.pay || {};
8
+ const { table = 'pay.transactions', backUrl } = config.integrations?.pay || {};
9
9
 
10
10
  const {
11
11
  host = 'https://api.monobank.ua',
@@ -24,9 +24,13 @@ const ccyObj = {
24
24
  export default async function monopayCreateTransaction({
25
25
  id, orderId, amount, referer, uid, debug, expiredDate, description, action, currency, language,
26
26
  }, pg = pgClients.client) {
27
- const backUrl = bUrl || referer?.slice?.(0, referer.indexOf('/', 7));
27
+ if (!referer) {
28
+ throw new Error('not enough params: referer required');
29
+ }
30
+
31
+ const rootUrl = referer.slice(0, referer.indexOf('/', 7));
28
32
 
29
- if (!host || !token || !backUrl) {
33
+ if (!host || !token) {
30
34
  throw new Error('invalid monopay integration settings');
31
35
  }
32
36
 
@@ -74,7 +78,7 @@ export default async function monopayCreateTransaction({
74
78
  }
75
79
 
76
80
  // redirect to api to check payment status and redirect user to page afterwards
77
- const serverUrl = `${backUrl}${prefix}/monopay/${paymentData.id}/success`;
81
+ const resultUrl = `${rootUrl}${backUrl ? backUrl.replace(/{{service}}/, 'monopay').replace(/{{id}}/, paymentData.id) : `${prefix}/monopay/${paymentData.id}/success`}`;
78
82
 
79
83
  const validity = expiredDate && new Date(expiredDate) ? Math.max(((new Date(expiredDate).setHours(0, 0, 0, 0) - new Date().setHours(0, 0, 0, 0)) / 1000 || 0), 86400) : 86400;
80
84
 
@@ -82,9 +86,9 @@ export default async function monopayCreateTransaction({
82
86
  amount: paymentAmount * 100, // amount=1 === 0.01 uah
83
87
  ccy: ccyObj[currency] || 980, // ISO 4217, 980 = UAH
84
88
  merchantPaymInfo,
85
- redirectUrl: serverUrl,
89
+ redirectUrl: resultUrl,
86
90
  // redirectUrl: (paymentRedirect ? `${rootPageUrl}${paymentRedirect.replace(/{{id}}/g, paymentData.id)}` : headers?.referer) || rootPageUrl,
87
- webHookUrl: serverUrl,
91
+ webHookUrl: resultUrl,
88
92
  validity, // 1 day by default, then invalid
89
93
  paymentType: 'debit',
90
94
  // displayType: 'iframe', // default = null > url to mono
@@ -8,15 +8,19 @@ import createPayment from './createPayment.js';
8
8
 
9
9
  const { prefix = '/api' } = config;
10
10
 
11
- const { table = 'pay.transactions', backUrl: bUrl } = config.integrations?.pay || {};
11
+ const { table = 'pay.transactions', backUrl } = config.integrations?.pay || {};
12
12
  const { host = 'https://www.portmone.com.ua', payeeId } = config.integrations?.portmone || {};
13
13
 
14
14
  export default async function portmoneCreateTransaction({
15
15
  id, orderId, amount, referer, uid, debug, expiredDate, description, action, currency = 'uah', language = 'uk', paytypes,
16
16
  }, pg = pgClients.client) {
17
- const backUrl = bUrl || referer?.slice?.(0, referer.indexOf('/', 7));
17
+ if (!referer) {
18
+ throw new Error('not enough params: referer required');
19
+ }
20
+
21
+ const rootUrl = referer.slice(0, referer.indexOf('/', 7));
18
22
 
19
- if (!payeeId || !backUrl) {
23
+ if (!payeeId) {
20
24
  throw new Error('invalid portmone integration settings');
21
25
  }
22
26
 
@@ -64,6 +68,8 @@ export default async function portmoneCreateTransaction({
64
68
  throw new Error('Transaction date already expired');
65
69
  }
66
70
 
71
+ const resultUrl = `${rootUrl}${backUrl ? backUrl.replace(/{{service}}/, 'portmone').replace(/{{id}}/, paymentData.id) : `${prefix}/portmone/${paymentData.id}/success`}`;
72
+
67
73
  if (id && paymentData?.redirect_url) {
68
74
  return paymentData.redirect_url;
69
75
  }
@@ -74,8 +80,8 @@ export default async function portmoneCreateTransaction({
74
80
  shop_order_number: paymentData.id,
75
81
  bill_amount: paymentAmount,
76
82
  bill_currency: currency.toUpperCase(),
77
- success_url: `${backUrl}${prefix}/portmone/${paymentData.id}/success`,
78
- failure_url: `${backUrl}${prefix}/portmone/${paymentData.id}/error`,
83
+ success_url: resultUrl,
84
+ failure_url: resultUrl.replace(/\/success$/, '/error'),
79
85
  lang: language === 'uk' ? 'ua' : language,
80
86
  };
81
87
 
@@ -37,7 +37,7 @@ export default async function processTransaction(id, { body, origin } = {}) {
37
37
  if (!service) throw new Error('invalid payment: service required');
38
38
  if (!controller[service]) { throw new Error('invalid params: service'); }
39
39
 
40
- if (trxStatus === 'success') {
40
+ if (['error', 'success'].includes(trxStatus)) {
41
41
  throw new Error('Transaction was already processed');
42
42
  }
43
43
 
@@ -46,7 +46,7 @@ export default async function processTransaction(id, { body, origin } = {}) {
46
46
  const { status = 'error', err_description: err } = result || {};
47
47
 
48
48
  if (status !== 'success') {
49
- throw new Error(err || 'Помилка перевірки статусу платежу');
49
+ // throw new Error(err || 'Помилка перевірки статусу платежу');
50
50
  }
51
51
 
52
52
  const balance = await pg.query(`select balance from ${table}
@@ -55,6 +55,9 @@ export default async function processTransaction(id, { body, origin } = {}) {
55
55
  and balance is not null
56
56
  order by created_at desc limit 1`, [userId]).then(el => el.rows?.[0]?.balance || 0);
57
57
 
58
+ const newBalance = status === 'success' ? +result.amount + +balance : balance;
59
+ const successDate = status === 'success' ? newDate() : undefined;
60
+
58
61
  const row = await dataUpdate({
59
62
  table,
60
63
  data: {
@@ -64,16 +67,16 @@ export default async function processTransaction(id, { body, origin } = {}) {
64
67
  paytype: result?.paytype, // card, gpay etc
65
68
  receipt_url: result?.receipt_url,
66
69
  status,
67
- balance: +result.amount + +balance,
68
- success_date: newDate(),
70
+ balance: newBalance,
71
+ success_date: successDate,
69
72
  },
70
73
  id,
71
74
  uid: userId,
72
75
  });
73
76
 
74
77
  if (process.env.NODE_ENV === 'test') {
75
- console.info(`id:${id}, status: ${result.status}, before: ${balance || 0}, sum: ${+result.amount}, after: ${+result.amount + +balance}`);
78
+ console.info(`id:${id}, status: ${result.status}, before: ${balance || 0}, sum: ${+result.amount}, after: ${newBalance}`);
76
79
  }
77
80
 
78
- return { ...row, balance: +result.amount + +balance };
81
+ return { ...row, balance: newBalance };
79
82
  }