Skip to main content

Workflows

markSplitOrderPaymentsAsCapturedWorkflow

This documentation provides a reference to the markSplitOrderPaymentsAsCapturedWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow marks all split order payments associated with a payment collection as captured. It updates the payment status to “captured” and sets the captured amount equal to the authorized amount. This is typically triggered after a successful payment capture. Source code

Examples

packages/modules/b2c-core/src/subscribers/split-payment-payment-captured.ts
import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
import { Modules, PaymentEvents } from '@medusajs/framework/utils'
import { markSplitOrderPaymentsAsCapturedWorkflow } from '../workflows/split-order-payment/workflows'

export default async function paymentCapturedHandler({
  event,
  container
}: SubscriberArgs<{ id: string }>) {
  const payment_id = event.data.id
  const paymentService = container.resolve(Modules.PAYMENT)

  const payment = await paymentService.retrievePayment(payment_id, {
    relations: ['payment_collection']
  })

  await markSplitOrderPaymentsAsCapturedWorkflow.run({
    container,
    input: payment.payment_collection_id
  })
}

export const config: SubscriberConfig = {
  event: PaymentEvents.CAPTURED,
  context: {
    subscriberId: 'split-payment-payment-captured-handler'
  }
}

Steps

  • useQueryGraphStep: Fetches all split order payments for the payment collection.
  • updateSplitOrderPaymentsStep: Updates the payments to set status to “captured” and captured_amount to authorized_amount.

Input

payment_collection_id
string
required
The ID of the payment collection whose split order payments should be marked as captured.

Output

SplitOrderPaymentDTO[]
SplitOrderPaymentDTO[]
Array of updated split order payments.

partialPaymentRefundWorkflow

This documentation provides a reference to the partialPaymentRefundWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow processes a partial refund for a split order payment. It validates the refund amount, processes the refund with the payment provider, and adds a refund transaction to the order. This workflow is nested within the refundSplitOrderPaymentWorkflow. Source code

Examples

packages/modules/b2c-core/src/workflows/split-order-payment/workflows/refund-split-order-payment.ts
import { partialPaymentRefundWorkflow } from './partial-payment-refund'

export const refundSplitOrderPaymentWorkflow = createWorkflow(
  {
    name: 'refund-split-order-payment'
  },
  function (input: RefundSplitOrderPaymentsDTO) {
    const updatePayload = validateRefundSplitOrderPaymentStep(input)
    const splitOrderPayment = updateSplitOrderPaymentsStep(
      transform(updatePayload, (updatePayload) => [updatePayload])
    )
    partialPaymentRefundWorkflow.runAsStep({ input })
    return new WorkflowResponse(splitOrderPayment)
  }
)

Steps

  • selectAndValidatePaymentRefundStep: Validates the refund amount and retrieves payment details.
  • refundPaymentsStep: Processes the refund with the payment provider using Medusa’s core step.
  • addOrderTransactionStep: Adds a refund transaction record to the order.

Input

input
RefundSplitOrderPaymentsDTO
required
The refund details.

Output

PaymentDTO[]
PaymentDTO[]
Array of refunded payments.

refundSplitOrderPaymentWorkflow

This documentation provides a reference to the refundSplitOrderPaymentWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow refunds a split order payment by validating the refund, updating the split payment record, and processing the actual refund through the payment provider. It updates the payment status to “partially_refunded” or “refunded” based on the remaining amount. Source code

Examples

packages/modules/b2c-core/src/workflows/order/workflows/cancel-order.ts
import { refundSplitOrderPaymentWorkflow } from '../../split-order-payment/workflows'

// ... inside cancelOrderWorkflow

parallelize(
  deleteReservationsByLineItemsStep(lineItemIds),
  cancelOrdersStep({ orderIds: [order.id] }),
  refundSplitOrderPaymentWorkflow.runAsStep({
    input: {
      id: order.split_order_payment.id,
      amount: order.split_order_payment.captured_amount
    }
  }),
  createPayoutReversalStep({
    payout_id: payoutId,
    amount: order.split_order_payment.captured_amount,
    currency_code: order.currency_code
  }),
  emitEventStep({
    eventName: OrderWorkflowEvents.CANCELED,
    data: { id: order.id }
  })
)

Steps

Input

input
RefundSplitOrderPaymentsDTO
required
The refund details.

Output

SplitOrderPaymentDTO[]
SplitOrderPaymentDTO[]
Array containing the updated split order payment.

Steps

createSplitOrderPaymentsStep

Creates split order payment records for each order in a multi-vendor cart and links them to their respective orders. If an error occurs after creation, the step’s compensation function automatically deletes the created payments and their links. Source code

Examples

packages/modules/b2c-core/src/workflows/cart/workflows/split-and-complete-cart.ts
import { createSplitOrderPaymentsStep } from "../../split-order-payment/steps";

// ... inside splitAndCompleteCartWorkflow

const splitPaymentsToCreate = transform(
  { createdOrders, payment },
  ({ createdOrders, payment }) => {
    return createdOrders.map((order) => ({
      order_id: order.id,
      status: "pending",
      currency_code: order.currency_code,
      authorized_amount: MathBN.convert(
        order.summary?.accounting_total || 0
      ).toNumber(),
      payment_collection_id: payment!.payment_collection_id,
    }));
  }
);

parallelize(
  createSplitOrderPaymentsStep(splitPaymentsToCreate),
  // ... other parallel steps
);

Input

input
CreateSplitOrderPaymentsDTO[]
required
Array of split order payments to create.

Output

SplitOrderPaymentDTO[]
SplitOrderPaymentDTO[]
Array of created split order payments.

selectAndValidatePaymentRefundStep

Validates that a refund amount is valid for a split order payment by checking the refundable amount against captures and previous refunds. Returns the payment details needed for processing the refund. Source code

Input

input
RefundSplitOrderPaymentsDTO
required
The refund validation details.

Output

object
object

updateSplitOrderPaymentsStep

Updates split order payment records. This step includes a compensation function that restores the previous payment state if a subsequent step in the workflow fails. Source code

Input

input
UpdateSplitOrderPaymentsDTO[]
required
Array of split order payment updates.

Output

SplitOrderPaymentDTO[]
SplitOrderPaymentDTO[]
Array of updated split order payments.

validateRefundSplitOrderPaymentStep

Validates a refund request by checking that the refund amount is positive and does not exceed the refundable amount (captured - already refunded). Calculates the new status based on whether the full amount is being refunded. Source code

Input

input
RefundSplitOrderPaymentsDTO
required
The refund validation details.

Output

UpdateSplitOrderPaymentsDTO
UpdateSplitOrderPaymentsDTO
The validated update payload.