feat: Implement medusa payments provider (#13772)
* feat: Implement medusa payments provider * chore: Improvements after testing * chore: Add typings to medusa payments * fix: Final changes to complete medusa payment provider * update package * fix: Final changes to complete medusa payment provider --------- Co-authored-by: adrien2p <adrien.deperetti@gmail.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
adrien2p
Oli Juhl
parent
1defb3c29b
commit
ef7b9b9375
@@ -223,18 +223,26 @@ export type MedusaCloudOptions = {
|
||||
* The environment handle of the Medusa Cloud environment.
|
||||
*/
|
||||
environmentHandle?: string
|
||||
/**
|
||||
* The sandbox handle of the Medusa Cloud sandbox.
|
||||
*/
|
||||
sandboxHandle?: string
|
||||
/**
|
||||
* The API key used to access Medusa Cloud services.
|
||||
*/
|
||||
apiKey?: string
|
||||
/**
|
||||
* The webhook secret used to verify webhooks.
|
||||
*/
|
||||
webhookSecret?: string
|
||||
/**
|
||||
* The endpoint of the Medusa Cloud payment service.
|
||||
*/
|
||||
paymentsEndpoint?: string
|
||||
/**
|
||||
* The endpoint of the Medusa Cloud email service.
|
||||
*/
|
||||
emailsEndpoint?: string
|
||||
/**
|
||||
* The sandbox handle of the Medusa Cloud sandbox.
|
||||
*/
|
||||
sandboxHandle?: string
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -160,6 +160,12 @@ export interface RetrievePaymentInput extends PaymentProviderInput {}
|
||||
*/
|
||||
export interface CancelPaymentInput extends PaymentProviderInput {}
|
||||
|
||||
export interface RetrieveAccountHolderInput extends PaymentProviderInput {
|
||||
/**
|
||||
* The ID of the account holder to retrieve.
|
||||
*/
|
||||
id: string
|
||||
}
|
||||
/**
|
||||
* The data to create an account holder.
|
||||
*/
|
||||
@@ -289,6 +295,17 @@ export interface RetrievePaymentOutput extends PaymentProviderOutput {}
|
||||
*/
|
||||
export interface CancelPaymentOutput extends PaymentProviderOutput {}
|
||||
|
||||
/**
|
||||
* The result of retrieving an account holder in the third-party payment provider. The `data`
|
||||
* property is stored as-is in Medusa's account holder's `data` property.
|
||||
*/
|
||||
export interface RetrieveAccountHolderOutput extends PaymentProviderOutput {
|
||||
/**
|
||||
* The ID of the account holder in the payment provider.
|
||||
*/
|
||||
id: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The result of creating an account holder in the third-party payment provider. The `data`
|
||||
* property is stored as-is in Medusa's account holder's `data` property.
|
||||
@@ -405,6 +422,40 @@ export interface IPaymentProvider {
|
||||
|
||||
cancelPayment(data: CancelPaymentInput): Promise<CancelPaymentOutput>
|
||||
|
||||
/**
|
||||
* This method is used when retrieving an account holder in Medusa, allowing you to retrieve
|
||||
* the equivalent account in the third-party payment provider. An account holder is useful to
|
||||
* later save payment methods, such as credit cards, for a customer in the
|
||||
* third-party payment provider using the {@link savePaymentMethod} method.
|
||||
*
|
||||
* @param data - Input data including the details of the account holder to retrieve.
|
||||
* @returns The retrieved account holder. If an error occurs, throw it.
|
||||
*
|
||||
* @since 2.11.0
|
||||
*
|
||||
* @example
|
||||
* import { MedusaError } from "@medusajs/framework/utils"
|
||||
*
|
||||
* class MyPaymentProviderService extends AbstractPaymentProvider<
|
||||
* Options
|
||||
* > {
|
||||
* async retrieveAccountHolder({ id }: RetrieveAccountHolderInput) {
|
||||
*
|
||||
* // assuming you have a client that retrieves the account holder
|
||||
* const providerAccountHolder = await this.client.retrieveAccountHolder({
|
||||
* id
|
||||
* })
|
||||
*
|
||||
* return {
|
||||
* id: providerAccountHolder.id,
|
||||
* data: providerAccountHolder as unknown as Record<string, unknown>
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
retrieveAccountHolder?(
|
||||
data: RetrieveAccountHolderInput
|
||||
): Promise<RetrieveAccountHolderOutput>
|
||||
|
||||
/**
|
||||
* This method is used when creating an account holder in Medusa, allowing you to create
|
||||
* the equivalent account in the third-party payment provider. An account holder is useful to
|
||||
|
||||
@@ -2016,6 +2016,8 @@ describe("defineConfig", function () {
|
||||
process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE = "test-environment"
|
||||
process.env.MEDUSA_CLOUD_API_KEY = "test-api-key"
|
||||
process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT = "test-emails-endpoint"
|
||||
process.env.MEDUSA_CLOUD_PAYMENTS_ENDPOINT = "test-payments-endpoint"
|
||||
process.env.MEDUSA_CLOUD_WEBHOOK_SECRET = "test-webhook-secret"
|
||||
const config = defineConfig()
|
||||
process.env = { ...originalEnv }
|
||||
|
||||
@@ -2112,6 +2114,15 @@ describe("defineConfig", function () {
|
||||
"resolve": "@medusajs/medusa/order",
|
||||
},
|
||||
"payment": {
|
||||
"options": {
|
||||
"cloud": {
|
||||
"api_key": "test-api-key",
|
||||
"endpoint": "test-payments-endpoint",
|
||||
"environment_handle": "test-environment",
|
||||
"sandbox_handle": undefined,
|
||||
"webhook_secret": "test-webhook-secret",
|
||||
},
|
||||
},
|
||||
"resolve": "@medusajs/medusa/payment",
|
||||
},
|
||||
"pricing": {
|
||||
@@ -2165,7 +2176,9 @@ describe("defineConfig", function () {
|
||||
"apiKey": "test-api-key",
|
||||
"emailsEndpoint": "test-emails-endpoint",
|
||||
"environmentHandle": "test-environment",
|
||||
"paymentsEndpoint": "test-payments-endpoint",
|
||||
"sandboxHandle": undefined,
|
||||
"webhookSecret": "test-webhook-secret",
|
||||
},
|
||||
"databaseUrl": "postgres://localhost/medusa-starter-default",
|
||||
"http": {
|
||||
@@ -2197,6 +2210,8 @@ describe("defineConfig", function () {
|
||||
process.env.MEDUSA_CLOUD_SANDBOX_HANDLE = "test-sandbox"
|
||||
process.env.MEDUSA_CLOUD_API_KEY = "test-api-key"
|
||||
process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT = "test-emails-endpoint"
|
||||
process.env.MEDUSA_CLOUD_PAYMENTS_ENDPOINT = "test-payments-endpoint"
|
||||
process.env.MEDUSA_CLOUD_WEBHOOK_SECRET = "test-webhook-secret"
|
||||
const config = defineConfig()
|
||||
process.env = { ...originalEnv }
|
||||
|
||||
@@ -2293,6 +2308,15 @@ describe("defineConfig", function () {
|
||||
"resolve": "@medusajs/medusa/order",
|
||||
},
|
||||
"payment": {
|
||||
"options": {
|
||||
"cloud": {
|
||||
"api_key": "test-api-key",
|
||||
"endpoint": "test-payments-endpoint",
|
||||
"environment_handle": undefined,
|
||||
"sandbox_handle": "test-sandbox",
|
||||
"webhook_secret": "test-webhook-secret",
|
||||
},
|
||||
},
|
||||
"resolve": "@medusajs/medusa/payment",
|
||||
},
|
||||
"pricing": {
|
||||
@@ -2346,7 +2370,9 @@ describe("defineConfig", function () {
|
||||
"apiKey": "test-api-key",
|
||||
"emailsEndpoint": "test-emails-endpoint",
|
||||
"environmentHandle": undefined,
|
||||
"paymentsEndpoint": "test-payments-endpoint",
|
||||
"sandboxHandle": "test-sandbox",
|
||||
"webhookSecret": "test-webhook-secret",
|
||||
},
|
||||
"databaseUrl": "postgres://localhost/medusa-starter-default",
|
||||
"http": {
|
||||
@@ -2378,13 +2404,17 @@ describe("defineConfig", function () {
|
||||
process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE = "test-environment"
|
||||
process.env.MEDUSA_CLOUD_API_KEY = "test-api-key"
|
||||
process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT = "test-emails-endpoint"
|
||||
process.env.MEDUSA_CLOUD_PAYMENTS_ENDPOINT = "test-payments-endpoint"
|
||||
process.env.MEDUSA_CLOUD_WEBHOOK_SECRET = "test-webhook-secret"
|
||||
const config = defineConfig({
|
||||
projectConfig: {
|
||||
http: {} as any,
|
||||
cloud: {
|
||||
environmentHandle: "overriden-environment",
|
||||
apiKey: "overriden-api-key",
|
||||
webhookSecret: "overriden-webhook-secret",
|
||||
emailsEndpoint: "overriden-emails-endpoint",
|
||||
paymentsEndpoint: "overriden-payments-endpoint",
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -2483,6 +2513,15 @@ describe("defineConfig", function () {
|
||||
"resolve": "@medusajs/medusa/order",
|
||||
},
|
||||
"payment": {
|
||||
"options": {
|
||||
"cloud": {
|
||||
"api_key": "overriden-api-key",
|
||||
"endpoint": "overriden-payments-endpoint",
|
||||
"environment_handle": "overriden-environment",
|
||||
"sandbox_handle": undefined,
|
||||
"webhook_secret": "overriden-webhook-secret",
|
||||
},
|
||||
},
|
||||
"resolve": "@medusajs/medusa/payment",
|
||||
},
|
||||
"pricing": {
|
||||
@@ -2536,7 +2575,9 @@ describe("defineConfig", function () {
|
||||
"apiKey": "overriden-api-key",
|
||||
"emailsEndpoint": "overriden-emails-endpoint",
|
||||
"environmentHandle": "overriden-environment",
|
||||
"paymentsEndpoint": "overriden-payments-endpoint",
|
||||
"sandboxHandle": undefined,
|
||||
"webhookSecret": "overriden-webhook-secret",
|
||||
},
|
||||
"databaseUrl": "postgres://localhost/medusa-starter-default",
|
||||
"http": {
|
||||
|
||||
@@ -365,19 +365,16 @@ function normalizeProjectConfig(
|
||||
projectConfig: InputConfig["projectConfig"],
|
||||
{ isCloud }: { isCloud: boolean }
|
||||
): ConfigModule["projectConfig"] {
|
||||
const {
|
||||
http,
|
||||
redisOptions,
|
||||
sessionOptions,
|
||||
cloud,
|
||||
...restOfProjectConfig
|
||||
} = projectConfig || {}
|
||||
const { http, redisOptions, sessionOptions, cloud, ...restOfProjectConfig } =
|
||||
projectConfig || {}
|
||||
|
||||
const mergedCloudOptions: MedusaCloudOptions = {
|
||||
environmentHandle: process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE,
|
||||
sandboxHandle: process.env.MEDUSA_CLOUD_SANDBOX_HANDLE,
|
||||
apiKey: process.env.MEDUSA_CLOUD_API_KEY,
|
||||
webhookSecret: process.env.MEDUSA_CLOUD_WEBHOOK_SECRET,
|
||||
emailsEndpoint: process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT,
|
||||
paymentsEndpoint: process.env.MEDUSA_CLOUD_PAYMENTS_ENDPOINT,
|
||||
...cloud,
|
||||
}
|
||||
const hasCloudOptions = Object.values(mergedCloudOptions).some(
|
||||
@@ -492,7 +489,18 @@ function applyCloudOptionsToModules(
|
||||
...(module.options ?? {}),
|
||||
}
|
||||
break
|
||||
// Will add payment module soon
|
||||
case Modules.PAYMENT:
|
||||
module.options = {
|
||||
cloud: {
|
||||
api_key: config.apiKey,
|
||||
webhook_secret: config.webhookSecret,
|
||||
endpoint: config.paymentsEndpoint,
|
||||
environment_handle: config.environmentHandle,
|
||||
sandbox_handle: config.sandboxHandle,
|
||||
},
|
||||
...(module.options ?? {}),
|
||||
}
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user