fix: Update stripe options input (#7919)

This commit is contained in:
Stevche Radevski
2024-07-03 12:51:38 +02:00
committed by GitHub
parent da645e4073
commit 19d7d01805
6 changed files with 53 additions and 49 deletions

View File

@@ -2,14 +2,10 @@ import { ModuleExports } from "@medusajs/types"
import { PaymentModuleService } from "@services"
import loadProviders from "./loaders/providers"
import loadDefaults from "./loaders/defaults"
const service = PaymentModuleService
const loaders = [loadProviders, loadDefaults] as any
export const moduleDefinition: ModuleExports = {
service,
loaders,
const moduleDefinition: ModuleExports = {
service: PaymentModuleService,
loaders: [loadProviders],
}
export default moduleDefinition

View File

@@ -1,26 +0,0 @@
import { CreatePaymentProviderDTO, LoaderOptions } from "@medusajs/types"
import { PaymentProviderService } from "@services"
export default async ({ container }: LoaderOptions): Promise<void> => {
const providersToLoad = container.resolve<string[]>("payment_providers")
const paymentProviderService = container.resolve<PaymentProviderService>(
"paymentProviderService"
)
const providers = await paymentProviderService.list({
id: providersToLoad,
})
const loadedProvidersMap = new Map(providers.map((p) => [p.id, p]))
const providersToCreate: CreatePaymentProviderDTO[] = []
for (const id of providersToLoad) {
if (loadedProvidersMap.has(id)) {
continue
}
providersToCreate.push({ id })
}
await paymentProviderService.create(providersToCreate)
}

View File

@@ -1,2 +1 @@
export * from "./providers"
export * from "./defaults"

View File

@@ -1,8 +1,14 @@
import { moduleProviderLoader } from "@medusajs/modules-sdk"
import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types"
import {
CreatePaymentProviderDTO,
LoaderOptions,
ModuleProvider,
ModulesSdkTypes,
} from "@medusajs/types"
import { Lifetime, asFunction, asValue } from "awilix"
import * as providers from "../providers"
import { PaymentProviderService } from "@services"
const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => {
@@ -37,4 +43,32 @@ export default async ({
providers: options?.providers || [],
registerServiceFn: registrationFn,
})
await registerProvidersInDb({ container })
}
const registerProvidersInDb = async ({
container,
}: LoaderOptions): Promise<void> => {
const providersToLoad = container.resolve<string[]>("payment_providers")
const paymentProviderService = container.resolve<PaymentProviderService>(
"paymentProviderService"
)
const providers = await paymentProviderService.list({
id: providersToLoad,
})
const loadedProvidersMap = new Map(providers.map((p) => [p.id, p]))
const providersToCreate: CreatePaymentProviderDTO[] = []
for (const id of providersToLoad) {
if (loadedProvidersMap.has(id)) {
continue
}
providersToCreate.push({ id })
}
await paymentProviderService.create(providersToCreate)
}

View File

@@ -23,7 +23,6 @@ import {
ErrorCodes,
ErrorIntentStatus,
PaymentIntentOptions,
StripeCredentials,
StripeOptions,
} from "../types"
import {
@@ -31,7 +30,7 @@ import {
getSmallestUnit,
} from "../utils/get-smallest-unit"
abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> {
abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
protected readonly options_: StripeOptions
protected stripe_: Stripe
protected container_: MedusaContainer
@@ -54,7 +53,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> {
abstract get paymentIntentOptions(): PaymentIntentOptions
private validateOptions(options: StripeCredentials): void {
private validateOptions(options: StripeOptions): void {
if (!isDefined(options.apiKey)) {
throw new Error("Required option `apiKey` is missing in Stripe plugin")
}
@@ -114,7 +113,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> {
const { currency_code, amount } = input
const description = (extra?.payment_description ??
this.options_?.payment_description) as string
this.options_?.paymentDescription) as string
const intentRequest: Stripe.PaymentIntentCreateParams = {
description,
@@ -125,7 +124,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> {
...intentRequestData,
}
if (this.options_?.automatic_payment_methods) {
if (this.options_?.automaticPaymentMethods) {
intentRequest.automatic_payment_methods = { enabled: true }
}

View File

@@ -1,22 +1,24 @@
export interface StripeCredentials {
apiKey: string
webhookSecret: string
}
export interface StripeOptions {
credentials: Record<string, StripeCredentials>
/**
* The API key for the Stripe account
*/
apiKey: string
/**
* The webhook secret used to verify webhooks
*/
webhookSecret: string
/**
* Use this flag to capture payment immediately (default is false)
*/
capture?: boolean
/**
* set `automatic_payment_methods` to `{ enabled: true }`
* set `automatic_payment_methods` on the intent request to `{ enabled: true }`
*/
automatic_payment_methods?: boolean
automaticPaymentMethods?: boolean
/**
* Set a default description on the intent if the context does not provide one
*/
payment_description?: string
paymentDescription?: string
}
export interface PaymentIntentOptions {