DEV Community

StackOverflowWarrior
StackOverflowWarrior

Posted on • Edited on

Day 9 of 100 Days of Cloud: Building a Paystack-Zoho CRM Pipeline(Part 1)

Welcome to Day 9 of our 100 Days of Cloud journey! Today, we'll explore how to establish a robust data pipeline between Paystack and Zoho CRM using Python scripts. This pipeline automates the transfer of customer data, enhancing lead management efficiency. Let's dive into the details step-by-step.

Setting Up the Paystack-Zoho Pipeline

Prerequisites

Before starting, ensure you have completed these prerequisites:

  1. Create a Zoho CRM Client Application: This is crucial for obtaining OAuth tokens needed for authentication. You can find detailed instructions in the Zoho CRM documentation.

.env Configuration

To securely store credentials, we utilize a .env file. Make sure to create one in your project directory and populate it with the necessary variables like ZOHO_CLIENT_ID, ZOHO_CLIENT_SECRET, and ZOHO_REDIRECT_URI. Here’s a sample:

ZOHO_CLIENT_ID=your_client_id_here
ZOHO_CLIENT_SECRET=your_client_secret_here
ZOHO_REDIRECT_URI=your_redirect_uri_here
Enter fullscreen mode Exit fullscreen mode

Generating OAuth Tokens

OAuth tokens are essential for authentication with Zoho CRM. Here's how you can generate them using Python:

from dotenv import load_dotenv
import os
import requests

# Load environment variables from .env file
load_dotenv()

# Zoho CRM API configuration
CLIENT_ID = os.environ.get('ZOHO_CLIENT_ID')
CLIENT_SECRET = os.environ.get('ZOHO_CLIENT_SECRET')
REDIRECT_URI = os.environ.get('ZOHO_REDIRECT_URI')

# Step 1: Redirect user for authorization
authorization_url = f'https://accounts.zoho.com/oauth/v2/auth?scope=ZohoCRM.modules.ALL&client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}'
print("Go to the following URL and authorize access:")
print(authorization_url)

# Step 2: Obtain authorization code from redirect URL
authorization_code = input("Enter authorization code from redirect URL: ")

# Step 3: Exchange authorization code for access token
token_url = 'https://accounts.zoho.com/oauth/v2/token'
data = {
    'code': authorization_code,
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
    'redirect_uri': REDIRECT_URI,
    'grant_type': 'authorization_code'
}

response = requests.post(token_url, data=data)

if response.status_code == 200:
    access_token = response.json()['access_token']
    print(f"Access Token: {access_token}")
else:
    print("Failed to obtain access token:", response.json())
Enter fullscreen mode Exit fullscreen mode

Local Webhook Testing with ngrok

During development, use ngrok to test webhooks locally. Here’s how:

  1. Download and Install ngrok: Get it from ngrok.com.
  2. Start ngrok: Run ngrok http <port> in your terminal (e.g., ngrok http 5000) after starting your local server. Ngrok will provide a temporary public URL (https://abcd1234.ngrok.io) that tunnels requests to your local machine.
  3. Configure Webhook Endpoint: Use this ngrok URL in your Paystack settings or any service sending webhooks.

Integrating Paystack and Zoho CRM

The heart of our pipeline involves processing Paystack webhook events and transferring data to Zoho CRM. Here’s a simplified Flask app for handling Paystack webhooks and pushing data to Zoho CRM:

from flask import Flask, request, jsonify
import requests
import hmac
import hashlib
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

app = Flask(__name__)

# Paystack Webhook Secret
app.config['PAYSTACK_WEBHOOK_SECRET'] = os.environ.get('PAYSTACK_WEBHOOK_SECRET')

# Zoho CRM API Configuration
ZOHO_API_ENDPOINT = 'https://www.zohoapis.com/crm/v2'
ZOHO_ACCESS_TOKEN = os.environ.get('ZOHO_ACCESS_TOKEN')

# Endpoint to receive Paystack webhook events
@app.route('/', methods=['POST'])
def paystack_webhook():
    payload = request.get_data()
    signature = request.headers.get('x-paystack-signature', '')

    # Verify the webhook signature
    calculated_signature = hmac.new(
        app.config['PAYSTACK_WEBHOOK_SECRET'].encode('utf-8'),
        payload,
        digestmod=hashlib.sha512
    ).hexdigest()

    if not hmac.compare_digest(calculated_signature, signature):
        return jsonify({'error': 'Invalid webhook signature'}), 401

    event = request.json

    if event['event'] == 'charge.success':
        # Extract necessary data from the event
        reference = event['data']['reference']
        amount_paid = event['data']['amount']
        customer_name = f"{event['data']['customer']['first_name']} {event['data']['customer']['last_name']}"
        customer_email = event['data']['customer']['email']
        customer_phone = event['data']['customer']['phone']

        # Format data for Zoho CRM API (Creating a Lead)
        zoho_data = {
            'data': [
                {
                    'Reference': reference,
                    'Last_Name': customer_name.split()[1],
                    'First_Name': customer_name.split()[0],
                    'Email': customer_email,
                    'Phone': customer_phone,
                    'Lead_Source': 'Paystack',
                    'Annual_Revenue': amount_paid
                }
            ]
        }

        # Make API call to Zoho CRM
        headers = {
            'Authorization': f'Zoho-oauthtoken {ZOHO_ACCESS_TOKEN}',
            'Content-Type': 'application/json'
        }

        try:
            response = requests.post(
                f"{ZOHO_API_ENDPOINT}/Leads",
                json=zoho_data,
                headers=headers
            )

            if response.status_code == 201:
                return jsonify({'message': 'Data pushed to Zoho CRM successfully'}), 201
            else:
                return jsonify({'error': 'Failed to push data to Zoho CRM', 'details': response.text}), response.status_code

        except Exception as e:
            return jsonify({'error': str(e)}), 500

    else:
        return jsonify({'message': 'Unsupported event type'}), 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
Enter fullscreen mode Exit fullscreen mode

GitHub Actions Workflow

GitHub Actions can automate testing and deployment tasks. Here’s an overview of a GitHub Actions workflow:

  1. Continuous Integration: Run tests (Lead_API_test.py) on every push to ensure functionality.
  2. Deployment: Automate Docker image creation and push to AWS ECR for deployment.

Conclusion

You've now successfully set up a Paystack-Zoho CRM pipeline! By following these steps, you can automate the transfer of customer data from Paystack to Zoho CRM, enhancing your lead management capabilities. Explore further by customizing scripts and scaling with Docker and AWS ECR for production deployment.

Stay tuned for more cloud automation and integration tutorials in our 100 Days of Cloud journey. Happy coding and cloud exploring!


Today's journey into cloud automation has equipped you with practical skills in setting up integrations between Paystack and Zoho CRM. Tomorrow, we'll explore more cloud services to expand our knowledge. Keep learning and building!

Keep Clouding! ☁️

Top comments (0)