Welcome to Day 10 of our 100 Days of Cloud series! Today, we're focusing on a powerful integration technique using Google Apps Script. Instead of just fetching data and updating a Google Sheet on a schedule, we’ll be turning our script into a web app that acts as a webhook. This will allow Paystack to push data to our Google Sheet in real-time whenever a payment event occurs.
What is a Webhook?
A webhook is a way for an application to provide other applications with real-time information. It’s a lightweight, event-driven approach to integrating different services. In our case, Paystack can send payment data directly to our Google Apps Script web app, which will then write this data into a Google Sheet automatically.
Setting Up Your Environment
Before diving into the code, ensure you have:
- Google Account: Needed for Google Sheets and Google Apps Script.
- Paystack Account: For API access and setting up webhooks.
- Google Sheet: Where the Paystack data will be recorded.
Step 1: Create and Prepare Your Google Sheet
- Open Google Sheets and create a new spreadsheet. Name it "Paystack Payments Data" or something similar.
- Set up the columns you want to track. For example:
Transaction ID
Email
Amount
Date
Status
Step 2: Open Google Apps Script
- In your Google Sheet, go to Extensions > Apps Script.
- This opens the Google Apps Script editor. Clear any existing code and prepare to add your webhook script.
Step 3: Write the Webhook Script
Replace any existing code in the Apps Script editor with the following:
// Webhook function to handle incoming requests from Paystack
function doPost(e) {
try {
// Parse the JSON payload sent by Paystack
const jsonPayload = JSON.parse(e.postData.contents);
// Extract transaction data
const transaction = jsonPayload.data;
// Get the active sheet
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Append data to the sheet
sheet.appendRow([
transaction.id,
transaction.email,
transaction.amount,
new Date(transaction.created_at),
transaction.status
]);
// Return a success response to Paystack
return ContentService.createTextOutput('Success');
} catch (error) {
// Log error and return error response
Logger.log(error.toString());
return ContentService.createTextOutput('Error');
}
}
Explanation of the Code
-
doPost(e): This is the main function that handles POST requests. It’s automatically invoked when your web app receives data.
- Parsing the Payload: The JSON payload sent by Paystack is parsed.
- Extracting Data: Transaction details are extracted from the payload.
- Writing to Sheet: The data is appended to the active Google Sheet.
- Error Handling: Logs errors and returns a simple success or error message.
Step 4: Deploy the Script as a Web App
- Click on Deploy > New deployment in the Apps Script editor.
- Choose Web app as the deployment type.
- Configure the deployment settings:
- Description: Provide a description for your deployment.
- Execute as: Choose “Me” to ensure the script runs with your permissions.
- Who has access: Select “Anyone” or “Anyone with Google account” based on your security needs.
- Click Deploy and authorize the app to access your Google Sheet.
- Copy the web app URL provided after deployment. This URL will be used as the webhook endpoint.
Step 5: Set Up Paystack Webhook
- Log in to your Paystack dashboard.
- Navigate to Settings > Webhooks.
- Click on + Add New Webhook.
- Enter the web app URL you obtained from the Google Apps Script deployment.
- Choose the events you want to receive. For instance, select
successful
payments or other relevant events. - Save the webhook configuration.
Step 6: Testing the Webhook
- Trigger a Test Event: Perform a test transaction in Paystack or use the “Test Webhook” feature in the Paystack dashboard to send a test payload to your webhook URL.
- Check Your Google Sheet: Verify that the test data appears in your Google Sheet.
Troubleshooting
- Permission Issues: Ensure that the script has permission to access and modify your Google Sheet.
- Data Format: If the data isn’t being written correctly, check the format of the JSON payload from Paystack and adjust the parsing logic if necessary.
- Deployment Errors: Double-check deployment settings and URL. Make sure your web app is accessible and correctly set up.
Conclusion
You’ve successfully set up a Google Apps Script webhook to handle Paystack payment events and update a Google Sheet in real-time. This integration will help streamline your payment tracking and ensure your data is always up-to-date.
Stay tuned for more cloud automation tips and tricks in our 100 Days of Cloud series. If you have any questions or run into issues, feel free to leave a comment below!
Happy automating! 🚀
Top comments (0)