تقنية

كيفية استخدام واجهة برمجة تطبيقات مدفوعات الشريط مع البرنامج النصي لتطبيقات Google


يستخدم مولد رابط مدفوعات الشريط واجهة برمجة تطبيقات روابط دفع الشريط مع برنامج تطبيق تطبيقات Google لإنشاء روابط الدفع بكميات كبيرة.

يستخدم مفتاح API Stripe للاتصال بشريط وإنشاء روابط الدفع. تتم كتابة الروابط التي تم إنشاؤها في ورقة Google وإضافتها أيضًا إلى ذاكرة التخزين المؤقت للنص. إذا كنت تستخدم نفس اسم المنتج وكمية ، فستحصل على نفس الرابط القابل لإعادة الاستخدام من ذاكرة التخزين المؤقت نفسها.

يولد الرمز روابط الدفع للمدفوعات لمرة واحدة ولكن يمكن تمديدها لدعم المدفوعات المتكررة وكذلك القسائم ومعدلات الضرائب.

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

const StripePaymentsAPI = {
  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 = 'https://digitalinspiration.com';
    const STRIPE_CANCEL_URL = 'https://digitalinspiration.com';
    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;
  },
};



Source link


اكتشاف المزيد من مرابع التكنولوجيا

اشترك للحصول على أحدث التدوينات المرسلة إلى بريدك الإلكتروني.

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى

اكتشاف المزيد من مرابع التكنولوجيا

اشترك الآن للاستمرار في القراءة والحصول على حق الوصول إلى الأرشيف الكامل.

Continue reading