@unchainedshop/plugins 3.0.0-rc9 → 3.0.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/lib/files/gridfs/{gridfs-adapter.d.ts → adapter.d.ts} +2 -4
- package/lib/files/gridfs/adapter.d.ts.map +1 -0
- package/lib/files/gridfs/{gridfs-adapter.js → adapter.js} +1 -1
- package/lib/files/gridfs/adapter.js.map +1 -0
- package/lib/files/gridfs/handler-express.d.ts +10 -0
- package/lib/files/gridfs/handler-express.d.ts.map +1 -0
- package/lib/files/gridfs/{gridfs-webhook.js → handler-express.js} +3 -2
- package/lib/files/gridfs/handler-express.js.map +1 -0
- package/lib/files/gridfs/handler-fastify.d.ts +4 -0
- package/lib/files/gridfs/handler-fastify.d.ts.map +1 -0
- package/lib/files/gridfs/handler-fastify.js +91 -0
- package/lib/files/gridfs/handler-fastify.js.map +1 -0
- package/lib/files/gridfs/index.d.ts +4 -10
- package/lib/files/gridfs/index.d.ts.map +1 -1
- package/lib/files/gridfs/index.js +3 -27
- package/lib/files/gridfs/index.js.map +1 -1
- package/lib/files/gridfs/module.d.ts +26 -0
- package/lib/files/gridfs/module.d.ts.map +1 -0
- package/lib/files/gridfs/module.js +33 -0
- package/lib/files/gridfs/module.js.map +1 -0
- package/lib/files/minio/handler-express.d.ts +3 -0
- package/lib/files/minio/handler-express.d.ts.map +1 -0
- package/lib/files/minio/{minio-webhook.js → handler-express.js} +3 -2
- package/lib/files/minio/handler-express.js.map +1 -0
- package/lib/files/minio/handler-fastify.d.ts +4 -0
- package/lib/files/minio/handler-fastify.d.ts.map +1 -0
- package/lib/files/minio/handler-fastify.js +34 -0
- package/lib/files/minio/handler-fastify.js.map +1 -0
- package/lib/files/minio/{minio-adapter.d.ts → index.d.ts} +1 -1
- package/lib/files/minio/index.d.ts.map +1 -0
- package/lib/files/minio/{minio-adapter.js → index.js} +1 -1
- package/lib/files/minio/index.js.map +1 -0
- package/lib/filters/local-search.d.ts.map +1 -1
- package/lib/filters/local-search.js +3 -23
- package/lib/filters/local-search.js.map +1 -1
- package/lib/payment/apple-iap/adapter.d.ts +1 -1
- package/lib/payment/apple-iap/adapter.d.ts.map +1 -1
- package/lib/payment/apple-iap/adapter.js +5 -155
- package/lib/payment/apple-iap/adapter.js.map +1 -1
- package/lib/payment/apple-iap/fix-periods.d.ts +8 -0
- package/lib/payment/apple-iap/fix-periods.d.ts.map +1 -0
- package/lib/payment/apple-iap/fix-periods.js +22 -0
- package/lib/payment/apple-iap/fix-periods.js.map +1 -0
- package/lib/payment/apple-iap/handler-express.d.ts +3 -0
- package/lib/payment/apple-iap/handler-express.d.ts.map +1 -0
- package/lib/payment/apple-iap/handler-express.js +110 -0
- package/lib/payment/apple-iap/handler-express.js.map +1 -0
- package/lib/payment/apple-iap/handler-fastify.d.ts +4 -0
- package/lib/payment/apple-iap/handler-fastify.d.ts.map +1 -0
- package/lib/payment/apple-iap/handler-fastify.js +104 -0
- package/lib/payment/apple-iap/handler-fastify.js.map +1 -0
- package/lib/payment/apple-iap/index.d.ts +3 -1
- package/lib/payment/apple-iap/index.d.ts.map +1 -1
- package/lib/payment/apple-iap/index.js +2 -1
- package/lib/payment/apple-iap/index.js.map +1 -1
- package/lib/payment/apple-iap/module.d.ts +22 -0
- package/lib/payment/apple-iap/module.d.ts.map +1 -0
- package/lib/payment/apple-iap/{module/configureAppleTransactionsModule.js → module.js} +7 -2
- package/lib/payment/apple-iap/module.js.map +1 -0
- package/lib/payment/apple-iap/verify-receipt.d.ts +5 -0
- package/lib/payment/apple-iap/verify-receipt.d.ts.map +1 -0
- package/lib/payment/apple-iap/verify-receipt.js +26 -0
- package/lib/payment/apple-iap/verify-receipt.js.map +1 -0
- package/lib/payment/cryptopay/handler-express.d.ts +3 -0
- package/lib/payment/cryptopay/handler-express.d.ts.map +1 -0
- package/lib/payment/cryptopay/{middleware.js → handler-express.js} +3 -2
- package/lib/payment/cryptopay/{middleware.js.map → handler-express.js.map} +1 -1
- package/lib/payment/cryptopay/handler-fastify.d.ts +4 -0
- package/lib/payment/cryptopay/handler-fastify.d.ts.map +1 -0
- package/lib/payment/cryptopay/handler-fastify.js +69 -0
- package/lib/payment/cryptopay/handler-fastify.js.map +1 -0
- package/lib/payment/cryptopay/index.d.ts +4 -4
- package/lib/payment/cryptopay/index.d.ts.map +1 -1
- package/lib/payment/cryptopay/index.js +3 -4
- package/lib/payment/cryptopay/index.js.map +1 -1
- package/lib/payment/cryptopay/module.d.ts +54 -0
- package/lib/payment/cryptopay/module.d.ts.map +1 -0
- package/lib/payment/cryptopay/{module/configureCryptopayModule.js → module.js} +8 -3
- package/lib/payment/cryptopay/module.js.map +1 -0
- package/lib/payment/cryptopay/plugin.js +1 -1
- package/lib/payment/cryptopay/plugin.js.map +1 -1
- package/lib/payment/datatrans-v2/{middleware.d.ts → handler-express.d.ts} +1 -1
- package/lib/payment/datatrans-v2/handler-express.d.ts.map +1 -0
- package/lib/payment/datatrans-v2/{middleware.js → handler-express.js} +2 -2
- package/lib/payment/datatrans-v2/handler-express.js.map +1 -0
- package/lib/payment/datatrans-v2/handler-fastify.d.ts +3 -0
- package/lib/payment/datatrans-v2/handler-fastify.d.ts.map +1 -0
- package/lib/payment/datatrans-v2/handler-fastify.js +66 -0
- package/lib/payment/datatrans-v2/handler-fastify.js.map +1 -0
- package/lib/payment/datatrans-v2/index.d.ts +1 -1
- package/lib/payment/datatrans-v2/index.d.ts.map +1 -1
- package/lib/payment/datatrans-v2/index.js +0 -1
- package/lib/payment/datatrans-v2/index.js.map +1 -1
- package/lib/payment/paypal-checkout.js +2 -2
- package/lib/payment/paypal-checkout.js.map +1 -1
- package/lib/payment/payrexx/{middleware.d.ts → handler-express.d.ts} +1 -1
- package/lib/payment/payrexx/handler-express.d.ts.map +1 -0
- package/lib/payment/payrexx/{middleware.js → handler-express.js} +2 -2
- package/lib/payment/payrexx/handler-express.js.map +1 -0
- package/lib/payment/payrexx/handler-fastify.d.ts +3 -0
- package/lib/payment/payrexx/handler-fastify.d.ts.map +1 -0
- package/lib/payment/payrexx/handler-fastify.js +87 -0
- package/lib/payment/payrexx/handler-fastify.js.map +1 -0
- package/lib/payment/payrexx/index.d.ts +1 -1
- package/lib/payment/payrexx/index.d.ts.map +1 -1
- package/lib/payment/payrexx/index.js +0 -1
- package/lib/payment/payrexx/index.js.map +1 -1
- package/lib/payment/postfinance-checkout/{middleware.d.ts → handler-express.d.ts} +1 -1
- package/lib/payment/postfinance-checkout/handler-express.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/{middleware.js → handler-express.js} +1 -1
- package/lib/payment/postfinance-checkout/handler-express.js.map +1 -0
- package/lib/payment/postfinance-checkout/handler-fastify.d.ts +3 -0
- package/lib/payment/postfinance-checkout/handler-fastify.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/handler-fastify.js +39 -0
- package/lib/payment/postfinance-checkout/handler-fastify.js.map +1 -0
- package/lib/payment/postfinance-checkout/index.d.ts +1 -1
- package/lib/payment/postfinance-checkout/index.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/index.js +0 -1
- package/lib/payment/postfinance-checkout/index.js.map +1 -1
- package/lib/payment/saferpay/adapter.d.ts +0 -1
- package/lib/payment/saferpay/adapter.d.ts.map +1 -1
- package/lib/payment/saferpay/adapter.js +0 -1
- package/lib/payment/saferpay/adapter.js.map +1 -1
- package/lib/payment/saferpay/{middleware.d.ts → handler-express.d.ts} +1 -1
- package/lib/payment/saferpay/handler-express.d.ts.map +1 -0
- package/lib/payment/saferpay/{middleware.js → handler-express.js} +2 -2
- package/lib/payment/saferpay/handler-express.js.map +1 -0
- package/lib/payment/saferpay/handler-fastify.d.ts +3 -0
- package/lib/payment/saferpay/handler-fastify.d.ts.map +1 -0
- package/lib/payment/saferpay/handler-fastify.js +56 -0
- package/lib/payment/saferpay/handler-fastify.js.map +1 -0
- package/lib/payment/saferpay/index.d.ts +3 -2
- package/lib/payment/saferpay/index.d.ts.map +1 -1
- package/lib/payment/saferpay/index.js +2 -2
- package/lib/payment/saferpay/index.js.map +1 -1
- package/lib/payment/saferpay/module.d.ts +24 -0
- package/lib/payment/saferpay/module.d.ts.map +1 -0
- package/lib/payment/saferpay/{module/configureSaferpayTransactionsModule.js → module.js} +8 -3
- package/lib/payment/saferpay/module.js.map +1 -0
- package/lib/payment/stripe/{middleware.d.ts → handler-express.d.ts} +1 -1
- package/lib/payment/stripe/handler-express.d.ts.map +1 -0
- package/lib/payment/stripe/{middleware.js → handler-express.js} +2 -2
- package/lib/payment/stripe/handler-express.js.map +1 -0
- package/lib/payment/stripe/handler-fastify.d.ts +7 -0
- package/lib/payment/stripe/handler-fastify.d.ts.map +1 -0
- package/lib/payment/stripe/handler-fastify.js +112 -0
- package/lib/payment/stripe/handler-fastify.js.map +1 -0
- package/lib/payment/stripe/index.d.ts +1 -1
- package/lib/payment/stripe/index.d.ts.map +1 -1
- package/lib/payment/stripe/index.js +0 -1
- package/lib/payment/stripe/index.js.map +1 -1
- package/lib/presets/all-express.d.ts +7 -0
- package/lib/presets/all-express.d.ts.map +1 -0
- package/lib/presets/all-express.js +27 -0
- package/lib/presets/all-express.js.map +1 -0
- package/lib/presets/all-fastify.d.ts +7 -0
- package/lib/presets/all-fastify.d.ts.map +1 -0
- package/lib/presets/all-fastify.js +64 -0
- package/lib/presets/all-fastify.js.map +1 -0
- package/lib/presets/all.d.ts +69 -0
- package/lib/presets/all.d.ts.map +1 -0
- package/lib/presets/all.js +30 -0
- package/lib/presets/all.js.map +1 -0
- package/lib/presets/base-express.js +2 -2
- package/lib/presets/base-express.js.map +1 -1
- package/lib/presets/base-fastify.d.ts +4 -0
- package/lib/presets/base-fastify.d.ts.map +1 -0
- package/lib/presets/base-fastify.js +18 -0
- package/lib/presets/base-fastify.js.map +1 -0
- package/lib/presets/{base-modules.d.ts → base.d.ts} +3 -7
- package/lib/presets/base.d.ts.map +1 -0
- package/lib/presets/{base-modules.js → base.js} +3 -9
- package/lib/presets/base.js.map +1 -0
- package/lib/presets/crypto-express.js +2 -2
- package/lib/presets/crypto-express.js.map +1 -1
- package/lib/presets/crypto-fastify.d.ts +4 -0
- package/lib/presets/crypto-fastify.d.ts.map +1 -0
- package/lib/presets/crypto-fastify.js +12 -0
- package/lib/presets/crypto-fastify.js.map +1 -0
- package/lib/presets/crypto.d.ts +8 -0
- package/lib/presets/crypto.d.ts.map +1 -0
- package/lib/presets/crypto.js +9 -0
- package/lib/presets/crypto.js.map +1 -0
- package/lib/pricing/product-discount.js +2 -0
- package/lib/pricing/product-discount.js.map +1 -1
- package/lib/warehousing/eth-minter.js +2 -2
- package/lib/warehousing/eth-minter.js.map +1 -1
- package/lib/worker/BaseWorker.d.ts.map +1 -1
- package/lib/worker/BaseWorker.js +1 -3
- package/lib/worker/BaseWorker.js.map +1 -1
- package/lib/worker/error-notifications.js +1 -1
- package/lib/worker/error-notifications.js.map +1 -1
- package/lib/worker/zombie-killer.js +4 -1
- package/lib/worker/zombie-killer.js.map +1 -1
- package/package.json +41 -37
- package/src/files/gridfs/{gridfs-adapter.ts → adapter.ts} +1 -3
- package/src/files/gridfs/{gridfs-webhook.ts → handler-express.ts} +5 -3
- package/src/files/gridfs/handler-fastify.ts +122 -0
- package/src/files/gridfs/index.ts +4 -29
- package/src/files/gridfs/module.ts +39 -0
- package/src/files/minio/{minio-webhook.ts → handler-express.ts} +3 -1
- package/src/files/minio/handler-fastify.ts +44 -0
- package/src/filters/local-search.ts +3 -25
- package/src/payment/apple-iap/adapter.ts +6 -193
- package/src/payment/apple-iap/fix-periods.ts +34 -0
- package/src/payment/apple-iap/handler-express.ts +134 -0
- package/src/payment/apple-iap/handler-fastify.ts +137 -0
- package/src/payment/apple-iap/index.ts +5 -1
- package/src/payment/apple-iap/module.ts +29 -0
- package/src/payment/apple-iap/verify-receipt.ts +27 -0
- package/src/payment/cryptopay/{middleware.ts → handler-express.ts} +5 -5
- package/src/payment/cryptopay/handler-fastify.ts +89 -0
- package/src/payment/cryptopay/index.ts +5 -4
- package/src/payment/cryptopay/{module/configureCryptopayModule.ts → module.ts} +38 -39
- package/src/payment/cryptopay/plugin.ts +3 -3
- package/src/payment/datatrans-v2/{middleware.ts → handler-express.ts} +1 -1
- package/src/payment/datatrans-v2/handler-fastify.ts +88 -0
- package/src/payment/datatrans-v2/index.ts +0 -2
- package/src/payment/paypal-checkout.ts +2 -2
- package/src/payment/payrexx/{middleware.ts → handler-express.ts} +1 -1
- package/src/payment/payrexx/handler-fastify.ts +105 -0
- package/src/payment/payrexx/index.ts +0 -2
- package/src/payment/postfinance-checkout/handler-fastify.ts +52 -0
- package/src/payment/postfinance-checkout/index.ts +0 -2
- package/src/payment/saferpay/adapter.ts +2 -4
- package/src/payment/saferpay/{middleware.ts → handler-express.ts} +3 -3
- package/src/payment/saferpay/handler-fastify.ts +75 -0
- package/src/payment/saferpay/index.ts +5 -2
- package/src/payment/saferpay/{module/configureSaferpayTransactionsModule.ts → module.ts} +11 -3
- package/src/payment/stripe/{middleware.ts → handler-express.ts} +1 -1
- package/src/payment/stripe/handler-fastify.ts +139 -0
- package/src/payment/stripe/index.ts +0 -2
- package/src/presets/all-express.ts +51 -0
- package/src/presets/all-fastify.ts +87 -0
- package/src/presets/all.ts +35 -0
- package/src/presets/base-express.ts +2 -2
- package/src/presets/base-fastify.ts +20 -0
- package/src/presets/{base-modules.ts → base.ts} +3 -17
- package/src/presets/crypto-express.ts +2 -2
- package/src/presets/crypto-fastify.ts +15 -0
- package/src/presets/crypto.ts +11 -0
- package/src/pricing/product-discount.ts +2 -0
- package/src/warehousing/eth-minter.ts +2 -2
- package/src/worker/BaseWorker.ts +1 -4
- package/src/worker/error-notifications.ts +1 -1
- package/src/worker/zombie-killer.ts +4 -4
- package/lib/files/gridfs/gridfs-adapter.d.ts.map +0 -1
- package/lib/files/gridfs/gridfs-adapter.js.map +0 -1
- package/lib/files/gridfs/gridfs-webhook.d.ts +0 -11
- package/lib/files/gridfs/gridfs-webhook.d.ts.map +0 -1
- package/lib/files/gridfs/gridfs-webhook.js.map +0 -1
- package/lib/files/minio/minio-adapter.d.ts.map +0 -1
- package/lib/files/minio/minio-adapter.js.map +0 -1
- package/lib/files/minio/minio-webhook.d.ts +0 -2
- package/lib/files/minio/minio-webhook.d.ts.map +0 -1
- package/lib/files/minio/minio-webhook.js.map +0 -1
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.d.ts +0 -9
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.d.ts.map +0 -1
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.js.map +0 -1
- package/lib/payment/cryptopay/middleware.d.ts +0 -2
- package/lib/payment/cryptopay/middleware.d.ts.map +0 -1
- package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts +0 -25
- package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts.map +0 -1
- package/lib/payment/cryptopay/module/configureCryptopayModule.js.map +0 -1
- package/lib/payment/datatrans-v2/middleware.d.ts.map +0 -1
- package/lib/payment/datatrans-v2/middleware.js.map +0 -1
- package/lib/payment/payrexx/middleware.d.ts.map +0 -1
- package/lib/payment/payrexx/middleware.js.map +0 -1
- package/lib/payment/postfinance-checkout/middleware.d.ts.map +0 -1
- package/lib/payment/postfinance-checkout/middleware.js.map +0 -1
- package/lib/payment/saferpay/middleware.d.ts.map +0 -1
- package/lib/payment/saferpay/middleware.js.map +0 -1
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.d.ts +0 -10
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.d.ts.map +0 -1
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.js.map +0 -1
- package/lib/payment/stripe/middleware.d.ts.map +0 -1
- package/lib/payment/stripe/middleware.js.map +0 -1
- package/lib/plugins-index.d.ts +0 -18
- package/lib/plugins-index.d.ts.map +0 -1
- package/lib/plugins-index.js +0 -52
- package/lib/plugins-index.js.map +0 -1
- package/lib/presets/base-modules.d.ts.map +0 -1
- package/lib/presets/base-modules.js.map +0 -1
- package/lib/presets/crypto-modules.d.ts +0 -11
- package/lib/presets/crypto-modules.d.ts.map +0 -1
- package/lib/presets/crypto-modules.js +0 -14
- package/lib/presets/crypto-modules.js.map +0 -1
- package/src/payment/apple-iap/module/configureAppleTransactionsModule.ts +0 -25
- package/src/plugins-index.ts +0 -90
- package/src/presets/crypto-modules.ts +0 -23
- /package/src/files/minio/{minio-adapter.ts → index.ts} +0 -0
- /package/src/payment/postfinance-checkout/{middleware.ts → handler-express.ts} +0 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
2
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
+
import { Context } from '@unchainedshop/api';
|
|
4
|
+
import { OrderStatus } from '@unchainedshop/core-orders';
|
|
5
|
+
import { CryptopayModule } from './module.js';
|
|
6
|
+
import { ProductPriceRate } from '@unchainedshop/core-products';
|
|
7
|
+
|
|
8
|
+
const { CRYPTOPAY_SECRET, CRYPTOPAY_MAX_RATE_AGE = '360' } = process.env;
|
|
9
|
+
|
|
10
|
+
const logger = createLogger('unchained:core-payment:cryptopay');
|
|
11
|
+
|
|
12
|
+
const cryptopayHandler: RouteHandlerMethod = async (
|
|
13
|
+
req: FastifyRequest & {
|
|
14
|
+
unchainedContext: Context & {
|
|
15
|
+
modules: CryptopayModule;
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
reply,
|
|
19
|
+
) => {
|
|
20
|
+
const { modules, services } = req.unchainedContext;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const { secret, price, wallet, ping } = req.body as Record<string, any>;
|
|
24
|
+
if (secret !== CRYPTOPAY_SECRET) {
|
|
25
|
+
logger.warn(`Cryptopay Plugin: Invalid Cryptopay Secret provided`);
|
|
26
|
+
throw new Error('Secret invalid');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (wallet) {
|
|
30
|
+
const { address, blockHeight, amount, contract, decimals, currency } = wallet;
|
|
31
|
+
const { orderPaymentId } = await modules.cryptopay.updateWalletAddress({
|
|
32
|
+
addressId: address,
|
|
33
|
+
blockHeight,
|
|
34
|
+
amount,
|
|
35
|
+
contract,
|
|
36
|
+
decimals,
|
|
37
|
+
currency,
|
|
38
|
+
});
|
|
39
|
+
const orderPayment =
|
|
40
|
+
orderPaymentId &&
|
|
41
|
+
(await modules.orders.payments.findOrderPayment({
|
|
42
|
+
orderPaymentId,
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
if (orderPayment) {
|
|
46
|
+
// Try to process order to next status
|
|
47
|
+
// TODO: Not sure if it's correct to use processOrder here if status is PENDING!
|
|
48
|
+
const order = await modules.orders.findOrder({ orderId: orderPayment.orderId });
|
|
49
|
+
if (order.status === null) {
|
|
50
|
+
await services.orders.checkoutOrder(order._id, {});
|
|
51
|
+
} else if (order.status === OrderStatus.PENDING) {
|
|
52
|
+
await services.orders.processOrder(order, {});
|
|
53
|
+
} else {
|
|
54
|
+
throw new Error('Already processed');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (price) {
|
|
60
|
+
const { baseCurrency, token, rate, timestamp } = price;
|
|
61
|
+
|
|
62
|
+
const timestampDate = new Date(timestamp);
|
|
63
|
+
const expiresAt = new Date(new Date().getTime() + parseInt(CRYPTOPAY_MAX_RATE_AGE, 10) * 1000);
|
|
64
|
+
|
|
65
|
+
const rateData: ProductPriceRate = {
|
|
66
|
+
baseCurrency,
|
|
67
|
+
quoteCurrency: token,
|
|
68
|
+
rate,
|
|
69
|
+
expiresAt,
|
|
70
|
+
timestamp: timestampDate,
|
|
71
|
+
};
|
|
72
|
+
logger.info(`update rate ${JSON.stringify(price)}, ${JSON.stringify(rateData)}`);
|
|
73
|
+
await modules.products.prices.rates.updateRates([rateData]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (ping) {
|
|
77
|
+
const { blockHeight, currency } = ping;
|
|
78
|
+
await modules.cryptopay.updateMostRecentBlock(currency, blockHeight);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
reply.status(200);
|
|
82
|
+
return reply.send({ success: true });
|
|
83
|
+
} catch (e) {
|
|
84
|
+
reply.status(500);
|
|
85
|
+
return reply.send({ success: false, error: e.message });
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default cryptopayHandler;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import './plugin.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
export * from './module/configureCryptopayModule.js';
|
|
3
|
+
import cryptopayModules, { type CryptopayModule } from './module.js';
|
|
5
4
|
|
|
6
|
-
export
|
|
5
|
+
export type { CryptopayModule };
|
|
6
|
+
|
|
7
|
+
export default cryptopayModules;
|
|
@@ -1,38 +1,14 @@
|
|
|
1
1
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
|
-
import { CryptopayTransaction, CryptopayTransactionsCollection } from '
|
|
2
|
+
import { CryptopayTransaction, CryptopayTransactionsCollection } from './db/CryptopayTransactions.js';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
getWalletAddress: (addressId: string) => Promise<CryptopayTransaction>;
|
|
6
|
-
getWalletAddressesByOrderPaymentId: (orderPaymentId: string) => Promise<CryptopayTransaction[]>;
|
|
7
|
-
updateMostRecentBlock: (currency: string, blockHeight: number) => Promise<void>;
|
|
8
|
-
updateWalletAddress: (walletData: {
|
|
9
|
-
addressId: string;
|
|
10
|
-
blockHeight: number;
|
|
11
|
-
amount: string;
|
|
12
|
-
contract: string;
|
|
13
|
-
currency: string;
|
|
14
|
-
decimals: number;
|
|
15
|
-
}) => Promise<CryptopayTransaction>;
|
|
16
|
-
mapOrderPaymentToWalletAddress: (walletData: {
|
|
17
|
-
addressId: string;
|
|
18
|
-
contract: string;
|
|
19
|
-
currency: string;
|
|
20
|
-
orderPaymentId: string;
|
|
21
|
-
}) => Promise<CryptopayTransaction>;
|
|
22
|
-
getNextDerivationNumber: (currency: string) => Promise<number>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const configureCryptopayModule = ({ db }): CryptopayModule => {
|
|
4
|
+
const configureCryptopayModule = ({ db }) => {
|
|
26
5
|
const CryptoTransactions = CryptopayTransactionsCollection(db);
|
|
27
6
|
|
|
28
|
-
const getWalletAddress
|
|
7
|
+
const getWalletAddress = async (addressId: string): Promise<CryptopayTransaction> => {
|
|
29
8
|
return CryptoTransactions.findOne({ _id: addressId.toLowerCase() });
|
|
30
9
|
};
|
|
31
10
|
|
|
32
|
-
const updateMostRecentBlock
|
|
33
|
-
currency,
|
|
34
|
-
blockHeight,
|
|
35
|
-
) => {
|
|
11
|
+
const updateMostRecentBlock = async (currency: string, blockHeight: number): Promise<void> => {
|
|
36
12
|
await CryptoTransactions.updateMany(
|
|
37
13
|
{
|
|
38
14
|
currency,
|
|
@@ -45,12 +21,17 @@ export const configureCryptopayModule = ({ db }): CryptopayModule => {
|
|
|
45
21
|
);
|
|
46
22
|
};
|
|
47
23
|
|
|
48
|
-
const mapOrderPaymentToWalletAddress
|
|
24
|
+
const mapOrderPaymentToWalletAddress = async ({
|
|
49
25
|
addressId,
|
|
50
26
|
contract,
|
|
51
27
|
currency,
|
|
52
28
|
orderPaymentId,
|
|
53
|
-
}
|
|
29
|
+
}: {
|
|
30
|
+
addressId: string;
|
|
31
|
+
contract: string;
|
|
32
|
+
currency: string;
|
|
33
|
+
orderPaymentId: string;
|
|
34
|
+
}): Promise<CryptopayTransaction> => {
|
|
54
35
|
await CryptoTransactions.updateOne(
|
|
55
36
|
{
|
|
56
37
|
_id: addressId.toLowerCase(),
|
|
@@ -77,25 +58,33 @@ export const configureCryptopayModule = ({ db }): CryptopayModule => {
|
|
|
77
58
|
return CryptoTransactions.findOne({ _id: addressId.toLowerCase() });
|
|
78
59
|
};
|
|
79
60
|
|
|
80
|
-
const getNextDerivationNumber
|
|
61
|
+
const getNextDerivationNumber = async (currency: string): Promise<number> => {
|
|
81
62
|
return (await CryptoTransactions.countDocuments({ currency })) + 1;
|
|
82
63
|
};
|
|
83
64
|
|
|
84
|
-
const getWalletAddressesByOrderPaymentId
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
65
|
+
const getWalletAddressesByOrderPaymentId = async (
|
|
66
|
+
orderPaymentId: string,
|
|
67
|
+
): Promise<CryptopayTransaction[]> => {
|
|
68
|
+
return CryptoTransactions.find({
|
|
69
|
+
orderPaymentId,
|
|
70
|
+
}).toArray();
|
|
71
|
+
};
|
|
90
72
|
|
|
91
|
-
const updateWalletAddress
|
|
73
|
+
const updateWalletAddress = async ({
|
|
92
74
|
addressId,
|
|
93
75
|
blockHeight,
|
|
94
76
|
amount,
|
|
95
77
|
contract,
|
|
96
78
|
currency,
|
|
97
79
|
decimals,
|
|
98
|
-
}
|
|
80
|
+
}: {
|
|
81
|
+
addressId: string;
|
|
82
|
+
blockHeight: number;
|
|
83
|
+
amount: string;
|
|
84
|
+
contract: string;
|
|
85
|
+
currency: string;
|
|
86
|
+
decimals: number;
|
|
87
|
+
}): Promise<CryptopayTransaction> => {
|
|
99
88
|
await CryptoTransactions.updateOne(
|
|
100
89
|
{
|
|
101
90
|
_id: addressId.toLowerCase(),
|
|
@@ -130,3 +119,13 @@ export const configureCryptopayModule = ({ db }): CryptopayModule => {
|
|
|
130
119
|
getWalletAddressesByOrderPaymentId,
|
|
131
120
|
};
|
|
132
121
|
};
|
|
122
|
+
|
|
123
|
+
export default {
|
|
124
|
+
cryptopay: {
|
|
125
|
+
configure: configureCryptopayModule,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export type CryptopayModule = {
|
|
130
|
+
cryptopay: ReturnType<typeof configureCryptopayModule>;
|
|
131
|
+
};
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
PaymentError,
|
|
11
11
|
OrderPricingSheet,
|
|
12
12
|
} from '@unchainedshop/core';
|
|
13
|
-
import { CryptopayModule } from './module
|
|
13
|
+
import { CryptopayModule } from './module.js';
|
|
14
14
|
|
|
15
15
|
const logger = createLogger('unchained:core-payment:cryptopay');
|
|
16
16
|
|
|
@@ -59,7 +59,7 @@ const Cryptopay: IPaymentAdapter = {
|
|
|
59
59
|
},
|
|
60
60
|
|
|
61
61
|
actions: (config, context) => {
|
|
62
|
-
const { modules } = context as typeof context & { modules:
|
|
62
|
+
const { modules } = context as typeof context & { modules: CryptopayModule };
|
|
63
63
|
|
|
64
64
|
const setConversionRates = async (currencyCode: string, existingAddresses: any[]) => {
|
|
65
65
|
const originCurrencyObj = await modules.currencies.findCurrency({ isoCode: currencyCode });
|
|
@@ -267,7 +267,7 @@ const Cryptopay: IPaymentAdapter = {
|
|
|
267
267
|
|
|
268
268
|
1. Check that generated crypto addresses are unique and no order payments share the same crypto address, only repeat when derivations completely exhausted.
|
|
269
269
|
2. When derivation path get's close to "exhaustedness" (95%), send a special e-mail alert to tell the user should generate a new xpub.
|
|
270
|
-
3. Extend the
|
|
270
|
+
3. Extend the handler to report the highest seen block number so we can calculate confirmations,
|
|
271
271
|
4. Order rejection should trigger an e-mail advising the vendor to actively send back funds
|
|
272
272
|
|
|
273
273
|
To allow paying in ETH or BTC or Tokens for CHF orders, it should work like this:
|
|
@@ -9,7 +9,7 @@ const {
|
|
|
9
9
|
DATATRANS_SECURITY = Security.DYNAMIC_SIGN,
|
|
10
10
|
} = process.env;
|
|
11
11
|
|
|
12
|
-
const logger = createLogger('unchained:core-payment:datatrans:
|
|
12
|
+
const logger = createLogger('unchained:core-payment:datatrans:handler');
|
|
13
13
|
|
|
14
14
|
export const datatransHandler = async (req, res) => {
|
|
15
15
|
const resolvedContext = req.unchainedContext as Context;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
2
|
+
import { Context } from '@unchainedshop/api';
|
|
3
|
+
import generateSignature, { Security } from './generateSignature.js';
|
|
4
|
+
import { StatusResponseSuccess } from './api/types.js';
|
|
5
|
+
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
DATATRANS_SIGN_KEY,
|
|
9
|
+
DATATRANS_SIGN2_KEY,
|
|
10
|
+
DATATRANS_SECURITY = Security.DYNAMIC_SIGN,
|
|
11
|
+
} = process.env;
|
|
12
|
+
|
|
13
|
+
const logger = createLogger('unchained:core-payment:datatrans:handler');
|
|
14
|
+
|
|
15
|
+
export const datatransHandler: RouteHandlerMethod = async (
|
|
16
|
+
req: FastifyRequest & {
|
|
17
|
+
unchainedContext: Context;
|
|
18
|
+
},
|
|
19
|
+
reply,
|
|
20
|
+
) => {
|
|
21
|
+
const resolvedContext = req.unchainedContext as Context;
|
|
22
|
+
const { modules, services } = resolvedContext;
|
|
23
|
+
const signature = req.headers['datatrans-signature'] as string;
|
|
24
|
+
if (signature) {
|
|
25
|
+
const [rawTimestamp, rawHash] = signature.split(',');
|
|
26
|
+
const [, hash] = rawHash.split('=');
|
|
27
|
+
const [, timestamp] = rawTimestamp.split('=');
|
|
28
|
+
|
|
29
|
+
const comparableSignature = await generateSignature({
|
|
30
|
+
security: DATATRANS_SECURITY as any,
|
|
31
|
+
signKey: DATATRANS_SIGN2_KEY || DATATRANS_SIGN_KEY,
|
|
32
|
+
})(timestamp, req.body as string);
|
|
33
|
+
|
|
34
|
+
if (hash !== comparableSignature) {
|
|
35
|
+
logger.error(`hash mismatch: ${signature} / ${comparableSignature}`, req.body as string);
|
|
36
|
+
reply.status(403);
|
|
37
|
+
return reply.send('Hash mismatch');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const transaction: StatusResponseSuccess = JSON.parse(req.body as string) as StatusResponseSuccess;
|
|
41
|
+
|
|
42
|
+
logger.info(`received request`, {
|
|
43
|
+
type: transaction.type,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (transaction.status === 'authorized') {
|
|
47
|
+
const userId = transaction.refno2;
|
|
48
|
+
const referenceId = Buffer.from(transaction.refno, 'base64').toString('hex');
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
if (transaction.type === 'card_check') {
|
|
52
|
+
const paymentProviderId = referenceId;
|
|
53
|
+
const paymentCredentials = await services.orders.registerPaymentCredentials(
|
|
54
|
+
paymentProviderId,
|
|
55
|
+
{ userId, transactionContext: { transactionId: transaction.transactionId } },
|
|
56
|
+
);
|
|
57
|
+
logger.info(`registered payment credentials for ${userId}`, {
|
|
58
|
+
userId,
|
|
59
|
+
});
|
|
60
|
+
reply.status(200);
|
|
61
|
+
return reply.send(paymentCredentials);
|
|
62
|
+
}
|
|
63
|
+
if (transaction.type === 'payment') {
|
|
64
|
+
const orderPaymentId = referenceId;
|
|
65
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
66
|
+
orderPaymentId,
|
|
67
|
+
});
|
|
68
|
+
if (!orderPayment) throw new Error(`Order Payment with id ${orderPaymentId} not found`);
|
|
69
|
+
|
|
70
|
+
const order = await services.orders.checkoutOrder(orderPayment.orderId, {
|
|
71
|
+
paymentContext: { userId, transactionId: transaction.transactionId },
|
|
72
|
+
});
|
|
73
|
+
reply.status(200);
|
|
74
|
+
logger.info(`confirmed checkout for order ${order.orderNumber}`, {
|
|
75
|
+
orderId: order._id,
|
|
76
|
+
});
|
|
77
|
+
return reply.send(order);
|
|
78
|
+
}
|
|
79
|
+
} catch (e) {
|
|
80
|
+
logger.error(`rejected to checkout with message`, e);
|
|
81
|
+
reply.status(500);
|
|
82
|
+
return reply.send({ name: e.name, code: e.code, message: e.message });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
reply.status(404);
|
|
87
|
+
return reply.send();
|
|
88
|
+
};
|
|
@@ -38,9 +38,9 @@ const environment = () => {
|
|
|
38
38
|
const PaypalCheckout: IPaymentAdapter = {
|
|
39
39
|
...PaymentAdapter,
|
|
40
40
|
|
|
41
|
-
key: '
|
|
41
|
+
key: 'shop.unchained.payment.paypal',
|
|
42
42
|
label: 'Paypal',
|
|
43
|
-
version: '1.0.
|
|
43
|
+
version: '1.0.1',
|
|
44
44
|
|
|
45
45
|
initialConfiguration: [],
|
|
46
46
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context } from '@unchainedshop/api';
|
|
2
2
|
import { createLogger } from '@unchainedshop/logger';
|
|
3
3
|
|
|
4
|
-
const logger = createLogger('unchained:core-payment:payrexx:
|
|
4
|
+
const logger = createLogger('unchained:core-payment:payrexx:handler');
|
|
5
5
|
|
|
6
6
|
export const payrexxHandler = async (request, response) => {
|
|
7
7
|
const resolvedContext = request.unchainedContext as Context;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Context } from '@unchainedshop/api';
|
|
2
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
+
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
4
|
+
|
|
5
|
+
const logger = createLogger('unchained:core-payment:payrexx:handler');
|
|
6
|
+
|
|
7
|
+
export const payrexxHandler: RouteHandlerMethod = async (
|
|
8
|
+
request: FastifyRequest & {
|
|
9
|
+
unchainedContext: Context;
|
|
10
|
+
},
|
|
11
|
+
reply,
|
|
12
|
+
) => {
|
|
13
|
+
const resolvedContext = request.unchainedContext as Context;
|
|
14
|
+
const { modules, services } = resolvedContext;
|
|
15
|
+
|
|
16
|
+
const { transaction } = request.body as Record<string, any>;
|
|
17
|
+
|
|
18
|
+
if (!transaction) {
|
|
19
|
+
logger.info(`unhandled event type`, {
|
|
20
|
+
type: Object.keys(request.body).join(','),
|
|
21
|
+
});
|
|
22
|
+
reply.status(200);
|
|
23
|
+
return reply.send(
|
|
24
|
+
JSON.stringify({
|
|
25
|
+
ignored: true,
|
|
26
|
+
message: `Unhandled event type: ${Object.keys(request.body).join(',')}. Supported type: transaction`,
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
if (transaction.referenceId === '__IGNORE_WEBHOOK__' || transaction.status === 'waiting') {
|
|
31
|
+
// Ignore confirmed transactions, because those hooks are generated through calling the confirm()
|
|
32
|
+
// method in the payment adapter and could lead to double bookings.
|
|
33
|
+
logger.info(`unhandled transaction state: ${transaction.status}`);
|
|
34
|
+
reply.status(200);
|
|
35
|
+
return reply.send(
|
|
36
|
+
JSON.stringify({
|
|
37
|
+
ignored: true,
|
|
38
|
+
message: `Unhandled transaction state: ${transaction.status}`,
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
logger.info(`Processing event`, {
|
|
44
|
+
transactionId: transaction.id,
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
if (transaction.preAuthorizationId) {
|
|
48
|
+
// Pre-Authorization Flow, referenceId is a userId
|
|
49
|
+
const { referenceId: paymentProviderId, invoice } = transaction;
|
|
50
|
+
const userId = '';
|
|
51
|
+
logger.info(`register credentials for: ${userId}`);
|
|
52
|
+
await services.orders.registerPaymentCredentials(paymentProviderId, {
|
|
53
|
+
userId,
|
|
54
|
+
transactionContext: { gatewayId: invoice.paymentRequestId },
|
|
55
|
+
});
|
|
56
|
+
logger.info(`registration successful`, {
|
|
57
|
+
paymentProviderId,
|
|
58
|
+
userId,
|
|
59
|
+
});
|
|
60
|
+
reply.status(200);
|
|
61
|
+
return reply.send(
|
|
62
|
+
JSON.stringify({
|
|
63
|
+
message: 'registration successful',
|
|
64
|
+
paymentProviderId,
|
|
65
|
+
userId,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
} else {
|
|
69
|
+
const { referenceId: orderPaymentId, invoice } = transaction;
|
|
70
|
+
logger.info(`checkout with orderPaymentId: ${orderPaymentId}`);
|
|
71
|
+
await modules.orders.payments.logEvent(orderPaymentId, {
|
|
72
|
+
transactionId: transaction.id,
|
|
73
|
+
});
|
|
74
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
75
|
+
orderPaymentId,
|
|
76
|
+
});
|
|
77
|
+
if (!orderPayment) {
|
|
78
|
+
throw new Error(`order payment not found with orderPaymentId: ${orderPaymentId}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const order = await services.orders.checkoutOrder(orderPayment.orderId, {
|
|
82
|
+
paymentContext: {
|
|
83
|
+
gatewayId: invoice.paymentRequestId,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
logger.info(`checkout successful`, {
|
|
87
|
+
orderPaymentId,
|
|
88
|
+
orderId: order._id,
|
|
89
|
+
});
|
|
90
|
+
reply.status(200);
|
|
91
|
+
return reply.send(
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
message: 'checkout successful',
|
|
94
|
+
orderId: order._id,
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
logger.error(error, {
|
|
100
|
+
transactionId: transaction.id,
|
|
101
|
+
});
|
|
102
|
+
reply.status(500);
|
|
103
|
+
return reply.send(error.message);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Context } from '@unchainedshop/api';
|
|
2
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
+
import { WebhookData } from './types.js';
|
|
4
|
+
import { getTransaction, getTransactionCompletion } from './api.js';
|
|
5
|
+
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
6
|
+
|
|
7
|
+
const logger = createLogger('unchained:core-payment:postfinance-checkout');
|
|
8
|
+
|
|
9
|
+
export const postfinanceCheckoutHandler: RouteHandlerMethod = async (
|
|
10
|
+
req: FastifyRequest & {
|
|
11
|
+
unchainedContext: Context;
|
|
12
|
+
},
|
|
13
|
+
reply,
|
|
14
|
+
) => {
|
|
15
|
+
const context = req.unchainedContext as Context;
|
|
16
|
+
const { services, modules } = 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 modules.orders.payments.findOrderPayment({
|
|
26
|
+
orderPaymentId,
|
|
27
|
+
});
|
|
28
|
+
if (!orderPayment) throw new Error('Order Payment not found');
|
|
29
|
+
|
|
30
|
+
const order = await services.orders.checkoutOrder(orderPayment.orderId, {
|
|
31
|
+
paymentContext: {
|
|
32
|
+
transactionId: transactionCompletion.linkedTransaction,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
logger.info(
|
|
36
|
+
`PostFinance Checkout Webhook: Transaction ${transactionCompletion.linkedTransaction} marked order payment ID ${transaction.metaData.orderPaymentId} as paid`,
|
|
37
|
+
);
|
|
38
|
+
reply.status(200);
|
|
39
|
+
return reply.send(`Order marked as paid: ${order.orderNumber}`);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
logger.error(`PostFinance Checkout Webhook: Unchained rejected to checkout with message`, e);
|
|
42
|
+
reply.status(500);
|
|
43
|
+
return reply.send({ name: e.name, code: e.code, message: e.message });
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
logger.error(
|
|
47
|
+
`PostFinance Checkout Webhook: Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`,
|
|
48
|
+
);
|
|
49
|
+
reply.status(404);
|
|
50
|
+
return reply.send();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
2
|
import { PaymentPageInitializeInput, SaferpayClient } from './api/index.js';
|
|
3
3
|
import { buildSignature } from './buildSignature.js';
|
|
4
|
-
import { SaferpayTransactionsModule } from './module
|
|
4
|
+
import { SaferpayTransactionsModule } from './module.js';
|
|
5
5
|
import {
|
|
6
6
|
OrderPricingSheet,
|
|
7
7
|
IPaymentAdapter,
|
|
@@ -10,8 +10,6 @@ import {
|
|
|
10
10
|
PaymentError,
|
|
11
11
|
} from '@unchainedshop/core';
|
|
12
12
|
|
|
13
|
-
export * from './middleware.js';
|
|
14
|
-
|
|
15
13
|
const {
|
|
16
14
|
SAFERPAY_BASE_URL = 'https://test.saferpay.com/api',
|
|
17
15
|
SAFERPAY_CUSTOMER_ID,
|
|
@@ -48,7 +46,7 @@ export const WordlineSaferpay: IPaymentAdapter = {
|
|
|
48
46
|
|
|
49
47
|
actions: (config, context) => {
|
|
50
48
|
const { modules } = context as typeof context & {
|
|
51
|
-
modules:
|
|
49
|
+
modules: SaferpayTransactionsModule;
|
|
52
50
|
};
|
|
53
51
|
|
|
54
52
|
const createSaferPayClient = () => {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Context } from '@unchainedshop/api';
|
|
2
2
|
import { createLogger } from '@unchainedshop/logger';
|
|
3
3
|
import { buildSignature } from './buildSignature.js';
|
|
4
|
-
import { SaferpayTransactionsModule } from './module
|
|
4
|
+
import { SaferpayTransactionsModule } from './module.js';
|
|
5
5
|
|
|
6
|
-
const logger = createLogger('unchained:core-payment:saferpay:
|
|
6
|
+
const logger = createLogger('unchained:core-payment:saferpay:handler');
|
|
7
7
|
|
|
8
8
|
export const saferpayHandler = async (request, response) => {
|
|
9
9
|
const resolvedContext = request.unchainedContext as Context & {
|
|
10
|
-
modules:
|
|
10
|
+
modules: SaferpayTransactionsModule;
|
|
11
11
|
};
|
|
12
12
|
const { modules, services } = resolvedContext;
|
|
13
13
|
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Context } from '@unchainedshop/api';
|
|
2
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
+
import { buildSignature } from './buildSignature.js';
|
|
4
|
+
import { SaferpayTransactionsModule } from './module.js';
|
|
5
|
+
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
6
|
+
|
|
7
|
+
const logger = createLogger('unchained:core-payment:saferpay:handler');
|
|
8
|
+
|
|
9
|
+
export const saferpayHandler: RouteHandlerMethod = async (
|
|
10
|
+
request: FastifyRequest & {
|
|
11
|
+
unchainedContext: Context;
|
|
12
|
+
},
|
|
13
|
+
reply,
|
|
14
|
+
) => {
|
|
15
|
+
const resolvedContext = request.unchainedContext as Context & {
|
|
16
|
+
modules: SaferpayTransactionsModule;
|
|
17
|
+
};
|
|
18
|
+
const { modules, services } = resolvedContext;
|
|
19
|
+
|
|
20
|
+
const { orderPaymentId, signature, transactionId } = request.query as Record<string, string>;
|
|
21
|
+
const isValidRequest =
|
|
22
|
+
typeof orderPaymentId === 'string' &&
|
|
23
|
+
typeof signature === 'string' &&
|
|
24
|
+
typeof transactionId === 'string' &&
|
|
25
|
+
orderPaymentId &&
|
|
26
|
+
transactionId &&
|
|
27
|
+
signature;
|
|
28
|
+
|
|
29
|
+
if (!isValidRequest) {
|
|
30
|
+
logger.warn(`orderPaymentId missing in query`);
|
|
31
|
+
reply.status(404);
|
|
32
|
+
return reply.send();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
logger.info(`checkout with orderPaymentId: ${orderPaymentId}`);
|
|
37
|
+
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
38
|
+
orderPaymentId,
|
|
39
|
+
});
|
|
40
|
+
if (!orderPayment) {
|
|
41
|
+
throw new Error(`order payment not found with orderPaymentId: ${orderPaymentId}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const correctSignature = await buildSignature(transactionId, orderPaymentId);
|
|
45
|
+
|
|
46
|
+
if (correctSignature !== signature) {
|
|
47
|
+
throw new Error('Invalid signature');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const order = await services.orders.checkoutOrder(orderPayment.orderId, {
|
|
51
|
+
paymentContext: {
|
|
52
|
+
transactionId,
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
logger.info(`checkout successful`, {
|
|
56
|
+
orderPaymentId,
|
|
57
|
+
orderId: order._id,
|
|
58
|
+
});
|
|
59
|
+
reply.status(200);
|
|
60
|
+
return reply.send(
|
|
61
|
+
JSON.stringify({
|
|
62
|
+
message: 'checkout successful',
|
|
63
|
+
orderPaymentId,
|
|
64
|
+
orderId: order._id,
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logger.error(error, {
|
|
69
|
+
orderPaymentId,
|
|
70
|
+
transactionId,
|
|
71
|
+
});
|
|
72
|
+
reply.status(500);
|
|
73
|
+
return reply.send(error.message);
|
|
74
|
+
}
|
|
75
|
+
};
|