วิธีนำเข้าธุรกรรม PayPal ลงใน Google ชีต

เผยแพร่แล้ว: 2022-06-16

PayPal transactions in Google Sheets

บทช่วยสอนนี้จะแสดงวิธีนำเข้าธุรกรรม PayPal เข้าสู่ Google ชีตด้วยความช่วยเหลือของ Google Apps Script คุณสามารถเลือกนำเข้าการชำระเงินด้วย PayPal แบบมาตรฐาน การชำระค่าสมัครแบบประจำ การบริจาค หรือแม้แต่การคืนเงินและการปฏิเสธการชำระเงินลงใน Google ชีต

เมื่อนำเข้าข้อมูลไปยัง Google ชีตแล้ว คุณสามารถส่งออกข้อมูลเป็นไฟล์ CSV และนำเข้าไปยังซอฟต์แวร์บัญชี Quickbooks ได้ ผู้ใช้ Tally ในอินเดียสามารถส่งออกธุรกรรม PayPal จาก Google ชีตเป็นรูปแบบ XML และนำเข้าจำนวนมากไปยัง Tally

ดูเพิ่มเติมที่: ทำให้ PayPal เป็นแบบอัตโนมัติด้วย Google ฟอร์ม

นำเข้าธุรกรรม PayPal ใน Google ชีต

สำหรับตัวอย่างนี้ เราจะนำเข้ารายชื่อผู้บริจาคลงใน Google ชีตที่บริจาคผ่าน PayPal

1. สร้างข้อมูลรับรอง API ภายใน PayPal

ลงชื่อเข้าใช้แดชบอร์ดนักพัฒนาซอฟต์แวร์ PayPal ของคุณ (developer.paypal.com) และสร้างแอปใหม่ในโหมดออนไลน์ ตั้งชื่อแอปของคุณ - Transaction Importer for Google Sheets แล้วคลิกปุ่มสร้างแอป

PayPal จะสร้างรหัสลูกค้าและรหัสลับลูกค้าซึ่งคุณจะต้องใช้ในขั้นตอนถัดไป ในส่วนการตั้งค่า Live App ให้ทำเครื่องหมายที่ตัวเลือก Transaction Search และปิดตัวเลือกอื่นๆ ทั้งหมด เนื่องจากเราต้องการให้คีย์ API แสดงรายการธุรกรรมเท่านั้น และไม่มีฟังก์ชันอื่นๆ คลิกบันทึกเพื่อดำเนินการต่อ

Paypal Account Credentials

2. สร้างโครงการ Google ชีต

ไปที่ sheets.new เพื่อสร้าง Google ชีตใหม่ ไปที่เมนูส่วนขยายและเลือก Apps Script เพื่อเปิดตัวแก้ไข Apps Script

คัดลอกและวางโค้ดในตัวแก้ไข อย่าลืมเปลี่ยนรหัสธุรกรรมด้วยรหัสของคุณเอง คุณสามารถใช้ T0002 สำหรับการสมัครสมาชิก PayPal, T0014 สำหรับการชำระเงินบริจาค หรือ T1107 สำหรับการคืนเงินและการปฏิเสธการชำระเงินของ PayPal

ความคิดเห็น /* @OnlyCurrentDoc */ เป็นความคิดเห็นของ Google Apps Script ที่บอกให้ Google Apps Script เรียกใช้โค้ดใน Google ชีตปัจจุบันเท่านั้น และไม่ต้องการเข้าถึงสเปรดชีตอื่นใน Google ไดรฟ์ของคุณ

 /* @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 คุณอาจต้องให้สิทธิ์สคริปต์เนื่องจากต้องใช้สิทธิ์ในการเชื่อมต่อกับ PayPal API และเขียนข้อมูลไปยัง Google ชีตในนามของคุณ

แค่นั้นแหละ. หากมีธุรกรรมของ PayPal ที่จะนำเข้าในช่วงวันที่ที่เลือก สคริปต์จะทำงานและนำเข้าธุรกรรมนั้นไปยัง Google ชีต

Run PayPal Importer

ในส่วนถัดไปของบทช่วยสอน เราจะเรียนรู้วิธีส่งออกธุรกรรม PayPal จาก Google ชีตไปยังไฟล์ XML เพื่อนำเข้าซอฟต์แวร์บัญชี Tally

ดูเพิ่มเติมที่: ส่งใบแจ้งหนี้ PayPal จาก Google ชีต