Edits history of script submission #23672 for ' Update Payout (stripe)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Payout
     * Update a payout's metadata (the only editable field); keys are merged into the existing metadata.
     */
    export async function main(
      auth: RT.Stripe,
      payout_id: string,
      metadata: { [key: string]: string }
    ) {
      const response = await fetch(
        `https://api.stripe.com/v1/payouts/${payout_id}`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/x-www-form-urlencoded",
            Accept: "application/json",
            "Stripe-Version": "2025-03-31.basil",
          },
          body: toForm({ metadata }),
        }
      )
    
      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
    }
    

    Submitted by hugo989 3 hours ago