1
- import { EOL } from "os"
2
-
3
1
import Stripe from "stripe"
4
2
5
3
import {
@@ -13,9 +11,7 @@ import {
13
11
import {
14
12
AbstractPaymentProvider ,
15
13
isDefined ,
16
- isPaymentProviderError ,
17
14
isPresent ,
18
- MedusaError ,
19
15
PaymentActions ,
20
16
PaymentSessionStatus ,
21
17
} from "@medusajs/framework/utils"
@@ -60,23 +56,37 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
60
56
return this . options_
61
57
}
62
58
63
- getPaymentIntentOptions ( ) : PaymentIntentOptions {
64
- const options : PaymentIntentOptions = { }
59
+ normalizePaymentIntentParameters (
60
+ extra ?: Record < string , unknown >
61
+ ) : Partial < Stripe . PaymentIntentCreateParams > {
62
+ const res = { } as Partial < Stripe . PaymentIntentCreateParams >
65
63
66
- if ( this ?. paymentIntentOptions ?. capture_method ) {
67
- options . capture_method = this . paymentIntentOptions . capture_method
68
- }
64
+ res . description = ( extra ?. payment_description ??
65
+ this . options_ ?. paymentDescription ) as string
69
66
70
- if ( this ?. paymentIntentOptions ?. setup_future_usage ) {
71
- options . setup_future_usage = this . paymentIntentOptions . setup_future_usage
72
- }
67
+ res . capture_method =
68
+ ( extra ?. capture_method as "automatic" | "manual" ) ??
69
+ this . paymentIntentOptions . capture_method ??
70
+ ( this . options_ . capture ? "automatic" : "manual" )
73
71
74
- if ( this ?. paymentIntentOptions ?. payment_method_types ) {
75
- options . payment_method_types =
76
- this . paymentIntentOptions . payment_method_types
77
- }
72
+ res . setup_future_usage =
73
+ ( extra ?. setup_future_usage as "off_session" | "on_session" | undefined ) ??
74
+ this . paymentIntentOptions . setup_future_usage
75
+
76
+ res . payment_method_types = this . paymentIntentOptions
77
+ . payment_method_types as string [ ]
78
+
79
+ res . automatic_payment_methods =
80
+ ( extra ?. automatic_payment_methods as { enabled : true } | undefined ) ??
81
+ ( this . options_ ?. automaticPaymentMethods ? { enabled : true } : undefined )
78
82
79
- return options
83
+ res . off_session = extra ?. off_session as boolean | undefined
84
+
85
+ res . confirm = extra ?. confirm as boolean | undefined
86
+
87
+ res . payment_method = extra ?. payment_method as string | undefined
88
+
89
+ return res
80
90
}
81
91
82
92
async getPaymentStatus (
@@ -106,24 +116,16 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
106
116
async initiatePayment (
107
117
input : CreatePaymentProviderSession
108
118
) : Promise < PaymentProviderError | PaymentProviderSessionResponse > {
109
- const intentRequestData = this . getPaymentIntentOptions ( )
110
119
const { email, extra, session_id, customer } = input . context
111
120
const { currency_code, amount } = input
112
121
113
- const description = ( extra ?. payment_description ??
114
- this . options_ ?. paymentDescription ) as string
122
+ const additionalParameters = this . normalizePaymentIntentParameters ( extra )
115
123
116
124
const intentRequest : Stripe . PaymentIntentCreateParams = {
117
- description,
118
125
amount : getSmallestUnit ( amount , currency_code ) ,
119
126
currency : currency_code ,
120
127
metadata : { session_id : session_id ! } ,
121
- capture_method : this . options_ . capture ? "automatic" : "manual" ,
122
- ...intentRequestData ,
123
- }
124
-
125
- if ( this . options_ ?. automaticPaymentMethods ) {
126
- intentRequest . automatic_payment_methods = { enabled : true }
128
+ ...additionalParameters ,
127
129
}
128
130
129
131
if ( customer ?. metadata ?. stripe_id ) {
@@ -273,15 +275,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
273
275
const stripeId = context . customer ?. metadata ?. stripe_id
274
276
275
277
if ( stripeId !== data . customer ) {
276
- const result = await this . initiatePayment ( input )
277
- if ( isPaymentProviderError ( result ) ) {
278
- return this . buildError (
279
- "An error occurred in updatePayment during the initiate of the new payment for the new customer" ,
280
- result
281
- )
282
- }
283
-
284
- return result
278
+ return await this . initiatePayment ( input )
285
279
} else {
286
280
if ( isPresent ( amount ) && data . amount === amountNumeric ) {
287
281
return { data }
@@ -300,25 +294,6 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
300
294
}
301
295
}
302
296
303
- async updatePaymentData ( sessionId : string , data : Record < string , unknown > ) {
304
- try {
305
- // Prevent from updating the amount from here as it should go through
306
- // the updatePayment method to perform the correct logic
307
- if ( isPresent ( data . amount ) ) {
308
- throw new MedusaError (
309
- MedusaError . Types . INVALID_DATA ,
310
- "Cannot update amount, use updatePayment instead"
311
- )
312
- }
313
-
314
- return ( await this . stripe_ . paymentIntents . update ( sessionId , {
315
- ...data ,
316
- } ) ) as unknown as PaymentProviderSessionResponse [ "data" ]
317
- } catch ( e ) {
318
- return this . buildError ( "An error occurred in updatePaymentData" , e )
319
- }
320
- }
321
-
322
297
async getWebhookActionAndData (
323
298
webhookData : ProviderWebhookPayload [ "payload" ]
324
299
) : Promise < WebhookActionResult > {
@@ -374,18 +349,17 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
374
349
this . options_ . webhookSecret
375
350
)
376
351
}
377
- protected buildError (
378
- message : string ,
379
- error : Stripe . StripeRawError | PaymentProviderError | Error
380
- ) : PaymentProviderError {
352
+ protected buildError ( message : string , error : Error ) : PaymentProviderError {
353
+ const errorDetails =
354
+ "raw" in error ? ( error . raw as Stripe . StripeRawError ) : error
355
+
381
356
return {
382
- error : message ,
383
- code : "code" in error ? error . code : "unknown" ,
384
- detail : isPaymentProviderError ( error )
385
- ? `${ error . error } ${ EOL } ${ error . detail ?? "" } `
386
- : "detail" in error
387
- ? error . detail
388
- : error . message ?? "" ,
357
+ error : `${ message } : ${ error . message } ` ,
358
+ code : "code" in errorDetails ? errorDetails . code : "unknown" ,
359
+ detail :
360
+ "detail" in errorDetails
361
+ ? `${ error . message } : ${ errorDetails . detail } `
362
+ : error . message ,
389
363
}
390
364
}
391
365
}
0 commit comments