@unchainedshop/plugins 4.0.0-rc.8 → 4.0.0

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.
Files changed (321) hide show
  1. package/lib/accounts/google-oauth.d.ts +4 -0
  2. package/lib/accounts/google-oauth.d.ts.map +1 -0
  3. package/lib/accounts/google-oauth.js +145 -0
  4. package/lib/accounts/google-oauth.js.map +1 -0
  5. package/lib/accounts/linkedin-oauth.d.ts +4 -0
  6. package/lib/accounts/linkedin-oauth.d.ts.map +1 -0
  7. package/lib/accounts/linkedin-oauth.js +82 -0
  8. package/lib/accounts/linkedin-oauth.js.map +1 -0
  9. package/lib/delivery/send-message.js.map +1 -1
  10. package/lib/enrollments/licensed.js +15 -2
  11. package/lib/enrollments/licensed.js.map +1 -1
  12. package/lib/files/gridfs/adapter.d.ts.map +1 -1
  13. package/lib/files/gridfs/adapter.js +11 -8
  14. package/lib/files/gridfs/adapter.js.map +1 -1
  15. package/lib/files/gridfs/gridfs-adapter.d.ts +3 -0
  16. package/lib/files/gridfs/gridfs-adapter.d.ts.map +1 -0
  17. package/lib/files/gridfs/gridfs-adapter.js +97 -0
  18. package/lib/files/gridfs/gridfs-adapter.js.map +1 -0
  19. package/lib/files/gridfs/gridfs-webhook.d.ts +11 -0
  20. package/lib/files/gridfs/gridfs-webhook.d.ts.map +1 -0
  21. package/lib/files/gridfs/gridfs-webhook.js +84 -0
  22. package/lib/files/gridfs/gridfs-webhook.js.map +1 -0
  23. package/lib/files/gridfs/handler-express.d.ts.map +1 -1
  24. package/lib/files/gridfs/handler-express.js +21 -25
  25. package/lib/files/gridfs/handler-express.js.map +1 -1
  26. package/lib/files/gridfs/handler-fastify.d.ts.map +1 -1
  27. package/lib/files/gridfs/handler-fastify.js +50 -13
  28. package/lib/files/gridfs/handler-fastify.js.map +1 -1
  29. package/lib/files/gridfs/module.d.ts +2 -2
  30. package/lib/files/minio/handler-express.d.ts.map +1 -1
  31. package/lib/files/minio/handler-express.js +4 -7
  32. package/lib/files/minio/handler-express.js.map +1 -1
  33. package/lib/files/minio/handler-fastify.js +2 -2
  34. package/lib/files/minio/handler-fastify.js.map +1 -1
  35. package/lib/files/minio/index.d.ts +1 -1
  36. package/lib/files/minio/index.d.ts.map +1 -1
  37. package/lib/files/minio/index.js +7 -7
  38. package/lib/files/minio/index.js.map +1 -1
  39. package/lib/files/minio/minio-adapter.d.ts +6 -0
  40. package/lib/files/minio/minio-adapter.d.ts.map +1 -0
  41. package/lib/files/minio/minio-adapter.js +181 -0
  42. package/lib/files/minio/minio-adapter.js.map +1 -0
  43. package/lib/files/minio/minio-webhook.d.ts +2 -0
  44. package/lib/files/minio/minio-webhook.d.ts.map +1 -0
  45. package/lib/files/minio/minio-webhook.js +33 -0
  46. package/lib/files/minio/minio-webhook.js.map +1 -0
  47. package/lib/filters/local-search.js +1 -1
  48. package/lib/filters/local-search.js.map +1 -1
  49. package/lib/filters/strict-equal.js +0 -18
  50. package/lib/filters/strict-equal.js.map +1 -1
  51. package/lib/payment/apple-iap/adapter.js +12 -10
  52. package/lib/payment/apple-iap/adapter.js.map +1 -1
  53. package/lib/payment/apple-iap/db/AppleTransactionsCollection.d.ts +1 -1
  54. package/lib/payment/apple-iap/db/AppleTransactionsCollection.d.ts.map +1 -1
  55. package/lib/payment/apple-iap/handler-express.d.ts.map +1 -1
  56. package/lib/payment/apple-iap/handler-express.js +14 -10
  57. package/lib/payment/apple-iap/handler-express.js.map +1 -1
  58. package/lib/payment/apple-iap/handler-fastify.d.ts.map +1 -1
  59. package/lib/payment/apple-iap/handler-fastify.js +15 -8
  60. package/lib/payment/apple-iap/handler-fastify.js.map +1 -1
  61. package/lib/payment/apple-iap/module/configureAppleTransactionsModule.d.ts +9 -0
  62. package/lib/payment/apple-iap/module/configureAppleTransactionsModule.d.ts.map +1 -0
  63. package/lib/payment/apple-iap/module/configureAppleTransactionsModule.js +17 -0
  64. package/lib/payment/apple-iap/module/configureAppleTransactionsModule.js.map +1 -0
  65. package/lib/payment/apple-iap/module.d.ts +4 -4
  66. package/lib/payment/apple-iap/module.d.ts.map +1 -1
  67. package/lib/payment/apple-iap/module.js +4 -2
  68. package/lib/payment/apple-iap/module.js.map +1 -1
  69. package/lib/payment/braintree.js +3 -1
  70. package/lib/payment/braintree.js.map +1 -1
  71. package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts +1 -1
  72. package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts.map +1 -1
  73. package/lib/payment/cryptopay/derive-btc-address.d.ts.map +1 -1
  74. package/lib/payment/cryptopay/derive-btc-address.js +2 -3
  75. package/lib/payment/cryptopay/derive-btc-address.js.map +1 -1
  76. package/lib/payment/cryptopay/derive-eth-address.d.ts.map +1 -1
  77. package/lib/payment/cryptopay/derive-eth-address.js +5 -3
  78. package/lib/payment/cryptopay/derive-eth-address.js.map +1 -1
  79. package/lib/payment/cryptopay/handle-webhook.js +2 -2
  80. package/lib/payment/cryptopay/handle-webhook.js.map +1 -1
  81. package/lib/payment/cryptopay/handler-express.d.ts.map +1 -1
  82. package/lib/payment/cryptopay/handler-express.js +4 -7
  83. package/lib/payment/cryptopay/handler-express.js.map +1 -1
  84. package/lib/payment/cryptopay/handler-fastify.js +1 -1
  85. package/lib/payment/cryptopay/handler-fastify.js.map +1 -1
  86. package/lib/payment/cryptopay/middleware.d.ts +2 -0
  87. package/lib/payment/cryptopay/middleware.d.ts.map +1 -0
  88. package/lib/payment/cryptopay/middleware.js +75 -0
  89. package/lib/payment/cryptopay/middleware.js.map +1 -0
  90. package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts +25 -0
  91. package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts.map +1 -0
  92. package/lib/payment/cryptopay/module/configureCryptopayModule.js +75 -0
  93. package/lib/payment/cryptopay/module/configureCryptopayModule.js.map +1 -0
  94. package/lib/payment/cryptopay/module.d.ts +2 -2
  95. package/lib/payment/cryptopay/module.d.ts.map +1 -1
  96. package/lib/payment/cryptopay/module.js +4 -4
  97. package/lib/payment/cryptopay/module.js.map +1 -1
  98. package/lib/payment/cryptopay/plugin.d.ts.map +1 -1
  99. package/lib/payment/cryptopay/plugin.js +18 -4
  100. package/lib/payment/cryptopay/plugin.js.map +1 -1
  101. package/lib/payment/datatrans-v2/handler-express.d.ts.map +1 -1
  102. package/lib/payment/datatrans-v2/handler-express.js +13 -11
  103. package/lib/payment/datatrans-v2/handler-express.js.map +1 -1
  104. package/lib/payment/datatrans-v2/handler-fastify.d.ts.map +1 -1
  105. package/lib/payment/datatrans-v2/handler-fastify.js +11 -5
  106. package/lib/payment/datatrans-v2/handler-fastify.js.map +1 -1
  107. package/lib/payment/datatrans-v2/index.js +63 -37
  108. package/lib/payment/datatrans-v2/index.js.map +1 -1
  109. package/lib/payment/datatrans-v2/middleware.d.ts +2 -0
  110. package/lib/payment/datatrans-v2/middleware.d.ts.map +1 -0
  111. package/lib/payment/datatrans-v2/middleware.js +68 -0
  112. package/lib/payment/datatrans-v2/middleware.js.map +1 -0
  113. package/lib/payment/datatrans-v2/parseRegistrationData.d.ts +1 -1
  114. package/lib/payment/datatrans-v2/parseRegistrationData.d.ts.map +1 -1
  115. package/lib/payment/paypal-checkout.js +4 -2
  116. package/lib/payment/paypal-checkout.js.map +1 -1
  117. package/lib/payment/payrexx/api/index.d.ts +1 -1
  118. package/lib/payment/payrexx/api/index.d.ts.map +1 -1
  119. package/lib/payment/payrexx/api/makeFetcher.d.ts +1 -1
  120. package/lib/payment/payrexx/api/makeFetcher.d.ts.map +1 -1
  121. package/lib/payment/payrexx/api/makeFetcher.js +1 -1
  122. package/lib/payment/payrexx/api/makeFetcher.js.map +1 -1
  123. package/lib/payment/payrexx/handler-express.d.ts.map +1 -1
  124. package/lib/payment/payrexx/handler-express.js +15 -15
  125. package/lib/payment/payrexx/handler-express.js.map +1 -1
  126. package/lib/payment/payrexx/handler-fastify.js +23 -15
  127. package/lib/payment/payrexx/handler-fastify.js.map +1 -1
  128. package/lib/payment/payrexx/index.js +21 -15
  129. package/lib/payment/payrexx/index.js.map +1 -1
  130. package/lib/payment/payrexx/middleware.d.ts +2 -0
  131. package/lib/payment/payrexx/middleware.d.ts.map +1 -0
  132. package/lib/payment/payrexx/middleware.js +86 -0
  133. package/lib/payment/payrexx/middleware.js.map +1 -0
  134. package/lib/payment/postfinance-checkout/api-client.d.ts.map +1 -1
  135. package/lib/payment/postfinance-checkout/api-client.js +7 -7
  136. package/lib/payment/postfinance-checkout/api-client.js.map +1 -1
  137. package/lib/payment/postfinance-checkout/handler-express.js +7 -8
  138. package/lib/payment/postfinance-checkout/handler-express.js.map +1 -1
  139. package/lib/payment/postfinance-checkout/handler-fastify.d.ts.map +1 -1
  140. package/lib/payment/postfinance-checkout/handler-fastify.js +6 -4
  141. package/lib/payment/postfinance-checkout/handler-fastify.js.map +1 -1
  142. package/lib/payment/postfinance-checkout/index.d.ts.map +1 -1
  143. package/lib/payment/postfinance-checkout/index.js +20 -7
  144. package/lib/payment/postfinance-checkout/index.js.map +1 -1
  145. package/lib/payment/postfinance-checkout/middleware.d.ts +2 -0
  146. package/lib/payment/postfinance-checkout/middleware.d.ts.map +1 -0
  147. package/lib/payment/postfinance-checkout/middleware.js +38 -0
  148. package/lib/payment/postfinance-checkout/middleware.js.map +1 -0
  149. package/lib/payment/saferpay/adapter.d.ts.map +1 -1
  150. package/lib/payment/saferpay/adapter.js +9 -0
  151. package/lib/payment/saferpay/adapter.js.map +1 -1
  152. package/lib/payment/saferpay/handler-express.d.ts.map +1 -1
  153. package/lib/payment/saferpay/handler-express.js +10 -8
  154. package/lib/payment/saferpay/handler-express.js.map +1 -1
  155. package/lib/payment/saferpay/handler-fastify.d.ts.map +1 -1
  156. package/lib/payment/saferpay/handler-fastify.js +11 -5
  157. package/lib/payment/saferpay/handler-fastify.js.map +1 -1
  158. package/lib/payment/saferpay/middleware.d.ts +2 -0
  159. package/lib/payment/saferpay/middleware.d.ts.map +1 -0
  160. package/lib/payment/saferpay/middleware.js +57 -0
  161. package/lib/payment/saferpay/middleware.js.map +1 -0
  162. package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.d.ts +10 -0
  163. package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.d.ts.map +1 -0
  164. package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.js +24 -0
  165. package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.js.map +1 -0
  166. package/lib/payment/saferpay/module.d.ts +2 -2
  167. package/lib/payment/stripe/handler-express.js +25 -17
  168. package/lib/payment/stripe/handler-express.js.map +1 -1
  169. package/lib/payment/stripe/handler-fastify.d.ts.map +1 -1
  170. package/lib/payment/stripe/handler-fastify.js +33 -12
  171. package/lib/payment/stripe/handler-fastify.js.map +1 -1
  172. package/lib/payment/stripe/index.js +21 -15
  173. package/lib/payment/stripe/index.js.map +1 -1
  174. package/lib/payment/stripe/middleware.d.ts +6 -0
  175. package/lib/payment/stripe/middleware.d.ts.map +1 -0
  176. package/lib/payment/stripe/middleware.js +114 -0
  177. package/lib/payment/stripe/middleware.js.map +1 -0
  178. package/lib/payment/stripe/stripe.d.ts +2 -2
  179. package/lib/payment/stripe/stripe.d.ts.map +1 -1
  180. package/lib/payment/stripe/stripe.js +14 -11
  181. package/lib/payment/stripe/stripe.js.map +1 -1
  182. package/lib/plugins-index.d.ts +48 -0
  183. package/lib/plugins-index.d.ts.map +1 -0
  184. package/lib/plugins-index.js +107 -0
  185. package/lib/plugins-index.js.map +1 -0
  186. package/lib/presets/all.d.ts +8 -6
  187. package/lib/presets/all.d.ts.map +1 -1
  188. package/lib/presets/all.js +3 -1
  189. package/lib/presets/all.js.map +1 -1
  190. package/lib/pricing/delivery-swiss-tax.d.ts.map +1 -1
  191. package/lib/pricing/delivery-swiss-tax.js +13 -20
  192. package/lib/pricing/delivery-swiss-tax.js.map +1 -1
  193. package/lib/pricing/order-discount.d.ts.map +1 -1
  194. package/lib/pricing/order-discount.js +5 -3
  195. package/lib/pricing/order-discount.js.map +1 -1
  196. package/lib/pricing/order-round.d.ts.map +1 -1
  197. package/lib/pricing/order-round.js +4 -3
  198. package/lib/pricing/order-round.js.map +1 -1
  199. package/lib/pricing/product-catalog-price-options.d.ts +2 -0
  200. package/lib/pricing/product-catalog-price-options.d.ts.map +1 -0
  201. package/lib/pricing/product-catalog-price-options.js +52 -0
  202. package/lib/pricing/product-catalog-price-options.js.map +1 -0
  203. package/lib/pricing/product-catalog-price.d.ts.map +1 -1
  204. package/lib/pricing/product-catalog-price.js +26 -2
  205. package/lib/pricing/product-catalog-price.js.map +1 -1
  206. package/lib/pricing/product-price-rateconversion.js +3 -3
  207. package/lib/pricing/product-price-rateconversion.js.map +1 -1
  208. package/lib/pricing/product-swiss-tax.d.ts +1 -5
  209. package/lib/pricing/product-swiss-tax.d.ts.map +1 -1
  210. package/lib/pricing/product-swiss-tax.js +26 -22
  211. package/lib/pricing/product-swiss-tax.js.map +1 -1
  212. package/lib/pricing/tax/ch.d.ts +7 -2
  213. package/lib/pricing/tax/ch.d.ts.map +1 -1
  214. package/lib/pricing/tax/ch.js +16 -1
  215. package/lib/pricing/tax/ch.js.map +1 -1
  216. package/lib/pricing/utils/isDeliveryAddressInCountry.d.ts +7 -0
  217. package/lib/pricing/utils/isDeliveryAddressInCountry.d.ts.map +1 -0
  218. package/lib/pricing/utils/isDeliveryAddressInCountry.js +13 -0
  219. package/lib/pricing/utils/isDeliveryAddressInCountry.js.map +1 -0
  220. package/lib/warehousing/eth-minter.js +23 -11
  221. package/lib/warehousing/eth-minter.js.map +1 -1
  222. package/lib/worker/budgetsms.d.ts +10 -0
  223. package/lib/worker/budgetsms.d.ts.map +1 -0
  224. package/lib/worker/budgetsms.js +217 -0
  225. package/lib/worker/budgetsms.js.map +1 -0
  226. package/lib/worker/bulk-import.d.ts +1 -1
  227. package/lib/worker/bulk-import.d.ts.map +1 -1
  228. package/lib/worker/bulk-import.js +11 -33
  229. package/lib/worker/bulk-import.js.map +1 -1
  230. package/lib/worker/bulkgate.d.ts +2 -0
  231. package/lib/worker/bulkgate.d.ts.map +1 -0
  232. package/lib/worker/bulkgate.js +104 -0
  233. package/lib/worker/bulkgate.js.map +1 -0
  234. package/lib/worker/email.d.ts.map +1 -1
  235. package/lib/worker/email.js +25 -7
  236. package/lib/worker/email.js.map +1 -1
  237. package/lib/worker/enrollment-order-generator.d.ts.map +1 -1
  238. package/lib/worker/enrollment-order-generator.js +7 -49
  239. package/lib/worker/enrollment-order-generator.js.map +1 -1
  240. package/lib/worker/error-notifications.js +1 -1
  241. package/lib/worker/error-notifications.js.map +1 -1
  242. package/lib/worker/export-token.d.ts.map +1 -1
  243. package/lib/worker/export-token.js +0 -1
  244. package/lib/worker/export-token.js.map +1 -1
  245. package/lib/worker/message.d.ts +1 -1
  246. package/lib/worker/message.d.ts.map +1 -1
  247. package/lib/worker/message.js +4 -2
  248. package/lib/worker/message.js.map +1 -1
  249. package/lib/worker/sms.js +17 -17
  250. package/lib/worker/sms.js.map +1 -1
  251. package/lib/worker/twilio.d.ts +2 -0
  252. package/lib/worker/twilio.d.ts.map +1 -0
  253. package/lib/worker/twilio.js +60 -0
  254. package/lib/worker/twilio.js.map +1 -0
  255. package/package.json +22 -33
  256. package/src/delivery/send-message.ts +1 -1
  257. package/src/enrollments/licensed.ts +15 -2
  258. package/src/files/gridfs/adapter.ts +12 -7
  259. package/src/files/gridfs/handler-express.ts +23 -25
  260. package/src/files/gridfs/handler-fastify.ts +53 -14
  261. package/src/files/minio/handler-express.ts +4 -7
  262. package/src/files/minio/handler-fastify.ts +2 -2
  263. package/src/files/minio/index.ts +16 -16
  264. package/src/filters/local-search.ts +1 -1
  265. package/src/filters/strict-equal.ts +0 -21
  266. package/src/payment/apple-iap/adapter.ts +12 -14
  267. package/src/payment/apple-iap/db/AppleTransactionsCollection.ts +1 -1
  268. package/src/payment/apple-iap/handler-express.ts +17 -11
  269. package/src/payment/apple-iap/handler-fastify.ts +19 -9
  270. package/src/payment/apple-iap/module.ts +9 -4
  271. package/src/payment/braintree.ts +4 -1
  272. package/src/payment/cryptopay/db/CryptopayTransactions.ts +1 -1
  273. package/src/payment/cryptopay/derive-btc-address.ts +1 -3
  274. package/src/payment/cryptopay/derive-eth-address.ts +5 -3
  275. package/src/payment/cryptopay/handle-webhook.ts +6 -6
  276. package/src/payment/cryptopay/handler-express.ts +4 -7
  277. package/src/payment/cryptopay/handler-fastify.ts +1 -1
  278. package/src/payment/cryptopay/module.ts +8 -5
  279. package/src/payment/cryptopay/plugin.ts +21 -4
  280. package/src/payment/datatrans-v2/handler-express.ts +17 -12
  281. package/src/payment/datatrans-v2/handler-fastify.ts +15 -6
  282. package/src/payment/datatrans-v2/index.ts +82 -50
  283. package/src/payment/paypal-checkout.ts +4 -2
  284. package/src/payment/payrexx/api/index.ts +1 -1
  285. package/src/payment/payrexx/api/makeFetcher.ts +2 -2
  286. package/src/payment/payrexx/handler-express.ts +24 -32
  287. package/src/payment/payrexx/handler-fastify.ts +33 -33
  288. package/src/payment/payrexx/index.ts +26 -16
  289. package/src/payment/postfinance-checkout/api-client.ts +13 -8
  290. package/src/payment/postfinance-checkout/handler-express.ts +8 -8
  291. package/src/payment/postfinance-checkout/handler-fastify.ts +7 -4
  292. package/src/payment/postfinance-checkout/index.ts +20 -8
  293. package/src/payment/saferpay/adapter.ts +14 -2
  294. package/src/payment/saferpay/handler-express.ts +14 -13
  295. package/src/payment/saferpay/handler-fastify.ts +14 -9
  296. package/src/payment/stripe/handler-express.ts +33 -33
  297. package/src/payment/stripe/handler-fastify.ts +40 -27
  298. package/src/payment/stripe/index.ts +18 -16
  299. package/src/payment/stripe/stripe.ts +17 -12
  300. package/src/presets/all.ts +3 -1
  301. package/src/pricing/delivery-swiss-tax.ts +16 -31
  302. package/src/pricing/order-discount.ts +6 -4
  303. package/src/pricing/order-round.ts +5 -4
  304. package/src/pricing/product-catalog-price-options.ts +67 -0
  305. package/src/pricing/product-catalog-price.ts +25 -2
  306. package/src/pricing/product-price-rateconversion.ts +3 -3
  307. package/src/pricing/product-swiss-tax.ts +39 -40
  308. package/src/pricing/tax/ch.ts +33 -7
  309. package/src/pricing/utils/isDeliveryAddressInCountry.test.ts +106 -0
  310. package/src/pricing/utils/isDeliveryAddressInCountry.ts +26 -0
  311. package/src/warehousing/eth-minter.ts +30 -11
  312. package/src/worker/budgetsms.ts +268 -0
  313. package/src/worker/bulk-import.ts +12 -53
  314. package/src/worker/bulkgate.ts +138 -0
  315. package/src/worker/email.ts +30 -8
  316. package/src/worker/enrollment-order-generator.ts +14 -72
  317. package/src/worker/error-notifications.ts +2 -2
  318. package/src/worker/export-token.ts +0 -1
  319. package/src/worker/message.ts +5 -3
  320. package/src/worker/{sms.ts → twilio.ts} +15 -26
  321. package/src/pricing/product-swiss-tax.test.ts +0 -139
