Integrate Paypal in Raspberry Pi, Arduino or ESP Projects

Integrate Paypal in Raspberry Pi or ESP Projects

INTRODUCTION:

Paypal is one of the best online payment services which is used worldwide. In this blog, we are going to see how we can get the PayPal payment notification (IPN) on python. The method we are going to use uses webhooks and MQTT and this payment method can be used to receive a payment notification from Arduino, Raspberry pi, nodemcu, ESP or any other board you know. For this project, we will be using PayPal IPN but we will not be using any web hosting or PHP servers so this tutorial is completely different from all other PayPal IPN tutorial out there. So this tutorial will help you implement paypal in raspberry pi and paypal in ESP.

APPLICATIONS

  •  Can be used as a payment option on vending machines made by raspberry pi or nodemcu.
  • Can be used on live streams and you can trigger anything from Raspberry pi. if payment is received.

I want you to write in comments, where this method can be implemented.

VIDEO TUTORIAL:

Complete Video tutorial

LET’S GET STARTED:

SETUP ZAPIER ACCOUNT:

The first thing we have to do is go to zapier.com which is an online service to automate the workflows, It works like IFTTT. You will have to sign up for an account or sign up using google or facebook.

payment solution arduino raspberry-pi
zapier sign up page

MAKE A ZAP:

After signing up, you will have to click on make a zap and in the when this happens part search for webhooks and you will able to see webhooks by zapier, click it.

Click on make a zap
Click on make a zap

SETUP TRIGGER PART:

search "webhook by zapier" in when this happens window
search “webhook by zapier” in when this happens window

Then you will have to select the trigger event, we will select CATCH HOOK here and then we will press continue.

Select the trigger event in App & event selected window
Select the trigger event in App & event selected window

In the Hook customized window there will be a custom webhook URL, we have to copy it then press continue.

copy custom webhook URL
copy custom webhook URL

TEST WORKFLOW USING PAYPAL WEBHOOK SIMULATOR:

Now to test our connection and to assure our workflow process we will go to PayPal developers.

payment solution arduino raspberry-pi
PayPal developers

Now we will go to developers.paypal.com and go to Webhook Simulator and paste the customized URL here that we copied earlier. Select an Event Type and click on the send test.

web hook simulator pay pal
webhook simulator PayPal

Now go to zapier and press pull in Hooks or Get more samples and you will get the hook there, and it will assure its working.

Click on get samples to assure workflow process
payment solution arduino raspberrypi
Click on get samples to assure workflow process

SETUP PUBNUB ACCOUNT AND GETTING KEYSET:

Now the next step is we have to go to pubnub.com   which is an online service and provides Cloud MQTT Service so we are going to use this service to get messages using MQTT.

paypal arduino raspberry-pi esp
PubNub Login Page

And you will have to sign up for an account After signing up, login and create a new app.

create a new app on pubnub.com
payment solution arduino raspberry-pi
create a new app on pubnub.com

Write the name of your app Press continue

paypal arduino raspberry-pi esp
Enter the name of your app on pubnub

Click on the app you have created recently and you will see that there is publish key and subscribe key you have to copy them.

Copy pubnub keyset
payment solution arduino raspberry-pi
Copy pubnub keyset

SETTING UP ACTION ON ZAPIER:

Now go back to zapier.com and now we will add Action we have already defined the trigger and now we have to define action so we can get MQTT messages on Raspberry Pi or nodemcu or Arduino or any python running machine.

paypal arduino raspberry-pi esp
Edit Action on Zapier

Click on edit action And search for webhook by zapier.

paypal arduino raspberry-pi esp
Search webhook by zapier

and in the events, select GET and press continue now.

paypal arduino raspberry-pi esp
Select Action event

we have to put some parameters like we have to put URL and here is the format of URL. We will Replace the publish and subscribe it with our publish and subscribe key and then we will put this in zapier get URL.

http://pubsub.pubnub.com//publish/publish-key/subscribe-key/0/paymentTrigger/0/{“requester”:”pi”,”trigger”:”anything”,”status”:1}

Enter above URL customized by you in the Zapier action window.
payment solution arduino raspberry-pi
Enter the above URL customized by you in the Zapier action window
paypal arduino raspberry-pi esp
turn your zap on

and then we will press continue we can skip the test now and don’t forget to turn your zap on.

