@opengis/pay 1.1.14 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/pay",
3
- "version": "1.1.14",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "description": "pay plugins for billing",
6
6
  "main": "plugin.js",
@@ -21,10 +21,11 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@opengis/admin": "^0.2.14",
24
- "@opengis/fastify-table": "^1.1.96",
24
+ "@opengis/fastify-table": "^1.2.64",
25
25
  "cross-env": "^7.0.3",
26
26
  "fastify": "^4.26.1",
27
27
  "fastify-plugin": "^4.0.0",
28
+ "liqpayjs-sdk": "^3.3.1",
28
29
  "request-promise": "^4.2.6"
29
30
  },
30
31
  "devDependencies": {
package/plugin.js CHANGED
@@ -1,11 +1,8 @@
1
1
  import fp from 'fastify-plugin';
2
+ import path from 'node:path';
2
3
 
3
4
  import { config, execMigrations } from '@opengis/fastify-table/utils.js';
4
5
 
5
- import portmoneRedirect from './server/routes/portmone.redirect.js';
6
- import portmoneStatus from './server/routes/portmone.status.js';
7
- import paymentInfo from './server/routes/payment.info.js';
8
-
9
6
  const { prefix = '/api' } = config;
10
7
 
11
8
  /**
@@ -16,35 +13,10 @@ const { prefix = '/api' } = config;
16
13
 
17
14
  async function plugin(app) {
18
15
  app.register(import('./server/plugins/cron.js'));
16
+ app.register(import('./server/routes/portmone/index.mjs'), { prefix });
17
+ app.register(import('./server/routes/liqpay/index.mjs'), { prefix });
19
18
 
20
- app.route({
21
- method: 'GET',
22
- url: `${prefix}/portmone-redirect`,
23
- config: {
24
- policy: ['site'],
25
- },
26
- handler: portmoneRedirect,
27
- });
28
-
29
- app.route({
30
- method: 'GET',
31
- url: `${prefix}/payment-info/:id`,
32
- config: {
33
- policy: ['site'],
34
- },
35
- handler: paymentInfo,
36
- });
37
-
38
- app.route({
39
- method: 'POST',
40
- url: `${prefix}/portmone/:id/:status`,
41
- config: {
42
- policy: ['public'],
43
- },
44
- handler: portmoneStatus,
45
- });
46
-
47
- execMigrations().catch(err => console.log(err));
19
+ execMigrations(path.join(process.cwd(), 'server/migrations')).catch(err => console.log(err));
48
20
  }
49
21
 
50
22
  export default fp(plugin);
@@ -11,6 +11,7 @@ alter table billing.payments add column if not exists payment_status text NOT NU
11
11
  alter table billing.payments add column if not exists success_date timestamp with time zone;
12
12
  alter table billing.payments add column if not exists payment_data json;
13
13
  alter table billing.payments add column if not exists payment_amount numeric;
14
+ alter table billing.payments add column if not exists payment_service text;
14
15
  alter table billing.payments add column if not exists total_balance numeric;
15
16
  alter table billing.payments add column if not exists referer_url text;
16
17
  alter table billing.payments add column if not exists cdate timestamp without time zone DEFAULT now();
@@ -24,7 +25,8 @@ COMMENT ON COLUMN billing.payments.payment_id IS 'ID';
24
25
  COMMENT ON COLUMN billing.payments.account_id IS 'ID акаунту клієнта';
25
26
  COMMENT ON COLUMN billing.payments.payment_status IS 'Статус транзації';
26
27
  COMMENT ON COLUMN billing.payments.success_date IS 'Дата успішного завершення транзакції';
27
- COMMENT ON COLUMN billing.payments.payment_data IS 'Дані від portmone';
28
+ COMMENT ON COLUMN billing.payments.payment_data IS 'Дані від сервісу оплати';
29
+ COMMENT ON COLUMN billing.payments.payment_service IS 'Сервіс оплати (portmone, liqpay)';
28
30
  COMMENT ON COLUMN billing.payments.payment_amount IS 'Сума поповнення рахунку';
29
31
  COMMENT ON COLUMN billing.payments.total_balance IS 'Поточний баланс';
30
32
  COMMENT ON COLUMN billing.payments.referer_url IS 'Посилання';
@@ -1,6 +1,6 @@
1
1
  import { addCron, pgClients } from '@opengis/fastify-table/utils.js';
2
2
 
3
- import updateTrxStatus from '../routes/utils/portmone.check.status.js';
3
+ import updateTrxStatus from '../routes/portmone/utils/portmone.check.status.js';
4
4
 
5
5
  async function updateOldTrxsStatus() {
6
6
  const inprogress = pgClients.client.pk?.['billing.payments']
@@ -18,5 +18,5 @@ async function updateOldTrxsStatus() {
18
18
  }
19
19
 
20
20
  export default async function cron() {
21
- // addCron(updateOldTrxsStatus, 60 * 60 * 24);
21
+ // addCron(updateOldTrxsStatus, 60 * 60 * 24);
22
22
  }
@@ -0,0 +1,142 @@
1
+ import request from 'request-promise';
2
+ import LiqPay from 'liqpayjs-sdk';
3
+
4
+ import {
5
+ config, logger, pgClients, dataInsert, dataUpdate, handlebarsSync,
6
+ } from '@opengis/fastify-table/utils.js';
7
+
8
+ const { prefix = '/api' } = config;
9
+
10
+ const { publicKey, privateKey, paymentRedirect } = config.integrations?.liqpay || {};
11
+ const liqpay = new LiqPay(publicKey, privateKey);
12
+
13
+ const host = 'https://www.liqpay.ua';
14
+
15
+ export default async function liqpayRedirect(req, reply) {
16
+ const {
17
+ pg = pgClients.client, query = {}, user = {}, headers = {},
18
+ } = req;
19
+
20
+ if (!publicKey || !privateKey) {
21
+ return reply.status(400).send('invalid liqpay integration settings');
22
+ }
23
+
24
+ const domain = req.hostname.split(':').shift();
25
+
26
+ 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])
27
+ .then(el => el.rows?.[0] || {}) : {};
28
+
29
+ if (data.payment_status && data.payment_status === 'success') {
30
+ return { message: 'already processed', status: 400 };
31
+ }
32
+
33
+ const paymentAmount = data.payment_amount || query.price;
34
+
35
+ if (!paymentAmount) {
36
+ return { message: 'price is required', status: 400 };
37
+ }
38
+
39
+ const accountId = data.account_id
40
+ || (user?.uid ? await pg.query('select account_id from billing.account_user where user_id=$1 limit 1', [user?.uid]).then(el => el.rows?.[0]?.account_id) : null);
41
+
42
+ if (!accountId) {
43
+ return { message: 'account_id is required', status: 400 };
44
+ }
45
+
46
+ const { payment_id: paymentId } = !data.payment_id
47
+ ? await dataInsert({
48
+ table: 'billing.payments',
49
+ data: {
50
+ payment_amount: paymentAmount,
51
+ account_id: accountId,
52
+ payment_service: 'liqpay',
53
+ payment_status: 'inprogress',
54
+ },
55
+ uid: user?.uid || '0',
56
+ }).then(el => el.rows?.[0] || {})
57
+ : { payment_id: data.payment_id };
58
+
59
+ if (!paymentId) {
60
+ return { message: 'payment not found', status: 404 };
61
+ }
62
+
63
+ await dataUpdate({
64
+ table: 'billing.payments',
65
+ id: paymentId,
66
+ data: {
67
+ referer_url: paymentRedirect ? handlebarsSync.compile(paymentRedirect)({ id: paymentId }) : headers?.referer,
68
+ payment_status: 'inprogress',
69
+ },
70
+ uid: user?.uid || '0',
71
+ });
72
+
73
+ const protocol = headers?.referer?.split?.('://')?.shift?.() || req.protocol || 'https';
74
+ const rootPageUrl = `${protocol || 'https'}://${domain}`;
75
+
76
+ const resultUrl = paymentRedirect
77
+ ? `${rootPageUrl}${handlebarsSync.compile(paymentRedirect)({ id: paymentId })}`
78
+ : headers?.referer || `${rootPageUrl}`;
79
+ const serverUrl = `${rootPageUrl}${prefix}/liqpay/${paymentId}/success`;
80
+
81
+ const html = liqpay.cnb_form({
82
+ action: 'pay',
83
+ amount: paymentAmount,
84
+ currency: 'UAH',
85
+ description: 'regular payment',
86
+ order_id: paymentId,
87
+ version: '3',
88
+ server_url: serverUrl, // update payment info - api url
89
+ result_url: resultUrl, // redirect user after payment - page url
90
+ });
91
+
92
+ const logObj = {
93
+ type: 'redirect',
94
+ paymentId,
95
+ accountId,
96
+ price: paymentAmount,
97
+ service: 'liqpay',
98
+ domain,
99
+ paymentRedirectUrl: resultUrl,
100
+ serverUrl,
101
+ uid: user?.uid || '0',
102
+ };
103
+
104
+ if (config.debug && query?.debug) {
105
+ return reply.status(302).headers({ 'Content-Type': 'text/html' }).send(html);
106
+ }
107
+
108
+ const body = {
109
+ data: html.match(/<input type="hidden" name="data" value="([^"]+)"/)?.[1],
110
+ signature: html.match(/<input type="hidden" name="signature" value="([^"]+)"/)?.[1],
111
+ };
112
+
113
+ try {
114
+ const response = await request({
115
+ url: `${host}/api/3/checkout`,
116
+ method: 'POST',
117
+ form: body,
118
+ followAllRedirects: false,
119
+ jar: true,
120
+ timeout: 6000,
121
+ simple: false, // prevent not 2xx code trigger catch
122
+ resolveWithFullResponse: true,
123
+ });
124
+
125
+ if (response.statusCode !== 302 || !response.headers?.location) {
126
+ throw new Error(response);
127
+ }
128
+
129
+ if (response.headers?.location?.includes?.('throw_error')) {
130
+ throw new Error(response.headers?.location);
131
+ }
132
+
133
+ Object.assign(logObj, { paymentId, redirect: response.headers?.location, status: 302 });
134
+ logger.file('payments', logObj);
135
+ return reply.redirect(response.headers.location);
136
+ }
137
+ catch (err) {
138
+ Object.assign(logObj, { error: err.toString() });
139
+ logger.file('payments/error', logObj);
140
+ return reply.status(500).send(err.toString());
141
+ }
142
+ }
@@ -0,0 +1,168 @@
1
+ import LiqPay from 'liqpayjs-sdk';
2
+
3
+ import {
4
+ config, logger, pgClients, dataUpdate, getTemplate,
5
+ } from '@opengis/fastify-table/utils.js';
6
+
7
+ import { sendNotification } from '@opengis/admin/utils.js';
8
+
9
+ const newDate = () => {
10
+ const date = new Date();
11
+ const tzOffset = date.getTimezoneOffset();
12
+ const currentTimeWithTimezome = date - tzOffset * 60 * 1000;
13
+ return new Date(currentTimeWithTimezome).toISOString();
14
+ };
15
+
16
+ const { publicKey, privateKey } = config.integrations?.liqpay || {};
17
+ const liqpay = new LiqPay(publicKey, privateKey);
18
+
19
+ /**
20
+ * Апі перенаправлює з liqpay назад на сторінку замовлення
21
+ *
22
+ * @method POST
23
+ * @alias liqpayStatus
24
+ * @summary redirect з liqpay на сторінку замовлення
25
+ * @type api
26
+ * @param {String} paymentId - Ідентифікатор замовлення
27
+ * @param {String} refillSum - Сума поповнення рахунку
28
+ * @returns {Number} status - номер помилки. Повертається, якщо була допущенна помилка в отриманих параметрах, або з бази. Це може бути 400 або 500
29
+ * @returns {String} error - опис помилки
30
+ * @returns {String} redirect - шлях до переадресації
31
+ * @returns {String} message - повідомлення про успішне виконання і передача певних даних або помилку
32
+ * @returns {String} headers - заголовки HTTP
33
+ * @returns {String} file - шлях файла або його назва
34
+ */
35
+
36
+ export default async function liqpayStatus(req, reply) {
37
+ const {
38
+ pg = pgClients.client, params = {}, headers = {}, body = {}, user = {}, unittest,
39
+ } = req;
40
+
41
+ if (!publicKey || !privateKey) {
42
+ return reply.status(400).send('invalid liqpay integration settings');
43
+ }
44
+
45
+ if (!body?.data || !body?.signature) {
46
+ return reply.status(400).send('not enough body params: data, signature');
47
+ }
48
+
49
+ const sign = liqpay.str_to_sign(
50
+ privateKey
51
+ + body.data
52
+ + privateKey,
53
+ );
54
+
55
+ const origin = headers?.origin || headers?.referer;
56
+ const { status: responseStatus, id: paymentId } = params;
57
+
58
+ if (body.signature !== sign) {
59
+ logger.file('payments/warn', {
60
+ type: 'status',
61
+ service: 'liqpay',
62
+ paymentId,
63
+ origin,
64
+ uid: user?.uid || '0',
65
+ message: 'Transaction signature does not match',
66
+ });
67
+ return reply.status(403).send('access restricted: signature mismatch');
68
+ }
69
+
70
+ const data = await pg.query('select * from billing.payments where payment_id=$1', [paymentId])
71
+ .then(el => el.rows?.[0] || {});
72
+
73
+ const {
74
+ payment_id: trx,
75
+ payment_amount: refillSum = 0,
76
+ referer_url: redirectUrl,
77
+ payment_status: trxStatus,
78
+ payment_service: paymentService,
79
+ account_id: accountId,
80
+ } = data;
81
+
82
+ if (trxStatus === 'success') {
83
+ logger.file('payments/skip', {
84
+ type: 'status',
85
+ service: 'liqpay',
86
+ paymentId,
87
+ origin,
88
+ uid: user?.uid || '0',
89
+ message: 'Transaction was already processed',
90
+ });
91
+ return reply.status(400).send('Transaction was already processed');
92
+ }
93
+
94
+ if (!trx) {
95
+ return reply.status(404).send('Transaction not found');
96
+ }
97
+
98
+ if (paymentService !== 'liqpay') {
99
+ return reply.status(400).send('Payment not registered via liqpay service');
100
+ }
101
+
102
+ logger.file('payments', {
103
+ type: 'status',
104
+ service: 'liqpay',
105
+ paymentId,
106
+ origin,
107
+ uid: user?.uid || '0',
108
+ refill: refillSum,
109
+ body,
110
+ params,
111
+ });
112
+
113
+ // last trx contains total balance
114
+ const { total_balance: totalBalance = 0 } = await pg.query(`select total_balance from billing.payments
115
+ where account_id=$1
116
+ and payment_status in ('success', 'error')
117
+ and total_balance is not null
118
+ order by cdate desc limit 1`, [accountId]).then(el => el.rows?.[0] || {});
119
+
120
+ await dataUpdate({
121
+ table: 'billing.payments',
122
+ data: {
123
+ payment_data: body,
124
+ payment_status: responseStatus,
125
+ total_balance: +refillSum + +totalBalance,
126
+ success_date: newDate(),
127
+ },
128
+ id: paymentId,
129
+ uid: user?.uid || '0',
130
+ });
131
+
132
+ if (unittest) {
133
+ console.log(`id:${trx}, status: ${responseStatus}, before: ${totalBalance || 0}, after: ${+refillSum + +totalBalance} (${refillSum})`);
134
+ }
135
+
136
+ const { account_email: to } = await pg.query('select account_email from billing.accounts where account_id=$1', [accountId])
137
+ .then(el => el.rows?.[0] || {});
138
+
139
+ logger.file('payments', {
140
+ type: 'status',
141
+ service: 'liqpay',
142
+ paymentId,
143
+ responseStatus,
144
+ to,
145
+ origin,
146
+ redirect: redirectUrl,
147
+ uid: user.uid || '0',
148
+ });
149
+
150
+ Object.assign(data, {
151
+ domain: req.hostname,
152
+ total_balance: +refillSum + +totalBalance,
153
+ });
154
+
155
+ if (to) {
156
+ const pt = await getTemplate('pt', 'marketplace-service-payment-client-template');
157
+ const options = {
158
+ pg,
159
+ to,
160
+ title: `Поповнення рахунку на порталі ${req.hostname}`,
161
+ template: pt?.html || pt,
162
+ data,
163
+ nocache: config?.local,
164
+ };
165
+ await sendNotification(options);
166
+ }
167
+ return reply.redirect(redirectUrl || '/');
168
+ }
@@ -0,0 +1,22 @@
1
+ import liqpayRedirect from './controllers/liqpay.redirect.js';
2
+ import liqpayStatus from './controllers/liqpay.status.js';
3
+
4
+ export default async function plugin(app, opts) {
5
+ app.route({
6
+ method: 'GET',
7
+ url: '/liqpay-redirect',
8
+ config: {
9
+ policy: ['site'],
10
+ },
11
+ handler: liqpayRedirect,
12
+ });
13
+ // /payment-info/:id
14
+ app.route({
15
+ method: 'POST',
16
+ url: '/liqpay/:id/:status',
17
+ config: {
18
+ policy: ['public'],
19
+ },
20
+ handler: liqpayStatus,
21
+ });
22
+ }
@@ -4,6 +4,8 @@ import {
4
4
  config, pgClients, logger, dataInsert, dataUpdate, handlebarsSync,
5
5
  } from '@opengis/fastify-table/utils.js';
6
6
 
7
+ const { host, payeeId, paymentRedirect = config.paymentRedirect } = config.integrations?.pay || {};
8
+
7
9
  /**
8
10
  * Апі перенаправлює до стороннього сервісу оплати карткою онлайн
9
11
  *
@@ -25,9 +27,6 @@ export default async function PortmoneRedirect(req, reply) {
25
27
  pg = pgClients.client, user = {}, query = {}, headers = {},
26
28
  } = req;
27
29
 
28
- const { host, payeeId, paymentRedirect = config.paymentRedirect } = config.integrations?.pay || {};
29
- const domain = req.hostname.split(':').shift();
30
-
31
30
  if (!host) {
32
31
  return { message: 'empty integration host', status: 400 };
33
32
  }
@@ -36,6 +35,8 @@ export default async function PortmoneRedirect(req, reply) {
36
35
  return { message: 'empty integration payeeId', status: 400 };
37
36
  }
38
37
 
38
+ const domain = req.hostname.split(':').shift();
39
+
39
40
  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])
40
41
  .then(el => el.rows?.[0] || {}) : {};
41
42
 
@@ -58,7 +59,7 @@ export default async function PortmoneRedirect(req, reply) {
58
59
 
59
60
  if ((!headers?.referer || !headers.referer.includes(domain))) {
60
61
  logger.file('payments/warn', {
61
- message: 'invalid referer', domain, referer: headers?.referer, uid: user?.uid,
62
+ message: 'invalid referer', service: 'portmone', domain, referer: headers?.referer, uid: user?.uid,
62
63
  });
63
64
  if (!config.auth?.disableRestricted) return { message: 'access restricted: referer', status: 403 };
64
65
  }
@@ -69,6 +70,7 @@ export default async function PortmoneRedirect(req, reply) {
69
70
  data: {
70
71
  payment_amount: paymentAmount,
71
72
  account_id: accountId,
73
+ payment_service: 'portmone',
72
74
  payment_status: 'inprogress',
73
75
  },
74
76
  uid: user?.uid || '0',
@@ -103,6 +105,7 @@ export default async function PortmoneRedirect(req, reply) {
103
105
 
104
106
  const logObj = {
105
107
  type: 'redirect',
108
+ service: 'portmone',
106
109
  payeeId,
107
110
  paymentId,
108
111
  accountId,
@@ -4,6 +4,15 @@ import {
4
4
 
5
5
  import { sendNotification } from '@opengis/admin/utils.js';
6
6
 
7
+ const newDate = () => {
8
+ const date = new Date();
9
+ const tzOffset = date.getTimezoneOffset();
10
+ const currentTimeWithTimezome = date - tzOffset * 60 * 1000;
11
+ return new Date(currentTimeWithTimezome).toISOString();
12
+ };
13
+
14
+ const { host } = config.integrations?.pay || {};
15
+
7
16
  /**
8
17
  * Апі перенаправлює з portmone назад на сторінку замовлення
9
18
  *
@@ -25,7 +34,6 @@ export default async function portmoneStatus(req, reply) {
25
34
  const {
26
35
  pg = pgClients.client, params = {}, headers = {}, body = {}, user = {}, unittest,
27
36
  } = req;
28
- const { host } = config.integrations?.pay || {};
29
37
 
30
38
  if (!host) {
31
39
  return { message: 'empty integration host', status: 400 };
@@ -50,6 +58,7 @@ export default async function portmoneStatus(req, reply) {
50
58
  if (trxStatus === 'success') {
51
59
  logger.file('payments/skip', {
52
60
  type: 'status',
61
+ service: 'portmone',
53
62
  paymentId,
54
63
  origin,
55
64
  uid: user?.uid || '0',
@@ -68,6 +77,7 @@ export default async function portmoneStatus(req, reply) {
68
77
  if (!origin?.startsWith(host)) {
69
78
  logger.file('payments/warn', {
70
79
  type: 'status',
80
+ service: 'portmone',
71
81
  paymentId,
72
82
  origin,
73
83
  uid: user?.uid || '0',
@@ -79,6 +89,7 @@ export default async function portmoneStatus(req, reply) {
79
89
 
80
90
  logger.file('payments', {
81
91
  type: 'status',
92
+ service: 'portmone',
82
93
  paymentId,
83
94
  origin,
84
95
  uid: user?.uid || '0',
@@ -98,7 +109,7 @@ export default async function portmoneStatus(req, reply) {
98
109
  payment_data: body,
99
110
  payment_status: responseStatus,
100
111
  total_balance: +(refill) + (+totalBalance || 0),
101
- success_date: new Date(),
112
+ success_date: newDate(),
102
113
  },
103
114
  id: paymentId,
104
115
  uid: user?.uid || '0',
@@ -113,6 +124,7 @@ export default async function portmoneStatus(req, reply) {
113
124
 
114
125
  logger.file('payments', {
115
126
  type: 'status',
127
+ service: 'portmone',
116
128
  paymentId,
117
129
  responseStatus,
118
130
  to,
@@ -143,12 +155,13 @@ export default async function portmoneStatus(req, reply) {
143
155
  catch (err) {
144
156
  logger.file('payments/error', {
145
157
  type: 'status',
158
+ service: 'portmone',
146
159
  paymentId,
147
160
  origin,
148
161
  responseStatus,
149
162
  uid: user?.uid || '0',
150
163
  error: err.toString(),
151
164
  });
152
- return { error: err.toString(), status: 500 };
165
+ return reply.status(500).send(err.toString());
153
166
  }
154
167
  }
@@ -0,0 +1,30 @@
1
+ import portmoneRedirect from './controllers/portmone.redirect.js';
2
+ import portmoneStatus from './controllers/portmone.status.js';
3
+ import paymentInfo from './controllers/payment.info.js';
4
+
5
+ export default async function plugin(app, opts) {
6
+ app.route({
7
+ method: 'GET',
8
+ url: '/portmone-redirect',
9
+ config: {
10
+ policy: ['site'],
11
+ },
12
+ handler: portmoneRedirect,
13
+ });
14
+ app.route({
15
+ method: 'GET',
16
+ url: '/payment-info/:id',
17
+ config: {
18
+ policy: ['site'],
19
+ },
20
+ handler: paymentInfo,
21
+ });
22
+ app.route({
23
+ method: 'POST',
24
+ url: '/portmone/:id/:status',
25
+ config: {
26
+ policy: ['public'],
27
+ },
28
+ handler: portmoneStatus,
29
+ });
30
+ }