import {
buildPayoutBody,
mutationPreview,
normalizePayoutSummary,
type MutationGuardInput,
type PayPalAuthInput,
type PayoutItemInput,
type PayoutRecipientType,
} from './paypal-helpers.ts'
import { paypalCreatePayout, paypalGetPayout, paypalGetPayoutItem } from './paypal-core.ts'
export type { PayoutItemInput, PayoutRecipientType }
export type CreatePayoutInput = MutationGuardInput & {
receiver?: string
amount?: string | number
currencyCode?: string
recipientType?: PayoutRecipientType
note?: string
emailSubject?: string
emailMessage?: string
senderBatchId?: string
senderItemId?: string
recipientWallet?: 'PAYPAL' | 'VENMO'
items?: Array<PayoutItemInput>
requestId?: string
includeRaw?: boolean
}
export type GetPayoutInput = PayPalAuthInput & {
id: string
includeRaw?: boolean
}
export type GetPayoutItemInput = PayPalAuthInput & {
itemId: string
includeRaw?: boolean
}
export { buildPayoutBody, buildPayoutItem } from './paypal-helpers.ts'
/**
* Create a PayPal Payouts batch (reimbursement / mass pay).
* Pass `dryRun: true` to preview. Live send requires `confirm: true`.
*/
export async function createPayout(input: CreatePayoutInput) {
const body = buildPayoutBody(input)
const preview = mutationPreview(input, {
action: 'create payout batch',
method: 'POST',
path: '/v1/payments/payouts',
body,
requireConfirm: true,
})
if (preview) return preview
const raw = await paypalCreatePayout({ ...input, body, requestId: input.requestId })
const summary = normalizePayoutSummary(raw)
return input.includeRaw ? { summary, raw } : summary
}
/**
* Read one payout batch by payout_batch_id.
*/
export async function getPayout(input: GetPayoutInput) {
if (!input.id) throw new Error('getPayout requires id.')
const raw = await paypalGetPayout({ ...input, payoutBatchId: input.id })
const summary = normalizePayoutSummary(raw)
return input.includeRaw ? { summary, raw } : summary
}
/**
* Read one payout item by payout_item_id.
*/
export async function getPayoutItem(input: GetPayoutItemInput) {
if (!input.itemId) throw new Error('getPayoutItem requires itemId.')
const raw = await paypalGetPayoutItem({ ...input, payoutItemId: input.itemId })
return input.includeRaw ? { raw } : raw
}
export default async function payouts(input: CreatePayoutInput) {
return await createPayout(input)
}