Edits history of script submission #12737 for ' Create plan (paypal)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paypal = {
      clientId: string;
      clientSecret: string;
    };
    
    async function getToken(auth: Paypal): Promise<string> {
      const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`);
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`,
        },
        body: new URLSearchParams({
          grant_type: "client_credentials",
        }),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`Could not get token: ${response.status} ${text}`);
      }
      const json = await response.json();
      return json.access_token;
    }
    /**
     * Create plan
     * Creates a plan that defines pricing and billing cycle details for subscriptions.
     */
    export async function main(
      auth: Paypal,
      Prefer: string,
      PayPal_Request_Id: string,
      body: {
        product_id: string;
        name: string;
        status?: "CREATED" | "INACTIVE" | "ACTIVE";
        description?: string;
        billing_cycles: {
          pricing_scheme?: {
            version?: number;
            fixed_price?: { currency_code: string; value: string };
            pricing_model?: "VOLUME" | "TIERED";
            tiers?: {
              starting_quantity: string;
              ending_quantity?: string;
              amount: { currency_code: string; value: string };
            }[];
            create_time?: string;
            update_time?: string;
          };
          frequency: {
            interval_unit: "DAY" | "WEEK" | "MONTH" | "YEAR";
            interval_count?: number;
          };
          tenure_type: "REGULAR" | "TRIAL";
          sequence: number;
          total_cycles?: number;
        }[];
        payment_preferences: {
          auto_bill_outstanding?: false | true;
          setup_fee?: { currency_code: string; value: string };
          setup_fee_failure_action?: "CONTINUE" | "CANCEL";
          payment_failure_threshold?: number;
        };
        taxes?: { percentage: string; inclusive?: false | true };
        quantity_supported?: false | true;
      },
    ) {
      const token = await getToken(auth);
      const url = new URL(`https://api-m.paypal.com/v1/billing/plans`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Prefer: Prefer,
          "PayPal-Request-Id": PayPal_Request_Id,
          "Content-Type": "application/json",
          Authorization: "Bearer " + token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago

  • bun
    //native
    type Paypal = {
      token: string;
    };
    /**
     * Create plan
     * Creates a plan that defines pricing and billing cycle details for subscriptions.
     */
    export async function main(
      auth: Paypal,
      Prefer: string,
      PayPal_Request_Id: string,
      body: {
        product_id: string;
        name: string;
        status?: "CREATED" | "INACTIVE" | "ACTIVE";
        description?: string;
        billing_cycles: {
          pricing_scheme?: {
            version?: number;
            fixed_price?: { currency_code: string; value: string };
            pricing_model?: "VOLUME" | "TIERED";
            tiers?: {
              starting_quantity: string;
              ending_quantity?: string;
              amount: { currency_code: string; value: string };
            }[];
            create_time?: string;
            update_time?: string;
          };
          frequency: {
            interval_unit: "DAY" | "WEEK" | "MONTH" | "YEAR";
            interval_count?: number;
          };
          tenure_type: "REGULAR" | "TRIAL";
          sequence: number;
          total_cycles?: number;
        }[];
        payment_preferences: {
          auto_bill_outstanding?: false | true;
          setup_fee?: { currency_code: string; value: string };
          setup_fee_failure_action?: "CONTINUE" | "CANCEL";
          payment_failure_threshold?: number;
        };
        taxes?: { percentage: string; inclusive?: false | true };
        quantity_supported?: false | true;
      },
    ) {
      const url = new URL(`https://api-m.paypal.com/v1/billing/plans`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Prefer: Prefer,
          "PayPal-Request-Id": PayPal_Request_Id,
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago