Come importare le transazioni PayPal in Fogli Google
Pubblicato: 2022-06-16Questo tutorial ti mostrerà come importare le transazioni PayPal in Fogli Google con l'aiuto di Google Apps Script. Puoi scegliere di importare pagamenti PayPal standard, pagamenti ricorrenti di abbonamenti, donazioni o persino rimborsi e storni di addebito in Fogli Google.
Una volta che i dati sono stati importati in Fogli Google, puoi esportarli in un file CSV e importarli nel software di contabilità Quickbooks. Gli utenti di Tally in India possono esportare le transazioni PayPal da Fogli Google in formato XML e importarle in blocco in Tally.
Vedi anche: Automatizza PayPal con Moduli Google
Importa transazioni PayPal in Fogli Google
Per questo esempio, importeremo l'elenco dei donatori in Fogli Google che hanno effettuato le donazioni tramite PayPal.
1. Crea credenziali API all'interno di PayPal
Accedi alla dashboard per sviluppatori PayPal (developer.paypal.com) e crea una nuova app in modalità live. Assegna un nome alla tua app - Transaction Importer for Google Sheets
e fai clic sul pulsante Crea app.
PayPal creerà un ID cliente e una chiave segreta del cliente di cui avrai bisogno in un passaggio successivo. Nella sezione Impostazioni dell'app live, seleziona l'opzione Transaction Search
e disattiva tutte le altre opzioni poiché desideriamo solo che le chiavi API elenchino le transazioni e non abbiano altre funzionalità. Fare clic su Salva per continuare.
2. Crea un progetto Fogli Google
Vai a sheets.new
per creare un nuovo foglio Google. Vai al menu Estensioni e scegli Apps Script per aprire l'editor di Apps Script.
Copia e incolla il codice nell'editor. Ricordati di sostituire il codice transazione con il tuo. Puoi utilizzare T0002
per gli abbonamenti PayPal, T0014
per i pagamenti di donazioni o T1107
per i rimborsi e gli storni di addebito PayPal.
Il commento /* @OnlyCurrentDoc */
è un commento di Google Apps Script che indica a Google Apps Script di eseguire solo il codice all'interno del foglio Google corrente e non richiedere l'accesso a nessun altro foglio di lavoro nel tuo 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. Esegui la funzione di importazione PayPal
All'interno dell'editor di script, fai clic sul pulsante Esegui per importare le transazioni da PayPal. Potrebbe essere necessario autorizzare lo script poiché richiede autorizzazioni per connettersi all'API PayPal e anche per scrivere dati su Fogli Google per tuo conto.

Questo è tutto. Se sono presenti transazioni PayPal da importare nell'intervallo di date selezionato, lo script verrà eseguito e le transazioni verranno importate in Fogli Google.
Nella parte successiva del tutorial, impareremo come esportare le transazioni PayPal da Fogli Google in un file XML per l'importazione nel software di contabilità Tally.
Vedi anche: Invia fatture PayPal da Fogli Google