PAYPAL SETTINGS:

we have tested the workflow using the simulator and now its time to put the webhook URL which we were using in the simulator, in the PayPal IPN settings in order to receive the notification when someone in real makes payment.

The first thing you have to go is to go in account settings in your PayPal account.

go to account Settings
go to Account Settings

And then click on notifications.

Click on notifications
Click on notifications

Update instant payment notifications or IPN.

Update instant payment notifications (IPN)
Update instant payment notifications (IPN)

Click on Edit Settings and update the information and turn on the IPN service.

update IPN info
update IPN info

and you are good to go now. we have completed the setup of online services and now we have to write the Raspberry Pi code.

RASPBERRY PI SETUP:

all you have to do is to install the PubNub library which you can do by typing,

pip2 install pubnub== 3.9.0

PYTHON CODE:

This code can be run on any device that can run Python 2, this was tested on raspberry pi 3B and windows 10 machine.

# Import all the libraries

from pubnub import Pubnub

import time




# Initialize the Pubnub Keys
# Replace them with your keysets
pub_key = "pub-c-XXXXXXXXXXXXX"
sub_key = "sub-c-XXXXXXXXXXXXX"


def init():  # initalize the pubnub keys and start subscribing

    global pubnub  # Pubnub Initialization

    pubnub = Pubnub(publish_key=pub_key, subscribe_key=sub_key)
    pubnub.subscribe(channels='paymentTrigger', callback=callback, error=callback, reconnect=reconnect,
                     disconnect=disconnect)


def get_message(controlCommand): 
    if (controlCommand.has_key("trigger")):
        if (controlCommand["trigger"] == "anything" and controlCommand["status"] == 1):

            print("payment recieved")

        else:

            pass

    else:
        pass


def callback(message, channel):  # this function waits for the message from the aleatrigger channel
    print(message)
    if (message.has_key("requester")):
        get_message(message)
    else:
        pass


def error(message):  # if there is error in the channel,print the  error
    print("ERROR : " + str(message))


def reconnect(message):  # responds if server connects with pubnub
    print("RECONNECTED")


def disconnect(message):  # responds if server disconnects with pubnub
    print("DISCONNECTED")


if __name__ == '__main__':
    init()  # Initialize the Script

now we will have to replace the publish and subscribe key with your keysets and we can run the code.

NODEMCU SETUP:

In order to run this code on nodemcu, we will have to use pubnub library for nodeMCU and we can do that by going into library manager in Arduino IDE and installing PubNub library by Vladimir. You will have to replace the WIFI SSID, WIFI password, Publish and subscribe keys in the code.

After installing the library all we have to do is to restart the IDE and connect our board with PC, select the board type and select the com port and in the end, we will have to upload the code.

ET// PubNub example using ESP8266.
#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>
static char ssid[] = "XXXXXXXX";
static char pass[] = "XXXXXXXX";
const static char pubkey[]  = "pub-c-XXXXXXXXXXX";
const static char subkey[]  = "sub-c-XXXXXXXXXXX";
const static char channel[] = "paymentTrigger";
String message;
void setup() {
    Serial.begin(9600);
    Serial.println("Attempting to connect...");
    WiFi.begin(ssid, pass);
    if(WiFi.waitForConnectResult() != WL_CONNECTED) { // Connect to WiFi.
        Serial.println("Couldn't connect to WiFi.");
        while(1) delay(100);
    }
    else {
        Serial.print("Connected to SSID: ");
        Serial.println(ssid);
        PubNub.begin(pubkey, subkey); // Start PubNub.
        Serial.println("PubNub is set up.");
    }
}
void loop() {
    { // Subscribe.
        PubSubClient* sclient = PubNub.subscribe(channel); // Subscribe.
        if (0 == sclient) { 
            Serial.println("Error subscribing to channel.");
            delay(1000);
            return;
        }
        while (sclient->wait_for_data()) { // Print messages.
            Serial.write(sclient->read());
            
        }
        Serial.println("Payment Recieved");
        sclient->stop();
    }

    delay(1000);
}

Replace X’s with the respective values.

THAT’S IT:

Hopefully by following the tutorial you have integrated payment solution in your Raspberry pi, Arduino or ESP project . If you love the tutorial please write in comments and don’t forget to subscribe us on youtube.

and check out our facebook page.

for any query, you can contact us.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.