@unchainedshop/plugins 4.6.0 → 4.6.2

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 (214) hide show
  1. package/lib/delivery/post/adapter.d.ts +2 -0
  2. package/lib/delivery/post/adapter.js +22 -0
  3. package/lib/delivery/post/index.d.ts +4 -0
  4. package/lib/delivery/post/index.js +10 -0
  5. package/lib/delivery/send-message/adapter.d.ts +2 -0
  6. package/lib/delivery/send-message/adapter.js +39 -0
  7. package/lib/delivery/send-message/index.d.ts +4 -0
  8. package/lib/delivery/send-message/index.js +10 -0
  9. package/lib/delivery/stores/adapter.d.ts +2 -0
  10. package/lib/delivery/stores/adapter.js +42 -0
  11. package/lib/delivery/stores/index.d.ts +4 -0
  12. package/lib/delivery/stores/index.js +10 -0
  13. package/lib/enrollments/licensed/adapter.d.ts +6 -0
  14. package/lib/enrollments/licensed/adapter.js +51 -0
  15. package/lib/enrollments/licensed/index.d.ts +4 -0
  16. package/lib/enrollments/licensed/index.js +10 -0
  17. package/lib/files/gridfs/api.d.ts +7 -0
  18. package/lib/files/gridfs/api.js +163 -0
  19. package/lib/files/minio/adapter.d.ts +4 -0
  20. package/lib/files/minio/adapter.js +160 -0
  21. package/lib/files/minio/api.d.ts +2 -0
  22. package/lib/files/minio/api.js +41 -0
  23. package/lib/files/temp-upload/api.d.ts +6 -0
  24. package/lib/files/temp-upload/api.js +49 -0
  25. package/lib/files/temp-upload/index.d.ts +3 -0
  26. package/lib/files/temp-upload/index.js +15 -0
  27. package/lib/filters/local-search/adapter.d.ts +2 -0
  28. package/lib/filters/local-search/adapter.js +56 -0
  29. package/lib/filters/local-search/index.d.ts +4 -0
  30. package/lib/filters/local-search/index.js +16 -0
  31. package/lib/filters/strict-equal/adapter.d.ts +2 -0
  32. package/lib/filters/strict-equal/adapter.js +23 -0
  33. package/lib/filters/strict-equal/index.d.ts +4 -0
  34. package/lib/filters/strict-equal/index.js +10 -0
  35. package/lib/payment/apple-iap/api.d.ts +2 -0
  36. package/lib/payment/apple-iap/api.js +119 -0
  37. package/lib/payment/cryptopay/adapter.d.ts +2 -0
  38. package/lib/payment/cryptopay/adapter.js +157 -0
  39. package/lib/payment/cryptopay/api.d.ts +2 -0
  40. package/lib/payment/cryptopay/api.js +23 -0
  41. package/lib/payment/datatrans-v2/adapter.d.ts +2 -0
  42. package/lib/payment/datatrans-v2/adapter.js +406 -0
  43. package/lib/payment/datatrans-v2/api.d.ts +2 -0
  44. package/lib/payment/datatrans-v2/api.js +108 -0
  45. package/lib/payment/invoice/adapter.d.ts +2 -0
  46. package/lib/payment/invoice/adapter.js +26 -0
  47. package/lib/payment/invoice/index.d.ts +4 -0
  48. package/lib/payment/invoice/index.js +10 -0
  49. package/lib/payment/invoice-prepaid/adapter.d.ts +2 -0
  50. package/lib/payment/invoice-prepaid/adapter.js +29 -0
  51. package/lib/payment/invoice-prepaid/index.d.ts +4 -0
  52. package/lib/payment/invoice-prepaid/index.js +10 -0
  53. package/lib/payment/payrexx/adapter.d.ts +2 -0
  54. package/lib/payment/payrexx/adapter.js +161 -0
  55. package/lib/payment/payrexx/api.d.ts +2 -0
  56. package/lib/payment/payrexx/api.js +97 -0
  57. package/lib/payment/postfinance-checkout/adapter.d.ts +2 -0
  58. package/lib/payment/postfinance-checkout/adapter.js +189 -0
  59. package/lib/payment/postfinance-checkout/webhook.d.ts +2 -0
  60. package/lib/payment/postfinance-checkout/webhook.js +48 -0
  61. package/lib/payment/saferpay/webhook.d.ts +2 -0
  62. package/lib/payment/saferpay/webhook.js +64 -0
  63. package/lib/payment/stripe/adapter.d.ts +2 -0
  64. package/lib/payment/stripe/adapter.js +121 -0
  65. package/lib/payment/stripe/api.d.ts +6 -0
  66. package/lib/payment/stripe/api.js +165 -0
  67. package/lib/payment/stripe/stripe.js +12 -10
  68. package/lib/presets/base.d.ts +1 -0
  69. package/lib/presets/base.js +1 -0
  70. package/lib/pricing/delivery-swiss-tax/adapter.d.ts +3 -0
  71. package/lib/pricing/delivery-swiss-tax/adapter.js +62 -0
  72. package/lib/pricing/delivery-swiss-tax/index.d.ts +4 -0
  73. package/lib/pricing/delivery-swiss-tax/index.js +10 -0
  74. package/lib/pricing/discount-100-off/adapter.d.ts +3 -0
  75. package/lib/pricing/discount-100-off/adapter.js +30 -0
  76. package/lib/pricing/discount-100-off/index.d.ts +4 -0
  77. package/lib/pricing/discount-100-off/index.js +10 -0
  78. package/lib/pricing/discount-half-price-manual/adapter.d.ts +3 -0
  79. package/lib/pricing/discount-half-price-manual/adapter.js +30 -0
  80. package/lib/pricing/discount-half-price-manual/index.d.ts +4 -0
  81. package/lib/pricing/discount-half-price-manual/index.js +10 -0
  82. package/lib/pricing/free-delivery/adapter.d.ts +3 -0
  83. package/lib/pricing/free-delivery/adapter.js +27 -0
  84. package/lib/pricing/free-delivery/index.d.ts +4 -0
  85. package/lib/pricing/free-delivery/index.js +10 -0
  86. package/lib/pricing/free-payment/adapter.d.ts +3 -0
  87. package/lib/pricing/free-payment/adapter.js +27 -0
  88. package/lib/pricing/free-payment/index.d.ts +4 -0
  89. package/lib/pricing/free-payment/index.js +10 -0
  90. package/lib/pricing/order-delivery/adapter.d.ts +3 -0
  91. package/lib/pricing/order-delivery/adapter.js +33 -0
  92. package/lib/pricing/order-delivery/index.d.ts +4 -0
  93. package/lib/pricing/order-delivery/index.js +10 -0
  94. package/lib/pricing/order-discount/adapter.d.ts +3 -0
  95. package/lib/pricing/order-discount/adapter.js +74 -0
  96. package/lib/pricing/order-discount/index.d.ts +4 -0
  97. package/lib/pricing/order-discount/index.js +10 -0
  98. package/lib/pricing/order-items/adapter.d.ts +3 -0
  99. package/lib/pricing/order-items/adapter.js +43 -0
  100. package/lib/pricing/order-items/index.d.ts +4 -0
  101. package/lib/pricing/order-items/index.js +10 -0
  102. package/lib/pricing/order-payment/adapter.d.ts +3 -0
  103. package/lib/pricing/order-payment/adapter.js +33 -0
  104. package/lib/pricing/order-payment/index.d.ts +4 -0
  105. package/lib/pricing/order-payment/index.js +10 -0
  106. package/lib/pricing/product-catalog-price/adapter.d.ts +3 -0
  107. package/lib/pricing/product-catalog-price/adapter.js +60 -0
  108. package/lib/pricing/product-catalog-price/index.d.ts +4 -0
  109. package/lib/pricing/product-catalog-price/index.js +10 -0
  110. package/lib/pricing/product-discount/adapter.d.ts +3 -0
  111. package/lib/pricing/product-discount/adapter.js +69 -0
  112. package/lib/pricing/product-discount/index.d.ts +4 -0
  113. package/lib/pricing/product-discount/index.js +10 -0
  114. package/lib/pricing/product-price-rateconversion/adapter.d.ts +3 -0
  115. package/lib/pricing/product-price-rateconversion/adapter.js +53 -0
  116. package/lib/pricing/product-price-rateconversion/index.d.ts +4 -0
  117. package/lib/pricing/product-price-rateconversion/index.js +10 -0
  118. package/lib/pricing/product-swiss-tax/adapter.d.ts +3 -0
  119. package/lib/pricing/product-swiss-tax/adapter.js +76 -0
  120. package/lib/pricing/product-swiss-tax/index.d.ts +4 -0
  121. package/lib/pricing/product-swiss-tax/index.js +10 -0
  122. package/lib/quotations/manual/adapter.d.ts +2 -0
  123. package/lib/quotations/manual/adapter.js +19 -0
  124. package/lib/quotations/manual/index.d.ts +4 -0
  125. package/lib/quotations/manual/index.js +10 -0
  126. package/lib/warehousing/erc-metadata/api.d.ts +12 -0
  127. package/lib/warehousing/erc-metadata/api.js +43 -0
  128. package/lib/warehousing/erc-metadata/index.d.ts +3 -0
  129. package/lib/warehousing/erc-metadata/index.js +15 -0
  130. package/lib/warehousing/eth-minter/adapter.d.ts +2 -0
  131. package/lib/warehousing/eth-minter/adapter.js +119 -0
  132. package/lib/warehousing/eth-minter/index.d.ts +4 -0
  133. package/lib/warehousing/eth-minter/index.js +10 -0
  134. package/lib/warehousing/store/adapter.d.ts +2 -0
  135. package/lib/warehousing/store/adapter.js +33 -0
  136. package/lib/warehousing/store/index.d.ts +4 -0
  137. package/lib/warehousing/store/index.js +10 -0
  138. package/lib/worker/budgetsms/adapter.d.ts +22 -0
  139. package/lib/worker/budgetsms/adapter.js +169 -0
  140. package/lib/worker/budgetsms/index.d.ts +4 -0
  141. package/lib/worker/budgetsms/index.js +10 -0
  142. package/lib/worker/bulk-export/adapter.d.ts +3 -0
  143. package/lib/worker/bulk-export/adapter.js +64 -0
  144. package/lib/worker/bulk-export/index.d.ts +4 -0
  145. package/lib/worker/bulk-export/index.js +10 -0
  146. package/lib/worker/bulk-export.d.ts +2 -0
  147. package/lib/worker/bulk-export.js +64 -0
  148. package/lib/worker/bulk-import/adapter.d.ts +3 -0
  149. package/lib/worker/bulk-import/adapter.js +65 -0
  150. package/lib/worker/bulk-import/api.d.ts +7 -0
  151. package/lib/worker/bulk-import/api.js +53 -0
  152. package/lib/worker/bulk-import/index.d.ts +4 -0
  153. package/lib/worker/bulk-import/index.js +18 -0
  154. package/lib/worker/bulkgate/adapter.d.ts +12 -0
  155. package/lib/worker/bulkgate/adapter.js +91 -0
  156. package/lib/worker/bulkgate/index.d.ts +4 -0
  157. package/lib/worker/bulkgate/index.js +10 -0
  158. package/lib/worker/email/adapter.d.ts +9 -0
  159. package/lib/worker/email/adapter.js +150 -0
  160. package/lib/worker/email/index.d.ts +4 -0
  161. package/lib/worker/email/index.js +10 -0
  162. package/lib/worker/enrollment-order-generator/adapter.d.ts +4 -0
  163. package/lib/worker/enrollment-order-generator/adapter.js +77 -0
  164. package/lib/worker/enrollment-order-generator/index.d.ts +4 -0
  165. package/lib/worker/enrollment-order-generator/index.js +13 -0
  166. package/lib/worker/error-notifications/adapter.d.ts +10 -0
  167. package/lib/worker/error-notifications/adapter.js +62 -0
  168. package/lib/worker/error-notifications/index.d.ts +4 -0
  169. package/lib/worker/error-notifications/index.js +13 -0
  170. package/lib/worker/export-token/adapter.d.ts +4 -0
  171. package/lib/worker/export-token/adapter.js +25 -0
  172. package/lib/worker/export-token/index.d.ts +4 -0
  173. package/lib/worker/export-token/index.js +13 -0
  174. package/lib/worker/external/adapter.d.ts +3 -0
  175. package/lib/worker/external/adapter.js +13 -0
  176. package/lib/worker/external/index.d.ts +4 -0
  177. package/lib/worker/external/index.js +10 -0
  178. package/lib/worker/heartbeat/adapter.d.ts +8 -0
  179. package/lib/worker/heartbeat/adapter.js +26 -0
  180. package/lib/worker/heartbeat/index.d.ts +4 -0
  181. package/lib/worker/heartbeat/index.js +10 -0
  182. package/lib/worker/http-request/adapter.d.ts +8 -0
  183. package/lib/worker/http-request/adapter.js +55 -0
  184. package/lib/worker/http-request/index.d.ts +4 -0
  185. package/lib/worker/http-request/index.js +10 -0
  186. package/lib/worker/message/adapter.d.ts +11 -0
  187. package/lib/worker/message/adapter.js +42 -0
  188. package/lib/worker/message/index.d.ts +4 -0
  189. package/lib/worker/message/index.js +10 -0
  190. package/lib/worker/push-notification/adapter.d.ts +9 -0
  191. package/lib/worker/push-notification/adapter.js +87 -0
  192. package/lib/worker/push-notification/index.d.ts +4 -0
  193. package/lib/worker/push-notification/index.js +10 -0
  194. package/lib/worker/twilio/adapter.d.ts +8 -0
  195. package/lib/worker/twilio/adapter.js +59 -0
  196. package/lib/worker/twilio/index.d.ts +4 -0
  197. package/lib/worker/twilio/index.js +10 -0
  198. package/lib/worker/update-coinbase-rates/adapter.d.ts +4 -0
  199. package/lib/worker/update-coinbase-rates/adapter.js +61 -0
  200. package/lib/worker/update-coinbase-rates/index.d.ts +4 -0
  201. package/lib/worker/update-coinbase-rates/index.js +13 -0
  202. package/lib/worker/update-ecb-rates/adapter.d.ts +4 -0
  203. package/lib/worker/update-ecb-rates/adapter.js +94 -0
  204. package/lib/worker/update-ecb-rates/index.d.ts +4 -0
  205. package/lib/worker/update-ecb-rates/index.js +13 -0
  206. package/lib/worker/update-token-ownership/adapter.d.ts +5 -0
  207. package/lib/worker/update-token-ownership/adapter.js +58 -0
  208. package/lib/worker/update-token-ownership/index.d.ts +4 -0
  209. package/lib/worker/update-token-ownership/index.js +13 -0
  210. package/lib/worker/zombie-killer/adapter.d.ts +13 -0
  211. package/lib/worker/zombie-killer/adapter.js +90 -0
  212. package/lib/worker/zombie-killer/index.d.ts +4 -0
  213. package/lib/worker/zombie-killer/index.js +10 -0
  214. package/package.json +1 -1
