تقنية
كيفية استخدام واجهة برمجة تطبيقات Stripe Payments مع Google Apps Script

يستخدم Stripe Payments Link Generator واجهة برمجة تطبيقات Stripe Payment Links مع Google Apps Script لإنشاء روابط الدفع بشكل مجمّع.
ويستخدم مفتاح Stripe 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;
},
};
اكتشاف المزيد من مرابع التكنولوجيا
اشترك للحصول على أحدث التدوينات المرسلة إلى بريدك الإلكتروني.