当前位置:优学网  >  在线题库

使用谷歌脚本生成周期性支付链接条

发表时间:2022-07-19 00:20:08 阅读:59

我是API领域的初学者,我正在努力开发新技能.我在网上找到了这个代码.我一般理解,但我想添加选项,添加一个描述到我的付款链接生成,但我不习惯这种格式.我搜索了Stripe文档,但Google脚本需要进行转换才能正常工作,因此它在Stripe文档中看起来不一样.

最后,我想创建一个经常性付款链接,而不是一次性付款链接.

我不知道我是否足够清楚.致以最诚挚的问候

/**
 *
 *  Author   :  Amit Agarwal
 *  Email    :  amit@labnol.org
 *  Website  :  https://digitalinspiration.com/
 *  License. :  MIT Attribution required
 *
 * */

const StripeAPI = {
  getCache(key) {
    return CacheService.getScriptCache().get(key);
  },

  setCache(key, value) {
    CacheService.getScriptCache().put(key, value, 21600);
  },

  convertPayload(params = {}) {
    return Object.entries(params)
      .map(([key, value]) => [encodeURIComponent(key), encodeURIComponent(value)].join('='))
      .join('&');
  },

  getData(endpoint, params) {
    const response = UrlFetchApp.fetch(`${endpoint}?${this.convertPayload(params)}`, {
      headers: {
        Authorization: `Bearer ${STRIPE_API_KEY}`,
      },
      muteHttpExceptions: true,
    });
    return JSON.parse(response);
  },

  postData(endpoint, params) {
    const response = UrlFetchApp.fetch(endpoint, {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${STRIPE_API_KEY}`,
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      muteHttpExceptions: true,
      payload: this.convertPayload(params),
    });

    return JSON.parse(response);
  },

  getProductId(name) {
    const productId = this.getCache(name);
    if (productId) return productId;
    const api = 'https://api.stripe.com/v1/products';
    const { data = [] } = this.getData(api, { limit: 100 });
    const { id: newProductId } =
      data.find(({ name: productName }) => productName === name) || this.postData(api, { name });
    this.setCache(name, newProductId);
    return newProductId;
  },

  getPriceId(name, price = '1234', currency = 'USD') {
    const product_id = this.getProductId(name);
    const key = product_id + price + currency;
    const priceId = this.getCache(key);
    if (priceId) return priceId;
    const api = 'https://api.stripe.com/v1/prices';
    const { data = [] } = this.getData(api, { limit: 100, currency, product: product_id });
    const { id: newPriceId } =
      data.find(({ unit_amount }) => String(unit_amount) === String(price)) ||
      this.postData(api, { currency, product: product_id, unit_amount: price });
    this.setCache(key, newPriceId);
    return newPriceId;
  },

  createLink(name, amount, currency) {
    const key = `link${amount}${currency}${name}`;
    const paymentLink = this.getCache(key);
    if (paymentLink) return paymentLink;
    const priceId = this.getPriceId(name, Math.ceil(amount * 100), currency);
    const { url } = this.postData('https://api.stripe.com/v1/payment_links', {
      'line_items[0][price]': priceId,
      'line_items[0][quantity]': 1,
    });
    this.setCache(key, url);
    return url;
  },

  createSession(name, amount, currency) {
    const STRIPE_SUCCESS_URL = 'youtube.com';
    const STRIPE_CANCEL_URL = 'bing.fr';
    const key = `session${amount}${currency}${name}`;
    const sessionLink = this.getCache(key);
    if (sessionLink) return sessionLink;
    const { url } = this.postData('https://api.stripe.com/v1/checkout/sessions', {
      cancel_url: STRIPE_CANCEL_URL,
      success_url: STRIPE_SUCCESS_URL,
      mode: 'payment',
      billing_address_collection: 'required',
      'payment_method_types[]': 'card',
      'line_items[0][price_data][currency]': currency,
      'line_items[0][price_data][product_data][name]': name,
      'line_items[0][price_data][unit_amount]': Math.ceil(amount * 100),
      'line_items[0][quantity]': 1,
    });
    this.setCache(key, url);
    return url;
  },
};
🎖️ 优质答案
相关问题