fix(core-flows,workflows-sdk): compensate account holders only when its created (#12825)

* fix(core-flows,workflows-sdk): compensate account holders only when its created

* chore: remove only
This commit is contained in:
Riqwan Thamir
2025-06-26 12:30:08 +02:00
committed by GitHub
parent 10dff3e266
commit 9a62f359f1
5 changed files with 166 additions and 21 deletions
@@ -1,14 +1,14 @@
import {
IPaymentModuleService,
CreateAccountHolderDTO,
IPaymentModuleService,
} from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
import { isPresent, Modules } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
export const createPaymentAccountHolderStepId = "create-payment-account-holder"
/**
* This step creates the account holder in the payment provider.
*
*
* @example
* const accountHolder = createPaymentAccountHolderStep({
* provider_id: "pp_stripe_stripe",
@@ -27,7 +27,13 @@ export const createPaymentAccountHolderStep = createStep(
const accountHolder = await service.createAccountHolder(data)
return new StepResponse(accountHolder, accountHolder)
// createAccountHolder is an idempotent operation.
// We pass the account holder to the compensation step if it was actually created to avoid deleting the existing account holder.
const createdAccountHolder = isPresent(data.context.account_holder)
? null
: accountHolder
return new StepResponse(accountHolder, createdAccountHolder)
},
async (createdAccountHolder, { container }) => {
if (!createdAccountHolder) {
@@ -82,7 +82,7 @@ export const createPaymentSessionsWorkflow = createWorkflow(
list: false,
}).config({ name: "get-payment-collection" })
const { paymentCustomer, accountHolder } = when(
const { paymentCustomer, accountHolder, existingAccountHolder } = when(
"customer-id-exists",
{ input },
(data) => {
@@ -138,20 +138,14 @@ export const createPaymentSessionsWorkflow = createWorkflow(
const accountHolder = createPaymentAccountHolderStep(accountHolderInput)
return { paymentCustomer, accountHolder }
return { paymentCustomer, accountHolder, existingAccountHolder }
})
when(
"account-holder-created",
{ paymentCustomer, accountHolder, input },
(data) => {
return (
!isPresent(
data.paymentCustomer?.account_holders.find(
(ac) => ac.provider_id === data.input.provider_id
)
) && isPresent(data.accountHolder)
)
{ paymentCustomer, accountHolder, input, existingAccountHolder },
({ existingAccountHolder, accountHolder }) => {
return !isPresent(existingAccountHolder) && isPresent(accountHolder)
}
).then(() => {
createRemoteLinkStep([