@unchainedshop/plugins 1.1.4 → 1.1.9
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/files/gridfs/gridfs-adapter.js +1 -1
- package/lib/files/gridfs/gridfs-adapter.js.map +1 -1
- package/lib/files/gridfs/gridfs-webhook.d.ts +2 -1
- package/lib/files/gridfs/gridfs-webhook.js +3 -3
- package/lib/files/gridfs/gridfs-webhook.js.map +1 -1
- package/lib/files/minio/minio-webhook.d.ts +2 -1
- package/lib/files/minio/minio-webhook.js +31 -29
- package/lib/files/minio/minio-webhook.js.map +1 -1
- package/lib/payment/apple-iap/adapter.d.ts +2 -1
- package/lib/payment/apple-iap/adapter.js +94 -92
- package/lib/payment/apple-iap/adapter.js.map +1 -1
- package/lib/payment/apple-iap/db/AppleTransactionsCollection.d.ts +2 -2
- package/lib/payment/apple-iap/index.d.ts +2 -1
- package/lib/payment/apple-iap/index.js +2 -1
- package/lib/payment/apple-iap/index.js.map +1 -1
- package/lib/payment/bity/adapter.d.ts +2 -1
- package/lib/payment/bity/adapter.js +50 -48
- package/lib/payment/bity/adapter.js.map +1 -1
- package/lib/payment/bity/db/BityCredentialsCollection.d.ts +2 -2
- package/lib/payment/bity/index.d.ts +2 -1
- package/lib/payment/bity/index.js +2 -1
- package/lib/payment/bity/index.js.map +1 -1
- package/lib/payment/cryptopay.d.ts +2 -1
- package/lib/payment/cryptopay.js +60 -58
- package/lib/payment/cryptopay.js.map +1 -1
- package/lib/payment/datatrans-v2/index.d.ts +2 -1
- package/lib/payment/datatrans-v2/index.js +2 -1
- package/lib/payment/datatrans-v2/index.js.map +1 -1
- package/lib/payment/datatrans-v2/middleware.d.ts +2 -1
- package/lib/payment/datatrans-v2/middleware.js +100 -98
- package/lib/payment/datatrans-v2/middleware.js.map +1 -1
- package/lib/payment/datatrans-v2/roundedAmountFromOrder.d.ts +0 -2
- package/lib/payment/paypal-checkout.js +1 -1
- package/lib/payment/paypal-checkout.js.map +1 -1
- package/lib/payment/postfinance-checkout/index.d.ts +2 -1
- package/lib/payment/postfinance-checkout/index.js +2 -1
- package/lib/payment/postfinance-checkout/index.js.map +1 -1
- package/lib/payment/postfinance-checkout/middleware.d.ts +2 -1
- package/lib/payment/postfinance-checkout/middleware.js +36 -34
- package/lib/payment/postfinance-checkout/middleware.js.map +1 -1
- package/lib/payment/postfinance-checkout/utils.d.ts +0 -1
- package/lib/payment/stripe.d.ts +2 -1
- package/lib/payment/stripe.js +63 -60
- package/lib/payment/stripe.js.map +1 -1
- package/lib/pricing/product-price-cryptopay.d.ts +2 -1
- package/lib/pricing/product-price-cryptopay.js +20 -18
- package/lib/pricing/product-price-cryptopay.js.map +1 -1
- package/lib/pricing/utils/cache.d.ts +1 -2
- package/lib/worker/ZombieKillerWorker.js.map +1 -1
- package/license +274 -0
- package/package.json +20 -19
- package/src/files/gridfs/gridfs-adapter.ts +1 -1
- package/src/files/gridfs/gridfs-webhook.js +42 -41
- package/src/files/minio/minio-webhook.js +34 -31
- package/src/payment/apple-iap/adapter.ts +113 -110
- package/src/payment/apple-iap/index.ts +2 -1
- package/src/payment/bity/adapter.ts +51 -49
- package/src/payment/bity/index.ts +2 -1
- package/src/payment/cryptopay.ts +69 -67
- package/src/payment/datatrans-v2/index.ts +3 -1
- package/src/payment/datatrans-v2/middleware.ts +130 -124
- package/src/payment/paypal-checkout.ts +1 -1
- package/src/payment/postfinance-checkout/index.ts +3 -1
- package/src/payment/postfinance-checkout/middleware.ts +42 -40
- package/src/payment/stripe.ts +82 -74
- package/src/pricing/product-price-cryptopay.ts +20 -18
- package/src/worker/ZombieKillerWorker.ts +5 -5
- package/tsconfig.build.json +13 -5
- package/lib/worker/MessageWorker.d.ts +0 -10
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMiddlewareWithCurrentContext } from '
|
|
1
|
+
import { useMiddlewareWithCurrentContext } from '@unchainedshop/api';
|
|
2
2
|
import bodyParser from 'body-parser';
|
|
3
3
|
import { createLogger } from '@unchainedshop/logger';
|
|
4
4
|
import { Context } from '@unchainedshop/types/api';
|
|
@@ -17,131 +17,137 @@ const logger = createLogger('unchained:core-payment:datatrans');
|
|
|
17
17
|
|
|
18
18
|
const { postUrl, cancelUrl, errorUrl, successUrl, returnUrl } = getPaths(true);
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
useMiddlewareWithCurrentContext(
|
|
30
|
-
|
|
31
|
-
useMiddlewareWithCurrentContext(
|
|
32
|
-
|
|
33
|
-
useMiddlewareWithCurrentContext(
|
|
34
|
-
|
|
35
|
-
useMiddlewareWithCurrentContext(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
20
|
+
export default (app) => {
|
|
21
|
+
useMiddlewareWithCurrentContext(
|
|
22
|
+
app,
|
|
23
|
+
postUrl,
|
|
24
|
+
bodyParser.text({
|
|
25
|
+
type: 'application/json',
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
useMiddlewareWithCurrentContext(app, cancelUrl, bodyParser.urlencoded({ extended: false }));
|
|
30
|
+
|
|
31
|
+
useMiddlewareWithCurrentContext(app, errorUrl, bodyParser.urlencoded({ extended: false }));
|
|
32
|
+
|
|
33
|
+
useMiddlewareWithCurrentContext(app, successUrl, bodyParser.urlencoded({ extended: false }));
|
|
34
|
+
|
|
35
|
+
useMiddlewareWithCurrentContext(app, returnUrl, bodyParser.urlencoded({ extended: false }));
|
|
36
|
+
|
|
37
|
+
useMiddlewareWithCurrentContext(app, postUrl, async (req, res) => {
|
|
38
|
+
const resolvedContext = req.unchainedContext as Context;
|
|
39
|
+
const { services, modules } = resolvedContext;
|
|
40
|
+
const signature = req.headers['datatrans-signature'];
|
|
41
|
+
if (req.method === 'POST' && signature) {
|
|
42
|
+
const [rawTimestamp, rawHash] = signature.split(',');
|
|
43
|
+
const [, hash] = rawHash.split('=');
|
|
44
|
+
const [, timestamp] = rawTimestamp.split('=');
|
|
45
|
+
|
|
46
|
+
const comparableSignature = generateSignature({
|
|
47
|
+
security: DATATRANS_SECURITY,
|
|
48
|
+
signKey: DATATRANS_SIGN2_KEY || DATATRANS_SIGN_KEY,
|
|
49
|
+
})(timestamp, req.body);
|
|
50
|
+
|
|
51
|
+
if (hash !== comparableSignature) {
|
|
52
|
+
logger.error(`Datatrans Plugin: Hash mismatch: ${signature} / ${comparableSignature}`, req.body);
|
|
53
|
+
res.writeHead(403);
|
|
54
|
+
res.end('Hash mismatch');
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
55
57
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
58
|
+
const transaction: StatusResponseSuccess = JSON.parse(req.body) as StatusResponseSuccess;
|
|
59
|
+
|
|
60
|
+
if (transaction.status === 'authorized') {
|
|
61
|
+
const userId = transaction.refno2;
|
|
62
|
+
const referenceId = Buffer.from(transaction.refno, 'base64').toString('hex');
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
if (transaction.type === 'card_check') {
|
|
66
|
+
const paymentProviderId = referenceId;
|
|
67
|
+
const paymentCredentials = await services.payment.registerPaymentCredentials(
|
|
68
|
+
paymentProviderId,
|
|
69
|
+
{ transactionContext: { transactionId: transaction.transactionId } },
|
|
70
|
+
{ ...resolvedContext, userId },
|
|
71
|
+
);
|
|
72
|
+
logger.info(`Datatrans Webhook: Unchained registered payment credentials for ${userId}`, {
|
|
73
|
+
userId,
|
|
74
|
+
});
|
|
75
|
+
res.writeHead(200);
|
|
76
|
+
res.end(JSON.stringify(paymentCredentials));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (transaction.type === 'payment') {
|
|
80
|
+
const orderPaymentId = referenceId;
|
|
81
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
82
|
+
orderPaymentId,
|
|
83
|
+
});
|
|
84
|
+
if (!orderPayment) throw new Error(`Order Payment with id ${orderPaymentId} not found`);
|
|
85
|
+
|
|
86
|
+
const order = await modules.orders.checkout(
|
|
87
|
+
orderPayment.orderId,
|
|
88
|
+
{ paymentContext: { transactionId: transaction.transactionId } },
|
|
89
|
+
resolvedContext,
|
|
90
|
+
);
|
|
91
|
+
res.writeHead(200);
|
|
92
|
+
logger.info(
|
|
93
|
+
`Datatrans Webhook: Unchained confirmed checkout for order ${order.orderNumber}`,
|
|
94
|
+
{
|
|
95
|
+
orderId: order._id,
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
res.end(JSON.stringify(order));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
} catch (e) {
|
|
102
|
+
logger.error(`Datatrans Webhook: Unchained rejected to checkout with message`, e);
|
|
103
|
+
res.writeHead(500);
|
|
104
|
+
res.end(JSON.stringify(e));
|
|
75
105
|
return;
|
|
76
106
|
}
|
|
77
|
-
if (transaction.type === 'payment') {
|
|
78
|
-
const orderPaymentId = referenceId;
|
|
79
|
-
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
80
|
-
orderPaymentId,
|
|
81
|
-
});
|
|
82
|
-
if (!orderPayment) throw new Error(`Order Payment with id ${orderPaymentId} not found`);
|
|
83
|
-
|
|
84
|
-
const order = await modules.orders.checkout(
|
|
85
|
-
orderPayment.orderId,
|
|
86
|
-
{ paymentContext: { transactionId: transaction.transactionId } },
|
|
87
|
-
resolvedContext,
|
|
88
|
-
);
|
|
89
|
-
res.writeHead(200);
|
|
90
|
-
logger.info(`Datatrans Webhook: Unchained confirmed checkout for order ${order.orderNumber}`, {
|
|
91
|
-
orderId: order._id,
|
|
92
|
-
});
|
|
93
|
-
res.end(JSON.stringify(order));
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
} catch (e) {
|
|
97
|
-
logger.error(`Datatrans Webhook: Unchained rejected to checkout with message`, e);
|
|
98
|
-
res.writeHead(500);
|
|
99
|
-
res.end(JSON.stringify(e));
|
|
100
|
-
return;
|
|
101
107
|
}
|
|
102
108
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
109
|
+
res.writeHead(404);
|
|
110
|
+
res.end();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
useMiddlewareWithCurrentContext(app, successUrl, async (req, res) => {
|
|
114
|
+
if (req.method === 'GET') {
|
|
115
|
+
const { datatransTrxId } = req.query;
|
|
116
|
+
res.end(`Payment successful\nTransaction ID: ${datatransTrxId}`);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
res.writeHead(404);
|
|
120
|
+
res.end();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
useMiddlewareWithCurrentContext(app, errorUrl, async (req, res) => {
|
|
124
|
+
if (req.method === 'GET') {
|
|
125
|
+
const { datatransTrxId } = req.query;
|
|
126
|
+
res.end(`Payment error\nTransaction ID: ${datatransTrxId}`);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
res.writeHead(404);
|
|
130
|
+
res.end();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
useMiddlewareWithCurrentContext(app, cancelUrl, async (req, res) => {
|
|
134
|
+
if (req.method === 'GET') {
|
|
135
|
+
const { datatransTrxId } = req.query;
|
|
136
|
+
res.end(`Payment cancelled\nTransaction ID: ${datatransTrxId}`);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
res.writeHead(404);
|
|
140
|
+
res.end();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
useMiddlewareWithCurrentContext(app, returnUrl, async (req, res) => {
|
|
144
|
+
if (req.method === 'GET') {
|
|
145
|
+
const { datatransTrxId } = req.query;
|
|
146
|
+
res.end(
|
|
147
|
+
`Secure Fields Payment authenticated\nTransaction ID: ${datatransTrxId}\nNeeds authorization`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
res.writeHead(404);
|
|
151
|
+
res.end();
|
|
152
|
+
});
|
|
153
|
+
};
|
|
@@ -2,7 +2,7 @@ import { IPaymentAdapter } from '@unchainedshop/types/payments';
|
|
|
2
2
|
import { PaymentDirector, PaymentAdapter, PaymentError } from '@unchainedshop/core-payment';
|
|
3
3
|
import { createLogger } from '@unchainedshop/logger';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import checkoutNodeJssdk from '@paypal/checkout-server-sdk'; // eslint-disable-line
|
|
6
6
|
|
|
7
7
|
const logger = createLogger('unchained:core-payment');
|
|
8
8
|
|
|
@@ -17,9 +17,11 @@ import {
|
|
|
17
17
|
voidTransaction,
|
|
18
18
|
} from './api';
|
|
19
19
|
import { orderIsPaid } from './utils';
|
|
20
|
-
import './middleware';
|
|
20
|
+
import setupPostfinance from './middleware';
|
|
21
21
|
import { CompletionModes, IntegrationModes, SignResponse } from './types';
|
|
22
22
|
|
|
23
|
+
export default setupPostfinance;
|
|
24
|
+
|
|
23
25
|
const {
|
|
24
26
|
PFCHECKOUT_SPACE_ID,
|
|
25
27
|
PFCHECKOUT_USER_ID,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import bodyParser from 'body-parser';
|
|
2
|
-
import { useMiddlewareWithCurrentContext } from '
|
|
2
|
+
import { useMiddlewareWithCurrentContext } from '@unchainedshop/api';
|
|
3
3
|
import { Context } from '@unchainedshop/types/api';
|
|
4
4
|
import { createLogger } from '@unchainedshop/logger';
|
|
5
5
|
import { WebhookData } from './types';
|
|
@@ -9,46 +9,48 @@ const { PFCHECKOUT_WEBHOOK_PATH = '/graphql/postfinance-checkout' } = process.en
|
|
|
9
9
|
|
|
10
10
|
const logger = createLogger('unchained:core-payment:postfinance-checkout');
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
export default (app) => {
|
|
13
|
+
useMiddlewareWithCurrentContext(app, PFCHECKOUT_WEBHOOK_PATH, bodyParser.json());
|
|
13
14
|
|
|
14
|
-
useMiddlewareWithCurrentContext(PFCHECKOUT_WEBHOOK_PATH, async (req, res) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
useMiddlewareWithCurrentContext(app, PFCHECKOUT_WEBHOOK_PATH, async (req, res) => {
|
|
16
|
+
const context = req.unchainedContext as Context;
|
|
17
|
+
const data = req.body as WebhookData;
|
|
18
|
+
if (data.listenerEntityTechnicalName === 'TransactionCompletion') {
|
|
19
|
+
try {
|
|
20
|
+
const transactionCompletion = await getTransactionCompletion(data.entityId as unknown as string);
|
|
21
|
+
const transaction = await getTransaction(
|
|
22
|
+
transactionCompletion.linkedTransaction as unknown as string,
|
|
23
|
+
);
|
|
24
|
+
const { orderPaymentId } = transaction.metaData as { orderPaymentId: string };
|
|
25
|
+
const orderPayment = await context.modules.orders.payments.findOrderPayment({
|
|
26
|
+
orderPaymentId,
|
|
27
|
+
});
|
|
28
|
+
if (!orderPayment) throw new Error('Order Payment not found');
|
|
29
|
+
const order = await context.modules.orders.checkout(
|
|
30
|
+
orderPayment.orderId,
|
|
31
|
+
{
|
|
32
|
+
paymentContext: {
|
|
33
|
+
transactionId: transactionCompletion.linkedTransaction,
|
|
34
|
+
},
|
|
33
35
|
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
context,
|
|
37
|
+
);
|
|
38
|
+
logger.info(
|
|
39
|
+
`PostFinance Checkout Webhook: Transaction ${transactionCompletion.linkedTransaction} marked order payment ID ${transaction.metaData.orderPaymentId} as paid`,
|
|
40
|
+
);
|
|
41
|
+
res.writeHead(200);
|
|
42
|
+
res.end(`Order marked as paid: ${order.orderNumber}`);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
logger.error(`PostFinance Checkout Webhook: Unchained rejected to checkout with message`, e);
|
|
45
|
+
res.writeHead(500);
|
|
46
|
+
res.end(JSON.stringify(e));
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
logger.error(
|
|
50
|
+
`PostFinance Checkout Webhook: Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`,
|
|
39
51
|
);
|
|
40
|
-
res.writeHead(
|
|
41
|
-
res.end(
|
|
42
|
-
} catch (e) {
|
|
43
|
-
logger.error(`PostFinance Checkout Webhook: Unchained rejected to checkout with message`, e);
|
|
44
|
-
res.writeHead(500);
|
|
45
|
-
res.end(JSON.stringify(e));
|
|
52
|
+
res.writeHead(404);
|
|
53
|
+
res.end();
|
|
46
54
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
`PostFinance Checkout Webhook: Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`,
|
|
50
|
-
);
|
|
51
|
-
res.writeHead(404);
|
|
52
|
-
res.end();
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
+
});
|
|
56
|
+
};
|
package/src/payment/stripe.ts
CHANGED
|
@@ -1,19 +1,10 @@
|
|
|
1
1
|
import { Context } from '@unchainedshop/types/api';
|
|
2
2
|
import { IPaymentAdapter } from '@unchainedshop/types/payments';
|
|
3
3
|
import bodyParser from 'body-parser';
|
|
4
|
-
import { useMiddlewareWithCurrentContext } from '
|
|
4
|
+
import { useMiddlewareWithCurrentContext } from '@unchainedshop/api';
|
|
5
5
|
import { PaymentAdapter, PaymentDirector, PaymentError } from '@unchainedshop/core-payment';
|
|
6
6
|
import { createLogger } from '@unchainedshop/logger';
|
|
7
7
|
|
|
8
|
-
const logger = createLogger('unchained:core-payment:stripe');
|
|
9
|
-
|
|
10
|
-
const {
|
|
11
|
-
STRIPE_SECRET,
|
|
12
|
-
STRIPE_ENDPOINT_SECRET,
|
|
13
|
-
EMAIL_WEBSITE_NAME,
|
|
14
|
-
STRIPE_WEBHOOK_PATH = '/graphql/stripe',
|
|
15
|
-
} = process.env;
|
|
16
|
-
|
|
17
8
|
/*
|
|
18
9
|
Test Webhooks:
|
|
19
10
|
|
|
@@ -23,82 +14,99 @@ stripe listen --forward-to http://localhost:3000/graphql/stripe
|
|
|
23
14
|
stripe trigger payment_intent.succeeded
|
|
24
15
|
*/
|
|
25
16
|
|
|
26
|
-
|
|
17
|
+
import createStripeClient from 'stripe';
|
|
18
|
+
|
|
19
|
+
const logger = createLogger('unchained:core-payment:stripe');
|
|
20
|
+
|
|
21
|
+
const {
|
|
22
|
+
STRIPE_SECRET,
|
|
23
|
+
STRIPE_ENDPOINT_SECRET,
|
|
24
|
+
EMAIL_WEBSITE_NAME,
|
|
25
|
+
STRIPE_WEBHOOK_PATH = '/graphql/stripe',
|
|
26
|
+
} = process.env;
|
|
27
|
+
|
|
28
|
+
const stripe = createStripeClient(STRIPE_SECRET);
|
|
27
29
|
|
|
28
|
-
|
|
30
|
+
export default (app) => {
|
|
31
|
+
useMiddlewareWithCurrentContext(
|
|
32
|
+
app,
|
|
33
|
+
STRIPE_WEBHOOK_PATH,
|
|
34
|
+
bodyParser.raw({ type: 'application/json' }),
|
|
35
|
+
);
|
|
29
36
|
|
|
30
|
-
useMiddlewareWithCurrentContext(STRIPE_WEBHOOK_PATH, async (request, response) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
useMiddlewareWithCurrentContext(app, STRIPE_WEBHOOK_PATH, async (request, response) => {
|
|
38
|
+
const sig = request.headers['stripe-signature'];
|
|
39
|
+
const resolvedContext = request.unchainedContext as Context;
|
|
40
|
+
const { modules, services } = resolvedContext;
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
let event;
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
try {
|
|
45
|
+
event = stripe.webhooks.constructEvent(request.body, sig, STRIPE_ENDPOINT_SECRET);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
response.writeHead(400);
|
|
48
|
+
response.end(`Webhook Error: ${err.message}`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
44
51
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
try {
|
|
53
|
+
if (event.type === 'payment_intent.succeeded') {
|
|
54
|
+
const paymentIntent = event.data.object;
|
|
55
|
+
const orderPaymentId = paymentIntent.metadata?.orderPaymentId;
|
|
49
56
|
|
|
50
|
-
|
|
57
|
+
await modules.orders.payments.logEvent(orderPaymentId, event, resolvedContext.userId);
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
60
|
+
orderPaymentId,
|
|
61
|
+
});
|
|
55
62
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
const order = await modules.orders.checkout(
|
|
64
|
+
orderPayment.orderId,
|
|
65
|
+
{
|
|
66
|
+
transactionContext: {
|
|
67
|
+
paymentIntentId: paymentIntent.id,
|
|
68
|
+
},
|
|
69
|
+
paymentContext: {
|
|
70
|
+
paymentIntentId: paymentIntent.id,
|
|
71
|
+
},
|
|
64
72
|
},
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
resolvedContext,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
logger.info(`Stripe Webhook: Unchained confirmed checkout for order ${order.orderNumber}`, {
|
|
77
|
+
orderId: order._id,
|
|
78
|
+
});
|
|
79
|
+
} else if (event.type === 'setup_intent.succeeded') {
|
|
80
|
+
const setupIntent = event.data.object;
|
|
81
|
+
const { paymentProviderId, userId } = setupIntent.metadata;
|
|
82
|
+
|
|
83
|
+
await services.payment.registerPaymentCredentials(
|
|
84
|
+
paymentProviderId,
|
|
85
|
+
{
|
|
86
|
+
transactionContext: {
|
|
87
|
+
setupIntentId: setupIntent.id,
|
|
88
|
+
},
|
|
81
89
|
},
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
resolvedContext,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
logger.info(`Stripe Webhook: Unchained registered payment credentials for ${userId}`, {
|
|
94
|
+
userId,
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
response.writeHead(404);
|
|
98
|
+
response.end();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
} catch (err) {
|
|
102
|
+
response.writeHead(400);
|
|
103
|
+
response.end(`Webhook Error: ${err.message}`);
|
|
92
104
|
return;
|
|
93
105
|
}
|
|
94
|
-
|
|
95
|
-
response.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
// Return a 200 response to acknowledge receipt of the event
|
|
100
|
-
response.end(JSON.stringify({ received: true }));
|
|
101
|
-
});
|
|
106
|
+
// Return a 200 response to acknowledge receipt of the event
|
|
107
|
+
response.end(JSON.stringify({ received: true }));
|
|
108
|
+
});
|
|
109
|
+
};
|
|
102
110
|
|
|
103
111
|
const createRegistrationIntent = async ({ userId, name, email, paymentProviderId }, options = {}) => {
|
|
104
112
|
const customer = await stripe.customers.create({
|
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import { Context } from '@unchainedshop/types/api';
|
|
2
2
|
import { ProductPriceRate } from '@unchainedshop/types/products.pricing';
|
|
3
3
|
import bodyParser from 'body-parser';
|
|
4
|
-
import { useMiddlewareWithCurrentContext } from '
|
|
4
|
+
import { useMiddlewareWithCurrentContext } from '@unchainedshop/api';
|
|
5
5
|
|
|
6
6
|
const { CRYPTOPAY_SECRET, CRYPTOPAY_PRICING_WEBHOOK_PATH = '/graphql/cryptopay-pricing' } = process.env;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
export default (app) => {
|
|
9
|
+
useMiddlewareWithCurrentContext(app, CRYPTOPAY_PRICING_WEBHOOK_PATH, bodyParser.json());
|
|
9
10
|
|
|
10
|
-
useMiddlewareWithCurrentContext(CRYPTOPAY_PRICING_WEBHOOK_PATH, async (request, response) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
11
|
+
useMiddlewareWithCurrentContext(app, CRYPTOPAY_PRICING_WEBHOOK_PATH, async (request, response) => {
|
|
12
|
+
const resolvedContext = request.unchainedContext as Context;
|
|
13
|
+
const { baseCurrency, token, rate, timestamp, secret } = request.body;
|
|
14
|
+
if (secret !== CRYPTOPAY_SECRET) {
|
|
15
|
+
response.end(JSON.stringify({ success: false }));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const rateData: ProductPriceRate = {
|
|
19
|
+
baseCurrency,
|
|
20
|
+
quoteCurrency: token,
|
|
21
|
+
rate,
|
|
22
|
+
timestamp,
|
|
23
|
+
};
|
|
24
|
+
const success = await resolvedContext.modules.products.prices.rates.updateRate(rateData);
|
|
25
|
+
response.end(JSON.stringify({ success }));
|
|
26
|
+
});
|
|
27
|
+
};
|
|
@@ -70,11 +70,11 @@ export const ZombieKillerWorker: IWorkerAdapter<
|
|
|
70
70
|
const deletedFilesCount =
|
|
71
71
|
fileIdsToRemove.length > 0
|
|
72
72
|
? await services.files.removeFiles(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
73
|
+
{
|
|
74
|
+
fileIds: fileIdsToRemove,
|
|
75
|
+
},
|
|
76
|
+
unchainedAPI,
|
|
77
|
+
)
|
|
78
78
|
: 0;
|
|
79
79
|
|
|
80
80
|
// Return delete count
|