@unchainedshop/plugins 2.8.8 → 2.8.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,17 +22,18 @@ export const payrexxHandler = async (request, response) => {
22
22
  );
23
23
  return;
24
24
  }
25
- if (transaction.status === 'confirmed') {
25
+ if (
26
+ (transaction.status === 'confirmed' && transaction.referenceId === '__IGNORE_WEBHOOK__') ||
27
+ transaction.status === 'waiting'
28
+ ) {
26
29
  // Ignore confirmed transactions, because those hooks are generated through calling the confirm()
27
30
  // method in the payment adapter and could lead to double bookings.
28
- logger.verbose(`unhandled event state`, {
29
- type: Object.keys(request.body).join(','),
30
- });
31
+ logger.verbose(`unhandled transaction state: ${transaction.status}`);
31
32
  response.writeHead(200);
32
33
  response.end(
33
34
  JSON.stringify({
34
35
  ignored: true,
35
- message: `Unhandled event state: transaction.confirmed`,
36
+ message: `Unhandled transaction state: ${transaction.status}`,
36
37
  }),
37
38
  );
38
39
  return;
@@ -42,37 +43,62 @@ export const payrexxHandler = async (request, response) => {
42
43
  transactionId: transaction.id,
43
44
  });
44
45
  try {
45
- const { referenceId: orderPaymentId, invoice } = transaction;
46
- logger.verbose(`checkout with orderPaymentId: ${orderPaymentId}`);
47
- await modules.orders.payments.logEvent(orderPaymentId, {
48
- transactionId: transaction.id,
49
- });
50
- const orderPayment = await modules.orders.payments.findOrderPayment({
51
- orderPaymentId,
52
- });
53
- if (!orderPayment) {
54
- throw new Error(`order payment not found with orderPaymentId: ${orderPaymentId}`);
55
- }
56
- const order = await modules.orders.checkout(
57
- orderPayment.orderId,
58
- {
59
- paymentContext: {
60
- gatewayId: invoice.paymentRequestId,
46
+ if (transaction.preAuthorizationId) {
47
+ // Pre-Authorization Flow, referenceId is a userId
48
+ const { referenceId: paymentProviderId, invoice } = transaction;
49
+ const userId = '';
50
+ logger.verbose(`register credentials for: ${userId}`);
51
+ await modules.payment.registerCredentials(
52
+ paymentProviderId,
53
+ { userId, transactionContext: { gatewayId: invoice.paymentRequestId } },
54
+ resolvedContext,
55
+ );
56
+ logger.info(`registration successful`, {
57
+ paymentProviderId,
58
+ userId,
59
+ });
60
+ response.writeHead(200);
61
+ response.end(
62
+ JSON.stringify({
63
+ message: 'registration successful',
64
+ paymentProviderId,
65
+ userId,
66
+ }),
67
+ );
68
+ } else {
69
+ const { referenceId: orderPaymentId, invoice } = transaction;
70
+ logger.verbose(`checkout with orderPaymentId: ${orderPaymentId}`);
71
+ await modules.orders.payments.logEvent(orderPaymentId, {
72
+ transactionId: transaction.id,
73
+ });
74
+ const orderPayment = await modules.orders.payments.findOrderPayment({
75
+ orderPaymentId,
76
+ });
77
+ if (!orderPayment) {
78
+ throw new Error(`order payment not found with orderPaymentId: ${orderPaymentId}`);
79
+ }
80
+
81
+ const order = await modules.orders.checkout(
82
+ orderPayment.orderId,
83
+ {
84
+ paymentContext: {
85
+ gatewayId: invoice.paymentRequestId,
86
+ },
61
87
  },
62
- },
63
- resolvedContext,
64
- );
65
- logger.info(`checkout successful`, {
66
- orderPaymentId,
67
- orderId: order._id,
68
- });
69
- response.writeHead(200);
70
- response.end(
71
- JSON.stringify({
72
- message: 'checkout successful',
88
+ resolvedContext,
89
+ );
90
+ logger.info(`checkout successful`, {
91
+ orderPaymentId,
73
92
  orderId: order._id,
74
- }),
75
- );
93
+ });
94
+ response.writeHead(200);
95
+ response.end(
96
+ JSON.stringify({
97
+ message: 'checkout successful',
98
+ orderId: order._id,
99
+ }),
100
+ );
101
+ }
76
102
  } catch (error) {
77
103
  logger.error(error, {
78
104
  transactionId: transaction.id,
@@ -1,12 +1,19 @@
1
- export const mapOrderDataToGatewayObject = ({ order, orderPayment, pricing }, options = {}) => {
1
+ const getRedirects = () => {
2
2
  const {
3
- EMAIL_WEBSITE_NAME = 'Unchained',
4
3
  EMAIL_WEBSITE_URL,
5
4
  DATATRANS_SUCCESS_PATH = '/payrexx/success',
6
5
  DATATRANS_ERROR_PATH = '/payrexx/error',
7
6
  DATATRANS_CANCEL_PATH = '/payrexx/cancel',
8
7
  } = process.env;
9
8
 
9
+ return {
10
+ successRedirectUrl: `${EMAIL_WEBSITE_URL}${DATATRANS_SUCCESS_PATH}`,
11
+ failedRedirectUrl: `${EMAIL_WEBSITE_URL}${DATATRANS_ERROR_PATH}`,
12
+ cancelRedirectUrl: `${EMAIL_WEBSITE_URL}${DATATRANS_CANCEL_PATH}`,
13
+ };
14
+ };
15
+
16
+ export const mapOrderDataToGatewayObject = ({ order, orderPayment, pricing }, options = {}) => {
10
17
  const { currency, amount } = pricing.total({ useNetPrice: false });
11
18
 
12
19
  const customerData = {};
@@ -23,16 +30,36 @@ export const mapOrderDataToGatewayObject = ({ order, orderPayment, pricing }, op
23
30
  const gatewayObject = {
24
31
  amount: Math.round(amount),
25
32
  currency: currency.toUpperCase(),
26
- purpose: encodeURIComponent(`${EMAIL_WEBSITE_NAME} #${order._id}`),
27
- // preAuthorization: true -> tokenization!
33
+ purpose: encodeURIComponent(`${process.env.EMAIL_WEBSITE_NAME || 'Unchained'} #${order._id}`),
28
34
  reservation: true,
29
35
  skipResultPage: true,
30
- successRedirectUrl: `${EMAIL_WEBSITE_URL}${DATATRANS_SUCCESS_PATH}`,
31
- failedRedirectUrl: `${EMAIL_WEBSITE_URL}${DATATRANS_ERROR_PATH}`,
32
- cancelRedirectUrl: `${EMAIL_WEBSITE_URL}${DATATRANS_CANCEL_PATH}`,
36
+ ...getRedirects(),
33
37
  referenceId: orderPayment._id,
34
38
  ...customerData,
35
39
  ...options,
36
40
  };
37
41
  return gatewayObject;
38
42
  };
43
+
44
+ export const mapUserToGatewayObject = ({ userId, emailAddress, currencyCode }, options = {}) => {
45
+ const customerData = {};
46
+
47
+ if (emailAddress) {
48
+ customerData['fields[email][value]'] = emailAddress;
49
+ }
50
+
51
+ customerData['fields[custom_field_1][name][0]'] = 'userId';
52
+ customerData['fields[custom_field_1][value]'] = userId;
53
+
54
+ const gatewayObject = {
55
+ amount: Math.round(0),
56
+ currency: currencyCode,
57
+ purpose: encodeURIComponent(`${process.env.EMAIL_WEBSITE_NAME || 'Unchained'} ${userId}`),
58
+ preAuthorization: true,
59
+ skipResultPage: true,
60
+ ...getRedirects(),
61
+ referenceId: userId,
62
+ ...options,
63
+ };
64
+ return gatewayObject;
65
+ };