Edits history of script submission #23792 for ' List Payouts (stripe)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Payouts
     * List payouts, newest first, filtered by status, destination bank account/card or creation date. `created_gte`/`created_lte` take ISO 8601 date-times. Returns one page (`limit` 1-100, default 10); pass the last object's id as `starting_after` for the next page while `has_more` is true.
     */
    export async function main(
      auth: RT.Stripe,
      status: "pending" | "paid" | "failed" | "canceled" | undefined,
      destination: string | undefined,
      created_gte: string | undefined,
      created_lte: string | undefined,
      limit: number | undefined,
      starting_after: string | undefined,
      ending_before: string | undefined
    ) {
      const url = new URL(`https://api.stripe.com/v1/payouts`)
      url.search = toForm({
        status,
        destination,
        created: { gte: ts(created_gte), lte: ts(created_lte) },
        limit,
        starting_after,
        ending_before,
      }).toString()
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
          "Stripe-Version": "2025-03-31.basil",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    
    // Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
    function toForm(
      params: { [key: string]: any },
      prefix = "",
      out = new URLSearchParams()
    ) {
      for (const [key, value] of Object.entries(params)) {
        if (value == null || value === "") continue
        const name = prefix ? `${prefix}[${key}]` : key
        if (typeof value === "object") toForm(value, name, out)
        else out.append(name, String(value))
      }
      return out
    }
    
    const ts = (iso: string | undefined) =>
      iso ? Math.floor(new Date(iso).getTime() / 1000) : undefined
    

    Submitted by hugo989 3 hours ago