fix(medusa-payment-stripe): change webhook environment variables to plugin options (#6034)
* fix(medusa-payment-stripe): change webhook environment variables to plugin options * access options through service
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { constructWebhook } from "../../utils/utils"
|
||||
|
||||
const WEBHOOK_DELAY = process.env.STRIPE_WEBHOOK_DELAY ?? 5000 // 5s
|
||||
const WEBHOOK_RETRIES = process.env.STRIPE_WEBHOOK_RETRIES ?? 3
|
||||
import StripeProviderService from "../../../services/stripe-provider"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
try {
|
||||
const pluginOptions = req.scope.resolve<StripeProviderService>(
|
||||
"stripeProviderService"
|
||||
).options
|
||||
|
||||
const event = constructWebhook({
|
||||
signature: req.headers["stripe-signature"],
|
||||
body: req.body,
|
||||
@@ -16,8 +18,8 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
|
||||
// we delay the processing of the event to avoid a conflict caused by a race condition
|
||||
await eventBus.emit("medusa.stripe_payment_intent_update", event, {
|
||||
delay: WEBHOOK_DELAY,
|
||||
attempts: WEBHOOK_RETRIES,
|
||||
delay: pluginOptions.webhook_delay || 5000,
|
||||
attempts: pluginOptions.webhook_retries || 3,
|
||||
})
|
||||
} catch (err) {
|
||||
res.status(400).send(`Webhook Error: ${err.message}`)
|
||||
|
||||
@@ -4,10 +4,12 @@ import {
|
||||
IdempotencyKeyService,
|
||||
PostgresError,
|
||||
} from "@medusajs/medusa"
|
||||
import { ConfigModule, MedusaContainer } from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { AwilixContainer } from "awilix"
|
||||
import { EOL } from "os"
|
||||
import Stripe from "stripe"
|
||||
import { StripeOptions } from "../../types"
|
||||
|
||||
const PAYMENT_PROVIDER_KEY = "pp_stripe"
|
||||
|
||||
@@ -255,4 +257,4 @@ async function completeCartIfNecessary({
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,10 @@ abstract class StripeBase extends AbstractPaymentProcessor {
|
||||
|
||||
abstract get paymentIntentOptions(): PaymentIntentOptions
|
||||
|
||||
get options(): StripeOptions {
|
||||
return this.options_
|
||||
}
|
||||
|
||||
getStripe() {
|
||||
return this.stripe_
|
||||
}
|
||||
|
||||
@@ -15,6 +15,16 @@ export interface StripeOptions {
|
||||
* Set a default description on the intent if the context does not provide one
|
||||
*/
|
||||
payment_description?: string
|
||||
/**
|
||||
* The delay in milliseconds before processing the webhook event.
|
||||
* @defaultValue 5000
|
||||
*/
|
||||
webhook_delay?: number
|
||||
/**
|
||||
* The number of times to retry the webhook event processing in case of an error.
|
||||
* @defaultValue 3
|
||||
*/
|
||||
webhook_retries?: number
|
||||
}
|
||||
|
||||
export interface PaymentIntentOptions {
|
||||
|
||||
Reference in New Issue
Block a user