@@ -0,0 +1,161 @@
1
+ import { createLogger } from '@unchainedshop/logger';
2
+ import { mapOrderDataToGatewayObject, mapUserToGatewayObject } from "./payrexx.js";
3
+ import createPayrexxAPI, { GatewayObjectStatus } from "./api/index.js";
4
+ import { OrderPricingSheet, PaymentAdapter, PaymentError, } from '@unchainedshop/core';
5
+ const logger = createLogger('unchained:payrexx');
6
+ export const Payrexx = {
7
+ ...PaymentAdapter,
8
+ key: 'shop.unchained.payment.payrexx',
9
+ label: 'Payrexx',
10
+ version: '1.0.0',
11
+ typeSupported(type) {
12
+ return type === 'GENERIC';
13
+ },
14
+ actions: (config, context) => {
15
+ const { modules } = context;
16
+ const instance = config.find((c) => c.key === 'instance')?.value;
17
+ const adapterActions = {
18
+ ...PaymentAdapter.actions(config, context),
19
+ configurationError() {
20
+ if (!process.env.PAYREXX_SECRET) {
21
+ return PaymentError.INCOMPLETE_CONFIGURATION;
22
+ }
23
+ if (!instance) {
24
+ return PaymentError.INCOMPLETE_CONFIGURATION;
25
+ }
26
+ return null;
27
+ },
28
+ isActive: () => {
29
+ if (adapterActions.configurationError() === null)
30
+ return true;
31
+ return false;
32
+ },
33
+ isPayLaterAllowed() {
34
+ return false;
35
+ },
36
+ sign: async (transactionContext = {}) => {
37
+ const { orderPayment, userId, order } = context;
38
+ if (!order)
39
+ throw new Error('No order in context, can only sign with order context');
40
+ const api = createPayrexxAPI(instance, process.env.PAYREXX_SECRET);
41
+ if (orderPayment) {
42
+ const pricing = OrderPricingSheet({
43
+ calculation: order.calculation,
44
+ currencyCode: order.currencyCode,
45
+ });
46
+ const gatewayObject = mapOrderDataToGatewayObject({ order, orderPayment, pricing }, transactionContext);
47
+ const gateway = await api.createGateway(gatewayObject);
48
+ return JSON.stringify(gateway);
49
+ }
50
+ const user = userId && (await modules.users.findUserById(userId));
51
+ if (!user)
52
+ throw new Error('User not found, can only sign registrations with user context');
53
+ const emailAddress = modules.users.primaryEmail(user)?.address;
54
+ const gateway = await api.createGateway(mapUserToGatewayObject({
55
+ userId,
56
+ emailAddress,
57
+ currencyCode: order?.currencyCode,
58
+ }));
59
+ return JSON.stringify(gateway);
60
+ },
61
+ async register() {
62
+ throw new Error('Not implemented');
63
+ return null;
64
+ },
65
+ async confirm() {
66
+ const { orderPayment } = context;
67
+ if (!orderPayment?.transactionId) {
68
+ return false;
69
+ }
70
+ const { transactionId } = orderPayment;
71
+ const api = createPayrexxAPI(instance, process.env.PAYREXX_SECRET);
72
+ const gatewayObject = await api.getGateway(transactionId);
73
+ if (!gatewayObject) {
74
+ return false;
75
+ }
76
+ if (gatewayObject.status === GatewayObjectStatus.reserved) {
77
+ const allTransactions = gatewayObject.invoices?.flatMap((invoice) => invoice.transactions);
78
+ await Promise.all(allTransactions.map(async (transaction) => api.chargePreAuthorized(transaction.id, { referenceId: '__IGNORE_WEBHOOK__' })));
79
+ return true;
80
+ }
81
+ if (gatewayObject.status === GatewayObjectStatus.confirmed) {
82
+ return true;
83
+ }
84
+ return false;
85
+ },
86
+ async cancel() {
87
+ const { orderPayment } = context;
88
+ if (!orderPayment?.transactionId) {
89
+ return false;
90
+ }
91
+ const { transactionId } = orderPayment;
92
+ const api = createPayrexxAPI(instance, process.env.PAYREXX_SECRET);
93
+ const gatewayObject = await api.getGateway(transactionId);
94
+ if (!gatewayObject) {
95
+ return false;
96
+ }
97
+ if (gatewayObject.status === GatewayObjectStatus.reserved) {
98
+ const allTransactions = gatewayObject.invoices?.flatMap((invoice) => invoice.transactions);
99
+ await Promise.all(allTransactions.map(async (transaction) => api.deleteReservation(transaction.id)));
100
+ return true;
101
+ }
102
+ if (gatewayObject.status === GatewayObjectStatus.confirmed) {
103
+ return false;
104
+ }
105
+ return true;
106
+ },
107
+ charge: async ({ gatewayId, paymentCredentials }) => {
108
+ if (!gatewayId && !paymentCredentials) {
109
+ throw new Error('You have to provide gatewayId or paymentCredentials');
110
+ }
111
+ const { order, orderPayment } = context;
112
+ if (!order)
113
+ throw new Error('No order in context, can only charge with order context');
114
+ if (!orderPayment)
115
+ throw new Error('Order payment not found');
116
+ const api = createPayrexxAPI(instance, process.env.PAYREXX_SECRET);
117
+ const gatewayObject = await api.getGateway(gatewayId);
118
+ if (!gatewayObject) {
119
+ throw new Error('Could not load gateway from the Payrexx API');
120
+ }
121
+ const pricing = OrderPricingSheet({
122
+ calculation: order.calculation,
123
+ currencyCode: order.currencyCode,
124
+ });
125
+ const { currencyCode, amount } = pricing.total({ useNetPrice: false });
126
+ if (gatewayObject.status === 'reserved' ||
127
+ gatewayObject.status === 'confirmed') {
128
+ try {
129
+ if (gatewayObject.currency !== currencyCode.toUpperCase() ||
130
+ gatewayObject.amount !== Math.round(amount)) {
131
+ throw new Error('The price has changed since the intent has been created');
132
+ }
133
+ if (gatewayObject.referenceId !== orderPayment?._id) {
134
+ throw new Error('The order payment is different from the initiating intent');
135
+ }
136
+ logger.info(`Mark as charged, status is ${gatewayObject.status}`, {
137
+ orderPaymentId: gatewayObject.referenceId,
138
+ });
139
+ return {
140
+ transactionId: gatewayId,
141
+ gatewayObject,
142
+ };
143
+ }
144
+ catch (e) {
145
+ if (gatewayObject.status === GatewayObjectStatus.reserved) {
146
+ const allTransactions = gatewayObject.invoices?.flatMap((invoice) => invoice.transactions);
147
+ await Promise.all(allTransactions.map(async (transaction) => api.deleteReservation(transaction.id)));
148
+ }
149
+ throw e;
150
+ }
151
+ }
152
+ logger.info('Charge not possible', {
153
+ orderPaymentId: gatewayObject.referenceId,
154
+ status: gatewayObject.status,
155
+ });
156
+ throw new Error(`Gateway Status ${gatewayObject.status} does not allow checkout`);
157
+ },
158
+ };
159
+ return adapterActions;
160
+ },
161
+ };
@@ -0,0 +1,2 @@
1
+ import type { UnchainedCore } from '@unchainedshop/core';
2
+ export declare function payrexxWebhookHandler(request: Request, context: UnchainedCore): Promise<Response>;
@@ -0,0 +1,97 @@
1
+ import { createLogger } from '@unchainedshop/logger';
2
+ const logger = createLogger('unchained:payrexx');
3
+ export async function payrexxWebhookHandler(request, context) {
4
+ try {
5
+ const { modules, services } = context;
6
+ const body = await request.json();
7
+ const { transaction } = body;
8
+ if (!transaction) {
9
+ logger.info('Unhandled event type', {
10
+ type: Object.keys(body).join(','),
11
+ });
12
+ return new Response(JSON.stringify({
13
+ ignored: true,
14
+ message: `Unhandled event type: ${Object.keys(body).join(',')}. Supported type: transaction`,
15
+ }), {
16
+ status: 200,
17
+ headers: { 'Content-Type': 'application/json' },
18
+ });
19
+ }
20
+ if (transaction.referenceId === '__IGNORE_WEBHOOK__' || transaction.status === 'waiting') {
21
+ logger.info(`Unhandled transaction state: ${transaction.status}`);
22
+ return new Response(JSON.stringify({
23
+ ignored: true,
24
+ message: `Unhandled transaction state: ${transaction.status}`,
25
+ }), {
26
+ status: 200,
27
+ headers: { 'Content-Type': 'application/json' },
28
+ });
29
+ }
30
+ logger.info('Processing event', {
31
+ transactionId: transaction.id,
32
+ });
33
+ if (transaction.preAuthorizationId) {
34
+ const { referenceId: paymentProviderId, invoice } = transaction;
35
+ const userId = '';
36
+ logger.info(`Register credentials for: ${userId}`);
37
+ await services.orders.registerPaymentCredentials(paymentProviderId, {
38
+ userId,
39
+ transactionContext: { gatewayId: invoice.paymentRequestId },
40
+ });
41
+ logger.info('Registration successful', {
42
+ paymentProviderId,
43
+ userId,
44
+ });
45
+ return new Response(JSON.stringify({
46
+ message: 'registration successful',
47
+ paymentProviderId,
48
+ userId,
49
+ }), {
50
+ status: 200,
51
+ headers: { 'Content-Type': 'application/json' },
52
+ });
53
+ }
54
+ else {
55
+ const { referenceId: orderPaymentId, invoice } = transaction;
56
+ logger.info(`Checkout with orderPaymentId: ${orderPaymentId}`);
57
+ await modules.orders.payments.logEvent(orderPaymentId, {
58
+ transactionId: transaction.id,
59
+ });
60
+ const orderPayment = await modules.orders.payments.findOrderPayment({
61
+ orderPaymentId,
62
+ });
63
+ if (!orderPayment) {
64
+ throw new Error(`Order payment not found with orderPaymentId: ${orderPaymentId}`);
65
+ }
66
+ const order = await services.orders.checkoutOrder(orderPayment.orderId, {
67
+ paymentContext: {
68
+ gatewayId: invoice.paymentRequestId,
69
+ },
70
+ });
71
+ if (!order) {
72
+ throw new Error(`Order with id ${orderPayment.orderId} not found`);
73
+ }
74
+ logger.info('Checkout successful', {
75
+ orderPaymentId,
76
+ orderId: order._id,
77
+ });
78
+ return new Response(JSON.stringify({
79
+ message: 'checkout successful',
80
+ orderId: order._id,
81
+ }), {
82
+ status: 200,
83
+ headers: { 'Content-Type': 'application/json' },
84
+ });
85
+ }
86
+ }
87
+ catch (error) {
88
+ logger.error('Payrexx webhook error:', error);
89
+ return new Response(JSON.stringify({
90
+ message: error.message,
91
+ name: error.name,
92
+ }), {
93
+ status: 500,
94
+ headers: { 'Content-Type': 'application/json' },
95
+ });
96
+ }
97
+ }
@@ -0,0 +1,2 @@
1
+ import { type IPaymentAdapter } from '@unchainedshop/core';
2
+ export declare const PostfinanceCheckout: IPaymentAdapter;
@@ -0,0 +1,189 @@
1
+ import { createLogger } from '@unchainedshop/logger';
2
+ import { OrderPricingSheet, PaymentAdapter, PaymentError, } from '@unchainedshop/core';
3
+ import { confirmDeferredTransaction, createTransaction, getIframeJavascriptUrl, getLightboxJavascriptUrl, getPaymentPageUrl, getToken, getTransaction, refundTransaction, voidTransaction, } from "./api.js";
4
+ import { orderIsPaid } from "./utils.js";
5
+ import { CompletionModes, IntegrationModes } from "./types.js";
6
+ import { LineItemType, TokenizationMode, TransactionCompletionBehavior, TransactionState, CreationEntityState, } from "./api-types.js";
7
+ const { PFCHECKOUT_SPACE_ID, PFCHECKOUT_USER_ID, PFCHECKOUT_SECRET, PFCHECKOUT_SUCCESS_URL, PFCHECKOUT_FAILED_URL, } = process.env;
8
+ const logger = createLogger('unchained:postfinance-checkout');
9
+ const newError = ({ code, message }) => {
10
+ const error = new Error(message);
11
+ error.name = `POSTFINANCE_${code}`;
12
+ return error;
13
+ };
14
+ export const PostfinanceCheckout = {
15
+ ...PaymentAdapter,
16
+ key: 'shop.unchained.payment.postfinance-checkout',
17
+ label: 'Postfinance Checkout',
18
+ version: '1.0.0',
19
+ typeSupported(type) {
20
+ return type === 'GENERIC';
21
+ },
22
+ actions: (config, context) => {
23
+ const adapter = {
24
+ ...PaymentAdapter.actions(config, context),
25
+ getCompletionMode() {
26
+ return (config.find((item) => item.key === 'completionMode')?.value ||
27
+ CompletionModes.Deferred);
28
+ },
29
+ configurationError() {
30
+ if (!PFCHECKOUT_SPACE_ID ||
31
+ !PFCHECKOUT_USER_ID ||
32
+ !PFCHECKOUT_SECRET ||
33
+ !PFCHECKOUT_SUCCESS_URL ||
34
+ !PFCHECKOUT_FAILED_URL) {
35
+ return PaymentError.INCOMPLETE_CONFIGURATION;
36
+ }
37
+ return null;
38
+ },
39
+ isActive() {
40
+ if (adapter.configurationError() === null)
41
+ return true;
42
+ return false;
43
+ },
44
+ isPayLaterAllowed() {
45
+ return false;
46
+ },
47
+ async validate(credentials) {
48
+ if (!credentials.meta)
49
+ return false;
50
+ const { linkedSpaceId } = credentials.meta;
51
+ const tokenData = await getToken(linkedSpaceId, credentials.token);
52
+ return tokenData.state === CreationEntityState.ACTIVE;
53
+ },
54
+ sign: async (transactionContext = {}) => {
55
+ const { orderPayment, order } = context;
56
+ if (!order)
57
+ throw new Error('No order in context, can only sign with order context');
58
+ if (!orderPayment)
59
+ throw new Error('No order payment in context, can only sign with order payment context');
60
+ const { integrationMode = IntegrationModes.PaymentPage } = transactionContext;
61
+ const completionMode = adapter.getCompletionMode();
62
+ const pricing = OrderPricingSheet({
63
+ calculation: order.calculation,
64
+ currencyCode: order.currencyCode,
65
+ });
66
+ const totalAmount = pricing?.total({ useNetPrice: false }).amount;
67
+ const transaction = {
68
+ currency: order.currencyCode,
69
+ metaData: {
70
+ orderPaymentId: orderPayment._id,
71
+ },
72
+ successUrl: `${transactionContext?.successUrl || PFCHECKOUT_SUCCESS_URL}?order_id=${orderPayment.orderId}`,
73
+ failedUrl: `${transactionContext?.failedUrl || PFCHECKOUT_FAILED_URL}?order_id=${orderPayment.orderId}`,
74
+ customerId: order?.userId || context?.userId,
75
+ tokenizationMode: TokenizationMode.ALLOW_ONE_CLICK_PAYMENT,
76
+ };
77
+ if (completionMode === CompletionModes.Immediate) {
78
+ transaction.completionBehavior = TransactionCompletionBehavior.COMPLETE_IMMEDIATELY;
79
+ }
80
+ else if (completionMode === CompletionModes.Deferred) {
81
+ transaction.completionBehavior = TransactionCompletionBehavior.COMPLETE_DEFERRED;
82
+ }
83
+ if (totalAmount) {
84
+ const lineItemSum = {
85
+ name: `Bestellung ${orderPayment.orderId}`,
86
+ type: LineItemType.FEE,
87
+ quantity: 1,
88
+ uniqueId: orderPayment.orderId,
89
+ amountIncludingTax: totalAmount / 100,
90
+ };
91
+ transaction.lineItems = [lineItemSum];
92
+ }
93
+ const transactionId = await createTransaction(transaction);
94
+ if (!transactionId)
95
+ throw new Error('Could not create transaction');
96
+ let location = null;
97
+ if (integrationMode === IntegrationModes.PaymentPage) {
98
+ location = await getPaymentPageUrl(transactionId);
99
+ }
100
+ else if (integrationMode === IntegrationModes.Lightbox) {
101
+ location = await getLightboxJavascriptUrl(transactionId);
102
+ }
103
+ else if (integrationMode === IntegrationModes.iFrame) {
104
+ location = await getIframeJavascriptUrl(transactionId);
105
+ }
106
+ const res = {
107
+ transactionId,
108
+ location,
109
+ };
110
+ return JSON.stringify(res);
111
+ },
112
+ charge: async ({ transactionId, paymentCredentials, }) => {
113
+ if (paymentCredentials && !transactionId) {
114
+ throw new Error('Not implemented yet');
115
+ }
116
+ if (!transactionId)
117
+ return false;
118
+ const transaction = await getTransaction(transactionId);
119
+ if (!transaction) {
120
+ logger.error(`Transaction #${transactionId}: Transaction not found`);
121
+ throw newError({
122
+ code: `TRANSACTION_NOT_FOUND`,
123
+ message: 'Payment declined',
124
+ });
125
+ }
126
+ const { order, orderPayment } = context;
127
+ if (!orderPayment)
128
+ throw new Error('No order payment in context, cannot charge payment');
129
+ if (!order)
130
+ throw new Error('No order in context, can only sign with order context');
131
+ const isPaid = await orderIsPaid(order, transaction);
132
+ if (!isPaid) {
133
+ logger.error(`Transaction #${transactionId}: Invalid state / Amount incorrect`);
134
+ throw newError({
135
+ code: `STATE_${transaction.state?.toUpperCase()}`,
136
+ message: 'Invalid state / Amount incorrect',
137
+ });
138
+ }
139
+ if (transaction.metaData?.orderPaymentId !== orderPayment._id) {
140
+ logger.error(`Transaction #${transactionId}: Invalid state / Amount incorrect`);
141
+ throw newError({
142
+ code: `TRANSACTION_ALREADY_USED`,
143
+ message: 'Transaction already used in previous checkout',
144
+ });
145
+ }
146
+ const { id, ...tokenMeta } = transaction.token || {};
147
+ return {
148
+ transaction,
149
+ transactionId,
150
+ credentials: id && {
151
+ ...tokenMeta,
152
+ token: id.toString(),
153
+ },
154
+ };
155
+ },
156
+ cancel: async () => {
157
+ const { orderPayment, order } = context;
158
+ if (!order)
159
+ throw new Error('No order in context, can only cancel with order context');
160
+ if (!orderPayment?.transactionId) {
161
+ return false;
162
+ }
163
+ const { transactionId } = orderPayment;
164
+ const transaction = await getTransaction(transactionId);
165
+ const refund = transaction.state === TransactionState.FULFILL;
166
+ const pricing = OrderPricingSheet({
167
+ calculation: order.calculation,
168
+ currencyCode: order.currencyCode,
169
+ });
170
+ const totalAmount = pricing?.total({ useNetPrice: false }).amount;
171
+ return ((refund && refundTransaction(transactionId, order._id, totalAmount / 100)) ||
172
+ voidTransaction(transactionId));
173
+ },
174
+ confirm: async () => {
175
+ const { orderPayment } = context;
176
+ if (!orderPayment?.transactionId) {
177
+ return false;
178
+ }
179
+ const { transactionId } = orderPayment;
180
+ const transaction = await getTransaction(transactionId);
181
+ if (transaction.state === TransactionState.AUTHORIZED) {
182
+ return confirmDeferredTransaction(transactionId);
183
+ }
184
+ return false;
185
+ },
186
+ };
187
+ return adapter;
188
+ },
189
+ };
@@ -0,0 +1,2 @@
1
+ import type { UnchainedCore } from '@unchainedshop/core';
2
+ export declare function postfinanceCheckoutWebhookHandler(request: Request, context: UnchainedCore): Promise<Response>;
@@ -0,0 +1,48 @@
1
+ import { createLogger } from '@unchainedshop/logger';
2
+ import { getTransaction, getTransactionCompletion } from "./api.js";
3
+ const logger = createLogger('unchained:postfinance-checkout');
4
+ export async function postfinanceCheckoutWebhookHandler(request, context) {
5
+ try {
6
+ const { services, modules } = context;
7
+ const data = (await request.json());
8
+ if (data.listenerEntityTechnicalName === 'TransactionCompletion') {
9
+ const transactionCompletion = await getTransactionCompletion(data.entityId);
10
+ const transaction = await getTransaction(transactionCompletion ? transactionCompletion.linkedTransaction : data.entityId);
11
+ const { orderPaymentId } = transaction.metaData;
12
+ const orderPayment = await modules.orders.payments.findOrderPayment({
13
+ orderPaymentId,
14
+ });
15
+ if (!orderPayment) {
16
+ throw new Error('Order Payment not found');
17
+ }
18
+ const order = await services.orders.checkoutOrder(orderPayment.orderId, {
19
+ paymentContext: {
20
+ transactionId: transactionCompletion.linkedTransaction,
21
+ },
22
+ });
23
+ if (!order) {
24
+ throw new Error(`Order with id ${orderPayment.orderId} not found`);
25
+ }
26
+ logger.info(`Transaction ${transactionCompletion.linkedTransaction} marked order payment ID ${transaction.metaData?.orderPaymentId} as paid`);
27
+ return new Response(JSON.stringify({ orderNumber: order.orderNumber }), {
28
+ status: 200,
29
+ headers: { 'Content-Type': 'application/json' },
30
+ });
31
+ }
32
+ else {
33
+ logger.error(`Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`);
34
+ return new Response(null, { status: 404 });
35
+ }
36
+ }
37
+ catch (error) {
38
+ logger.error('Postfinance Checkout webhook error:', error);
39
+ return new Response(JSON.stringify({
40
+ name: error.name,
41
+ code: error.code,
42
+ message: error.message,
43
+ }), {
44
+ status: 500,
45
+ headers: { 'Content-Type': 'application/json' },
46
+ });
47
+ }
48
+ }
@@ -0,0 +1,2 @@
1
+ import type { UnchainedCore } from '@unchainedshop/core';
2
+ export declare function saferpayWebhookHandler(request: Request, context: UnchainedCore): Promise<Response>;
@@ -0,0 +1,64 @@
1
+ import { createLogger } from '@unchainedshop/logger';
2
+ import { timingSafeStringEqual } from '@unchainedshop/utils';
3
+ import { buildSignature } from "./buildSignature.js";
4
+ const logger = createLogger('unchained:saferpay');
5
+ export async function saferpayWebhookHandler(request, context) {
6
+ try {
7
+ const { modules, services } = context;
8
+ const url = new URL(request.url);
9
+ const orderPaymentId = url.searchParams.get('orderPaymentId');
10
+ const signature = url.searchParams.get('signature');
11
+ const transactionId = url.searchParams.get('transactionId');
12
+ const isValidRequest = typeof orderPaymentId === 'string' &&
13
+ typeof signature === 'string' &&
14
+ typeof transactionId === 'string' &&
15
+ orderPaymentId &&
16
+ transactionId &&
17
+ signature;
18
+ if (!isValidRequest) {
19
+ logger.warn('Missing required query parameters');
20
+ return new Response(null, { status: 404 });
21
+ }
22
+ logger.info(`Checkout with orderPaymentId: ${orderPaymentId}`);
23
+ const orderPayment = await modules.orders.payments.findOrderPayment({
24
+ orderPaymentId,
25
+ });
26
+ if (!orderPayment) {
27
+ throw new Error(`Order payment not found with orderPaymentId: ${orderPaymentId}`);
28
+ }
29
+ const correctSignature = await buildSignature(transactionId, orderPaymentId);
30
+ if (!(await timingSafeStringEqual(correctSignature, signature))) {
31
+ throw new Error('Invalid signature');
32
+ }
33
+ const order = await services.orders.checkoutOrder(orderPayment.orderId, {
34
+ paymentContext: {
35
+ transactionId,
36
+ },
37
+ });
38
+ if (!order) {
39
+ throw new Error(`Order with id ${orderPayment.orderId} not found`);
40
+ }
41
+ logger.info('Checkout successful', {
42
+ orderPaymentId,
43
+ orderId: order._id,
44
+ });
45
+ return new Response(JSON.stringify({
46
+ message: 'checkout successful',
47
+ orderPaymentId,
48
+ orderId: order._id,
49
+ }), {
50
+ status: 200,
51
+ headers: { 'Content-Type': 'application/json' },
52
+ });
53
+ }
54
+ catch (error) {
55
+ logger.error('Saferpay webhook error:', error);
56
+ return new Response(JSON.stringify({
57
+ message: error.message,
58
+ name: error.name,
59
+ }), {
60
+ status: 500,
61
+ headers: { 'Content-Type': 'application/json' },
62
+ });
63
+ }
64
+ }
@@ -0,0 +1,2 @@
1
+ import { type IPaymentAdapter } from '@unchainedshop/core';
2
+ export declare const Stripe: IPaymentAdapter;