@unchainedshop/plugins 1.2.40 → 2.0.0-beta2
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-webhook.js +1 -1
- package/lib/files/gridfs/gridfs-webhook.js.map +1 -1
- package/lib/files/minio/minio-webhook.js +1 -1
- package/lib/files/minio/minio-webhook.js.map +1 -1
- package/lib/payment/apple-iap/adapter.js +1 -1
- package/lib/payment/apple-iap/adapter.js.map +1 -1
- package/lib/payment/bity/adapter.js +2 -2
- package/lib/payment/bity/adapter.js.map +1 -1
- package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts +13 -0
- package/lib/payment/cryptopay/db/CryptopayTransactions.js +4 -0
- package/lib/payment/cryptopay/db/CryptopayTransactions.js.map +1 -0
- package/lib/payment/cryptopay/index.d.ts +5 -0
- package/lib/payment/cryptopay/index.js +6 -0
- package/lib/payment/cryptopay/index.js.map +1 -0
- package/lib/payment/{cryptopay.d.ts → cryptopay/middleware.d.ts} +0 -0
- package/lib/payment/cryptopay/middleware.js +79 -0
- package/lib/payment/cryptopay/middleware.js.map +1 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts +25 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.js +75 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.js.map +1 -0
- package/lib/payment/cryptopay/plugin.d.ts +3 -0
- package/lib/payment/cryptopay/plugin.js +201 -0
- package/lib/payment/cryptopay/plugin.js.map +1 -0
- package/lib/payment/datatrans-v2/middleware.js +1 -1
- package/lib/payment/datatrans-v2/middleware.js.map +1 -1
- package/lib/payment/postfinance-checkout/middleware.js +1 -1
- package/lib/payment/postfinance-checkout/middleware.js.map +1 -1
- package/lib/pricing/product-catalog-price.js.map +1 -1
- package/lib/pricing/product-price-rateconversion.js +23 -17
- package/lib/pricing/product-price-rateconversion.js.map +1 -1
- package/lib/pricing/product-price-round.js +3 -4
- package/lib/pricing/product-price-round.js.map +1 -1
- package/lib/pricing/utils/cache.d.ts +2 -1
- package/lib/warehousing/google-sheets.js +3 -2
- package/lib/warehousing/google-sheets.js.map +1 -1
- package/lib/{pricing/product-price-ECB-interbank-exchange.d.ts → warehousing/infinite-minter.d.ts} +0 -0
- package/lib/warehousing/infinite-minter.js +50 -0
- package/lib/warehousing/infinite-minter.js.map +1 -0
- package/lib/warehousing/store.js +2 -1
- package/lib/warehousing/store.js.map +1 -1
- package/lib/worker/BulkImportWorker.js +2 -3
- package/lib/worker/BulkImportWorker.js.map +1 -1
- package/lib/worker/ZombieKillerWorker.js +2 -3
- package/lib/worker/ZombieKillerWorker.js.map +1 -1
- package/lib/worker/email.js.map +1 -1
- package/lib/worker/export-token.d.ts +5 -0
- package/lib/worker/export-token.js +26 -0
- package/lib/worker/export-token.js.map +1 -0
- package/lib/worker/external.js +1 -0
- package/lib/worker/external.js.map +1 -1
- package/lib/{pricing/product-price-coinbase-exchange.d.ts → worker/update-coinbase-rates.d.ts} +0 -0
- package/lib/worker/update-coinbase-rates.js +69 -0
- package/lib/worker/update-coinbase-rates.js.map +1 -0
- package/lib/worker/update-ecb-rates.d.ts +1 -0
- package/lib/worker/update-ecb-rates.js +79 -0
- package/lib/worker/update-ecb-rates.js.map +1 -0
- package/lib/worker/update-token-ownership.d.ts +6 -0
- package/lib/worker/update-token-ownership.js +66 -0
- package/lib/worker/update-token-ownership.js.map +1 -0
- package/package.json +22 -19
- package/src/files/gridfs/gridfs-webhook.ts +1 -1
- package/src/files/minio/minio-webhook.js +1 -1
- package/src/payment/apple-iap/adapter.ts +1 -1
- package/src/payment/bity/adapter.ts +2 -2
- package/src/payment/cryptopay/db/CryptopayTransactions.ts +17 -0
- package/src/payment/cryptopay/index.ts +8 -0
- package/src/payment/cryptopay/middleware.ts +95 -0
- package/src/payment/cryptopay/module/configureCryptopayModule.ts +133 -0
- package/src/payment/cryptopay/plugin.ts +289 -0
- package/src/payment/cryptopay/readme.md +1 -0
- package/src/payment/datatrans-v2/middleware.ts +1 -1
- package/src/payment/postfinance-checkout/middleware.ts +1 -1
- package/src/pricing/product-catalog-price.ts +1 -0
- package/src/pricing/product-price-rateconversion.ts +25 -28
- package/src/pricing/product-price-round.ts +4 -5
- package/src/warehousing/google-sheets.ts +4 -2
- package/src/warehousing/infinite-minter.ts +66 -0
- package/src/warehousing/store.ts +3 -1
- package/src/worker/BulkImportWorker.ts +2 -3
- package/src/worker/ZombieKillerWorker.ts +2 -3
- package/src/worker/email.ts +0 -1
- package/src/worker/export-token.ts +33 -0
- package/src/worker/external.ts +1 -0
- package/src/worker/update-coinbase-rates.ts +91 -0
- package/src/worker/update-ecb-rates.ts +97 -0
- package/src/worker/update-token-ownership.ts +85 -0
- package/lib/payment/cryptopay.js +0 -161
- package/lib/payment/cryptopay.js.map +0 -1
- package/lib/pricing/product-price-ECB-interbank-exchange.js +0 -90
- package/lib/pricing/product-price-ECB-interbank-exchange.js.map +0 -1
- package/lib/pricing/product-price-coinbase-exchange.js +0 -60
- package/lib/pricing/product-price-coinbase-exchange.js.map +0 -1
- package/lib/pricing/product-price-cryptopay.d.ts +0 -2
- package/lib/pricing/product-price-cryptopay.js +0 -23
- package/lib/pricing/product-price-cryptopay.js.map +0 -1
- package/src/payment/cryptopay.ts +0 -197
- package/src/pricing/product-price-ECB-interbank-exchange.ts +0 -108
- package/src/pricing/product-price-coinbase-exchange.ts +0 -82
- package/src/pricing/product-price-cryptopay.ts +0 -27
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { IWorkerAdapter } from '@unchainedshop/types/worker';
|
|
2
|
+
import { WorkerAdapter, WorkerDirector } from '@unchainedshop/core-worker';
|
|
3
|
+
import later from '@breejs/later';
|
|
4
|
+
import fetch from 'node-fetch';
|
|
5
|
+
import { ProductPriceRate } from '@unchainedshop/types/products.pricing';
|
|
6
|
+
import { systemLocale } from '@unchainedshop/utils';
|
|
7
|
+
|
|
8
|
+
const getExchangeRates = async (base) => {
|
|
9
|
+
return fetch(`https://api.coinbase.com/v2/exchange-rates?currency=${base}`, {
|
|
10
|
+
method: 'GET',
|
|
11
|
+
})
|
|
12
|
+
.then((res) => res.json())
|
|
13
|
+
.then((r) => r?.data);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const everyMinute = later.parse.cron('* * * * *');
|
|
17
|
+
|
|
18
|
+
const UpdateCoinbaseRates: IWorkerAdapter<any, any> = {
|
|
19
|
+
...WorkerAdapter,
|
|
20
|
+
|
|
21
|
+
key: 'shop.unchained.worker.update-coinbase-rates',
|
|
22
|
+
label: 'Update Coinbase Rates',
|
|
23
|
+
version: '1.0',
|
|
24
|
+
type: 'UPDATE_COINBASE_RATES',
|
|
25
|
+
|
|
26
|
+
doWork: async (input, requestContext) => {
|
|
27
|
+
const { modules, services } = requestContext;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const currencyCode = await services.countries.resolveDefaultCurrencyCode(
|
|
31
|
+
{
|
|
32
|
+
isoCode: systemLocale.country,
|
|
33
|
+
},
|
|
34
|
+
requestContext,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const currencies = await modules.currencies.findCurrencies({ includeInactive: true });
|
|
38
|
+
const currencyCodes = currencies.map((currency) => currency.isoCode);
|
|
39
|
+
|
|
40
|
+
const {
|
|
41
|
+
currency: baseCurrency,
|
|
42
|
+
rates: pairs,
|
|
43
|
+
}: { currency: string; rates: Record<string, string> } = await getExchangeRates(currencyCode);
|
|
44
|
+
const timestamp = new Date();
|
|
45
|
+
|
|
46
|
+
// five minutes
|
|
47
|
+
const expiresAt = new Date(new Date().getTime() + 5 * 60 * 1000);
|
|
48
|
+
|
|
49
|
+
const rates: Array<ProductPriceRate> = Object.entries(pairs)
|
|
50
|
+
.map(([quoteCurrency, rate]) => {
|
|
51
|
+
return {
|
|
52
|
+
baseCurrency,
|
|
53
|
+
quoteCurrency,
|
|
54
|
+
rate: parseFloat(rate),
|
|
55
|
+
timestamp,
|
|
56
|
+
expiresAt,
|
|
57
|
+
};
|
|
58
|
+
})
|
|
59
|
+
.filter(
|
|
60
|
+
(rate) =>
|
|
61
|
+
currencyCodes.includes(rate.quoteCurrency) && rate.quoteCurrency !== rate.baseCurrency,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const success = await modules.products.prices.rates.updateRates(rates);
|
|
65
|
+
return {
|
|
66
|
+
success,
|
|
67
|
+
result: {
|
|
68
|
+
ratesUpdated: rates.length,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
} catch (e) {
|
|
72
|
+
return {
|
|
73
|
+
success: false,
|
|
74
|
+
error: {
|
|
75
|
+
name: 'UPDATE_COINBASE_RATES_FAILED',
|
|
76
|
+
message: 'Updating Coinbase Rates failed',
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
WorkerDirector.registerAdapter(UpdateCoinbaseRates);
|
|
84
|
+
|
|
85
|
+
WorkerDirector.configureAutoscheduling(UpdateCoinbaseRates, {
|
|
86
|
+
schedule: everyMinute,
|
|
87
|
+
retries: 0,
|
|
88
|
+
input: () => {
|
|
89
|
+
/* */
|
|
90
|
+
},
|
|
91
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { IWorkerAdapter } from '@unchainedshop/types/worker';
|
|
2
|
+
import { WorkerAdapter, WorkerDirector } from '@unchainedshop/core-worker';
|
|
3
|
+
import later from '@breejs/later';
|
|
4
|
+
import xmlJs from 'xml-js';
|
|
5
|
+
import fetch from 'node-fetch';
|
|
6
|
+
import { ProductPriceRate } from '@unchainedshop/types/products.pricing';
|
|
7
|
+
|
|
8
|
+
const getExchangeRates = async () => {
|
|
9
|
+
return fetch(`https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml`, {
|
|
10
|
+
method: 'GET',
|
|
11
|
+
})
|
|
12
|
+
.then((res) => res.text())
|
|
13
|
+
.then((text) => JSON.parse(xmlJs.xml2json(text)))
|
|
14
|
+
.then((json) =>
|
|
15
|
+
json.elements?.[0]?.elements
|
|
16
|
+
.filter((e) => e.name.toLowerCase() === 'cube')[0]
|
|
17
|
+
?.elements[0]?.elements.map((element) => element.attributes),
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html
|
|
22
|
+
// CET = UTC + 1
|
|
23
|
+
const everyDayAtFour = later.parse.cron('0 15 * * *');
|
|
24
|
+
|
|
25
|
+
const baseCurrency = 'EUR';
|
|
26
|
+
|
|
27
|
+
const UpdateECBRates: IWorkerAdapter<any, any> = {
|
|
28
|
+
...WorkerAdapter,
|
|
29
|
+
|
|
30
|
+
key: 'shop.unchained.worker.update-ecb-rates',
|
|
31
|
+
label: 'Update ECB Rates',
|
|
32
|
+
version: '1.0',
|
|
33
|
+
type: 'UPDATE_ECB_RATES',
|
|
34
|
+
|
|
35
|
+
doWork: async (input, requestContext) => {
|
|
36
|
+
const { modules } = requestContext;
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const data: Array<{ currency: string; rate: string }> = await getExchangeRates();
|
|
40
|
+
const timestamp = new Date();
|
|
41
|
+
const expiresAt = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
|
|
42
|
+
|
|
43
|
+
const currencies = await modules.currencies.findCurrencies({ includeInactive: true });
|
|
44
|
+
const currencyCodes = currencies.map((currency) => currency.isoCode);
|
|
45
|
+
|
|
46
|
+
if (!currencyCodes.includes(baseCurrency))
|
|
47
|
+
return {
|
|
48
|
+
success: true,
|
|
49
|
+
result: {
|
|
50
|
+
ratesUpdated: 0,
|
|
51
|
+
info: 'EUR not enabled',
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const rates: Array<ProductPriceRate> = data
|
|
56
|
+
.map((d) => {
|
|
57
|
+
return {
|
|
58
|
+
baseCurrency,
|
|
59
|
+
quoteCurrency: d.currency,
|
|
60
|
+
rate: parseFloat(d.rate),
|
|
61
|
+
timestamp,
|
|
62
|
+
expiresAt,
|
|
63
|
+
};
|
|
64
|
+
})
|
|
65
|
+
.filter(
|
|
66
|
+
(rate) =>
|
|
67
|
+
currencyCodes.includes(rate.quoteCurrency) && rate.quoteCurrency !== rate.baseCurrency,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const success = await modules.products.prices.rates.updateRates(rates);
|
|
71
|
+
return {
|
|
72
|
+
success,
|
|
73
|
+
result: {
|
|
74
|
+
ratesUpdated: rates.length,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
} catch (e) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: {
|
|
81
|
+
name: 'UPDATE_ECB_RATES_FAILED',
|
|
82
|
+
message: 'Updating ECB Rates failed',
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
WorkerDirector.registerAdapter(UpdateECBRates);
|
|
90
|
+
|
|
91
|
+
WorkerDirector.configureAutoscheduling(UpdateECBRates, {
|
|
92
|
+
schedule: everyDayAtFour,
|
|
93
|
+
retries: 5,
|
|
94
|
+
input: () => {
|
|
95
|
+
/* */
|
|
96
|
+
},
|
|
97
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { IWorkerAdapter, Work } from '@unchainedshop/types/worker';
|
|
2
|
+
import { WorkerDirector, WorkerAdapter, WorkerEventTypes } from '@unchainedshop/core-worker';
|
|
3
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
4
|
+
import later from '@breejs/later';
|
|
5
|
+
|
|
6
|
+
const everyMinute = later.parse.cron('* * * * *');
|
|
7
|
+
|
|
8
|
+
export const UpdateTokenOwnership: IWorkerAdapter<void, void> = {
|
|
9
|
+
...WorkerAdapter,
|
|
10
|
+
|
|
11
|
+
key: 'shop.unchained.worker-plugin.update-token-ownership',
|
|
12
|
+
label: 'External worker to update ownership of provided tokens',
|
|
13
|
+
version: '1.0',
|
|
14
|
+
type: 'UPDATE_TOKEN_OWNERSHIP',
|
|
15
|
+
external: true,
|
|
16
|
+
|
|
17
|
+
async doWork() {
|
|
18
|
+
throw new Error('Cannot do work for external workers');
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const RefreshTokens: IWorkerAdapter<void, any> = {
|
|
23
|
+
...WorkerAdapter,
|
|
24
|
+
|
|
25
|
+
key: 'shop.unchained.worker-plugin.refresh-tokens',
|
|
26
|
+
label: 'Worker to refresh tokens for accounts',
|
|
27
|
+
version: '1.0',
|
|
28
|
+
type: 'REFRESH_TOKENS',
|
|
29
|
+
external: false,
|
|
30
|
+
|
|
31
|
+
async doWork(input, requestContext) {
|
|
32
|
+
const { modules } = requestContext;
|
|
33
|
+
|
|
34
|
+
const tokens = await modules.warehousing.findTokens({});
|
|
35
|
+
const users = await modules.users.findUsers({ includeGuests: true });
|
|
36
|
+
const accounts = users.flatMap((user) => {
|
|
37
|
+
return user?.services?.web3?.flatMap((service) => {
|
|
38
|
+
if (service.verified) return [service.address];
|
|
39
|
+
return [];
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
tokens.forEach((token) => {
|
|
44
|
+
accounts.push(token.walletAddress);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const forked = await modules.worker.addWork(
|
|
48
|
+
{
|
|
49
|
+
type: 'UPDATE_TOKEN_OWNERSHIP',
|
|
50
|
+
retries: 0,
|
|
51
|
+
input: {
|
|
52
|
+
filter: {
|
|
53
|
+
tokens,
|
|
54
|
+
accounts: [...new Set(accounts)],
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
null,
|
|
59
|
+
);
|
|
60
|
+
return { success: true, result: { forked } };
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const configureUpdateTokenOwnership = (unchainedApi: UnchainedCore) => {
|
|
65
|
+
WorkerDirector.events.on(WorkerEventTypes.FINISHED, async ({ work }: { work: Work }) => {
|
|
66
|
+
if (work.type === 'UPDATE_TOKEN_OWNERSHIP' && work.success) {
|
|
67
|
+
await Promise.all(
|
|
68
|
+
(work.result?.tokens || []).map(unchainedApi.modules.warehousing.updateTokenOwnership),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default configureUpdateTokenOwnership;
|
|
75
|
+
|
|
76
|
+
WorkerDirector.registerAdapter(RefreshTokens);
|
|
77
|
+
WorkerDirector.registerAdapter(UpdateTokenOwnership);
|
|
78
|
+
|
|
79
|
+
WorkerDirector.configureAutoscheduling(RefreshTokens, {
|
|
80
|
+
schedule: everyMinute,
|
|
81
|
+
retries: 5,
|
|
82
|
+
input: () => {
|
|
83
|
+
/* */
|
|
84
|
+
},
|
|
85
|
+
});
|
package/lib/payment/cryptopay.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import bodyParser from 'body-parser';
|
|
2
|
-
import { useMiddlewareWithCurrentContext } from '@unchainedshop/api';
|
|
3
|
-
import { PaymentAdapter, PaymentDirector, PaymentError } from '@unchainedshop/core-payment';
|
|
4
|
-
import { OrderPricingSheet } from '@unchainedshop/core-orders';
|
|
5
|
-
import { ethers } from 'ethers';
|
|
6
|
-
import BIP32Factory from 'bip32';
|
|
7
|
-
import * as ecc from 'tiny-secp256k1';
|
|
8
|
-
import * as bitcoin from 'bitcoinjs-lib';
|
|
9
|
-
import { createLogger } from '@unchainedshop/logger';
|
|
10
|
-
const logger = createLogger('unchained:core-payment:cryptopay');
|
|
11
|
-
const { CRYPTOPAY_SECRET, CRYPTOPAY_WEBHOOK_PATH = '/payment/cryptopay', CRYPTOPAY_BTC_XPUB, CRYPTOPAY_ETH_XPUB, CRYPTOPAY_BTC_TESTNET = false, CRYPTOPAY_MAX_RATE_AGE = '360', // seconds
|
|
12
|
-
CRYPTOPAY_MAX_CONV_DIFF = '0.01', } = process.env;
|
|
13
|
-
var CryptopayCurrencies;
|
|
14
|
-
(function (CryptopayCurrencies) {
|
|
15
|
-
CryptopayCurrencies["BTC"] = "BTC";
|
|
16
|
-
CryptopayCurrencies["ETH"] = "ETH";
|
|
17
|
-
})(CryptopayCurrencies || (CryptopayCurrencies = {}));
|
|
18
|
-
const MAX_RATE_AGE = parseInt(CRYPTOPAY_MAX_RATE_AGE, 10);
|
|
19
|
-
const MAX_ALLOWED_DIFF = parseFloat(CRYPTOPAY_MAX_CONV_DIFF); // Accept payments when the converted amount differs by adapterActions much (in percent)
|
|
20
|
-
export default (app) => {
|
|
21
|
-
useMiddlewareWithCurrentContext(app, CRYPTOPAY_WEBHOOK_PATH, bodyParser.json());
|
|
22
|
-
useMiddlewareWithCurrentContext(app, CRYPTOPAY_WEBHOOK_PATH, async (request, response) => {
|
|
23
|
-
// Return a 200 response to acknowledge receipt of the event
|
|
24
|
-
const resolvedContext = request.unchainedContext;
|
|
25
|
-
const { currency, contract, decimals, address, amount, secret } = request.body;
|
|
26
|
-
if (secret !== CRYPTOPAY_SECRET) {
|
|
27
|
-
logger.warn(`Cryptopay Plugin: Invalid Cryptopay Secret provided`);
|
|
28
|
-
response.end(JSON.stringify({ success: false }));
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
const orderPayment = await resolvedContext.modules.orders.payments.findOrderPaymentByContextData({
|
|
32
|
-
context: { cryptoAddresses: { currency, address } },
|
|
33
|
-
});
|
|
34
|
-
if (orderPayment) {
|
|
35
|
-
if (currency === CryptopayCurrencies.ETH && contract && contract !== '') {
|
|
36
|
-
const ERC20CurrencyCount = await resolvedContext.modules.currencies.count({
|
|
37
|
-
includeInactive: false,
|
|
38
|
-
contractAddress: contract,
|
|
39
|
-
});
|
|
40
|
-
if (!ERC20CurrencyCount) {
|
|
41
|
-
logger.warn(`Cryptopay Plugin: ERC20 token address ${contract} not whitelisted.`);
|
|
42
|
-
response.end(JSON.stringify({ success: false }));
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const order = await resolvedContext.modules.orders.findOrder({
|
|
47
|
-
orderId: orderPayment.orderId,
|
|
48
|
-
});
|
|
49
|
-
const pricing = OrderPricingSheet({
|
|
50
|
-
calculation: order.calculation,
|
|
51
|
-
currency: order.currency,
|
|
52
|
-
});
|
|
53
|
-
const totalAmount = pricing?.total({ useNetPrice: false }).amount;
|
|
54
|
-
let convertedAmount;
|
|
55
|
-
if (order.currency === currency) {
|
|
56
|
-
convertedAmount = amount / 10 ** (decimals - 8); // All crypto native prices denoted with 8 decimals
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
// Need to convert
|
|
60
|
-
const rate = await resolvedContext.modules.products.prices.rates.getRate(order.currency, contract && contract !== '' ? contract : currency, // Convert to the smart contract if given
|
|
61
|
-
MAX_RATE_AGE);
|
|
62
|
-
if (rate) {
|
|
63
|
-
// We assume that we are converting to a fiat currency here (with 2 decimals).
|
|
64
|
-
// Paying an order with prices in crypto in another crypto is not supported.
|
|
65
|
-
convertedAmount = Math.round((amount / 10 ** decimals) * rate * 100);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
if (convertedAmount && convertedAmount >= totalAmount * (1 - MAX_ALLOWED_DIFF)) {
|
|
69
|
-
await resolvedContext.modules.orders.checkout(order._id, {
|
|
70
|
-
transactionContext: { address },
|
|
71
|
-
paymentContext: { address },
|
|
72
|
-
}, resolvedContext);
|
|
73
|
-
response.end(JSON.stringify({ success: true }));
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
logger.warn(`Cryptopay Plugin: OrderPayment ${orderPayment._id} not marked as paid. Converted amount is ${convertedAmount}`);
|
|
77
|
-
response.end(JSON.stringify({ success: false }));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
logger.info(`Cryptopay Plugin: No orderPayment with address ${address} and currency ${currency} found`);
|
|
82
|
-
response.end(JSON.stringify({ success: false }));
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
};
|
|
86
|
-
const Cryptopay = {
|
|
87
|
-
...PaymentAdapter,
|
|
88
|
-
key: 'shop.unchained.payment.cryptopay',
|
|
89
|
-
label: 'Cryptopay',
|
|
90
|
-
version: '1.0',
|
|
91
|
-
typeSupported(type) {
|
|
92
|
-
return type === 'GENERIC';
|
|
93
|
-
},
|
|
94
|
-
actions: (params) => {
|
|
95
|
-
const { modules } = params.context;
|
|
96
|
-
const adapterActions = {
|
|
97
|
-
...PaymentAdapter.actions(params),
|
|
98
|
-
// eslint-disable-next-line
|
|
99
|
-
configurationError() {
|
|
100
|
-
// eslint-disable-line
|
|
101
|
-
if (!CRYPTOPAY_SECRET) {
|
|
102
|
-
return PaymentError.INCOMPLETE_CONFIGURATION;
|
|
103
|
-
}
|
|
104
|
-
return null;
|
|
105
|
-
},
|
|
106
|
-
isActive() {
|
|
107
|
-
if (adapterActions.configurationError() === null)
|
|
108
|
-
return true;
|
|
109
|
-
return false;
|
|
110
|
-
},
|
|
111
|
-
isPayLaterAllowed() {
|
|
112
|
-
return false;
|
|
113
|
-
},
|
|
114
|
-
sign: async () => {
|
|
115
|
-
const { orderPayment } = params.paymentContext;
|
|
116
|
-
if (orderPayment?.context.length) {
|
|
117
|
-
// Do not derive address a second time for order payment, return existing address
|
|
118
|
-
const existingAddresses = orderPayment.context.filter((c) => c.currency);
|
|
119
|
-
if (existingAddresses) {
|
|
120
|
-
return JSON.stringify(existingAddresses);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const cryptoAddresses = [];
|
|
124
|
-
if (CRYPTOPAY_BTC_XPUB) {
|
|
125
|
-
const network = CRYPTOPAY_BTC_TESTNET ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
|
|
126
|
-
const bip32 = BIP32Factory(ecc);
|
|
127
|
-
const hardenedMaster = bip32.fromBase58(CRYPTOPAY_BTC_XPUB, network);
|
|
128
|
-
const btcDerivationNumber = await modules.orders.payments.countOrderPaymentsByContextData({
|
|
129
|
-
context: { cryptoAddresses: { currency: CryptopayCurrencies.BTC } },
|
|
130
|
-
});
|
|
131
|
-
const child = hardenedMaster.derivePath(`0/${btcDerivationNumber}`);
|
|
132
|
-
cryptoAddresses.push({
|
|
133
|
-
currency: CryptopayCurrencies.BTC,
|
|
134
|
-
address: bitcoin.payments.p2pkh({
|
|
135
|
-
pubkey: child.publicKey,
|
|
136
|
-
network,
|
|
137
|
-
}).address,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
if (CRYPTOPAY_ETH_XPUB) {
|
|
141
|
-
const hardenedMaster = ethers.utils.HDNode.fromExtendedKey(CRYPTOPAY_ETH_XPUB);
|
|
142
|
-
const ethDerivationNumber = await modules.orders.payments.countOrderPaymentsByContextData({
|
|
143
|
-
context: { cryptoAddresses: { currency: CryptopayCurrencies.ETH } },
|
|
144
|
-
});
|
|
145
|
-
cryptoAddresses.push({
|
|
146
|
-
currency: CryptopayCurrencies.ETH,
|
|
147
|
-
address: hardenedMaster.derivePath(`0/${ethDerivationNumber}`).address,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
await modules.orders.payments.updateContext(orderPayment._id, { cryptoAddresses }, params.context);
|
|
151
|
-
return JSON.stringify(cryptoAddresses);
|
|
152
|
-
},
|
|
153
|
-
charge: async () => {
|
|
154
|
-
return false;
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
return adapterActions;
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
PaymentDirector.registerAdapter(Cryptopay);
|
|
161
|
-
//# sourceMappingURL=cryptopay.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cryptopay.js","sourceRoot":"","sources":["../../src/payment/cryptopay.ts"],"names":[],"mappings":"AAEA,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,+BAA+B,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE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OAAO,EAAE,EAAE,eAAe,EAAE,EAAE,QAAQ,EAAE,mBAAmB,CAAC,GAAG,EAAE,EAAE;qBACpE,CAAC,CAAC;oBACH,eAAe,CAAC,IAAI,CAAC;wBACnB,QAAQ,EAAE,mBAAmB,CAAC,GAAG;wBACjC,OAAO,EAAE,cAAc,CAAC,UAAU,CAAC,KAAK,mBAAmB,EAAE,CAAC,CAAC,OAAO;qBACvE,CAAC,CAAC;iBACJ;gBACD,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CACzC,YAAY,CAAC,GAAG,EAChB,EAAE,eAAe,EAAE,EACnB,MAAM,CAAC,OAAO,CACf,CAAC;gBACF,OAAO,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;YACzC,CAAC;YAED,MAAM,EAAE,KAAK,IAAoB,EAAE;gBACjC,OAAO,KAAK,CAAC;YACf,CAAC;SACF,CAAC;QAEF,OAAO,cAAc,CAAC;IACxB,CAAC;CACF,CAAC;AAEF,eAAe,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC"}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import fetch from 'node-fetch';
|
|
2
|
-
import { ProductPricingDirector, ProductPricingAdapter } from '@unchainedshop/core-products';
|
|
3
|
-
import xmlJs from 'xml-js';
|
|
4
|
-
import Cache from './utils/cache';
|
|
5
|
-
const CACHE_PERIOD = 60 * 60 * 24; // 1 day
|
|
6
|
-
const SUPPORTED_CURRENCIES = [
|
|
7
|
-
'USD',
|
|
8
|
-
'JPY',
|
|
9
|
-
'BGN',
|
|
10
|
-
'CZK',
|
|
11
|
-
'DKK',
|
|
12
|
-
'GBP',
|
|
13
|
-
'HUF',
|
|
14
|
-
'PLN',
|
|
15
|
-
'RON',
|
|
16
|
-
'SEK',
|
|
17
|
-
'CHF',
|
|
18
|
-
'ISK',
|
|
19
|
-
'NOK',
|
|
20
|
-
'HRK',
|
|
21
|
-
'RUB',
|
|
22
|
-
'TRY',
|
|
23
|
-
'AUD',
|
|
24
|
-
'BRL',
|
|
25
|
-
'CAD',
|
|
26
|
-
'CNY',
|
|
27
|
-
'HKD',
|
|
28
|
-
'IDR',
|
|
29
|
-
'ILS',
|
|
30
|
-
'INR',
|
|
31
|
-
'KRW',
|
|
32
|
-
'MXN',
|
|
33
|
-
'MYR',
|
|
34
|
-
'NZD',
|
|
35
|
-
'PHP',
|
|
36
|
-
'SGD',
|
|
37
|
-
'THB',
|
|
38
|
-
'ZAR',
|
|
39
|
-
];
|
|
40
|
-
const cache = new Cache(CACHE_PERIOD);
|
|
41
|
-
const getEURexchangeRateForCurrency = async (currency) => {
|
|
42
|
-
const response = cache.get(currency, () => fetch(`https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml`, {
|
|
43
|
-
method: 'GET',
|
|
44
|
-
})
|
|
45
|
-
.then((res) => res.text())
|
|
46
|
-
.then((text) => JSON.parse(xmlJs.xml2json(text)))
|
|
47
|
-
.then((json) => json.elements?.[0]?.elements
|
|
48
|
-
.filter((e) => e.name.toLowerCase() === 'cube')[0]
|
|
49
|
-
?.elements[0]?.elements.filter((a) => a.attributes.currency === currency)?.[0]?.attributes));
|
|
50
|
-
return response;
|
|
51
|
-
};
|
|
52
|
-
const ProductPriceECBIntraBankExchange = {
|
|
53
|
-
...ProductPricingAdapter,
|
|
54
|
-
key: 'shop.unchained.pricing.price-ECB-intrabank-exchange',
|
|
55
|
-
version: '1.0',
|
|
56
|
-
label: 'Convert EUR to X with current exchange rate',
|
|
57
|
-
orderIndex: 1,
|
|
58
|
-
isActivatedFor: (context) => {
|
|
59
|
-
return SUPPORTED_CURRENCIES.indexOf(context.currency) !== -1;
|
|
60
|
-
},
|
|
61
|
-
actions: (params) => {
|
|
62
|
-
const pricingAdapter = ProductPricingAdapter.actions(params);
|
|
63
|
-
return {
|
|
64
|
-
...pricingAdapter,
|
|
65
|
-
calculate: async () => {
|
|
66
|
-
const { product, country, quantity, currency, modules } = params.context;
|
|
67
|
-
const { calculation = [] } = params.calculationSheet;
|
|
68
|
-
const EURprice = await modules.products.prices.price(product, {
|
|
69
|
-
country,
|
|
70
|
-
currency: 'EUR',
|
|
71
|
-
quantity,
|
|
72
|
-
});
|
|
73
|
-
if (!EURprice || !EURprice?.amount || calculation?.length)
|
|
74
|
-
return pricingAdapter.calculate();
|
|
75
|
-
const exchange = await getEURexchangeRateForCurrency(currency);
|
|
76
|
-
const convertedAmount = EURprice.amount * exchange.rate;
|
|
77
|
-
pricingAdapter.resultSheet().resetCalculation(params.calculationSheet);
|
|
78
|
-
pricingAdapter.resultSheet().addItem({
|
|
79
|
-
amount: convertedAmount * quantity,
|
|
80
|
-
isTaxable: EURprice.isTaxable,
|
|
81
|
-
isNetPrice: EURprice.isNetPrice,
|
|
82
|
-
meta: { adapter: ProductPriceECBIntraBankExchange.key },
|
|
83
|
-
});
|
|
84
|
-
return pricingAdapter.calculate();
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
ProductPricingDirector.registerAdapter(ProductPriceECBIntraBankExchange);
|
|
90
|
-
//# sourceMappingURL=product-price-ECB-interbank-exchange.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"product-price-ECB-interbank-exchange.js","sourceRoot":"","sources":["../../src/pricing/product-price-ECB-interbank-exchange.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC7F,OAAO,KAAK,MAAM,QAAQ,CAAC;AAE3B,OAAO,KAAK,MAAM,eAAe,CAAC;AAElC,MAAM,YAAY,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,QAAQ;AAE3C,MAAM,oBAAoB,GAAG;IAC3B,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,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AE,QAAQ,CAAC,SAAS;oBAC7B,UAAU,EAAE,QAAQ,CAAC,UAAU;oBAC/B,IAAI,EAAE,EAAE,OAAO,EAAE,gCAAgC,CAAC,GAAG,EAAE;iBACxD,CAAC,CAAC;gBAEH,OAAO,cAAc,CAAC,SAAS,EAAE,CAAC;YACpC,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,sBAAsB,CAAC,eAAe,CAAC,gCAAgC,CAAC,CAAC"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import fetch from 'node-fetch';
|
|
2
|
-
import { ProductPricingDirector, ProductPricingAdapter } from '@unchainedshop/core-products';
|
|
3
|
-
import Cache from './utils/cache';
|
|
4
|
-
const CACHE_PERIOD = 60 * 60 * 0.1; // 10 minutes
|
|
5
|
-
const SUPPORTED_CURRENCIES = ['BTC', 'ETH', 'XRP', 'USDT', 'BCH', 'BSV', 'LTC', 'EOS', 'BNB', 'XTZ'];
|
|
6
|
-
const cache = new Cache(CACHE_PERIOD);
|
|
7
|
-
const getFiatexchangeRateForCrypto = async (base, target) => {
|
|
8
|
-
const { data } = await cache.get(`${base}-${target}`, () => fetch(`https://api.coinbase.com/v2/exchange-rates?currency=${base}`, {
|
|
9
|
-
method: 'GET',
|
|
10
|
-
}).then((res) => res.json()));
|
|
11
|
-
return data?.rates?.[target];
|
|
12
|
-
};
|
|
13
|
-
const ProductPriceCoinbaseExchange = {
|
|
14
|
-
...ProductPricingAdapter,
|
|
15
|
-
key: 'shop.unchained.pricing.price-coinbase-exchange',
|
|
16
|
-
version: '1.0',
|
|
17
|
-
label: 'Convert fiat/crypto to crypto with current exchange rate',
|
|
18
|
-
orderIndex: 1,
|
|
19
|
-
isActivatedFor: (context) => {
|
|
20
|
-
return SUPPORTED_CURRENCIES.indexOf(context.currency.toUpperCase()) !== -1;
|
|
21
|
-
},
|
|
22
|
-
actions: (params) => {
|
|
23
|
-
const pricingAdapter = ProductPricingAdapter.actions(params);
|
|
24
|
-
const { services, modules } = params.context;
|
|
25
|
-
return {
|
|
26
|
-
...pricingAdapter,
|
|
27
|
-
calculate: async () => {
|
|
28
|
-
const { product, country, quantity, currency } = params.context;
|
|
29
|
-
const defaultCurrency = await services.countries.resolveDefaultCurrencyCode({
|
|
30
|
-
isoCode: country,
|
|
31
|
-
}, params.context);
|
|
32
|
-
const productPrice = await modules.products.prices.price(product, {
|
|
33
|
-
country,
|
|
34
|
-
currency: defaultCurrency,
|
|
35
|
-
quantity,
|
|
36
|
-
});
|
|
37
|
-
const { calculation = [] } = params.calculationSheet;
|
|
38
|
-
if (!productPrice ||
|
|
39
|
-
!productPrice?.amount ||
|
|
40
|
-
calculation?.length ||
|
|
41
|
-
defaultCurrency === currency)
|
|
42
|
-
return pricingAdapter.calculate();
|
|
43
|
-
const rate = await getFiatexchangeRateForCrypto(defaultCurrency, currency);
|
|
44
|
-
if (rate > 0) {
|
|
45
|
-
const convertedAmount = productPrice.amount * rate;
|
|
46
|
-
pricingAdapter.resultSheet().resetCalculation(params.calculationSheet);
|
|
47
|
-
pricingAdapter.resultSheet().addItem({
|
|
48
|
-
amount: convertedAmount * quantity,
|
|
49
|
-
isTaxable: productPrice?.isTaxable,
|
|
50
|
-
isNetPrice: productPrice?.isNetPrice,
|
|
51
|
-
meta: { adapter: ProductPriceCoinbaseExchange.key },
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
return pricingAdapter.calculate();
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
ProductPricingDirector.registerAdapter(ProductPriceCoinbaseExchange);
|
|
60
|
-
//# sourceMappingURL=product-price-coinbase-exchange.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"product-price-coinbase-exchange.js","sourceRoot":"","sources":["../../src/pricing/product-price-coinbase-exchange.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,sBAAsB,EAAE,qBAAq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AAE;qBACpD,CAAC,CAAC;iBACJ;gBAED,OAAO,cAAc,CAAC,SAAS,EAAE,CAAC;YACpC,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,sBAAsB,CAAC,eAAe,CAAC,4BAA4B,CAAC,CAAC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import bodyParser from 'body-parser';
|
|
2
|
-
import { useMiddlewareWithCurrentContext } from '@unchainedshop/api';
|
|
3
|
-
const { CRYPTOPAY_SECRET, CRYPTOPAY_PRICING_WEBHOOK_PATH = '/pricing/cryptopay' } = process.env;
|
|
4
|
-
export default (app) => {
|
|
5
|
-
useMiddlewareWithCurrentContext(app, CRYPTOPAY_PRICING_WEBHOOK_PATH, bodyParser.json());
|
|
6
|
-
useMiddlewareWithCurrentContext(app, CRYPTOPAY_PRICING_WEBHOOK_PATH, async (request, response) => {
|
|
7
|
-
const resolvedContext = request.unchainedContext;
|
|
8
|
-
const { baseCurrency, token, rate, timestamp, secret } = request.body;
|
|
9
|
-
if (secret !== CRYPTOPAY_SECRET) {
|
|
10
|
-
response.end(JSON.stringify({ success: false }));
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
const rateData = {
|
|
14
|
-
baseCurrency,
|
|
15
|
-
quoteCurrency: token,
|
|
16
|
-
rate,
|
|
17
|
-
timestamp,
|
|
18
|
-
};
|
|
19
|
-
const success = await resolvedContext.modules.products.prices.rates.updateRate(rateData);
|
|
20
|
-
response.end(JSON.stringify({ success }));
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
//# sourceMappingURL=product-price-cryptopay.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"product-price-cryptopay.js","sourceRoot":"","sources":["../../src/pricing/product-price-cryptopay.ts"],"names":[],"mappings":"AAEA,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,+BAA+B,EAAE,MAAM,oBAAoB,CAAC;AAErE,MAAM,EAAE,gBAAgB,EAAE,8BAA8B,GAAG,oBAAoB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;AAEhG,eAAe,CAAC,GAAG,EAAE,EAAE;IACrB,+BAA+B,CAAC,GAAG,EAAE,8BAA8B,EAAE,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;IAExF,+BAA+B,CAAC,GAAG,EAAE,8BAA8B,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE;QAC/F,MAAM,eAAe,GAAG,OAAO,CAAC,gBAA2B,CAAC;QAC5D,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;QACtE,IAAI,MAAM,KAAK,gBAAgB,EAAE;YAC/B,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;YACjD,OAAO;SACR;QACD,MAAM,QAAQ,GAAqB;YACjC,YAAY;YACZ,aAAa,EAAE,KAAK;YACpB,IAAI;YACJ,SAAS;SACV,CAAC;QACF,MAAM,OAAO,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACzF,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|