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 in this tutorial we will just see how we can use this with Raspberry Pi, but for Arduino and NodeMCU tutorials, you can check the link provided below.
Originally published at: https://highvoltages.co/tutorial/arduino-tutorial/payment-solution-arduino-raspberry-pi/
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:LET’S GET STARTED:SET UP 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.
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.
Then you will have to select the trigger event, we will select CATCH HOOK here and then we will press continue.
In the Hook customized window there will be a custom webhook URL, we have to copy it then press continue.
Now to test our connection and to assure our workflow process we will go to 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.
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.
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.
And you will have to sign up for an account After signing up, login and create a new app.
Write the name of your app Press continue
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.
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.
Click on edit action And search for webhook by zapier.
and in the events, select GET and press continue now.
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}
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.
And then click on notifications.
Update instant payment notifications or IPN.
Click on Edit Settings and update the information and turn on the IPN service.
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):
print(message)
if (message.has_key("requester")):
get_message(message)
else:
passdef 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.
THAT’S ITHopefully by following the tutorial you have integrated payment solution in your Raspberry Pi project. If you love the tutorial, please write in comments and don’t forget to subscribe us on YouTube.
Comments