@unchainedshop/plugins 2.11.3 → 2.11.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/payment/stripe/index.js +4 -3
- package/lib/payment/stripe/index.js.map +1 -1
- package/lib/payment/stripe/stripe.d.ts +11 -9
- package/lib/payment/stripe/stripe.js +24 -19
- package/lib/payment/stripe/stripe.js.map +1 -1
- package/package.json +1 -1
- package/src/payment/stripe/index.ts +4 -3
- package/src/payment/stripe/stripe.ts +50 -23
|
@@ -13,6 +13,7 @@ const Stripe = {
|
|
|
13
13
|
},
|
|
14
14
|
actions: (params) => {
|
|
15
15
|
const { modules } = params.context;
|
|
16
|
+
const descriptorPrefix = params.config.find(({ key }) => key === 'descriptorPrefix')?.value;
|
|
16
17
|
const adapterActions = {
|
|
17
18
|
...PaymentAdapter.actions(params),
|
|
18
19
|
configurationError() {
|
|
@@ -54,14 +55,14 @@ const Stripe = {
|
|
|
54
55
|
const { orderPayment, order, paymentProviderId } = params.paymentContext;
|
|
55
56
|
if (orderPayment) {
|
|
56
57
|
const pricing = await modules.orders.pricingSheet(order);
|
|
57
|
-
const paymentIntent = await createOrderPaymentIntent({ order, orderPayment, pricing }, transactionContext);
|
|
58
|
+
const paymentIntent = await createOrderPaymentIntent({ order, orderPayment, pricing, descriptorPrefix }, transactionContext);
|
|
58
59
|
return paymentIntent.client_secret;
|
|
59
60
|
}
|
|
60
61
|
const userId = order?.userId || params.paymentContext?.userId;
|
|
61
62
|
const user = await modules.users.findUserById(userId);
|
|
62
63
|
const email = modules.users.primaryEmail(user)?.address;
|
|
63
64
|
const name = user.profile.displayName || user.username || email;
|
|
64
|
-
const paymentIntent = await createRegistrationIntent({ userId, name, email, paymentProviderId }, transactionContext);
|
|
65
|
+
const paymentIntent = await createRegistrationIntent({ userId, name, email, paymentProviderId, descriptorPrefix }, transactionContext);
|
|
65
66
|
return paymentIntent.client_secret;
|
|
66
67
|
},
|
|
67
68
|
charge: async ({ paymentIntentId, paymentCredentials }) => {
|
|
@@ -75,7 +76,7 @@ const Stripe = {
|
|
|
75
76
|
const pricing = await modules.orders.pricingSheet(order);
|
|
76
77
|
const paymentIntentObject = paymentIntentId
|
|
77
78
|
? await stripe.paymentIntents.retrieve(paymentIntentId)
|
|
78
|
-
: await createOrderPaymentIntent({ orderPayment, order, pricing }, {
|
|
79
|
+
: await createOrderPaymentIntent({ orderPayment, order, pricing, descriptorPrefix }, {
|
|
79
80
|
customer: paymentCredentials.meta?.customer,
|
|
80
81
|
confirm: true,
|
|
81
82
|
payment_method: paymentCredentials.token,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/payment/stripe/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC5F,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,MAAM,EAAE,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEzF,cAAc,iBAAiB,CAAC;AAEhC,MAAM,MAAM,GAAG,YAAY,CAAC,+BAA+B,CAAC,CAAC;AAE7D,MAAM,MAAM,GAAoB;IAC9B,GAAG,cAAc;IAEjB,GAAG,EAAE,+BAA+B;IACpC,KAAK,EAAE,QAAQ;IACf,OAAO,EAAE,OAAO;IAEhB,aAAa,CAAC,IAAI;QAChB,OAAO,IAAI,KAAK,SAAS,CAAC;IAC5B,CAAC;IAED,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE;QAClB,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC;QAEnC,MAAM,cAAc,GAAG;YACrB,GAAG,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC;YAEjC,kBAAkB;gBAChB,IAAI,CAAC,MAAM,EAAE,CAAC;oBACZ,OAAO,YAAY,CAAC,wBAAwB,CAAC;gBAC/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,MAAM,CAAC,CAAC"}
|
|
@@ -8,15 +8,17 @@ export declare const upsertCustomer: ({ userId, name, email }: {
|
|
|
8
8
|
userId: any;
|
|
9
9
|
name: any;
|
|
10
10
|
email: any;
|
|
11
|
-
}) => Promise<
|
|
12
|
-
export declare const createRegistrationIntent: ({ userId, name, email, paymentProviderId }: {
|
|
13
|
-
userId:
|
|
14
|
-
name:
|
|
15
|
-
email:
|
|
16
|
-
paymentProviderId:
|
|
17
|
-
|
|
18
|
-
|
|
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, }: {
|
|
19
20
|
order: Order;
|
|
20
21
|
orderPayment: OrderPayment;
|
|
21
22
|
pricing: IOrderPricingSheet;
|
|
22
|
-
|
|
23
|
+
descriptorPrefix?: string;
|
|
24
|
+
}, options?: Record<string, any>) => Promise<Stripe.Response<Stripe.PaymentIntent>>;
|
|
@@ -3,12 +3,12 @@ const { STRIPE_SECRET, STRIPE_WEBHOOK_ENVIRONMENT, EMAIL_WEBSITE_NAME } = proces
|
|
|
3
3
|
const stripe = new Stripe(STRIPE_SECRET, {
|
|
4
4
|
apiVersion: '2024-04-10',
|
|
5
5
|
});
|
|
6
|
+
const environment = STRIPE_WEBHOOK_ENVIRONMENT ?? null;
|
|
6
7
|
export default stripe;
|
|
7
8
|
export const upsertCustomer = async ({ userId, name, email }) => {
|
|
8
9
|
try {
|
|
9
10
|
const { data } = await stripe.customers.search({ query: `metadata["userId"]:"${userId}"` });
|
|
10
11
|
const existingCustomer = data[0];
|
|
11
|
-
const environment = STRIPE_WEBHOOK_ENVIRONMENT ?? null;
|
|
12
12
|
if (existingCustomer.name !== name ||
|
|
13
13
|
existingCustomer.email !== email ||
|
|
14
14
|
existingCustomer.metadata.environment !== environment) {
|
|
@@ -20,9 +20,9 @@ export const upsertCustomer = async ({ userId, name, email }) => {
|
|
|
20
20
|
name,
|
|
21
21
|
email,
|
|
22
22
|
});
|
|
23
|
-
return updatedCustomer;
|
|
23
|
+
return updatedCustomer.id;
|
|
24
24
|
}
|
|
25
|
-
return existingCustomer;
|
|
25
|
+
return existingCustomer.id;
|
|
26
26
|
}
|
|
27
27
|
catch (e) {
|
|
28
28
|
const customer = await stripe.customers.create({
|
|
@@ -33,43 +33,48 @@ export const upsertCustomer = async ({ userId, name, email }) => {
|
|
|
33
33
|
name,
|
|
34
34
|
email,
|
|
35
35
|
});
|
|
36
|
-
return customer;
|
|
36
|
+
return customer.id;
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
|
-
export const createRegistrationIntent = async ({ userId, name, email, paymentProviderId }, options = {}) => {
|
|
40
|
-
const
|
|
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 }));
|
|
41
42
|
const setupIntent = await stripe.setupIntents.create({
|
|
42
|
-
|
|
43
|
+
description,
|
|
44
|
+
customer,
|
|
43
45
|
metadata: {
|
|
44
46
|
userId,
|
|
45
47
|
paymentProviderId,
|
|
46
|
-
environment
|
|
48
|
+
environment,
|
|
47
49
|
},
|
|
48
50
|
usage: 'off_session',
|
|
49
51
|
...options,
|
|
50
52
|
});
|
|
51
53
|
return setupIntent;
|
|
52
54
|
};
|
|
53
|
-
export const createOrderPaymentIntent = async ({ order, orderPayment, pricing, }, options = {}) => {
|
|
55
|
+
export const createOrderPaymentIntent = async ({ order, orderPayment, pricing, descriptorPrefix, }, options = {}) => {
|
|
56
|
+
const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${order._id}${order.orderNumber ? ` (#${order.orderNumber})` : ''}`;
|
|
54
57
|
const name = [order.billingAddress?.firstName, order.billingAddress?.lastName].filter(Boolean).join(' ') ||
|
|
55
58
|
order.billingAddress?.company;
|
|
56
|
-
const customer =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
const customer = options?.customer ||
|
|
60
|
+
(await upsertCustomer({
|
|
61
|
+
userId: order.userId,
|
|
62
|
+
name,
|
|
63
|
+
email: order.contact?.emailAddress,
|
|
64
|
+
}));
|
|
62
65
|
const { currency, amount } = pricing.total({ useNetPrice: false });
|
|
63
66
|
const paymentIntent = await stripe.paymentIntents.create({
|
|
64
67
|
amount: Math.round(amount),
|
|
65
68
|
currency: currency.toLowerCase(),
|
|
66
|
-
description
|
|
67
|
-
statement_descriptor_suffix: order._id,
|
|
69
|
+
description,
|
|
70
|
+
statement_descriptor_suffix: `${order._id.substring(0, 4)}..${order._id.substring(order._id.length - 4)}`,
|
|
68
71
|
setup_future_usage: 'off_session', // Verify your integration in this guide by including this parameter
|
|
69
|
-
customer
|
|
72
|
+
customer,
|
|
70
73
|
metadata: {
|
|
74
|
+
userId: order.userId,
|
|
71
75
|
orderPaymentId: orderPayment._id,
|
|
72
|
-
|
|
76
|
+
orderId: order._id,
|
|
77
|
+
environment,
|
|
73
78
|
},
|
|
74
79
|
...options,
|
|
75
80
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stripe.js","sourceRoot":"","sources":["../../../src/payment/stripe/stripe.ts"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,EAAE,aAAa,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;AAEtF,MAAM,MAAM,GAAG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;SACZ;QACD,GAAG,OAAO;KACX,CAAC,CAAC;IACH,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -21,6 +21,7 @@ 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;
|
|
24
25
|
const adapterActions = {
|
|
25
26
|
...PaymentAdapter.actions(params),
|
|
26
27
|
|
|
@@ -71,7 +72,7 @@ const Stripe: IPaymentAdapter = {
|
|
|
71
72
|
if (orderPayment) {
|
|
72
73
|
const pricing = await modules.orders.pricingSheet(order);
|
|
73
74
|
const paymentIntent = await createOrderPaymentIntent(
|
|
74
|
-
{ order, orderPayment, pricing },
|
|
75
|
+
{ order, orderPayment, pricing, descriptorPrefix },
|
|
75
76
|
transactionContext,
|
|
76
77
|
);
|
|
77
78
|
return paymentIntent.client_secret;
|
|
@@ -83,7 +84,7 @@ const Stripe: IPaymentAdapter = {
|
|
|
83
84
|
const name = user.profile.displayName || user.username || email;
|
|
84
85
|
|
|
85
86
|
const paymentIntent = await createRegistrationIntent(
|
|
86
|
-
{ userId, name, email, paymentProviderId },
|
|
87
|
+
{ userId, name, email, paymentProviderId, descriptorPrefix },
|
|
87
88
|
transactionContext,
|
|
88
89
|
);
|
|
89
90
|
return paymentIntent.client_secret;
|
|
@@ -104,7 +105,7 @@ const Stripe: IPaymentAdapter = {
|
|
|
104
105
|
const paymentIntentObject = paymentIntentId
|
|
105
106
|
? await stripe.paymentIntents.retrieve(paymentIntentId)
|
|
106
107
|
: await createOrderPaymentIntent(
|
|
107
|
-
{ orderPayment, order, pricing },
|
|
108
|
+
{ orderPayment, order, pricing, descriptorPrefix },
|
|
108
109
|
{
|
|
109
110
|
customer: paymentCredentials.meta?.customer,
|
|
110
111
|
confirm: true,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Order } from '@unchainedshop/types/orders.js';
|
|
2
2
|
import { OrderPayment } from '@unchainedshop/types/orders.payments.js';
|
|
3
|
-
import { IOrderPricingSheet
|
|
3
|
+
import { IOrderPricingSheet } from '@unchainedshop/types/orders.pricing.js';
|
|
4
4
|
import Stripe from 'stripe';
|
|
5
5
|
|
|
6
6
|
const { STRIPE_SECRET, STRIPE_WEBHOOK_ENVIRONMENT, EMAIL_WEBSITE_NAME } = process.env;
|
|
@@ -9,13 +9,14 @@ const stripe = new Stripe(STRIPE_SECRET, {
|
|
|
9
9
|
apiVersion: '2024-04-10',
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
+
const environment = STRIPE_WEBHOOK_ENVIRONMENT ?? null;
|
|
13
|
+
|
|
12
14
|
export default stripe;
|
|
13
15
|
|
|
14
|
-
export const upsertCustomer = async ({ userId, name, email }) => {
|
|
16
|
+
export const upsertCustomer = async ({ userId, name, email }): Promise<string> => {
|
|
15
17
|
try {
|
|
16
18
|
const { data } = await stripe.customers.search({ query: `metadata["userId"]:"${userId}"` });
|
|
17
19
|
const existingCustomer = data[0];
|
|
18
|
-
const environment = STRIPE_WEBHOOK_ENVIRONMENT ?? null;
|
|
19
20
|
|
|
20
21
|
if (
|
|
21
22
|
existingCustomer.name !== name ||
|
|
@@ -30,10 +31,10 @@ export const upsertCustomer = async ({ userId, name, email }) => {
|
|
|
30
31
|
name,
|
|
31
32
|
email,
|
|
32
33
|
});
|
|
33
|
-
return updatedCustomer;
|
|
34
|
+
return updatedCustomer.id;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
return existingCustomer;
|
|
37
|
+
return existingCustomer.id;
|
|
37
38
|
} catch (e) {
|
|
38
39
|
const customer = await stripe.customers.create({
|
|
39
40
|
metadata: {
|
|
@@ -43,21 +44,36 @@ export const upsertCustomer = async ({ userId, name, email }) => {
|
|
|
43
44
|
name,
|
|
44
45
|
email,
|
|
45
46
|
});
|
|
46
|
-
return customer;
|
|
47
|
+
return customer.id;
|
|
47
48
|
}
|
|
48
49
|
};
|
|
49
50
|
|
|
50
51
|
export const createRegistrationIntent = async (
|
|
51
|
-
{
|
|
52
|
-
|
|
52
|
+
{
|
|
53
|
+
userId,
|
|
54
|
+
name,
|
|
55
|
+
email,
|
|
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> = {},
|
|
53
66
|
) => {
|
|
54
|
-
const
|
|
67
|
+
const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${userId}${email ? ` (#${email})` : ''}`;
|
|
68
|
+
const customer = options?.customer || (await upsertCustomer({ userId, name, email }));
|
|
69
|
+
|
|
55
70
|
const setupIntent = await stripe.setupIntents.create({
|
|
56
|
-
|
|
71
|
+
description,
|
|
72
|
+
customer,
|
|
57
73
|
metadata: {
|
|
58
74
|
userId,
|
|
59
75
|
paymentProviderId,
|
|
60
|
-
environment
|
|
76
|
+
environment,
|
|
61
77
|
},
|
|
62
78
|
usage: 'off_session',
|
|
63
79
|
...options,
|
|
@@ -70,31 +86,42 @@ export const createOrderPaymentIntent = async (
|
|
|
70
86
|
order,
|
|
71
87
|
orderPayment,
|
|
72
88
|
pricing,
|
|
73
|
-
|
|
74
|
-
|
|
89
|
+
descriptorPrefix,
|
|
90
|
+
}: {
|
|
91
|
+
order: Order;
|
|
92
|
+
orderPayment: OrderPayment;
|
|
93
|
+
pricing: IOrderPricingSheet;
|
|
94
|
+
descriptorPrefix?: string;
|
|
95
|
+
},
|
|
96
|
+
options: Record<string, any> = {},
|
|
75
97
|
) => {
|
|
98
|
+
const description = `${descriptorPrefix || EMAIL_WEBSITE_NAME || 'Unchained'} ${order._id}${order.orderNumber ? ` (#${order.orderNumber})` : ''}`;
|
|
99
|
+
|
|
76
100
|
const name =
|
|
77
101
|
[order.billingAddress?.firstName, order.billingAddress?.lastName].filter(Boolean).join(' ') ||
|
|
78
102
|
order.billingAddress?.company;
|
|
79
103
|
|
|
80
|
-
const customer =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
104
|
+
const customer =
|
|
105
|
+
options?.customer ||
|
|
106
|
+
(await upsertCustomer({
|
|
107
|
+
userId: order.userId,
|
|
108
|
+
name,
|
|
109
|
+
email: order.contact?.emailAddress,
|
|
110
|
+
}));
|
|
85
111
|
|
|
86
|
-
const reference = EMAIL_WEBSITE_NAME || 'Unchained';
|
|
87
112
|
const { currency, amount } = pricing.total({ useNetPrice: false });
|
|
88
113
|
const paymentIntent = await stripe.paymentIntents.create({
|
|
89
114
|
amount: Math.round(amount),
|
|
90
115
|
currency: currency.toLowerCase(),
|
|
91
|
-
description
|
|
92
|
-
statement_descriptor_suffix: order._id,
|
|
116
|
+
description,
|
|
117
|
+
statement_descriptor_suffix: `${order._id.substring(0, 4)}..${order._id.substring(order._id.length - 4)}`,
|
|
93
118
|
setup_future_usage: 'off_session', // Verify your integration in this guide by including this parameter
|
|
94
|
-
customer
|
|
119
|
+
customer,
|
|
95
120
|
metadata: {
|
|
121
|
+
userId: order.userId,
|
|
96
122
|
orderPaymentId: orderPayment._id,
|
|
97
|
-
|
|
123
|
+
orderId: order._id,
|
|
124
|
+
environment,
|
|
98
125
|
},
|
|
99
126
|
...options,
|
|
100
127
|
});
|