Edits history of script submission #12777 for ' Revise plan or quantity of subscription (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;
    }
    /**
     * Revise plan or quantity of subscription
     * Updates the quantity of the product or service in a subscription. You can also use this method to switch the plan and update the `shipping_amount`, `shipping_address` values for the subscription. This type of update requires the buyer's consent.
     */
    export async function main(
      auth: Paypal,
      id: string,
      body: {
        plan_id?: string;
        quantity?: string;
        shipping_amount?: { currency_code: string; value: string };
        shipping_address?: {
          name?: {
            prefix?: string;
            given_name?: string;
            surname?: string;
            middle_name?: string;
            suffix?: string;
            alternate_full_name?: string;
            full_name?: string;
          };
          type?: "SHIPPING" | "PICKUP_IN_PERSON";
          address?: {
            address_line_1?: string;
            address_line_2?: string;
            address_line_3?: string;
            admin_area_4?: string;
            admin_area_3?: string;
            admin_area_2?: string;
            admin_area_1?: string;
            postal_code?: string;
            country_code: string;
            address_details?: {
              street_number?: string;
              street_name?: string;
              street_type?: string;
              delivery_service?: string;
              building_name?: string;
              sub_building?: string;
            };
          };
        };
        application_context?: {
          brand_name?: string;
          locale?: string;
          shipping_preference?:
            | "GET_FROM_FILE"
            | "NO_SHIPPING"
            | "SET_PROVIDED_ADDRESS";
          user_action?: "CONTINUE" | "SUBSCRIBE_NOW";
          payment_method?: {
            payer_selected?: string;
            payee_preferred?: "UNRESTRICTED" | "IMMEDIATE_PAYMENT_REQUIRED";
            standard_entry_class_code?: "TEL" | "WEB" | "CCD" | "PPD";
          };
          return_url: string;
          cancel_url: string;
        };
        plan?: {
          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;
            };
            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 };
        };
      },
    ) {
      const token = await getToken(auth);
      const url = new URL(
        `https://api-m.paypal.com/v1/billing/subscriptions/${id}/revise`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "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;
    };
    /**
     * Revise plan or quantity of subscription
     * Updates the quantity of the product or service in a subscription. You can also use this method to switch the plan and update the `shipping_amount`, `shipping_address` values for the subscription. This type of update requires the buyer's consent.
     */
    export async function main(
      auth: Paypal,
      id: string,
      body: {
        plan_id?: string;
        quantity?: string;
        shipping_amount?: { currency_code: string; value: string };
        shipping_address?: {
          name?: {
            prefix?: string;
            given_name?: string;
            surname?: string;
            middle_name?: string;
            suffix?: string;
            alternate_full_name?: string;
            full_name?: string;
          };
          type?: "SHIPPING" | "PICKUP_IN_PERSON";
          address?: {
            address_line_1?: string;
            address_line_2?: string;
            address_line_3?: string;
            admin_area_4?: string;
            admin_area_3?: string;
            admin_area_2?: string;
            admin_area_1?: string;
            postal_code?: string;
            country_code: string;
            address_details?: {
              street_number?: string;
              street_name?: string;
              street_type?: string;
              delivery_service?: string;
              building_name?: string;
              sub_building?: string;
            };
          };
        };
        application_context?: {
          brand_name?: string;
          locale?: string;
          shipping_preference?:
            | "GET_FROM_FILE"
            | "NO_SHIPPING"
            | "SET_PROVIDED_ADDRESS";
          user_action?: "CONTINUE" | "SUBSCRIBE_NOW";
          payment_method?: {
            payer_selected?: string;
            payee_preferred?: "UNRESTRICTED" | "IMMEDIATE_PAYMENT_REQUIRED";
            standard_entry_class_code?: "TEL" | "WEB" | "CCD" | "PPD";
          };
          return_url: string;
          cancel_url: string;
        };
        plan?: {
          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;
            };
            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 };
        };
      },
    ) {
      const url = new URL(
        `https://api-m.paypal.com/v1/billing/subscriptions/${id}/revise`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "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