如何将 PayPal 交易导入 Google 表格

已发表: 2022-06-16

PayPal transactions in Google Sheets

本教程将向您展示如何借助 Google Apps 脚本将 PayPal 交易导入 Google 表格。 您可以选择将标准 PayPal 付款、定期订阅付款、捐赠,甚至退款和拒付导入 Google 表格。

将数据导入 Google 表格后,您可以将其导出为 CSV 文件,然后将其导入 Quickbooks 会计软件。 印度的 Tally 用户可以将 PayPal 交易从 Google 表格导出为 XML 格式,然后将它们批量导入 Tally。

另请参阅:使用 Google 表单自动化 PayPal

在 Google 表格中导入 PayPal 交易

在本例中,我们将通过 PayPal 进行捐赠的捐赠者列表导入到 Google 表格中。

1. 在 PayPal 中创建 API 凭证

登录到您的 PayPal 开发者仪表板 (developer.paypal.com) 并在实时模式下创建一个新应用程序。 为您的应用程序命名 - Transaction Importer for Google Sheets ,然后单击创建应用程序按钮。

PayPal 将创建您在后续步骤中需要的客户端 ID 和客户端密钥。 在 Live App 设置部分下,选中Transaction Search选项并关闭所有其他选项,因为我们只希望 API 密钥列出交易并且没有其他功能。 单击保存以继续。

Paypal Account Credentials

2.创建一个谷歌表格项目

转到sheets.new以创建新的 Google 表格。 转到扩展菜单并选择 Apps 脚本以打开 Apps 脚本编辑器。

将代码复制粘贴到编辑器中。 记得用你自己的替换交易代码。 您可以将T0002用于 PayPal 订阅, T0014用于捐赠付款,或T1107用于 PayPal 退款和拒付。

/* @OnlyCurrentDoc */注释是一个 Google Apps 脚本注释,它告诉 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 导入交易。 您可能必须授权该脚本,因为它需要权限才能连接到 PayPal API 并代表您将数据写入 Google 表格。

而已。 如果在选定的日期范围内有任何 PayPal 交易要导入,脚本将运行并将交易导入 Google 表格。

Run PayPal Importer

在本教程的下一部分中,我们将学习如何将 PayPal 交易从 Google 表格导出到 XML 文件,以便导入到 Tally 会计软件中。

另请参阅:从 Google 表格发送 PayPal 发票