@unchainedshop/plugins 2.11.2 → 2.11.4

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.
@@ -13,6 +13,8 @@ const Stripe = {
13
13
  },
14
14
  actions: (params) => {
15
15
  const { modules } = params.context;
16
+ const descriptorPrefix = params.config.find(({ key }) => key === 'descriptorPrefix')?.value;
17
+ console.log({ descriptorPrefix });
16
18
  const adapterActions = {
17
19
  ...PaymentAdapter.actions(params),
18
20
  configurationError() {
@@ -54,14 +56,14 @@ const Stripe = {
54
56
  const { orderPayment, order, paymentProviderId } = params.paymentContext;
55
57
  if (orderPayment) {
56
58
  const pricing = await modules.orders.pricingSheet(order);
57
- const paymentIntent = await createOrderPaymentIntent({ order, orderPayment, pricing }, transactionContext);
59
+ const paymentIntent = await createOrderPaymentIntent({ order, orderPayment, pricing, descriptorPrefix }, transactionContext);
58
60
  return paymentIntent.client_secret;
59
61
  }
60
62
  const userId = order?.userId || params.paymentContext?.userId;
61
63
  const user = await modules.users.findUserById(userId);
62
64
  const email = modules.users.primaryEmail(user)?.address;
63
65
  const name = user.profile.displayName || user.username || email;
64
- const paymentIntent = await createRegistrationIntent({ userId, name, email, paymentProviderId }, transactionContext);
66
+ const paymentIntent = await createRegistrationIntent({ userId, name, email, paymentProviderId, descriptorPrefix }, transactionContext);
65
67
  return paymentIntent.client_secret;
66
68
  },
67
69
  charge: async ({ paymentIntentId, paymentCredentials }) => {
@@ -75,7 +77,7 @@ const Stripe = {
75
77
  const pricing = await modules.orders.pricingSheet(order);
76
78
  const paymentIntentObject = paymentIntentId
77
79
  ? await stripe.paymentIntents.retrieve(paymentIntentId)
78
- : await createOrderPaymentIntent({ orderPayment, order, pricing }, {
80
+ : await createOrderPaymentIntent({ orderPayment, order, pricing, descriptorPrefix }, {
79
81
  customer: paymentCredentials.meta?.customer,
80
82
  confirm: true,
81
83
  payment_method: paymentCredentials.token,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","s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C,KAAK,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAC;gBAEnE,IACE,mBAAmB,CAAC,QAAQ,KAAK,QAAQ,CAAC,WAAW,EAAE;oBACvD,mBAAmB,CAAC,MAAM,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EACjD,CAAC;oBACD,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;gBAC7E,CAAC;gBACD,IAAI,mBAAmB,CAAC,QAAQ,EAAE,cAAc,KAAK,YAAY,EAAE,GAAG,EAAE,CAAC;oBACvE,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;gBAC/E,CAAC;gBAED,IAAI,mBAAmB,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;oBAC/C,OAAO,mBAAmB,CAAC;gBAC7B,CAAC;gBAED,MAAM,CAAC,OAAO,CAAC,yDAAyD,EAAE;oBACxE,cAAc,EAAE,mBAAmB,CAAC,EAAE;iBACvC,CAAC,CAAC;gBAEH,OAAO,KAAK,CAAC;YACf,CAAC;SACF,CAAC;QAEF,OAAO,cAAc,CAAC;IACxB,CAAC;CACF,CAAC;AAEF,eAAe,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC"}
@@ -1,14 +1,24 @@
1
+ import { Order } from '@unchainedshop/types/orders.js';
2
+ import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
3
+ import { IOrderPricingSheet } from '@unchainedshop/types/orders.pricing.js';
1
4
  import Stripe from 'stripe';
2
5
  declare const stripe: Stripe;
3
6
  export default stripe;
4
- export declare const createRegistrationIntent: ({ userId, name, email, paymentProviderId }: {
7
+ export declare const upsertCustomer: ({ userId, name, email }: {
5
8
  userId: any;
6
9
  name: any;
7
10
  email: any;
8
- paymentProviderId: any;
9
- }, options?: {}) => Promise<Stripe.Response<Stripe.SetupIntent>>;
10
- export declare const createOrderPaymentIntent: ({ order, orderPayment, pricing }: {
11
- order: any;
12
- orderPayment: any;
13
- pricing: any;
14
- }, options?: {}) => Promise<Stripe.Response<Stripe.PaymentIntent>>;
11
+ }) => Promise<string>;
12
+ export declare const createRegistrationIntent: ({ userId, name, email, paymentProviderId, descriptorPrefix, }: {
13
+ userId: string;
14
+ name: string;
15
+ email: string;
16
+ paymentProviderId: string;
17
+ descriptorPrefix?: string;
18
+ }, options?: Record<string, any>) => Promise<Stripe.Response<Stripe.SetupIntent>>;
19
+ export declare const createOrderPaymentIntent: ({ order, orderPayment, pricing, descriptorPrefix, }: {
20
+ order: Order;
21
+ orderPayment: OrderPayment;
22
+ pricing: IOrderPricingSheet;
23
+ descriptorPrefix?: string;
24
+ }, options?: Record<string, any>) => Promise<Stripe.Response<Stripe.PaymentIntent>>;
@@ -1,44 +1,80 @@
1
- import { slugify } from '@unchainedshop/utils';
2
1
  import Stripe from 'stripe';
3
2
  const { STRIPE_SECRET, STRIPE_WEBHOOK_ENVIRONMENT, EMAIL_WEBSITE_NAME } = process.env;
4
3
  const stripe = new Stripe(STRIPE_SECRET, {
5
4
  apiVersion: '2024-04-10',
6
5
  });
6
+ const environment = STRIPE_WEBHOOK_ENVIRONMENT ?? null;
7
7
  export default stripe;
8
- export const createRegistrationIntent = async ({ userId, name, email, paymentProviderId }, options = {}) => {
9
- const customer = await stripe.customers.create({
10
- metadata: {
11
- userId,
12
- environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
13
- },
14
- name,
15
- email,
16
- });
8
+ export const upsertCustomer = async ({ userId, name, email }) => {
9
+ try {
10
+ const { data } = await stripe.customers.search({ query: `metadata["userId"]:"${userId}"` });
11
+ const existingCustomer = data[0];
12
+ if (existingCustomer.name !== name ||
13
+ existingCustomer.email !== email ||
14
+ existingCustomer.metadata.environment !== environment) {
15
+ const updatedCustomer = await stripe.customers.update(existingCustomer.id, {
16
+ metadata: {
17
+ userId,
18
+ environment,
19
+ },
20
+ name,
21
+ email,
22
+ });
23
+ return updatedCustomer.id;
24
+ }
25
+ return existingCustomer.id;
26
+ }
27
+ catch (e) {
28
+ const customer = await stripe.customers.create({
29
+ metadata: {
30
+ userId,
31
+ environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
32
+ },
33
+ name,
34
+ email,
35
+ });
36
+ return customer.id;
37
+ }
38
+ };
39
+ export const createRegistrationIntent = async ({ userId, name, email, paymentProviderId, descriptorPrefix, }, options = {}) => {
40
+ const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${userId}${email ? ` (#${email})` : ''}`;
41
+ const customer = options?.customer || (await upsertCustomer({ userId, name, email }));
17
42
  const setupIntent = await stripe.setupIntents.create({
18
- customer: customer.id,
43
+ description,
44
+ customer,
19
45
  metadata: {
20
46
  userId,
21
47
  paymentProviderId,
22
- environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
48
+ environment,
23
49
  },
24
50
  usage: 'off_session',
25
51
  ...options,
26
52
  });
27
53
  return setupIntent;
28
54
  };
29
- export const createOrderPaymentIntent = async ({ order, orderPayment, pricing }, options = {}) => {
30
- const reference = EMAIL_WEBSITE_NAME || order._id;
55
+ export const createOrderPaymentIntent = async ({ order, orderPayment, pricing, descriptorPrefix, }, options = {}) => {
56
+ const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${order._id}${order.orderNumber ? ` (#${order.orderNumber})` : ''}`;
57
+ const name = [order.billingAddress?.firstName, order.billingAddress?.lastName].filter(Boolean).join(' ') ||
58
+ order.billingAddress?.company;
59
+ const customer = options?.customer ||
60
+ (await upsertCustomer({
61
+ userId: order.userId,
62
+ name,
63
+ email: order.contact?.emailAddress,
64
+ }));
31
65
  const { currency, amount } = pricing.total({ useNetPrice: false });
32
66
  const paymentIntent = await stripe.paymentIntents.create({
33
67
  amount: Math.round(amount),
34
68
  currency: currency.toLowerCase(),
35
- description: `${reference}`,
36
- statement_descriptor_suffix: slugify(reference),
37
- receipt_email: order.contact?.emailAddress,
69
+ description,
70
+ statement_descriptor_suffix: order._id,
38
71
  setup_future_usage: 'off_session', // Verify your integration in this guide by including this parameter
72
+ customer,
39
73
  metadata: {
74
+ userId: order.userId,
40
75
  orderPaymentId: orderPayment._id,
41
- environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
76
+ orderId: order._id,
77
+ environment,
42
78
  },
43
79
  ...options,
44
80
  });
@@ -1 +1 @@
1
- {"version":3,"file":"stripe.js","sourceRoot":"","sources":["../../../src/payment/stripe/stripe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,EAAE,aAAa,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;AAEtF,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,EAAE;IACvC,UAAU,EAAE,YAAY;CACzB,CAAC,CAAC;AAEH,eAAe,MAAM,CAAC;AAEtB,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,EAC3C,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,iBAAiB,EAAE,EAC1C,OAAO,GAAG,EAAE,EACZ,EAAE;IACF,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;QAC7C,QAAQ,EAAE;YACR,MAAM;YACN,WAAW,EAAE,0BAA0B,IAAI,IAAI;SAChD;QACD,IAAI;QACJ,KAAK;KACN,CAAC,CAAC;IACH,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC;QACnD,QAAQ,EAAE,QAAQ,CAAC,EAAE;QACrB,QAAQ,EAAE;YACR,MAAM;YACN,iBAAiB;YACjB,WAAW,EAAE,0BAA0B,IAAI,IAAI;SAChD;QACD,KAAK,EAAE,aAAa;QACpB,GAAG,OAAO;KACX,CAAC,CAAC;IACH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,EAAE,EAAE,KA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AAE,CAAC,CAAC;IACnE,MAAM,aAAa,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC;QACvD,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;QAC1B,QAAQ,EAAE,QAAQ,CAAC,WAAW,EAAE;QAChC,WAAW;QACX,2BAA2B,EAAE,KAAK,CAAC,GAAG;QACtC,kBAAkB,EAAE,aAAa,EAAE,oEAAoE;QACvG,QAAQ;QACR,QAAQ,EAAE;YACR,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,cAAc,EAAE,YAAY,CAAC,GAAG;YAChC,OAAO,EAAE,KAAK,CAAC,GAAG;YAClB,WAAW;SACZ;QACD,GAAG,OAAO;KACX,CAAC,CAAC;IACH,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/plugins",
3
- "version": "2.11.2",
3
+ "version": "2.11.4",
4
4
  "main": "lib/plugins-index.js",
5
5
  "types": "lib/plugins-index.d.ts",
6
6
  "exports": {
@@ -81,34 +81,34 @@
81
81
  "postfinancecheckout": "^4.1.1",
82
82
  "stripe": "^15.12.0",
83
83
  "tiny-secp256k1": "^2.2.3",
84
- "twilio": "^5.2.0",
84
+ "twilio": "^5.2.2",
85
85
  "web-push": "^3.6.3"
86
86
  },
87
87
  "devDependencies": {
88
88
  "@redis/client": "^1.5.16",
89
- "@types/node": "^20.14.8",
90
- "@unchainedshop/api": "^2.11.2",
91
- "@unchainedshop/core-delivery": "^2.11.2",
92
- "@unchainedshop/core-enrollments": "^2.11.2",
93
- "@unchainedshop/core-filters": "^2.11.2",
94
- "@unchainedshop/core-messaging": "^2.11.2",
95
- "@unchainedshop/core-orders": "^2.11.2",
96
- "@unchainedshop/core-payment": "^2.11.2",
97
- "@unchainedshop/core-products": "^2.11.2",
98
- "@unchainedshop/core-quotations": "^2.11.2",
99
- "@unchainedshop/core-warehousing": "^2.11.2",
100
- "@unchainedshop/core-worker": "^2.11.2",
101
- "@unchainedshop/events": "^2.11.2",
102
- "@unchainedshop/file-upload": "^2.11.2",
103
- "@unchainedshop/logger": "^2.11.2",
104
- "@unchainedshop/types": "^2.11.2",
105
- "@unchainedshop/utils": "^2.11.2",
89
+ "@types/node": "^20.14.9",
90
+ "@unchainedshop/api": "^2.11.3",
91
+ "@unchainedshop/core-delivery": "^2.11.3",
92
+ "@unchainedshop/core-enrollments": "^2.11.3",
93
+ "@unchainedshop/core-filters": "^2.11.3",
94
+ "@unchainedshop/core-messaging": "^2.11.3",
95
+ "@unchainedshop/core-orders": "^2.11.3",
96
+ "@unchainedshop/core-payment": "^2.11.3",
97
+ "@unchainedshop/core-products": "^2.11.3",
98
+ "@unchainedshop/core-quotations": "^2.11.3",
99
+ "@unchainedshop/core-warehousing": "^2.11.3",
100
+ "@unchainedshop/core-worker": "^2.11.3",
101
+ "@unchainedshop/events": "^2.11.3",
102
+ "@unchainedshop/file-upload": "^2.11.3",
103
+ "@unchainedshop/logger": "^2.11.3",
104
+ "@unchainedshop/types": "^2.11.3",
105
+ "@unchainedshop/utils": "^2.11.3",
106
106
  "bluebird": "^3.7.2",
107
107
  "event-iterator": "^2.0.0",
108
108
  "express": "^4.19.2",
109
109
  "jest": "^29.7.0",
110
110
  "JSONStream": "^1.3.5",
111
- "lru-cache": "^10.2.2",
111
+ "lru-cache": "^10.3.0",
112
112
  "minio": "^8.0.1",
113
113
  "node-sheets": "^1.2.0",
114
114
  "nodemailer": "^6.9.14",
@@ -21,6 +21,8 @@ const Stripe: IPaymentAdapter = {
21
21
  actions: (params) => {
22
22
  const { modules } = params.context;
23
23
 
24
+ const descriptorPrefix = params.config.find(({ key }) => key === 'descriptorPrefix')?.value;
25
+ console.log({ descriptorPrefix });
24
26
  const adapterActions = {
25
27
  ...PaymentAdapter.actions(params),
26
28
 
@@ -71,7 +73,7 @@ const Stripe: IPaymentAdapter = {
71
73
  if (orderPayment) {
72
74
  const pricing = await modules.orders.pricingSheet(order);
73
75
  const paymentIntent = await createOrderPaymentIntent(
74
- { order, orderPayment, pricing },
76
+ { order, orderPayment, pricing, descriptorPrefix },
75
77
  transactionContext,
76
78
  );
77
79
  return paymentIntent.client_secret;
@@ -83,7 +85,7 @@ const Stripe: IPaymentAdapter = {
83
85
  const name = user.profile.displayName || user.username || email;
84
86
 
85
87
  const paymentIntent = await createRegistrationIntent(
86
- { userId, name, email, paymentProviderId },
88
+ { userId, name, email, paymentProviderId, descriptorPrefix },
87
89
  transactionContext,
88
90
  );
89
91
  return paymentIntent.client_secret;
@@ -104,7 +106,7 @@ const Stripe: IPaymentAdapter = {
104
106
  const paymentIntentObject = paymentIntentId
105
107
  ? await stripe.paymentIntents.retrieve(paymentIntentId)
106
108
  : await createOrderPaymentIntent(
107
- { orderPayment, order, pricing },
109
+ { orderPayment, order, pricing, descriptorPrefix },
108
110
  {
109
111
  customer: paymentCredentials.meta?.customer,
110
112
  confirm: true,
@@ -1,4 +1,6 @@
1
- import { slugify } from '@unchainedshop/utils';
1
+ import { Order } from '@unchainedshop/types/orders.js';
2
+ import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
3
+ import { IOrderPricingSheet } from '@unchainedshop/types/orders.pricing.js';
2
4
  import Stripe from 'stripe';
3
5
 
4
6
  const { STRIPE_SECRET, STRIPE_WEBHOOK_ENVIRONMENT, EMAIL_WEBSITE_NAME } = process.env;
@@ -7,26 +9,71 @@ const stripe = new Stripe(STRIPE_SECRET, {
7
9
  apiVersion: '2024-04-10',
8
10
  });
9
11
 
12
+ const environment = STRIPE_WEBHOOK_ENVIRONMENT ?? null;
13
+
10
14
  export default stripe;
11
15
 
16
+ export const upsertCustomer = async ({ userId, name, email }): Promise<string> => {
17
+ try {
18
+ const { data } = await stripe.customers.search({ query: `metadata["userId"]:"${userId}"` });
19
+ const existingCustomer = data[0];
20
+
21
+ if (
22
+ existingCustomer.name !== name ||
23
+ existingCustomer.email !== email ||
24
+ existingCustomer.metadata.environment !== environment
25
+ ) {
26
+ const updatedCustomer = await stripe.customers.update(existingCustomer.id, {
27
+ metadata: {
28
+ userId,
29
+ environment,
30
+ },
31
+ name,
32
+ email,
33
+ });
34
+ return updatedCustomer.id;
35
+ }
36
+
37
+ return existingCustomer.id;
38
+ } catch (e) {
39
+ const customer = await stripe.customers.create({
40
+ metadata: {
41
+ userId,
42
+ environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
43
+ },
44
+ name,
45
+ email,
46
+ });
47
+ return customer.id;
48
+ }
49
+ };
50
+
12
51
  export const createRegistrationIntent = async (
13
- { userId, name, email, paymentProviderId },
14
- options = {},
15
- ) => {
16
- const customer = await stripe.customers.create({
17
- metadata: {
18
- userId,
19
- environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
20
- },
52
+ {
53
+ userId,
21
54
  name,
22
55
  email,
23
- });
56
+ paymentProviderId,
57
+ descriptorPrefix,
58
+ }: {
59
+ userId: string;
60
+ name: string;
61
+ email: string;
62
+ paymentProviderId: string;
63
+ descriptorPrefix?: string;
64
+ },
65
+ options: Record<string, any> = {},
66
+ ) => {
67
+ const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${userId}${email ? ` (#${email})` : ''}`;
68
+ const customer = options?.customer || (await upsertCustomer({ userId, name, email }));
69
+
24
70
  const setupIntent = await stripe.setupIntents.create({
25
- customer: customer.id,
71
+ description,
72
+ customer,
26
73
  metadata: {
27
74
  userId,
28
75
  paymentProviderId,
29
- environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
76
+ environment,
30
77
  },
31
78
  usage: 'off_session',
32
79
  ...options,
@@ -34,19 +81,47 @@ export const createRegistrationIntent = async (
34
81
  return setupIntent;
35
82
  };
36
83
 
37
- export const createOrderPaymentIntent = async ({ order, orderPayment, pricing }, options = {}) => {
38
- const reference = EMAIL_WEBSITE_NAME || order._id;
84
+ export const createOrderPaymentIntent = async (
85
+ {
86
+ order,
87
+ orderPayment,
88
+ pricing,
89
+ descriptorPrefix,
90
+ }: {
91
+ order: Order;
92
+ orderPayment: OrderPayment;
93
+ pricing: IOrderPricingSheet;
94
+ descriptorPrefix?: string;
95
+ },
96
+ options: Record<string, any> = {},
97
+ ) => {
98
+ const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${order._id}${order.orderNumber ? ` (#${order.orderNumber})` : ''}`;
99
+
100
+ const name =
101
+ [order.billingAddress?.firstName, order.billingAddress?.lastName].filter(Boolean).join(' ') ||
102
+ order.billingAddress?.company;
103
+
104
+ const customer =
105
+ options?.customer ||
106
+ (await upsertCustomer({
107
+ userId: order.userId,
108
+ name,
109
+ email: order.contact?.emailAddress,
110
+ }));
111
+
39
112
  const { currency, amount } = pricing.total({ useNetPrice: false });
40
113
  const paymentIntent = await stripe.paymentIntents.create({
41
114
  amount: Math.round(amount),
42
115
  currency: currency.toLowerCase(),
43
- description: `${reference}`,
44
- statement_descriptor_suffix: slugify(reference),
45
- receipt_email: order.contact?.emailAddress,
116
+ description,
117
+ statement_descriptor_suffix: order._id,
46
118
  setup_future_usage: 'off_session', // Verify your integration in this guide by including this parameter
119
+ customer,
47
120
  metadata: {
121
+ userId: order.userId,
48
122
  orderPaymentId: orderPayment._id,
49
- environment: STRIPE_WEBHOOK_ENVIRONMENT ?? null,
123
+ orderId: order._id,
124
+ environment,
50
125
  },
51
126
  ...options,
52
127
  });