@opengis/pay 1.1.0 → 1.1.2

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.
@@ -1,13 +1,13 @@
1
1
  import request from 'request-promise';
2
2
 
3
3
  import {
4
- config, pgClients, logger, dataInsert,
4
+ config, pgClients, logger, dataInsert, dataUpdate,
5
5
  } from '@opengis/fastify-table/utils.js';
6
6
 
7
7
  /**
8
8
  * Апі перенаправлює до стороннього сервісу оплати карткою онлайн
9
9
  *
10
- * @method GET|POST
10
+ * @method GET
11
11
  * @alias PortmonePaymentRedirect
12
12
  * @summary redirect на сервіс оплати карткою
13
13
  * @type api
@@ -22,7 +22,7 @@ import {
22
22
 
23
23
  export default async function PortmoneRedirect(req, reply) {
24
24
  const {
25
- pg = pgClients.client, user = {}, query = {}, headers = {}, params = {}, body = {}, unittest,
25
+ pg = pgClients.client, user = {}, query = {}, headers = {},
26
26
  } = req;
27
27
 
28
28
  if (!user?.uid) {
@@ -30,6 +30,7 @@ export default async function PortmoneRedirect(req, reply) {
30
30
  }
31
31
 
32
32
  const { host, payeeId } = config.integration?.portmone || {};
33
+ const domain = `${req.protocol}://${req.hostname}`;
33
34
 
34
35
  if (!host) {
35
36
  return { message: 'empty integration host', status: 400 };
@@ -39,25 +40,42 @@ export default async function PortmoneRedirect(req, reply) {
39
40
  return { message: 'empty integration payeeId', status: 400 };
40
41
  }
41
42
 
42
- const data = params?.id ? await pg.query('select account_id, payment_amount from billing.payments where payment_id=$1', [params.id]).then(el => el.rows?.[0] || {}) : {};
43
+ const data = query.id ? await pg.query('select payment_id, account_id, payment_amount, payment_status from billing.payments where payment_id=$1', [query.id])
44
+ .then(el => el.rows?.[0] || {}) : {};
45
+
46
+ if (data.payment_status && data.payment_status === 'success') {
47
+ return { message: 'already processed', status: 400 };
48
+ }
43
49
 
44
- const paymentAmount = data?.payment_amount || body?.price || query?.price;
50
+ const paymentAmount = data.payment_amount || query.price;
45
51
 
46
52
  if (!paymentAmount) {
47
53
  return { message: 'price is required', status: 400 };
48
54
  }
49
55
 
50
- const { account_id: accountId } = params?.id
51
- ? { account_id: data?.account_id }
52
- : await pg.query('select account_id from billing.account_user where user_id=$1 limit 1', [user.uid])
53
- .then(el => el.rows?.[0] || {});
56
+ const accountId = data.account_id
57
+ || await pg.query('select account_id from billing.account_user where user_id=$1 limit 1', [user.uid])
58
+ .then(el => el.rows?.[0]?.account_id);
54
59
 
55
60
  if (!accountId) {
56
61
  return { message: 'account_id is required', status: 400 };
57
62
  }
58
63
 
59
- const { payment_id: paymentId } = params?.id
60
- ? { payment_id: params?.id }
64
+ if (!headers?.referer || !headers.referer.includes(domain)) {
65
+ logger.file('payments/warn', { message: 'invalid referer', referer: headers?.referer, uid: user.uid });
66
+ return { message: 'access restricted', status: 403 };
67
+ }
68
+
69
+ const { payment_id: paymentId } = data.payment_id
70
+ ? await dataUpdate({
71
+ table: 'billing.payments',
72
+ id: data.payment_id,
73
+ data: {
74
+ referer_url: headers?.referer,
75
+ payment_status: 'inprogress',
76
+ },
77
+ uid: user.uid,
78
+ })
61
79
  : await dataInsert({
62
80
  table: 'billing.payments',
63
81
  data: {
@@ -70,10 +88,9 @@ export default async function PortmoneRedirect(req, reply) {
70
88
  }).then(el => el.rows?.[0] || {});
71
89
 
72
90
  if (!paymentId) {
73
- return { message: 'Не вірне замовлення', status: 400 };
91
+ return { message: 'payment not found', status: 404 };
74
92
  }
75
93
 
76
- const domain = `${req.protocol}://${req.hostname}`;
77
94
  const formData = {
78
95
  payee_id: payeeId,
79
96
  description: `Послуга - поповнення рахунку на суму ${paymentAmount} грн`,
@@ -1,6 +1,5 @@
1
1
  import {
2
- config, logger, pgClients, getSelectVal,
3
- dataUpdate,
2
+ config, logger, pgClients, getSelectVal, dataUpdate, getTemplate,
4
3
  } from '@opengis/fastify-table/utils.js';
5
4
 
6
5
  import { sendNotification } from '@opengis/admin/utils.js';
@@ -68,14 +67,23 @@ export default async function portmoneStatus(req, reply) {
68
67
 
69
68
  // request portmone trx info to recheck request data is valid?
70
69
  if (!config.debug && !origin?.startsWith(host)) {
71
- logger.file('integrations/portmone/status/warn', {
72
- origin, uid: user.uid, refill, message: 'invalid referer',
70
+ logger.file('payments/warn', {
71
+ type: 'status',
72
+ origin,
73
+ uid: user.uid,
74
+ refill,
75
+ message: 'invalid referer',
73
76
  });
74
77
  return { message: 'access restricted', status: 403 };
75
78
  }
76
79
 
77
- logger.file('integrations/portmone/status/debug', {
78
- origin, uid: user.uid, refill, body, params,
80
+ logger.file('payments', {
81
+ type: 'status',
82
+ origin,
83
+ uid: user.uid,
84
+ refill,
85
+ body,
86
+ params,
79
87
  });
80
88
 
81
89
  // last trx contains total balance
@@ -111,16 +119,18 @@ export default async function portmoneStatus(req, reply) {
111
119
 
112
120
  Object.assign(data, {
113
121
  domain: req.hostname,
122
+ total_balance: +(refill || 0) + (+totalBalance || 0),
114
123
  account_id_text: accountId ? cls2?.[accountId] || accountId : 'Інформація відсутня',
115
124
  payment_status_text: trxStatus ? cls3?.[trxStatus] || trxStatus : 'Інформація відсутня',
116
125
  });
117
126
 
118
127
  if (to) {
128
+ const pt = await getTemplate('pt', 'marketplace-service-payment-client-template');
119
129
  const options = {
120
130
  pg,
121
131
  to,
122
132
  title: `Поповнення рахунку на порталі ${req.hostname}`,
123
- template: 'marketplace-service-payment-client-template',
133
+ template: pt?.html || pt,
124
134
  data,
125
135
  nocache: config?.local,
126
136
  };