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
@@ -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,
@@ -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
@@ -26,6 +26,8 @@ import {
RetrievePaymentOutput,
SavePaymentMethodInput,
SavePaymentMethodOutput,
UpdateAccountHolderInput,
UpdateAccountHolderOutput,
UpdatePaymentInput,
UpdatePaymentOutput,
WebhookActionResult,
@@ -378,6 +380,66 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
}
}
async updateAccountHolder({
context,
}: UpdateAccountHolderInput): Promise<UpdateAccountHolderOutput> {
const { account_holder, customer, idempotency_key } = context
if (!account_holder?.data?.id) {
throw this.buildError(
"No account holder in context",
new Error("No account holder provided while updating account holder")
)
}
// If no customer context was provided, we simply don't update anything within the provider
if (!customer) {
return {}
}
const accountHolderId = account_holder.data.id as string
const shipping = customer.billing_address
? ({
address: {
city: customer.billing_address.city,
country: customer.billing_address.country_code,
line1: customer.billing_address.address_1,
line2: customer.billing_address.address_2,
postal_code: customer.billing_address.postal_code,
state: customer.billing_address.province,
},
} as Stripe.CustomerCreateParams.Shipping)
: undefined
try {
const stripeCustomer = await this.stripe_.customers.update(
accountHolderId,
{
email: customer.email,
name:
customer.company_name ||
`${customer.first_name ?? ""} ${customer.last_name ?? ""}`.trim() ||
undefined,
phone: customer.phone as string | undefined,
...shipping,
},
{
idempotencyKey: idempotency_key,
}
)
return {
data: stripeCustomer as unknown as Record<string, unknown>,
}
} catch (e) {
throw this.buildError(
"An error occurred in updateAccountHolder when updating a Stripe customer",
e
)
}
}
async deleteAccountHolder({
context,
}: DeleteAccountHolderInput): Promise<DeleteAccountHolderOutput> {