fix: Update stripe options input (#7919)

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

No files matched your search

+3 -7
View File
@@ -2,14 +2,10 @@ import { ModuleExports } from "@medusajs/types"
import { PaymentModuleService } from "@services" import { PaymentModuleService } from "@services"
import loadProviders from "./loaders/providers" import loadProviders from "./loaders/providers"
import loadDefaults from "./loaders/defaults"
const service = PaymentModuleService const moduleDefinition: ModuleExports = {
const loaders = [loadProviders, loadDefaults] as any service: PaymentModuleService,
loaders: [loadProviders],
export const moduleDefinition: ModuleExports = {
service,
loaders,
} }
export default moduleDefinition export default moduleDefinition
@@ -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)
}
@@ -1,2 +1 @@
export * from "./providers" export * from "./providers"
export * from "./defaults"
@@ -1,8 +1,14 @@
import { moduleProviderLoader } from "@medusajs/modules-sdk" 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 { Lifetime, asFunction, asValue } from "awilix"
import * as providers from "../providers" import * as providers from "../providers"
import { PaymentProviderService } from "@services"
const registrationFn = async (klass, container, pluginOptions) => { const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => { Object.entries(pluginOptions.config || []).map(([name, config]) => {
@@ -37,4 +43,32 @@ export default async ({
providers: options?.providers || [], providers: options?.providers || [],
registerServiceFn: registrationFn, 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)
} }
@@ -23,7 +23,6 @@ import {
ErrorCodes, ErrorCodes,
ErrorIntentStatus, ErrorIntentStatus,
PaymentIntentOptions, PaymentIntentOptions,
StripeCredentials,
StripeOptions, StripeOptions,
} from "../types" } from "../types"
import { import {
@@ -31,7 +30,7 @@ import {
getSmallestUnit, getSmallestUnit,
} from "../utils/get-smallest-unit" } from "../utils/get-smallest-unit"
abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> { abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
protected readonly options_: StripeOptions protected readonly options_: StripeOptions
protected stripe_: Stripe protected stripe_: Stripe
protected container_: MedusaContainer protected container_: MedusaContainer
@@ -54,7 +53,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> {
abstract get paymentIntentOptions(): PaymentIntentOptions abstract get paymentIntentOptions(): PaymentIntentOptions
private validateOptions(options: StripeCredentials): void { private validateOptions(options: StripeOptions): void {
if (!isDefined(options.apiKey)) { if (!isDefined(options.apiKey)) {
throw new Error("Required option `apiKey` is missing in Stripe plugin") 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 { currency_code, amount } = input
const description = (extra?.payment_description ?? const description = (extra?.payment_description ??
this.options_?.payment_description) as string this.options_?.paymentDescription) as string
const intentRequest: Stripe.PaymentIntentCreateParams = { const intentRequest: Stripe.PaymentIntentCreateParams = {
description, description,
@@ -125,7 +124,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeCredentials> {
...intentRequestData, ...intentRequestData,
} }
if (this.options_?.automatic_payment_methods) { if (this.options_?.automaticPaymentMethods) {
intentRequest.automatic_payment_methods = { enabled: true } intentRequest.automatic_payment_methods = { enabled: true }
} }
@@ -1,22 +1,24 @@
export interface StripeCredentials {
apiKey: string
webhookSecret: string
}
export interface StripeOptions { 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) * Use this flag to capture payment immediately (default is false)
*/ */
capture?: boolean 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 * Set a default description on the intent if the context does not provide one
*/ */
payment_description?: string paymentDescription?: string
} }
export interface PaymentIntentOptions { export interface PaymentIntentOptions {