Edits history of script submission #22543 for ' Create Customer (stripe)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Customer
     * Create a customer. `payment_method` attaches an existing PaymentMethod id. `extra_params` passes any other documented field through as-is.
     */
    export async function main(
      auth: RT.Stripe,
      name: string | undefined,
      email: string | undefined,
      phone: string | undefined,
      description: string | undefined,
      address:
        | {
            line1?: string
            line2?: string
            city?: string
            state?: string
            postal_code?: string
            country?: string
          }
        | undefined,
      payment_method: string | undefined,
      metadata: { [key: string]: string } | undefined,
      extra_params: { [key: string]: any } | undefined
    ) {
      const response = await fetch(`https://api.stripe.com/v1/customers`, {
        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({
          name,
          email,
          phone,
          description,
          address,
          payment_method,
          metadata,
          ...extra_params,
        }),
      })
    
      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

  • bunnative
    //native
    
    type Stripe = {
      token: string;
    };
    export async function main(stripe_con: Stripe) {
      const STRIPE_CREATE_CUSTOMER_URL = `https://api.stripe.com/v1/customers`;
    
      const token = stripe_con["token"];
    
      const response = await fetch(STRIPE_CREATE_CUSTOMER_URL, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + token,
          "Content-Type": "application/json",
        },
      });
    
      return await response.json();
    }
    

    Submitted by hugo989 136 days ago