@unchainedshop/plugins 3.1.14 → 4.0.0-rc.10
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/delivery/pick-mup.js +0 -2
- package/lib/delivery/pick-mup.js.map +1 -1
- package/lib/files/gridfs/adapter.d.ts.map +1 -1
- package/lib/files/gridfs/adapter.js +3 -2
- package/lib/files/gridfs/adapter.js.map +1 -1
- package/lib/files/gridfs/handler-express.d.ts.map +1 -1
- package/lib/files/gridfs/handler-express.js +6 -2
- package/lib/files/gridfs/handler-express.js.map +1 -1
- package/lib/files/gridfs/handler-fastify.d.ts.map +1 -1
- package/lib/files/gridfs/handler-fastify.js +5 -2
- package/lib/files/gridfs/handler-fastify.js.map +1 -1
- package/lib/files/gridfs/module.d.ts +2 -2
- package/lib/files/gridfs/module.d.ts.map +1 -1
- package/lib/filters/local-search.d.ts.map +1 -1
- package/lib/filters/local-search.js +9 -2
- package/lib/filters/local-search.js.map +1 -1
- package/lib/filters/strict-equal.js +18 -0
- package/lib/filters/strict-equal.js.map +1 -1
- package/lib/payment/apple-iap/adapter.js +6 -13
- package/lib/payment/apple-iap/adapter.js.map +1 -1
- package/lib/payment/apple-iap/fix-periods.d.ts.map +1 -1
- package/lib/payment/apple-iap/fix-periods.js +3 -5
- package/lib/payment/apple-iap/fix-periods.js.map +1 -1
- package/lib/payment/apple-iap/handler-express.js +4 -4
- package/lib/payment/apple-iap/handler-express.js.map +1 -1
- package/lib/payment/apple-iap/handler-fastify.js +4 -4
- package/lib/payment/apple-iap/handler-fastify.js.map +1 -1
- package/lib/payment/apple-iap/module.d.ts +2 -2
- package/lib/payment/apple-iap/module.d.ts.map +1 -1
- package/lib/payment/braintree.js +3 -4
- package/lib/payment/braintree.js.map +1 -1
- package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts +1 -2
- package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts.map +1 -1
- package/lib/payment/cryptopay/db/CryptopayTransactions.js.map +1 -1
- package/lib/payment/cryptopay/handle-webhook.js +1 -1
- package/lib/payment/cryptopay/handle-webhook.js.map +1 -1
- package/lib/payment/cryptopay/handler-express.d.ts.map +1 -1
- package/lib/payment/cryptopay/handler-express.js +4 -57
- package/lib/payment/cryptopay/handler-express.js.map +1 -1
- package/lib/payment/cryptopay/handler-fastify.d.ts.map +1 -1
- package/lib/payment/cryptopay/handler-fastify.js +4 -56
- package/lib/payment/cryptopay/handler-fastify.js.map +1 -1
- package/lib/payment/cryptopay/module.d.ts +26 -26
- package/lib/payment/cryptopay/module.d.ts.map +1 -1
- package/lib/payment/cryptopay/module.js +26 -24
- package/lib/payment/cryptopay/module.js.map +1 -1
- package/lib/payment/cryptopay/plugin.d.ts.map +1 -1
- package/lib/payment/cryptopay/plugin.js +33 -79
- package/lib/payment/cryptopay/plugin.js.map +1 -1
- package/lib/payment/datatrans-v2/api/types.d.ts +41 -41
- package/lib/payment/datatrans-v2/api/types.d.ts.map +1 -1
- package/lib/payment/datatrans-v2/getPaths.d.ts +2 -2
- package/lib/payment/datatrans-v2/getPaths.d.ts.map +1 -1
- package/lib/payment/datatrans-v2/index.js +16 -14
- package/lib/payment/datatrans-v2/index.js.map +1 -1
- package/lib/payment/datatrans-v2/roundedAmountFromOrder.d.ts +1 -1
- package/lib/payment/datatrans-v2/roundedAmountFromOrder.d.ts.map +1 -1
- package/lib/payment/datatrans-v2/roundedAmountFromOrder.js +3 -3
- package/lib/payment/datatrans-v2/roundedAmountFromOrder.js.map +1 -1
- package/lib/payment/paypal-checkout.js +6 -8
- package/lib/payment/paypal-checkout.js.map +1 -1
- package/lib/payment/payrexx/api/index.d.ts +2 -2
- package/lib/payment/payrexx/api/index.d.ts.map +1 -1
- package/lib/payment/payrexx/api/makeFetcher.d.ts.map +1 -1
- package/lib/payment/payrexx/api/makeFetcher.js.map +1 -1
- package/lib/payment/payrexx/index.js +4 -4
- package/lib/payment/payrexx/index.js.map +1 -1
- package/lib/payment/payrexx/payrexx.js +2 -2
- package/lib/payment/payrexx/payrexx.js.map +1 -1
- package/lib/payment/postfinance-checkout/api-client.d.ts +16 -0
- package/lib/payment/postfinance-checkout/api-client.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/api-client.js +75 -0
- package/lib/payment/postfinance-checkout/api-client.js.map +1 -0
- package/lib/payment/postfinance-checkout/api-types.d.ts +98 -0
- package/lib/payment/postfinance-checkout/api-types.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/api-types.js +51 -0
- package/lib/payment/postfinance-checkout/api-types.js.map +1 -0
- package/lib/payment/postfinance-checkout/api.d.ts +5 -5
- package/lib/payment/postfinance-checkout/api.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/api.js +38 -62
- package/lib/payment/postfinance-checkout/api.js.map +1 -1
- package/lib/payment/postfinance-checkout/handler-express.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/handler-express.js +5 -5
- package/lib/payment/postfinance-checkout/handler-express.js.map +1 -1
- package/lib/payment/postfinance-checkout/handler-fastify.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/handler-fastify.js +6 -6
- package/lib/payment/postfinance-checkout/handler-fastify.js.map +1 -1
- package/lib/payment/postfinance-checkout/index.d.ts +2 -1
- package/lib/payment/postfinance-checkout/index.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/index.js +27 -30
- package/lib/payment/postfinance-checkout/index.js.map +1 -1
- package/lib/payment/postfinance-checkout/types.d.ts +4 -4
- package/lib/payment/postfinance-checkout/types.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/utils.d.ts +3 -3
- package/lib/payment/postfinance-checkout/utils.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/utils.js +5 -6
- package/lib/payment/postfinance-checkout/utils.js.map +1 -1
- package/lib/payment/saferpay/adapter.d.ts.map +1 -1
- package/lib/payment/saferpay/adapter.js +4 -6
- package/lib/payment/saferpay/adapter.js.map +1 -1
- package/lib/payment/saferpay/api/models/Transaction.d.ts +2 -2
- package/lib/payment/saferpay/api/models/Transaction.d.ts.map +1 -1
- package/lib/payment/saferpay/module.d.ts +2 -2
- package/lib/payment/saferpay/module.d.ts.map +1 -1
- package/lib/payment/stripe/index.js +5 -5
- package/lib/payment/stripe/index.js.map +1 -1
- package/lib/payment/stripe/stripe.js +3 -3
- package/lib/payment/stripe/stripe.js.map +1 -1
- package/lib/presets/all.d.ts +15 -13
- package/lib/presets/all.d.ts.map +1 -1
- package/lib/presets/all.js +3 -1
- package/lib/presets/all.js.map +1 -1
- package/lib/pricing/delivery-swiss-tax.d.ts.map +1 -1
- package/lib/pricing/delivery-swiss-tax.js +2 -2
- package/lib/pricing/delivery-swiss-tax.js.map +1 -1
- package/lib/pricing/order-delivery.js +1 -1
- package/lib/pricing/order-delivery.js.map +1 -1
- package/lib/pricing/order-discount.js +3 -3
- package/lib/pricing/order-discount.js.map +1 -1
- package/lib/pricing/order-items-discount.js +1 -1
- package/lib/pricing/order-items-discount.js.map +1 -1
- package/lib/pricing/order-items.js +1 -1
- package/lib/pricing/order-items.js.map +1 -1
- package/lib/pricing/order-payment.js +1 -1
- package/lib/pricing/order-payment.js.map +1 -1
- package/lib/pricing/order-round.d.ts +1 -1
- package/lib/pricing/order-round.d.ts.map +1 -1
- package/lib/pricing/order-round.js +8 -8
- package/lib/pricing/order-round.js.map +1 -1
- package/lib/pricing/product-catalog-price.d.ts +0 -1
- package/lib/pricing/product-catalog-price.d.ts.map +1 -1
- package/lib/pricing/product-catalog-price.js +6 -18
- package/lib/pricing/product-catalog-price.js.map +1 -1
- package/lib/pricing/product-price-rateconversion.js +2 -2
- package/lib/pricing/product-price-rateconversion.js.map +1 -1
- package/lib/pricing/product-round.d.ts +1 -1
- package/lib/pricing/product-round.d.ts.map +1 -1
- package/lib/pricing/product-round.js +2 -2
- package/lib/pricing/product-round.js.map +1 -1
- package/lib/pricing/product-swiss-tax.d.ts +1 -1
- package/lib/pricing/product-swiss-tax.d.ts.map +1 -1
- package/lib/pricing/product-swiss-tax.js +2 -2
- package/lib/pricing/product-swiss-tax.js.map +1 -1
- package/lib/warehousing/eth-minter.js +2 -2
- package/lib/warehousing/eth-minter.js.map +1 -1
- package/lib/worker/budgetsms.d.ts +10 -0
- package/lib/worker/budgetsms.d.ts.map +1 -0
- package/lib/worker/budgetsms.js +217 -0
- package/lib/worker/budgetsms.js.map +1 -0
- package/lib/worker/bulk-import.js +1 -1
- package/lib/worker/bulk-import.js.map +1 -1
- package/lib/worker/bulkgate.d.ts +2 -0
- package/lib/worker/bulkgate.d.ts.map +1 -0
- package/lib/worker/bulkgate.js +104 -0
- package/lib/worker/bulkgate.js.map +1 -0
- package/lib/worker/enrollment-order-generator.js +1 -1
- package/lib/worker/enrollment-order-generator.js.map +1 -1
- package/lib/worker/error-notifications.d.ts +4 -4
- package/lib/worker/error-notifications.d.ts.map +1 -1
- package/lib/worker/heartbeat.js +2 -8
- package/lib/worker/heartbeat.js.map +1 -1
- package/lib/worker/message.d.ts +1 -1
- package/lib/worker/message.d.ts.map +1 -1
- package/lib/worker/sms.js +16 -16
- package/lib/worker/sms.js.map +1 -1
- package/lib/worker/twilio.d.ts +2 -0
- package/lib/worker/twilio.d.ts.map +1 -0
- package/lib/worker/twilio.js +56 -0
- package/lib/worker/twilio.js.map +1 -0
- package/lib/worker/update-coinbase-rates.js +1 -1
- package/lib/worker/update-coinbase-rates.js.map +1 -1
- package/lib/worker/update-ecb-rates.js.map +1 -1
- package/package.json +32 -53
- package/src/delivery/pick-mup.ts +0 -2
- package/src/files/gridfs/adapter.ts +7 -2
- package/src/files/gridfs/handler-express.ts +9 -2
- package/src/files/gridfs/handler-fastify.ts +8 -2
- package/src/files/gridfs/module.ts +2 -2
- package/src/filters/local-search.ts +11 -2
- package/src/filters/strict-equal.ts +21 -0
- package/src/payment/apple-iap/adapter.ts +7 -14
- package/src/payment/apple-iap/fix-periods.ts +4 -7
- package/src/payment/apple-iap/handler-express.ts +4 -4
- package/src/payment/apple-iap/handler-fastify.ts +4 -4
- package/src/payment/apple-iap/module.ts +2 -2
- package/src/payment/braintree.ts +3 -4
- package/src/payment/cryptopay/db/CryptopayTransactions.ts +1 -2
- package/src/payment/cryptopay/denote-amount.test.ts +28 -0
- package/src/payment/cryptopay/denote-amount.ts +29 -0
- package/src/payment/cryptopay/derive-btc-address.test.ts +22 -0
- package/src/payment/cryptopay/derive-btc-address.ts +48 -0
- package/src/payment/cryptopay/derive-eth-address.test.ts +22 -0
- package/src/payment/cryptopay/derive-eth-address.ts +38 -0
- package/src/payment/cryptopay/handle-webhook.ts +103 -0
- package/src/payment/cryptopay/handler-express.ts +4 -70
- package/src/payment/cryptopay/handler-fastify.ts +6 -71
- package/src/payment/cryptopay/module.ts +44 -40
- package/src/payment/cryptopay/plugin.ts +50 -87
- package/src/payment/cryptopay/readme.md +23 -1
- package/src/payment/datatrans-v2/api/types.ts +41 -41
- package/src/payment/datatrans-v2/getPaths.ts +2 -2
- package/src/payment/datatrans-v2/index.ts +17 -15
- package/src/payment/datatrans-v2/roundedAmountFromOrder.ts +4 -4
- package/src/payment/paypal-checkout.ts +6 -11
- package/src/payment/payrexx/api/index.ts +2 -2
- package/src/payment/payrexx/api/makeFetcher.ts +1 -1
- package/src/payment/payrexx/index.ts +4 -4
- package/src/payment/payrexx/payrexx.ts +2 -2
- package/src/payment/postfinance-checkout/api-client.ts +106 -0
- package/src/payment/postfinance-checkout/api-types.ts +111 -0
- package/src/payment/postfinance-checkout/api.ts +58 -85
- package/src/payment/postfinance-checkout/handler-express.ts +5 -7
- package/src/payment/postfinance-checkout/handler-fastify.ts +6 -8
- package/src/payment/postfinance-checkout/index.ts +43 -35
- package/src/payment/postfinance-checkout/types.ts +6 -6
- package/src/payment/postfinance-checkout/utils.ts +7 -12
- package/src/payment/saferpay/adapter.ts +4 -6
- package/src/payment/saferpay/api/models/Transaction.ts +2 -2
- package/src/payment/saferpay/module.ts +2 -2
- package/src/payment/stripe/index.ts +5 -5
- package/src/payment/stripe/stripe.ts +3 -3
- package/src/presets/all.ts +3 -1
- package/src/pricing/delivery-swiss-tax.ts +6 -2
- package/src/pricing/order-delivery.ts +1 -1
- package/src/pricing/order-discount.ts +3 -3
- package/src/pricing/order-items-discount.ts +1 -1
- package/src/pricing/order-items.ts +1 -1
- package/src/pricing/order-payment.ts +1 -1
- package/src/pricing/order-round.ts +9 -9
- package/src/pricing/product-catalog-price.ts +6 -25
- package/src/pricing/product-price-rateconversion.ts +2 -2
- package/src/pricing/product-round.ts +3 -3
- package/src/pricing/product-swiss-tax.test.ts +6 -6
- package/src/pricing/product-swiss-tax.ts +2 -2
- package/src/warehousing/eth-minter.ts +2 -2
- package/src/worker/budgetsms.ts +268 -0
- package/src/worker/bulk-import.ts +1 -1
- package/src/worker/bulkgate.ts +138 -0
- package/src/worker/enrollment-order-generator.ts +3 -3
- package/src/worker/error-notifications.ts +4 -4
- package/src/worker/heartbeat.ts +4 -11
- package/src/worker/message.ts +1 -1
- package/src/worker/push-notification.ts +2 -2
- package/src/worker/twilio.ts +71 -0
- package/src/worker/update-coinbase-rates.ts +3 -6
- package/src/worker/update-ecb-rates.ts +3 -3
- package/lib/accounts/google-oauth.d.ts +0 -4
- package/lib/accounts/google-oauth.d.ts.map +0 -1
- package/lib/accounts/google-oauth.js +0 -145
- package/lib/accounts/google-oauth.js.map +0 -1
- package/lib/accounts/linkedin-oauth.d.ts +0 -4
- package/lib/accounts/linkedin-oauth.d.ts.map +0 -1
- package/lib/accounts/linkedin-oauth.js +0 -82
- package/lib/accounts/linkedin-oauth.js.map +0 -1
- package/lib/files/gridfs/gridfs-adapter.d.ts +0 -3
- package/lib/files/gridfs/gridfs-adapter.d.ts.map +0 -1
- package/lib/files/gridfs/gridfs-adapter.js +0 -97
- 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 +0 -84
- package/lib/files/gridfs/gridfs-webhook.js.map +0 -1
- package/lib/files/minio/minio-adapter.d.ts +0 -6
- package/lib/files/minio/minio-adapter.d.ts.map +0 -1
- package/lib/files/minio/minio-adapter.js +0 -181
- 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 +0 -33
- 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 +0 -17
- 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/middleware.js +0 -75
- package/lib/payment/cryptopay/middleware.js.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 +0 -75
- package/lib/payment/cryptopay/module/configureCryptopayModule.js.map +0 -1
- package/lib/payment/datatrans-v2/middleware.d.ts +0 -2
- package/lib/payment/datatrans-v2/middleware.d.ts.map +0 -1
- package/lib/payment/datatrans-v2/middleware.js +0 -68
- package/lib/payment/datatrans-v2/middleware.js.map +0 -1
- package/lib/payment/payrexx/middleware.d.ts +0 -2
- package/lib/payment/payrexx/middleware.d.ts.map +0 -1
- package/lib/payment/payrexx/middleware.js +0 -86
- package/lib/payment/payrexx/middleware.js.map +0 -1
- package/lib/payment/postfinance-checkout/middleware.d.ts +0 -2
- package/lib/payment/postfinance-checkout/middleware.d.ts.map +0 -1
- package/lib/payment/postfinance-checkout/middleware.js +0 -38
- package/lib/payment/postfinance-checkout/middleware.js.map +0 -1
- package/lib/payment/saferpay/middleware.d.ts +0 -2
- package/lib/payment/saferpay/middleware.d.ts.map +0 -1
- package/lib/payment/saferpay/middleware.js +0 -57
- 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 +0 -24
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.js.map +0 -1
- package/lib/payment/stripe/middleware.d.ts +0 -6
- package/lib/payment/stripe/middleware.d.ts.map +0 -1
- package/lib/payment/stripe/middleware.js +0 -114
- package/lib/payment/stripe/middleware.js.map +0 -1
- package/lib/plugins-index.d.ts +0 -48
- package/lib/plugins-index.d.ts.map +0 -1
- package/lib/plugins-index.js +0 -107
- package/lib/plugins-index.js.map +0 -1
- package/lib/worker/BaseWorker.d.ts +0 -30
- package/lib/worker/BaseWorker.d.ts.map +0 -1
- package/lib/worker/BaseWorker.js +0 -79
- package/lib/worker/BaseWorker.js.map +0 -1
- package/lib/worker/EventListenerWorker.d.ts +0 -6
- package/lib/worker/EventListenerWorker.d.ts.map +0 -1
- package/lib/worker/EventListenerWorker.js +0 -46
- package/lib/worker/EventListenerWorker.js.map +0 -1
- package/lib/worker/FailedRescheduler.d.ts +0 -15
- package/lib/worker/FailedRescheduler.d.ts.map +0 -1
- package/lib/worker/FailedRescheduler.js +0 -45
- package/lib/worker/FailedRescheduler.js.map +0 -1
- package/lib/worker/IntervalWorker.d.ts +0 -10
- package/lib/worker/IntervalWorker.d.ts.map +0 -1
- package/lib/worker/IntervalWorker.js +0 -37
- package/lib/worker/IntervalWorker.js.map +0 -1
- package/src/worker/BaseWorker.ts +0 -116
- package/src/worker/EventListenerWorker.ts +0 -61
- package/src/worker/FailedRescheduler.ts +0 -80
- package/src/worker/IntervalWorker.ts +0 -54
- package/src/worker/sms.ts +0 -87
|
@@ -5,7 +5,7 @@ import { fixPeriods } from './fix-periods.js';
|
|
|
5
5
|
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
6
6
|
import { AppleTransactionsModule } from './module.js';
|
|
7
7
|
|
|
8
|
-
const logger = createLogger('unchained:core-payment:iap');
|
|
8
|
+
const logger = createLogger('unchained:core-payment:apple-iap:handler');
|
|
9
9
|
|
|
10
10
|
const { APPLE_IAP_SHARED_SECRET } = process.env;
|
|
11
11
|
|
|
@@ -33,7 +33,7 @@ export const appleIAPHandler: RouteHandlerMethod = async (
|
|
|
33
33
|
throw new Error('shared secret not valid');
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
const transactions = responseBody?.unified_receipt?.latest_receipt_info;
|
|
37
37
|
const latestTransaction = transactions[0];
|
|
38
38
|
|
|
39
39
|
if (responseBody.notification_type === AppleNotificationTypes.INITIAL_BUY) {
|
|
@@ -48,7 +48,7 @@ export const appleIAPHandler: RouteHandlerMethod = async (
|
|
|
48
48
|
|
|
49
49
|
const order = await services.orders.checkoutOrder(orderPayment.orderId, {
|
|
50
50
|
paymentContext: {
|
|
51
|
-
|
|
51
|
+
receiptData: responseBody?.unified_receipt?.latest_receipt,
|
|
52
52
|
},
|
|
53
53
|
});
|
|
54
54
|
const orderId = order._id;
|
|
@@ -86,7 +86,7 @@ export const appleIAPHandler: RouteHandlerMethod = async (
|
|
|
86
86
|
|
|
87
87
|
await services.orders.registerPaymentCredentials(enrollment.payment.paymentProviderId, {
|
|
88
88
|
transactionContext: {
|
|
89
|
-
|
|
89
|
+
receiptData: responseBody?.unified_receipt?.latest_receipt,
|
|
90
90
|
},
|
|
91
91
|
userId: enrollment.userId,
|
|
92
92
|
});
|
package/src/payment/braintree.ts
CHANGED
|
@@ -52,7 +52,6 @@ const BraintreeDirect: IPaymentAdapter = {
|
|
|
52
52
|
return BRAINTREE_SANDBOX_TOKEN;
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
// eslint-disable-next-line
|
|
56
55
|
const getPrivateKey = () => {
|
|
57
56
|
return BRAINTREE_PRIVATE_KEY;
|
|
58
57
|
};
|
|
@@ -114,17 +113,17 @@ const BraintreeDirect: IPaymentAdapter = {
|
|
|
114
113
|
|
|
115
114
|
// eslint-disable-next-line
|
|
116
115
|
// @ts-ignore
|
|
117
|
-
const braintree = (await import('braintree')).default;
|
|
116
|
+
const braintree = (await import('braintree')).default;
|
|
118
117
|
const gateway = getGateway(braintree);
|
|
119
118
|
const address = order.billingAddress;
|
|
120
119
|
const pricing = OrderPricingSheet({
|
|
121
120
|
calculation: order.calculation,
|
|
122
|
-
|
|
121
|
+
currencyCode: order.currencyCode,
|
|
123
122
|
});
|
|
124
123
|
const rounded = Math.round(pricing.total({ useNetPrice: false }).amount / 10 || 0) * 10;
|
|
125
124
|
const saleRequest = {
|
|
126
125
|
amount: rounded / 100,
|
|
127
|
-
merchantAccountId: order.
|
|
126
|
+
merchantAccountId: order.currencyCode,
|
|
128
127
|
paymentMethodNonce: paypalPaymentMethodNonce,
|
|
129
128
|
orderId: order.orderNumber || order._id,
|
|
130
129
|
shipping: address
|
|
@@ -6,9 +6,8 @@ export type CryptopayTransaction = {
|
|
|
6
6
|
blockHeight: number;
|
|
7
7
|
mostRecentBlockHeight: number;
|
|
8
8
|
amount: mongodb.Decimal128;
|
|
9
|
-
|
|
9
|
+
currencyCode: string;
|
|
10
10
|
decimals: number;
|
|
11
|
-
contract: string;
|
|
12
11
|
orderPaymentId?: string;
|
|
13
12
|
} & TimestampFields;
|
|
14
13
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import denoteAmount from './denote-amount.js';
|
|
4
|
+
|
|
5
|
+
describe('Denote Amount', () => {
|
|
6
|
+
it('Handle amount with 18 decimals', () => {
|
|
7
|
+
// For example, Ethereum uses 18 decimals, this converts WEI to GWei
|
|
8
|
+
assert.equal(denoteAmount('1000000000000000000', 18), 1000000000n);
|
|
9
|
+
assert.equal(denoteAmount('100000000000000', 18), 100000n);
|
|
10
|
+
assert.equal(denoteAmount('100', 18), 0n);
|
|
11
|
+
});
|
|
12
|
+
it('Handle amount with 10 decimals', () => {
|
|
13
|
+
assert.equal(denoteAmount('100000000', 10), 10000000n);
|
|
14
|
+
assert.equal(denoteAmount('100000000000000', 10), 10000000000000n);
|
|
15
|
+
assert.equal(denoteAmount('100', 10), 10n);
|
|
16
|
+
});
|
|
17
|
+
it('Handle amount with 8 decimals', () => {
|
|
18
|
+
// For example, Bitcoin uses 8 decimals, this keeps Satoshi as is
|
|
19
|
+
assert.equal(denoteAmount('100000000', 8), 100000000n);
|
|
20
|
+
assert.equal(denoteAmount('100000000000000', 8), 100000000000000n);
|
|
21
|
+
assert.equal(denoteAmount('1', 8), 1n);
|
|
22
|
+
});
|
|
23
|
+
it('Handle amount with 2 decimals', () => {
|
|
24
|
+
assert.equal(denoteAmount('100000000', 2), 100000000n);
|
|
25
|
+
assert.equal(denoteAmount('100000000000000', 2), 100000000000000n);
|
|
26
|
+
assert.equal(denoteAmount('1', 2), 1n);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a cryptocurrency amount from its smallest unit (e.g., wei, satoshi) to a normalized value
|
|
3
|
+
* with 9 decimal places of precision.
|
|
4
|
+
*
|
|
5
|
+
* This function handles the denomination of cryptocurrency amounts by converting from the
|
|
6
|
+
* blockchain's native smallest units (like wei for Ethereum which is 10^-18 ETH, or satoshi for Bitcoin
|
|
7
|
+
* which is 10^-8 BTC) to a standardized format with 9 decimal places for consistent handling across
|
|
8
|
+
* different cryptocurrencies.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} amountAsString - The amount in the cryptocurrency's smallest unit (e.g., wei, satoshi)
|
|
11
|
+
* represented as a string to handle large numbers
|
|
12
|
+
* @param {number} decimals - The number of decimal places used by the cryptocurrency
|
|
13
|
+
* (e.g., 18 for Ethereum, 8 for Bitcoin)
|
|
14
|
+
* @returns {number} The converted amount with 9 decimal places of precision
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // Convert 1 ETH in wei (1000000000000000000) to normalized format
|
|
18
|
+
* denoteAmount("1000000000000000000", 18)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Convert 1 BTC in satoshi (100000000) to normalized format
|
|
22
|
+
* denoteAmount("100000000", 8)
|
|
23
|
+
*/
|
|
24
|
+
export default function denoteAmount(amountAsString: string, decimals: number) {
|
|
25
|
+
if (decimals <= 9) return BigInt(amountAsString); // No conversion needed if decimals are 9 or less
|
|
26
|
+
const exponent = BigInt(decimals) - 9n;
|
|
27
|
+
const amount = BigInt(amountAsString);
|
|
28
|
+
return amount / 10n ** exponent; // Denote to 9 decimals
|
|
29
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import deriveBtcAddress from './derive-btc-address.js';
|
|
4
|
+
|
|
5
|
+
describe('BTC Address Derivation', () => {
|
|
6
|
+
it('returns a deterministic BIP44 child address', () => {
|
|
7
|
+
assert.equal(
|
|
8
|
+
deriveBtcAddress(
|
|
9
|
+
'zpub6qpRm6gMdkrXvVKw7qgayHnqJGJkpE4rnYQdyJfZW87ed4Fk6HjVaThN12hv2rrL3EzfvvBW6ZpbMEe5UxZYEjhgeMigyvAbS5LiXPkKrys',
|
|
10
|
+
1,
|
|
11
|
+
),
|
|
12
|
+
'bc1qspcam3ntdpvhgz4vnxfdhas6qr8hf8tjfaz5p4',
|
|
13
|
+
);
|
|
14
|
+
assert.equal(
|
|
15
|
+
deriveBtcAddress(
|
|
16
|
+
'zpub6qpRm6gMdkrXvVKw7qgayHnqJGJkpE4rnYQdyJfZW87ed4Fk6HjVaThN12hv2rrL3EzfvvBW6ZpbMEe5UxZYEjhgeMigyvAbS5LiXPkKrys',
|
|
17
|
+
2,
|
|
18
|
+
),
|
|
19
|
+
'bc1qd0a7znq3pz3sc4armthddkyam57ey7qjc4v6vn',
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { HDKey } from '@scure/bip32';
|
|
2
|
+
import { NETWORK, p2wpkh, TEST_NETWORK } from '@scure/btc-signer';
|
|
3
|
+
|
|
4
|
+
// const resolvePath = (prefix) => {
|
|
5
|
+
// if (prefix === 'x') return `m/44'/0'`;
|
|
6
|
+
// if (prefix === 'y') return `m/49'/0'`;
|
|
7
|
+
// if (prefix === 'z') return `m/84'/0'`;
|
|
8
|
+
// if (prefix === 't') return `m/44'/1'`;
|
|
9
|
+
// if (prefix === 'u') return `m/49'/1'`;
|
|
10
|
+
// if (prefix === 'v') return `m/84'/1'`;
|
|
11
|
+
// return `m`;
|
|
12
|
+
// };
|
|
13
|
+
|
|
14
|
+
const resolveNetwork = (prefix) => {
|
|
15
|
+
if (['x', 'y', 'z'].includes(prefix)) return NETWORK;
|
|
16
|
+
return TEST_NETWORK;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default (xpub, index) => {
|
|
20
|
+
const prefix = xpub.slice(0, 1);
|
|
21
|
+
if (prefix !== 'z' && prefix !== 'v')
|
|
22
|
+
throw new Error('Cryptopay only supports native segwit (zpub/vpub) extended key format for BTC');
|
|
23
|
+
|
|
24
|
+
const hardenedMaster = HDKey.fromExtendedKey(
|
|
25
|
+
xpub,
|
|
26
|
+
prefix === 'z'
|
|
27
|
+
? {
|
|
28
|
+
public: 0x04b24746, // zpub public
|
|
29
|
+
private: 0x04b2430c, // zpriv private
|
|
30
|
+
}
|
|
31
|
+
: {
|
|
32
|
+
public: 0x045f1cf6,
|
|
33
|
+
private: 0x045f18bc,
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
hardenedMaster.wipePrivateData(); // Neuter
|
|
37
|
+
|
|
38
|
+
const child = hardenedMaster.deriveChild(0).deriveChild(index);
|
|
39
|
+
|
|
40
|
+
// We don't need this AFAIK:
|
|
41
|
+
// const derivationPath = resolvePath(prefix);
|
|
42
|
+
// const child = hardenedMaster.derive(`${derivationPath}/0/${btcDerivationNumber}`);
|
|
43
|
+
|
|
44
|
+
const network = resolveNetwork(prefix);
|
|
45
|
+
const pubKey = p2wpkh(child.publicKey /* hex.decode( as string)*/, network);
|
|
46
|
+
|
|
47
|
+
return pubKey.address;
|
|
48
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import deriveEthAddress from './derive-eth-address.js';
|
|
4
|
+
|
|
5
|
+
describe('ETH Address Derivation', () => {
|
|
6
|
+
it('returns a deterministic BIP44 child address', () => {
|
|
7
|
+
assert.equal(
|
|
8
|
+
deriveEthAddress(
|
|
9
|
+
'xpub6DTksA6DsGUgbEp1U29v3qGTt5P99iKThaqGEzerTfD73ZAg1F8j15TxWTPEjjA1uw1Zmwwc5dgVCXwTL2ZNhvdoLWNdqVuL1MDJMsQCLLs',
|
|
10
|
+
1,
|
|
11
|
+
),
|
|
12
|
+
'0x11CF7d607A1Dc45a002592891fF25DAAE6eC7dE9',
|
|
13
|
+
);
|
|
14
|
+
assert.equal(
|
|
15
|
+
deriveEthAddress(
|
|
16
|
+
'xpub6DTksA6DsGUgbEp1U29v3qGTt5P99iKThaqGEzerTfD73ZAg1F8j15TxWTPEjjA1uw1Zmwwc5dgVCXwTL2ZNhvdoLWNdqVuL1MDJMsQCLLs',
|
|
17
|
+
2,
|
|
18
|
+
),
|
|
19
|
+
'0x61663D505e3aB5b61a5344eA2c64b644c08013ea',
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { HDKey } from '@scure/bip32';
|
|
2
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
3
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
4
|
+
|
|
5
|
+
function getChecksumAddress(address) {
|
|
6
|
+
const chars = address.split('');
|
|
7
|
+
const expanded = new Uint8Array(40);
|
|
8
|
+
for (let i = 0; i < 40; i++) {
|
|
9
|
+
expanded[i] = chars[i].charCodeAt(0);
|
|
10
|
+
}
|
|
11
|
+
const hashed = keccak_256(expanded);
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < 40; i += 2) {
|
|
14
|
+
if (hashed[i >> 1] >> 4 >= 8) {
|
|
15
|
+
chars[i] = chars[i].toUpperCase();
|
|
16
|
+
}
|
|
17
|
+
if ((hashed[i >> 1] & 0x0f) >= 8) {
|
|
18
|
+
chars[i + 1] = chars[i + 1].toUpperCase();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return '0x' + chars.join('');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default (xpub, index) => {
|
|
26
|
+
const hardenedMaster = HDKey.fromExtendedKey(xpub);
|
|
27
|
+
hardenedMaster.wipePrivateData(); // Neuter
|
|
28
|
+
|
|
29
|
+
const child = hardenedMaster.deriveChild(0).deriveChild(index);
|
|
30
|
+
|
|
31
|
+
// ETH Address (secp256k1 + keccak_256 + checksum)
|
|
32
|
+
const childSigningKey = secp256k1.ProjectivePoint.fromHex(child.publicKey).toRawBytes(false);
|
|
33
|
+
const address = Buffer.from(keccak_256(childSigningKey.slice(1)))
|
|
34
|
+
.toString('hex')
|
|
35
|
+
.substring(24);
|
|
36
|
+
|
|
37
|
+
return getChecksumAddress(address);
|
|
38
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
2
|
+
import { Context } from '@unchainedshop/api';
|
|
3
|
+
import { OrderStatus } from '@unchainedshop/core-orders';
|
|
4
|
+
import { CryptopayModule } from './module.js';
|
|
5
|
+
import { ProductPriceRate } from '@unchainedshop/core-products';
|
|
6
|
+
|
|
7
|
+
const { CRYPTOPAY_SECRET, CRYPTOPAY_MAX_RATE_AGE = '360' } = process.env;
|
|
8
|
+
|
|
9
|
+
const logger = createLogger('unchained:core-payment:cryptopay:handler');
|
|
10
|
+
|
|
11
|
+
export default async function handleWebhook(
|
|
12
|
+
{
|
|
13
|
+
secret,
|
|
14
|
+
wallet,
|
|
15
|
+
price,
|
|
16
|
+
ping,
|
|
17
|
+
}: {
|
|
18
|
+
secret: string;
|
|
19
|
+
wallet?: {
|
|
20
|
+
address: string;
|
|
21
|
+
blockHeight: number;
|
|
22
|
+
amount: string;
|
|
23
|
+
contract?: string;
|
|
24
|
+
decimals?: number;
|
|
25
|
+
currencyCode: string;
|
|
26
|
+
};
|
|
27
|
+
price?: {
|
|
28
|
+
baseCurrency: string;
|
|
29
|
+
token: string;
|
|
30
|
+
rate: number;
|
|
31
|
+
timestamp: string; // ISO date string
|
|
32
|
+
};
|
|
33
|
+
ping?: {
|
|
34
|
+
blockHeight: number;
|
|
35
|
+
currencyCode: string;
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
context: Context & {
|
|
39
|
+
modules: CryptopayModule;
|
|
40
|
+
},
|
|
41
|
+
) {
|
|
42
|
+
const { modules, services } = context;
|
|
43
|
+
|
|
44
|
+
if (secret !== CRYPTOPAY_SECRET) {
|
|
45
|
+
logger.warn(`webhook called with invalid secret`);
|
|
46
|
+
throw new Error('Secret invalid');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (wallet) {
|
|
50
|
+
const { address, blockHeight, amount, contract, decimals, currencyCode } = wallet;
|
|
51
|
+
logger.debug('webhook received wallet data', wallet);
|
|
52
|
+
const { orderPaymentId } = await modules.cryptopay.updateWalletAddress({
|
|
53
|
+
address,
|
|
54
|
+
blockHeight,
|
|
55
|
+
amount,
|
|
56
|
+
contract,
|
|
57
|
+
decimals,
|
|
58
|
+
currencyCode,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const orderPayment =
|
|
62
|
+
orderPaymentId &&
|
|
63
|
+
(await modules.orders.payments.findOrderPayment({
|
|
64
|
+
orderPaymentId,
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
if (orderPayment) {
|
|
68
|
+
// Try to process order to next status
|
|
69
|
+
// TODO: Not sure if it's correct to use processOrder here if status is PENDING!
|
|
70
|
+
const order = await modules.orders.findOrder({ orderId: orderPayment.orderId });
|
|
71
|
+
if (order.status === null) {
|
|
72
|
+
await services.orders.checkoutOrder(order._id, {});
|
|
73
|
+
} else if (order.status === OrderStatus.PENDING) {
|
|
74
|
+
await services.orders.processOrder(order, {});
|
|
75
|
+
} else {
|
|
76
|
+
throw new Error('Already processed');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (price) {
|
|
82
|
+
logger.debug('webhook received price data', price);
|
|
83
|
+
const { baseCurrency, token, rate, timestamp } = price;
|
|
84
|
+
|
|
85
|
+
const timestampDate = new Date(timestamp);
|
|
86
|
+
const expiresAt = new Date(new Date().getTime() + parseInt(CRYPTOPAY_MAX_RATE_AGE, 10) * 1000);
|
|
87
|
+
|
|
88
|
+
const rateData: ProductPriceRate = {
|
|
89
|
+
baseCurrency,
|
|
90
|
+
quoteCurrency: token,
|
|
91
|
+
rate,
|
|
92
|
+
expiresAt,
|
|
93
|
+
timestamp: timestampDate,
|
|
94
|
+
};
|
|
95
|
+
await modules.products.prices.rates.updateRates([rateData]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (ping) {
|
|
99
|
+
logger.debug('webhook received ping data', ping);
|
|
100
|
+
const { blockHeight, currencyCode } = ping;
|
|
101
|
+
await modules.cryptopay.updateMostRecentBlock(currencyCode, blockHeight);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -1,83 +1,17 @@
|
|
|
1
1
|
import { createLogger } from '@unchainedshop/logger';
|
|
2
|
-
import
|
|
3
|
-
import { OrderStatus } from '@unchainedshop/core-orders';
|
|
4
|
-
import { CryptopayModule } from './module.js';
|
|
5
|
-
import { ProductPriceRate } from '@unchainedshop/core-products';
|
|
2
|
+
import handleWebhook from './handle-webhook.js';
|
|
6
3
|
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const logger = createLogger('unchained:core-payment:cryptopay');
|
|
4
|
+
const logger = createLogger('unchained:core-payment:cryptopay:handler');
|
|
10
5
|
|
|
11
6
|
const cryptopayHandler = async (req, res) => {
|
|
12
|
-
const resolvedContext = req.unchainedContext as Context & {
|
|
13
|
-
modules: CryptopayModule;
|
|
14
|
-
};
|
|
15
|
-
const { modules, services } = resolvedContext;
|
|
16
|
-
|
|
17
7
|
if (req.method === 'POST') {
|
|
18
8
|
try {
|
|
19
|
-
|
|
20
|
-
if (secret !== CRYPTOPAY_SECRET) {
|
|
21
|
-
logger.warn(`Cryptopay Plugin: Invalid Cryptopay Secret provided`);
|
|
22
|
-
throw new Error('Secret invalid');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (wallet) {
|
|
26
|
-
const { address, blockHeight, amount, contract, decimals, currency } = wallet;
|
|
27
|
-
const { orderPaymentId } = await modules.cryptopay.updateWalletAddress({
|
|
28
|
-
addressId: address,
|
|
29
|
-
blockHeight,
|
|
30
|
-
amount,
|
|
31
|
-
contract,
|
|
32
|
-
decimals,
|
|
33
|
-
currency,
|
|
34
|
-
});
|
|
35
|
-
const orderPayment =
|
|
36
|
-
orderPaymentId &&
|
|
37
|
-
(await modules.orders.payments.findOrderPayment({
|
|
38
|
-
orderPaymentId,
|
|
39
|
-
}));
|
|
40
|
-
|
|
41
|
-
if (orderPayment) {
|
|
42
|
-
// Try to process order to next status
|
|
43
|
-
// TODO: Not sure if it's correct to use processOrder here if status is PENDING!
|
|
44
|
-
const order = await modules.orders.findOrder({ orderId: orderPayment.orderId });
|
|
45
|
-
if (order.status === null) {
|
|
46
|
-
await services.orders.checkoutOrder(order._id, {});
|
|
47
|
-
} else if (order.status === OrderStatus.PENDING) {
|
|
48
|
-
await services.orders.processOrder(order, {});
|
|
49
|
-
} else {
|
|
50
|
-
throw new Error('Already processed');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (price) {
|
|
56
|
-
const { baseCurrency, token, rate, timestamp } = price;
|
|
57
|
-
|
|
58
|
-
const timestampDate = new Date(timestamp);
|
|
59
|
-
const expiresAt = new Date(new Date().getTime() + parseInt(CRYPTOPAY_MAX_RATE_AGE, 10) * 1000);
|
|
60
|
-
|
|
61
|
-
const rateData: ProductPriceRate = {
|
|
62
|
-
baseCurrency,
|
|
63
|
-
quoteCurrency: token,
|
|
64
|
-
rate,
|
|
65
|
-
expiresAt,
|
|
66
|
-
timestamp: timestampDate,
|
|
67
|
-
};
|
|
68
|
-
logger.info(`update rate ${JSON.stringify(price)}, ${JSON.stringify(rateData)}`);
|
|
69
|
-
await resolvedContext.modules.products.prices.rates.updateRates([rateData]);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (ping) {
|
|
73
|
-
const { blockHeight, currency } = ping;
|
|
74
|
-
await modules.cryptopay.updateMostRecentBlock(currency, blockHeight);
|
|
75
|
-
}
|
|
76
|
-
|
|
9
|
+
await handleWebhook(req.body, req.unchainedContext);
|
|
77
10
|
res.writeHead(200);
|
|
78
11
|
res.end(JSON.stringify({ success: true }));
|
|
79
12
|
return;
|
|
80
13
|
} catch (e) {
|
|
14
|
+
logger.error(e);
|
|
81
15
|
res.writeHead(500);
|
|
82
16
|
res.end(JSON.stringify({ success: false, error: e.message }));
|
|
83
17
|
return;
|
|
@@ -1,86 +1,21 @@
|
|
|
1
|
-
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
2
1
|
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
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;
|
|
2
|
+
import handleWebhook from './handle-webhook.js';
|
|
3
|
+
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
9
4
|
|
|
10
|
-
const logger = createLogger('unchained:core-payment:cryptopay');
|
|
5
|
+
const logger = createLogger('unchained:core-payment:cryptopay:handler');
|
|
11
6
|
|
|
12
7
|
const cryptopayHandler: RouteHandlerMethod = async (
|
|
13
8
|
req: FastifyRequest & {
|
|
14
|
-
unchainedContext:
|
|
15
|
-
modules: CryptopayModule;
|
|
16
|
-
};
|
|
9
|
+
unchainedContext: any;
|
|
17
10
|
},
|
|
18
11
|
reply,
|
|
19
12
|
) => {
|
|
20
|
-
const { modules, services } = req.unchainedContext;
|
|
21
|
-
|
|
22
13
|
try {
|
|
23
|
-
|
|
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
|
-
|
|
14
|
+
await handleWebhook(req.body as any, req.unchainedContext);
|
|
81
15
|
reply.status(200);
|
|
82
16
|
return reply.send({ success: true });
|
|
83
17
|
} catch (e) {
|
|
18
|
+
logger.error(e);
|
|
84
19
|
reply.status(500);
|
|
85
20
|
return reply.send({ success: false, error: e.message });
|
|
86
21
|
}
|