Skip to main content

Workflows

processPayoutWebhookActionWorkflow

This documentation provides a reference to the processPayoutWebhookActionWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow processes webhook events from the payout provider (e.g., Stripe Connect) and takes appropriate actions based on the webhook type. Currently, it handles account authorization events by updating the payout account status to active. Source code

Examples

packages/modules/b2c-core/src/subscribers/payout-webhook.ts
import { SubscriberArgs, SubscriberConfig } from "@medusajs/framework";
import {
  PayoutWebhookActionPayload,
  PayoutWebhookEvents,
} from "@mercurjs/framework";
import { PAYOUT_MODULE } from "../modules/payout";
import { PayoutModuleService } from "../modules/payout";
import { processPayoutWebhookActionWorkflow } from "../workflows/payout/workflows";

export default async function payoutWebhookHandler({
  event,
  container,
}: SubscriberArgs<PayoutWebhookActionPayload>) {
  const payoutService: PayoutModuleService = container.resolve(PAYOUT_MODULE);

  const input = event.data;

  // Parse webhook data
  const actionAndData = await payoutService.getWebhookActionAndData(input);

  if (!actionAndData) {
    return;
  }

  await processPayoutWebhookActionWorkflow(container).run({
    input: {
      action: actionAndData.action,
      data: actionAndData.data,
    },
  });
}

export const config: SubscriberConfig = {
  event: PayoutWebhookEvents.ACCOUNT_WEBHOOK_RECEIVED,
  context: {
    subscriberId: "payout-account-webhook-handler",
  },
};

Steps

Input

input
object
required
The webhook action and data.

Output

This workflow does not return a value.

Steps

createPayoutReversalStep

Creates a reversal (refund) for a payout that was previously processed. This is typically used when an order is canceled after the seller has already been paid out. The step handles errors gracefully and returns both the reversal result and error status. Source code

Examples

packages/modules/b2c-core/src/workflows/order/workflows/cancel-order.ts
import { createPayoutReversalStep } from '../../payout/steps'

// ... inside cancelOrderWorkflow

const payoutId = transform({ order }, ({ order }) => {
  return order.payouts && order.payouts[0] ? order.payouts[0].id : null
})

parallelize(
  // ... other steps
  createPayoutReversalStep({
    payout_id: payoutId,
    amount: order.split_order_payment.captured_amount,
    currency_code: order.currency_code
  })
)

Input

input
object
required
The payout reversal details.

Output

object
object

updatePayoutAccountStep

Updates a payout account’s details. This step includes a compensation function that restores the previous account state if a subsequent step in the workflow fails, ensuring data consistency. Source code

Examples

packages/modules/b2c-core/src/workflows/payout/workflows/process-payout-webhook-action.ts
import { PayoutAccountStatus, PayoutWebhookAction } from "@mercurjs/framework";
import { updatePayoutAccountStep } from '../steps'

// ... inside processPayoutWebhookActionWorkflow

when(
  { action: input.action },
  ({ action }) => action === PayoutWebhookAction.ACCOUNT_AUTHORIZED
).then(() => {
  updatePayoutAccountStep({
    id: input.data.account_id,
    status: PayoutAccountStatus.ACTIVE
  })
})

Input

input
UpdatePayoutAccountDTO
required
The payout account updates.

Output

PayoutAccountDTO
PayoutAccountDTO
The updated payout account.