feat: Add support to update account holder (#11499)

This commit is contained in:
Stevche Radevski
2025-02-18 11:04:25 +01:00
committed by GitHub
parent 32ad13813b
commit 99a6ecc12d
6 changed files with 242 additions and 1 deletions

View File

@@ -36,6 +36,8 @@ import {
WebhookActionResult,
CreateAccountHolderOutput,
InitiatePaymentOutput,
UpdateAccountHolderDTO,
UpdateAccountHolderOutput,
} from "@medusajs/framework/types"
import {
BigNumber,
@@ -1027,6 +1029,45 @@ export default class PaymentModuleService
return await this.baseRepository_.serialize(accountHolder)
}
@InjectManager()
async updateAccountHolder(
input: UpdateAccountHolderDTO,
@MedusaContext() sharedContext?: Context
): Promise<AccountHolderDTO> {
if (!input.context?.account_holder) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Missing account holder data while updating account holder."
)
}
let accountHolder: InferEntityType<typeof AccountHolder> | undefined
let providerAccountHolder: UpdateAccountHolderOutput | undefined
providerAccountHolder =
await this.paymentProviderService_.updateAccountHolder(
input.provider_id,
{
context: input.context,
}
)
// The data field can be empty when either the method is not supported or an account holder wasn't updated
// We still want to do the update as we might only be updating the metadata
accountHolder = await this.accountHolderService_.update(
{
id: input.id,
...(providerAccountHolder?.data
? { data: providerAccountHolder.data }
: {}),
metadata: input.metadata,
},
sharedContext
)
return await this.baseRepository_.serialize(accountHolder)
}
@InjectManager()
async deleteAccountHolder(
id: string,

View File

@@ -25,6 +25,8 @@ import {
RefundPaymentOutput,
SavePaymentMethodInput,
SavePaymentMethodOutput,
UpdateAccountHolderInput,
UpdateAccountHolderOutput,
UpdatePaymentInput,
UpdatePaymentOutput,
WebhookActionResult,
@@ -149,6 +151,21 @@ Please make sure that the provider is registered in the container and it is conf
return await provider.createAccountHolder(input)
}
async updateAccountHolder(
providerId: string,
input: UpdateAccountHolderInput
): Promise<UpdateAccountHolderOutput> {
const provider = this.retrieveProvider(providerId)
if (!provider.updateAccountHolder) {
this.#logger.warn(
`Provider ${providerId} does not support updating account holders`
)
return {} as unknown as UpdateAccountHolderOutput
}
return await provider.updateAccountHolder(input)
}
async deleteAccountHolder(
providerId: string,
input: DeleteAccountHolderInput