Edits history of script submission #12736 for ' Create payment token for a given payment source (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 payment token for a given payment source
     * Creates a Payment Token from the given payment source and adds it to the Vault of the associated customer.
     */
    export async function main(
      auth: Paypal,
      PayPal_Request_Id: string,
      body: {
        customer?: { id?: string };
        payment_source: {
          card?: {
            id?: string;
            name?: string;
            number?: string;
            expiry?: string;
            security_code?: string;
            last_digits?: string;
            card_type?:
              | "VISA"
              | "MASTERCARD"
              | "DISCOVER"
              | "AMEX"
              | "SOLO"
              | "JCB"
              | "STAR"
              | "DELTA"
              | "SWITCH"
              | "MAESTRO"
              | "CB_NATIONALE"
              | "CONFIGOGA"
              | "CONFIDIS"
              | "ELECTRON"
              | "CETELEM"
              | "CHINA_UNION_PAY";
            type?: "CREDIT" | "DEBIT" | "PREPAID" | "STORE" | "UNKNOWN";
            brand?:
              | "VISA"
              | "MASTERCARD"
              | "DISCOVER"
              | "AMEX"
              | "SOLO"
              | "JCB"
              | "STAR"
              | "DELTA"
              | "SWITCH"
              | "MAESTRO"
              | "CB_NATIONALE"
              | "CONFIGOGA"
              | "CONFIDIS"
              | "ELECTRON"
              | "CETELEM"
              | "CHINA_UNION_PAY";
            billing_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;
              };
            };
          } & {
            verification_method?: string;
            experience_context?: {
              brand_name?: string;
              locale?: string;
              return_url?: string;
              cancel_url?: string;
              shipping_preference?: string;
              vault_instruction?: string;
            };
          };
          token?: { id: string; type: "BILLING_AGREEMENT"; attributes?: {} };
        };
        metadata?: unknown;
      },
    ) {
      const token = await getToken(auth);
      const url = new URL(
        `https://api-m.paypal.com/v3/vault/payment-tokens`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "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 payment token for a given payment source
     * Creates a Payment Token from the given payment source and adds it to the Vault of the associated customer.
     */
    export async function main(
      auth: Paypal,
      PayPal_Request_Id: string,
      body: {
        customer?: { id?: string };
        payment_source: {
          card?: {
            id?: string;
            name?: string;
            number?: string;
            expiry?: string;
            security_code?: string;
            last_digits?: string;
            card_type?:
              | "VISA"
              | "MASTERCARD"
              | "DISCOVER"
              | "AMEX"
              | "SOLO"
              | "JCB"
              | "STAR"
              | "DELTA"
              | "SWITCH"
              | "MAESTRO"
              | "CB_NATIONALE"
              | "CONFIGOGA"
              | "CONFIDIS"
              | "ELECTRON"
              | "CETELEM"
              | "CHINA_UNION_PAY";
            type?: "CREDIT" | "DEBIT" | "PREPAID" | "STORE" | "UNKNOWN";
            brand?:
              | "VISA"
              | "MASTERCARD"
              | "DISCOVER"
              | "AMEX"
              | "SOLO"
              | "JCB"
              | "STAR"
              | "DELTA"
              | "SWITCH"
              | "MAESTRO"
              | "CB_NATIONALE"
              | "CONFIGOGA"
              | "CONFIDIS"
              | "ELECTRON"
              | "CETELEM"
              | "CHINA_UNION_PAY";
            billing_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;
              };
            };
          } & {
            verification_method?: string;
            experience_context?: {
              brand_name?: string;
              locale?: string;
              return_url?: string;
              cancel_url?: string;
              shipping_preference?: string;
              vault_instruction?: string;
            };
          };
          token?: { id: string; type: "BILLING_AGREEMENT"; attributes?: {} };
        };
        metadata?: unknown;
      },
    ) {
      const url = new URL(
        `https://api-m.paypal.com/v3/vault/payment-tokens`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "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