تقنية

كيفية استيراد معاملات PayPal إلى أوراق Google


سيوضح لك هذا البرنامج التعليمي كيفية استيراد معاملات PayPal إلى أوراق Google بمساعدة Script من Google Apps. يمكنك اختيار استيراد مدفوعات PayPal القياسية ، أو مدفوعات الاشتراك المتكررة ، أو التبرعات ، أو حتى المبالغ المستردة وتراجعات الرسوم في أوراق Google.

بمجرد استيراد البيانات إلى أوراق Google ، يمكنك تصديرها إلى ملف CSV واستيرادها إلى برنامج محاسبة QuickBooks. يمكن لمستخدمي Tally في الهند تصدير معاملات PayPal من أوراق Google إلى تنسيق XML واستيرادها بالجملة إلى حصيلة.

انظر أيضًا: أتمتة PayPal مع جوجل نماذج

استيراد معاملات PayPal في أوراق Google

في هذا المثال ، سنقوم باستيراد قائمة المانحين في أوراق Google التي تقدموا بالتبرعات من خلال PayPal.

1. إنشاء بيانات اعتماد API داخل PayPal

تسجيل الدخول إلى لوحة معلومات PayPal Developer الخاصة بك (Developer.Paypal.com) وإنشاء تطبيق جديد في الوضع المباشر. امنح تطبيقك اسمًا – Transaction Importer for Google Sheets وانقر فوق زر إنشاء التطبيق.

ستقوم PayPal بإنشاء معرف عميل ومفتاح سري للعميل ستحتاجه في خطوة لاحقة. ضمن قسم إعدادات التطبيق المباشر ، تحقق من Transaction Search الخيار وإيقاف تشغيل جميع الخيارات الأخرى لأننا نريد فقط أن يسرد مفاتيح API المعاملات وليس لها وظائف أخرى. انقر فوق حفظ للمتابعة.

بيانات اعتماد حساب PayPal

2. إنشاء مشروع Google Sheets

اذهب إلى sheets.new لإنشاء ورقة Google جديدة. انتقل إلى قائمة الامتدادات واختر برنامج Apps Script لفتح محرر برنامج Apps Script.

نسخ الصغار رمز في المحرر. تذكر أن تحل محل رمز المعاملة برمز الخاص بك. يمكنك استخدام T0002 للاشتراكات PayPal ، T0014 لمدفوعات التبرع ، أو T1107 لاسترداد PayPal واسترداد الرسوم.

ال /* @OnlyCurrentDoc */ التعليق هو تعليق برنامج Script Google الذي يخبر Script Google Apps بتشغيل الرمز داخل ورقة Google الحالية ولا يتطلب الوصول إلى أي جدول بيانات آخر في Google Drive.

/* @OnlyCurrentDoc */
/* Author: digitalinspiration.com */

const TRANSACTION_TYPE = 'T0001';

// Enter your own PayPal Client ID and Client Secret key
const PAYPAL_CLIENT_ID = '<YOUR_PAYPAL_CLIENT_ID>';
const PAYPAL_CLIENT_SECRET = '<YOUR_PAYPAL_CLIENT_SECRET>';

// Enter start and end dates in the format YYYY-MM-DD
const START_DATE = '2022-03-01';
const END_DATE = '2022-03-15';

// Generate the PayPal access token
const getPayPalAccessToken_ = () => {
  const credentials = `${PAYPAL_CLIENT_ID}:${PAYPAL_CLIENT_SECRET}`;
  const headers = {
    Authorization: ` Basic ${Utilities.base64Encode(credentials)}`,
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'Accept-Language': 'en_US'
  };

  const options = {
    method: 'POST',
    headers,
    contentType: 'application/x-www-form-urlencoded',
    payload: { grant_type: 'client_credentials' }
  };

  const request = UrlFetchApp.fetch('https://api.paypal.com/v1/oauth2/token', options);
  const { access_token } = JSON.parse(request);

  return access_token;
};

// Append the query parameters to the PayPal API URL
const buildAPIUrl_ = (queryParams) => {
  const baseUrl = [`https://api-m.paypal.com/v1/reporting/transactions`];
  Object.entries(queryParams).forEach(([key, value], index) => {
    const prefix = index === 0 ? '?' : '&';
    baseUrl.push(`${prefix}${key}=${value}`);
  });
  return baseUrl.join('');
};

// Fetch the list of PayPal transaction
const fetchTransactionBatchFromPayPal = (queryParams) => {
  const options = {
    headers: {
      Authorization: `Bearer ${getPayPalAccessToken_()}`,
      'Content-Type': 'application/json'
    }
  };

  const request = UrlFetchApp.fetch(buildAPIUrl_(queryParams), options);
  const { transaction_details, total_pages } = JSON.parse(request);
  return { transaction_details, total_pages };
};

// Extract the transaction details including the transaction ID,
// donation amount, transaction date and buyer's email and country code
const parsePayPalTransaction_ = ({ transaction_info, payer_info }) => [
  transaction_info.transaction_id,
  new Date(transaction_info.transaction_initiation_date),
  transaction_info.transaction_amount?.value,
  transaction_info.transaction_note || transaction_info.transaction_subject || '',
  payer_info?.payer_name?.alternate_full_name,
  payer_info?.email_address,
  payer_info?.country_code
];

const fetchPayPalTransactions_ = () => {
  const startDate = new Date(START_DATE);
  const endDate = new Date(END_DATE);
  startDate.setHours(0, 0, 0, 0);
  endDate.setHours(23, 59, 59, 999);

  const transactions = [];

  const params = {
    start_date: startDate.toISOString(),
    end_date: endDate.toISOString(),
    page_size: 100,
    transaction_type: TRANSACTION_TYPE,
    fields: 'transaction_info,payer_info'
  };

  for (let page = 1, hasMore = true; hasMore; page += 1) {
    const response = fetchTransactionBatchFromPayPal({ ...params, page });
    const { transaction_details = [], total_pages } = response;
    transaction_details.map(parsePayPalTransaction_).forEach((e) => transactions.push(e));
    hasMore = total_pages && total_pages > page;
  }

  return transactions;
};

// Import the transactions from PayPal and write them to the active Google Sheet
const importTransactionsToGoogleSheet = () => {
  const transactions = fetchPayPalTransactions_();
  const { length } = transactions;
  if (length > 0) {
    const sheet = SpreadsheetApp.getActiveSheet();
    sheet.getRange(1, 1, length, transactions[0].length).setValues(transactions);
    const status = `Imported ${length} PayPal transactions into Google Sheets`;
    SpreadsheetApp.getActiveSpreadsheet().toast(status);
  }
};

3. تشغيل وظيفة استيراد PayPal

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

هذا كل شيء. إذا كان هناك أي معاملات PayPal للاستيراد في نطاق التاريخ المحدد ، فسيتم تشغيل البرنامج النصي وسيتم استيراد المعاملات إلى أوراق Google.

تشغيل PayPal المستورد

في الجزء التالي من البرنامج التعليمي ، سنتعلم كيفية تصدير معاملات PayPal من أوراق Google إلى ملف XML للاستيراد إلى برنامج المحاسبة.

انظر أيضًا: أرسل فواتير PayPal من أوراق Google



Source link


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

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

اترك تعليقاً

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

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

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

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

Continue reading