@@ -12,27 +12,6 @@ const StrictQualFilter: IFilterAdapter = {
12
12
  return {
13
13
  ...FilterAdapter.actions(params),
14
14
 
15
- searchProducts: async ({ productIds }) => {
16
- // Search Products
17
- const { queryString } = params.searchQuery;
18
-
19
- if (!queryString && !productIds) {
20
- // If no query string is provided, return all product IDs
21
- return params.modules.products.findProductIds({});
22
- }
23
- return productIds;
24
- },
25
-
26
- searchAssortments: async ({ assortmentIds }) => {
27
- // Search Assortments
28
- const { queryString } = params.searchQuery;
29
- if (!queryString && !assortmentIds) {
30
- // If no query string is provided, return all assortment IDs
31
- return params.modules.assortments.findAssortmentIds({});
32
- }
33
- return assortmentIds;
34
- },
35
-
36
15
  transformProductSelector: async (lastSelector, options) => {
37
16
  const { key, value } = options || {};
38
17
 
@@ -3,7 +3,7 @@ import { createLogger } from '@unchainedshop/logger';
3
3
  import { AppleTransactionsModule } from './module.js';
4
4
  import { verifyReceipt } from './verify-receipt.js';
5
5
 
6
- const logger = createLogger('unchained:core-payment:apple-iap');
6
+ const logger = createLogger('unchained:apple-iap');
7
7
 
8
8
  const { APPLE_IAP_SHARED_SECRET } = process.env;
9
9
 
@@ -42,7 +42,7 @@ const AppleIAP: IPaymentAdapter = {
42
42
  },
43
43
 
44
44
  async sign() {
45
- throw new Error('Apple IAP does not support payment signing');
45
+ throw new Error('Payment signing not supported');
46
46
  },
47
47
 
48
48
  async validate() {
@@ -60,7 +60,7 @@ const AppleIAP: IPaymentAdapter = {
60
60
  const { status, latest_receipt_info: latestReceiptInfo } = response;
61
61
 
62
62
  if (status === 0) {
63
- logger.debug('Apple IAP Plugin: Receipt validated and updated for the user');
63
+ logger.debug('Receipt validated and updated for the user');
64
64
  const latestTransaction = latestReceiptInfo[latestReceiptInfo.length - 1];
65
65
  return {
66
66
  token: latestTransaction.web_order_line_item_id,
@@ -68,7 +68,7 @@ const AppleIAP: IPaymentAdapter = {
68
68
  };
69
69
  }
70
70
 
71
- logger.warn('Apple IAP Plugin: Receipt invalid', {
71
+ logger.warn('Receipt invalid', {
72
72
  status: response.status,
73
73
  });
74
74
  return null;
@@ -79,9 +79,11 @@ const AppleIAP: IPaymentAdapter = {
79
79
  const { transactionIdentifier } = meta || {};
80
80
 
81
81
  if (!transactionIdentifier) {
82
- throw new Error('Apple IAP Plugin: You have to set the transaction id on the order payment');
82
+ throw new Error('You have to set the transaction id on the order payment');
83
83
  }
84
84
 
85
+ if (!order) throw new Error('Order not found');
86
+
85
87
  const receiptResponse =
86
88
  receiptData &&
87
89
  (await verifyReceipt({
@@ -90,7 +92,7 @@ const AppleIAP: IPaymentAdapter = {
90
92
  }));
91
93
 
92
94
  if (receiptResponse && receiptResponse.status !== 0) {
93
- throw new Error('Apple IAP Plugin: Receipt invalid');
95
+ throw new Error('Receipt invalid');
94
96
  }
95
97
 
96
98
  const transactions =
@@ -99,9 +101,7 @@ const AppleIAP: IPaymentAdapter = {
99
101
  (transaction) => transaction?.transaction_id === transactionIdentifier,
100
102
  );
101
103
  if (!matchedTransaction) {
102
- throw new Error(
103
- `Apple IAP Plugin: Cannot match transaction with identifier ${transactionIdentifier}`,
104
- );
104
+ throw new Error(`Cannot match transaction with identifier ${transactionIdentifier}`);
105
105
  }
106
106
 
107
107
  const orderPositions = await modules.orders.positions.findOrderPositions({
@@ -117,7 +117,7 @@ const AppleIAP: IPaymentAdapter = {
117
117
  );
118
118
 
119
119
  if (items.length !== 1) {
120
- throw new Error('Apple IAP Plugin: You can only checkout 1 unique product at once');
120
+ throw new Error('You can only checkout 1 unique product at once');
121
121
  }
122
122
 
123
123
  const [[productId, quantity]] = items;
@@ -126,14 +126,12 @@ const AppleIAP: IPaymentAdapter = {
126
126
  parseInt(matchedTransaction.quantity, 10) === quantity &&
127
127
  matchedTransaction.product_id === productId;
128
128
 
129
- if (!isMatchesTransaction)
130
- throw new Error('Apple IAP Plugin: Product in order does not match transaction');
129
+ if (!isMatchesTransaction) throw new Error('Product in order does not match transaction');
131
130
 
132
131
  const alreadyProcessedTransaction =
133
132
  await modules.appleTransactions.findTransactionById(transactionIdentifier);
134
133
 
135
- if (alreadyProcessedTransaction)
136
- throw new Error('Apple IAP Plugin: Transaction already processed');
134
+ if (alreadyProcessedTransaction) throw new Error('Transaction already processed');
137
135
 
138
136
  // All good
139
137
  await modules.appleTransactions.createTransaction({
@@ -2,7 +2,7 @@ import { mongodb } from '@unchainedshop/mongodb';
2
2
  import { TimestampFields } from '@unchainedshop/mongodb';
3
3
 
4
4
  export type AppleTransaction = {
5
- _id?: string;
5
+ _id: string;
6
6
  matchedTransaction: any;
7
7
  orderId: string;
8
8
  } & TimestampFields;
@@ -3,7 +3,7 @@ import { EnrollmentStatus } from '@unchainedshop/core-enrollments';
3
3
  import { createLogger } from '@unchainedshop/logger';
4
4
  import { fixPeriods } from './fix-periods.js';
5
5
 
6
- const logger = createLogger('unchained:core-payment:apple-iap:handler');
6
+ const logger = createLogger('unchained:apple-iap:handler');
7
7
 
8
8
  const { APPLE_IAP_SHARED_SECRET } = process.env;
9
9
 
@@ -43,10 +43,16 @@ export const appleIAPHandler = async (req, res) => {
43
43
  receiptData: responseBody?.unified_receipt?.latest_receipt,
44
44
  },
45
45
  });
46
+
47
+ if (!order) throw new Error(`Order with id ${orderPayment.orderId} not found`);
48
+
46
49
  const orderId = order._id;
47
50
  const enrollment = await modules.enrollments.findEnrollment({
48
51
  orderId,
49
52
  });
53
+
54
+ if (!enrollment) throw new Error('Could not find any matching enrollment');
55
+
50
56
  await fixPeriods(
51
57
  {
52
58
  transactionId: latestTransaction.original_transaction_id,
@@ -72,9 +78,12 @@ export const appleIAPHandler = async (req, res) => {
72
78
  const originalOrder = await modules.orders.findOrder({
73
79
  orderId: originalOrderPayment.orderId,
74
80
  });
75
- const enrollment = await modules.enrollments.findEnrollment({
76
- orderId: originalOrder._id,
77
- });
81
+ const enrollment =
82
+ originalOrder &&
83
+ (await modules.enrollments.findEnrollment({
84
+ orderId: originalOrder._id,
85
+ }));
86
+ if (!enrollment?.payment) throw new Error('Could not find a valid enrollment payment method');
78
87
 
79
88
  await services.orders.registerPaymentCredentials(enrollment.payment.paymentProviderId, {
80
89
  transactionContext: {
@@ -88,7 +97,7 @@ export const appleIAPHandler = async (req, res) => {
88
97
  transactionId: latestTransaction.original_transaction_id,
89
98
  transactions,
90
99
  enrollmentId: enrollment._id,
91
- orderId: originalOrder._id,
100
+ orderId: originalOrder!._id,
92
101
  },
93
102
  resolvedContext,
94
103
  );
@@ -117,18 +126,15 @@ export const appleIAPHandler = async (req, res) => {
117
126
  logger.info(`Apple IAP Webhook: Updated enrollment from Apple`);
118
127
  }
119
128
 
120
- res.writeHead(200);
121
- res.end();
129
+ res.status(200).end();
122
130
  return;
123
131
  } catch (e) {
124
132
  logger.warn(`Apple IAP Webhook: ${e.message}`, e);
125
- res.writeHead(503);
126
- res.end(JSON.stringify({ name: e.name, code: e.code, message: e.message }));
133
+ res.status(503).send({ name: e.name, code: e.code, message: e.message });
127
134
  return;
128
135
  }
129
136
  }
130
- res.writeHead(404);
131
- res.end();
137
+ res.status(404).end();
132
138
  };
133
139
 
134
140
  export default appleIAPHandler;
@@ -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:apple-iap:handler');
8
+ const logger = createLogger('unchained:apple-iap:handler');
9
9
 
10
10
  const { APPLE_IAP_SHARED_SECRET } = process.env;
11
11
 
@@ -51,10 +51,16 @@ export const appleIAPHandler: RouteHandlerMethod = async (
51
51
  receiptData: responseBody?.unified_receipt?.latest_receipt,
52
52
  },
53
53
  });
54
+
55
+ if (!order) throw new Error(`Order with id ${orderPayment.orderId} not found`);
56
+
54
57
  const orderId = order._id;
55
58
  const enrollment = await modules.enrollments.findEnrollment({
56
59
  orderId,
57
60
  });
61
+
62
+ if (!enrollment) throw new Error('Enrollent not found');
63
+
58
64
  await fixPeriods(
59
65
  {
60
66
  transactionId: latestTransaction.original_transaction_id,
@@ -65,7 +71,7 @@ export const appleIAPHandler: RouteHandlerMethod = async (
65
71
  resolvedContext,
66
72
  );
67
73
 
68
- logger.info(`Apple IAP Webhook: Confirmed checkout for order ${order.orderNumber}`, {
74
+ logger.info(`Webhook confirmed checkout for order ${order.orderNumber}`, {
69
75
  orderId: order._id,
70
76
  });
71
77
  } else {
@@ -80,9 +86,13 @@ export const appleIAPHandler: RouteHandlerMethod = async (
80
86
  const originalOrder = await modules.orders.findOrder({
81
87
  orderId: originalOrderPayment.orderId,
82
88
  });
83
- const enrollment = await modules.enrollments.findEnrollment({
84
- orderId: originalOrder._id,
85
- });
89
+ const enrollment =
90
+ originalOrder &&
91
+ (await modules.enrollments.findEnrollment({
92
+ orderId: originalOrder._id,
93
+ }));
94
+
95
+ if (!enrollment?.payment) throw new Error('Could not find a valid enrollment payment method');
86
96
 
87
97
  await services.orders.registerPaymentCredentials(enrollment.payment.paymentProviderId, {
88
98
  transactionContext: {
@@ -96,13 +106,13 @@ export const appleIAPHandler: RouteHandlerMethod = async (
96
106
  transactionId: latestTransaction.original_transaction_id,
97
107
  transactions,
98
108
  enrollmentId: enrollment._id,
99
- orderId: originalOrder._id,
109
+ orderId: originalOrder!._id,
100
110
  },
101
111
  resolvedContext,
102
112
  );
103
113
 
104
114
  logger.info(
105
- `Apple IAP Webhook: Processed notification for ${latestTransaction.original_transaction_id} and type ${responseBody.notification_type}`,
115
+ `Webhook rocessed notification for ${latestTransaction.original_transaction_id} and type ${responseBody.notification_type}`,
106
116
  );
107
117
 
108
118
  if (responseBody.notification_type === AppleNotificationTypes.DID_RECOVER) {
@@ -128,9 +138,9 @@ export const appleIAPHandler: RouteHandlerMethod = async (
128
138
  reply.status(200);
129
139
  return reply.send();
130
140
  } catch (e) {
131
- logger.warn(`Apple IAP Webhook: ${e.message}`, e);
141
+ logger.error(e);
132
142
  reply.status(503);
133
- return reply.send({ name: e.name, code: e.code, message: e.message });
143
+ return reply.send({ success: false, name: e.name, code: e.code, message: e.message });
134
144
  }
135
145
  };
136
146
 
@@ -1,19 +1,24 @@
1
+ import { generateDbObjectId } from '@unchainedshop/mongodb';
1
2
  import { AppleTransaction, AppleTransactionsCollection } from './db/AppleTransactionsCollection.js';
2
3
 
3
4
  export const configureAppleTransactionsModule = async ({ db }) => {
4
5
  const AppleTransactions = await AppleTransactionsCollection(db);
5
6
 
6
7
  return {
7
- findTransactionById: async (transactionIdentifier: string): Promise<AppleTransaction> => {
8
+ findTransactionById: async (transactionIdentifier: string): Promise<AppleTransaction | null> => {
8
9
  return AppleTransactions.findOne({ _id: transactionIdentifier });
9
10
  },
10
11
 
11
- createTransaction: async (doc: AppleTransaction) => {
12
- await AppleTransactions.insertOne({
12
+ createTransaction: async (
13
+ doc: Omit<AppleTransaction, '_id' | 'created'> &
14
+ Pick<Partial<AppleTransaction>, '_id' | 'created'>,
15
+ ) => {
16
+ const { insertedId } = await AppleTransactions.insertOne({
17
+ _id: generateDbObjectId(),
13
18
  ...doc,
14
19
  created: new Date(),
15
20
  });
16
- return doc._id;
21
+ return insertedId;
17
22
  },
18
23
  };
19
24
  };
@@ -7,7 +7,7 @@ import {
7
7
  OrderPricingSheet,
8
8
  } from '@unchainedshop/core';
9
9
 
10
- const logger = createLogger('unchained:core-payment:braintree');
10
+ const logger = createLogger('unchained:braintree');
11
11
 
12
12
  const { BRAINTREE_SANDBOX_TOKEN, BRAINTREE_PRIVATE_KEY } = process.env;
13
13
 
@@ -108,6 +108,9 @@ const BraintreeDirect: IPaymentAdapter = {
108
108
 
109
109
  charge: async ({ paypalPaymentMethodNonce }) => {
110
110
  const { order } = context;
111
+
112
+ if (!order) throw new Error('Order is missing in payment context');
113
+
111
114
  if (!paypalPaymentMethodNonce)
112
115
  throw new Error('You have to provide paypalPaymentMethodNonce in paymentContext');
113
116
 
@@ -2,7 +2,7 @@ import { mongodb } from '@unchainedshop/mongodb';
2
2
  import { TimestampFields } from '@unchainedshop/mongodb';
3
3
 
4
4
  export type CryptopayTransaction = {
5
- _id?: string;
5
+ _id: string;
6
6
  blockHeight: number;
7
7
  mostRecentBlockHeight: number;
8
8
  amount: mongodb.Decimal128;
@@ -37,9 +37,7 @@ export default (xpub, index) => {
37
37
 
38
38
  const child = hardenedMaster.deriveChild(0).deriveChild(index);
39
39
 
40
- // We don't need this AFAIK:
41
- // const derivationPath = resolvePath(prefix);
42
- // const child = hardenedMaster.derive(`${derivationPath}/0/${btcDerivationNumber}`);
40
+ if (!child.publicKey) throw new Error('Cannot derive public key from extended public key');
43
41
 
44
42
  const network = resolveNetwork(prefix);
45
43
  const pubKey = p2wpkh(child.publicKey /* hex.decode( as string)*/, network);
@@ -1,6 +1,6 @@
1
1
  import { HDKey } from '@scure/bip32';
2
- import { keccak_256 } from '@noble/hashes/sha3';
3
- import { secp256k1 } from '@noble/curves/secp256k1';
2
+ import { keccak_256 } from '@noble/hashes/sha3.js';
3
+ import { secp256k1 } from '@noble/curves/secp256k1.js';
4
4
 
5
5
  function getChecksumAddress(address) {
6
6
  const chars = address.split('');
@@ -28,8 +28,10 @@ export default (xpub, index) => {
28
28
 
29
29
  const child = hardenedMaster.deriveChild(0).deriveChild(index);
30
30
 
31
+ if (!child.publicKey) throw new Error('Could not derive child key from extended public key');
32
+
31
33
  // ETH Address (secp256k1 + keccak_256 + checksum)
32
- const childSigningKey = secp256k1.ProjectivePoint.fromHex(child.publicKey).toRawBytes(false);
34
+ const childSigningKey = secp256k1.Point.fromBytes(child.publicKey).toBytes(false);
33
35
  const address = Buffer.from(keccak_256(childSigningKey.slice(1)))
34
36
  .toString('hex')
35
37
  .substring(24);
@@ -6,7 +6,7 @@ import { ProductPriceRate } from '@unchainedshop/core-products';
6
6
 
7
7
  const { CRYPTOPAY_SECRET, CRYPTOPAY_MAX_RATE_AGE = '360' } = process.env;
8
8
 
9
- const logger = createLogger('unchained:core-payment:cryptopay:handler');
9
+ const logger = createLogger('unchained:cryptopay:handler');
10
10
 
11
11
  export default async function handleWebhook(
12
12
  {
@@ -54,7 +54,7 @@ export default async function handleWebhook(
54
54
  blockHeight,
55
55
  amount,
56
56
  contract,
57
- decimals,
57
+ decimals: decimals ?? 0,
58
58
  currencyCode,
59
59
  });
60
60
 
@@ -68,10 +68,10 @@ export default async function handleWebhook(
68
68
  // Try to process order to next status
69
69
  // TODO: Not sure if it's correct to use processOrder here if status is PENDING!
70
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, {});
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
75
  } else {
76
76
  throw new Error('Already processed');
77
77
  }
@@ -1,24 +1,21 @@
1
1
  import { createLogger } from '@unchainedshop/logger';
2
2
  import handleWebhook from './handle-webhook.js';
3
3
 
4
- const logger = createLogger('unchained:core-payment:cryptopay:handler');
4
+ const logger = createLogger('unchained:cryptopay:handler');
5
5
 
6
6
  const cryptopayHandler = async (req, res) => {
7
7
  if (req.method === 'POST') {
8
8
  try {
9
9
  await handleWebhook(req.body, req.unchainedContext);
10
- res.writeHead(200);
11
- res.end(JSON.stringify({ success: true }));
10
+ res.status(200).send({ success: true });
12
11
  return;
13
12
  } catch (e) {
14
13
  logger.error(e);
15
- res.writeHead(500);
16
- res.end(JSON.stringify({ success: false, error: e.message }));
14
+ res.status(500).send({ success: false, error: e.message });
17
15
  return;
18
16
  }
19
17
  }
20
- res.writeHead(404);
21
- res.end(JSON.stringify({ success: false }));
18
+ res.status(404).send();
22
19
  };
23
20
 
24
21
  export default cryptopayHandler;
@@ -2,7 +2,7 @@ import { createLogger } from '@unchainedshop/logger';
2
2
  import handleWebhook from './handle-webhook.js';
3
3
  import { FastifyRequest, RouteHandlerMethod } from 'fastify';
4
4
 
5
- const logger = createLogger('unchained:core-payment:cryptopay:handler');
5
+ const logger = createLogger('unchained:cryptopay:handler');
6
6
 
7
7
  const cryptopayHandler: RouteHandlerMethod = async (
8
8
  req: FastifyRequest & {
@@ -4,7 +4,10 @@ import { CryptopayTransaction, CryptopayTransactionsCollection } from './db/Cryp
4
4
  const configureCryptopayModule = ({ db }) => {
5
5
  const CryptoTransactions = CryptopayTransactionsCollection(db);
6
6
 
7
- const getWalletAddress = async (address: string, contract?: string): Promise<CryptopayTransaction> => {
7
+ const getWalletAddress = async (
8
+ address: string,
9
+ contract?: string,
10
+ ): Promise<CryptopayTransaction | null> => {
8
11
  const addressId = [address, contract].filter(Boolean).join(':');
9
12
  return CryptoTransactions.findOne({ _id: addressId });
10
13
  };
@@ -35,7 +38,7 @@ const configureCryptopayModule = ({ db }) => {
35
38
  }): Promise<CryptopayTransaction> => {
36
39
  const addressId = [address, contract].filter(Boolean).join(':');
37
40
 
38
- return CryptoTransactions.findOneAndUpdate(
41
+ return (await CryptoTransactions.findOneAndUpdate(
39
42
  {
40
43
  _id: addressId,
41
44
  },
@@ -54,7 +57,7 @@ const configureCryptopayModule = ({ db }) => {
54
57
  },
55
58
  },
56
59
  { upsert: true, returnDocument: 'after' },
57
- );
60
+ )) as CryptopayTransaction;
58
61
  };
59
62
 
60
63
  const getNextDerivationNumber = async (currencyCode: string): Promise<number> => {
@@ -92,7 +95,7 @@ const configureCryptopayModule = ({ db }) => {
92
95
  decimals: number;
93
96
  }): Promise<CryptopayTransaction> => {
94
97
  const addressId = [address, contract].filter(Boolean).join(':');
95
- return CryptoTransactions.findOneAndUpdate(
98
+ return (await CryptoTransactions.findOneAndUpdate(
96
99
  {
97
100
  _id: addressId,
98
101
  },
@@ -111,7 +114,7 @@ const configureCryptopayModule = ({ db }) => {
111
114
  },
112
115
  },
113
116
  { upsert: true, returnDocument: 'after' },
114
- );
117
+ )) as CryptopayTransaction;
115
118
  };
116
119
 
117
120
  return {
@@ -12,7 +12,7 @@ import deriveBtcAddress from './derive-btc-address.js';
12
12
  import deriveEthAddress from './derive-eth-address.js';
13
13
  import denoteAmount from './denote-amount.js';
14
14
 
15
- const logger = createLogger('unchained:core-payment:cryptopay');
15
+ const logger = createLogger('unchained:cryptopay');
16
16
 
17
17
  const { CRYPTOPAY_SECRET, CRYPTOPAY_BTC_XPUB, CRYPTOPAY_ETH_XPUB } = process.env;
18
18
 
@@ -44,6 +44,9 @@ const Cryptopay: IPaymentAdapter = {
44
44
 
45
45
  const setConversionRates = async (currencyCode: string, existingAddresses: any[]) => {
46
46
  const originCurrencyObj = await modules.currencies.findCurrency({ isoCode: currencyCode });
47
+
48
+ if (!originCurrencyObj?.isActive) throw new Error('Origin currency is not supported');
49
+
47
50
  const updatedAddresses = await Promise.all(
48
51
  existingAddresses.map(async (addressData) => {
49
52
  const targetCurrencyObj = await modules.currencies.findCurrency({
@@ -113,6 +116,8 @@ const Cryptopay: IPaymentAdapter = {
113
116
  if (!orderPayment)
114
117
  throw new Error('Payment Credential Registration is not yet supported for Cryptopay');
115
118
 
119
+ if (!order) throw new Error('Order context is required for signing with Cryptopay');
120
+
116
121
  const existingAddresses = await modules.cryptopay.getWalletAddressesByOrderPaymentId(
117
122
  orderPayment._id,
118
123
  );
@@ -166,6 +171,11 @@ const Cryptopay: IPaymentAdapter = {
166
171
  charge: async () => {
167
172
  const { order, orderPayment } = context;
168
173
 
174
+ if (!orderPayment)
175
+ throw new Error('Order Payment context is required for charging with Cryptopay');
176
+
177
+ if (!order) throw new Error('Order context is required for charging with Cryptopay');
178
+
169
179
  const foundWalletsWithBalances = await modules.cryptopay.getWalletAddressesByOrderPaymentId(
170
180
  orderPayment._id,
171
181
  );
@@ -180,18 +190,25 @@ const Cryptopay: IPaymentAdapter = {
180
190
  });
181
191
  const totalAmount = BigInt(pricing?.total({ useNetPrice: false }).amount);
182
192
 
183
- if (walletForOrderPayment.currencyCode !== order.currencyCode) {
193
+ if (walletForOrderPayment && walletForOrderPayment.currencyCode !== order.currencyCode) {
184
194
  const baseCurrency = await modules.currencies.findCurrency({
185
195
  isoCode: walletForOrderPayment.currencyCode,
186
196
  });
197
+
198
+ if (!baseCurrency) throw new Error('Base currency not found');
199
+
187
200
  const quoteCurrency = await modules.currencies.findCurrency({ isoCode: order.currencyCode });
188
201
 
189
- const { /* min, */ max } = await modules.products.prices.rates.getRateRange(
202
+ if (!quoteCurrency) throw new Error('Quote currency not found');
203
+
204
+ const rate = await modules.products.prices.rates.getRateRange(
190
205
  baseCurrency,
191
206
  quoteCurrency,
192
207
  order.confirmed || new Date(),
193
208
  );
194
209
 
210
+ if (!rate) throw new Error('No conversion rate found');
211
+
195
212
  const convertedAmount = denoteAmount(
196
213
  walletForOrderPayment.amount.toString(),
197
214
  walletForOrderPayment.decimals,
@@ -199,7 +216,7 @@ const Cryptopay: IPaymentAdapter = {
199
216
 
200
217
  // Add a Promille 🍻
201
218
  // const minAmount = parseFloat(convertedAmount.toString()) * min * 0.999;
202
- const maxAmount = parseFloat(convertedAmount.toString()) * max * 1.001;
219
+ const maxAmount = parseFloat(convertedAmount.toString()) * rate.max * 1.001;
203
220
 
204
221
  if (maxAmount && maxAmount >= totalAmount) {
205
222
  // Enough sent
@@ -9,12 +9,19 @@ const {
9
9
  DATATRANS_SECURITY = Security.DYNAMIC_SIGN,
10
10
  } = process.env;
11
11
 
12
- const logger = createLogger('unchained:core-payment:datatrans:handler');
12
+ const logger = createLogger('unchained:datatrans:handler');
13
13
 
14
14
  export const datatransHandler = async (req, res) => {
15
15
  const resolvedContext = req.unchainedContext as Context;
16
16
  const { modules, services } = resolvedContext;
17
17
  const signature = req.headers['datatrans-signature'];
18
+
19
+ if (!DATATRANS_SIGN_KEY && !DATATRANS_SIGN2_KEY) {
20
+ logger.warn('No sign key configured');
21
+ res.status(404).end();
22
+ return;
23
+ }
24
+
18
25
  if (req.method === 'POST' && signature) {
19
26
  const [rawTimestamp, rawHash] = signature.split(',');
20
27
  const [, hash] = rawHash.split('=');
@@ -22,13 +29,12 @@ export const datatransHandler = async (req, res) => {
22
29
 
23
30
  const comparableSignature = await generateSignature({
24
31
  security: DATATRANS_SECURITY as any,
25
- signKey: DATATRANS_SIGN2_KEY || DATATRANS_SIGN_KEY,
32
+ signKey: DATATRANS_SIGN2_KEY! || DATATRANS_SIGN_KEY!,
26
33
  })(timestamp, req.body);
27
34
 
28
35
  if (hash !== comparableSignature) {
29
36
  logger.error(`hash mismatch: ${signature} / ${comparableSignature}`, req.body);
30
- res.writeHead(403);
31
- res.end('Hash mismatch');
37
+ res.status(403).send('Hash mismatch');
32
38
  return;
33
39
  }
34
40
 
@@ -52,8 +58,7 @@ export const datatransHandler = async (req, res) => {
52
58
  logger.info(`registered payment credentials for ${userId}`, {
53
59
  userId,
54
60
  });
55
- res.writeHead(200);
56
- res.end(JSON.stringify(paymentCredentials));
61
+ res.status(200).send(paymentCredentials);
57
62
  return;
58
63
  }
59
64
  if (transaction.type === 'payment') {
@@ -66,21 +71,21 @@ export const datatransHandler = async (req, res) => {
66
71
  const order = await services.orders.checkoutOrder(orderPayment.orderId, {
67
72
  paymentContext: { userId, transactionId: transaction.transactionId },
68
73
  });
69
- res.writeHead(200);
74
+
75
+ if (!order) throw new Error(`Order with id ${orderPayment.orderId} not found`);
76
+
70
77
  logger.info(`confirmed checkout for order ${order.orderNumber}`, {
71
78
  orderId: order._id,
72
79
  });
73
- res.end(JSON.stringify(order));
80
+ res.status(200).send(order);
74
81
  return;
75
82
  }
76
83
  } catch (e) {
77
84
  logger.error(`rejected to checkout with message`, e);
78
- res.writeHead(500);
79
- res.end(JSON.stringify({ name: e.name, code: e.code, message: e.message }));
85
+ res.status(500).send({ name: e.name, code: e.code, message: e.message });
80
86
  return;
81
87
  }
82
88
  }
83
89
  }
84
- res.writeHead(404);
85
- res.end();
90
+ res.status(404).end();
86
91
  };