Created
July 6, 2021 13:11
-
-
Save pigeonflight/15a679a1715b7def9288eaae887ff3cc to your computer and use it in GitHub Desktop.
Fast API with deta.sh and Paypal IPN (first working test)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI, Request | |
import sys | |
import urllib.parse | |
import requests | |
app = FastAPI() | |
VERIFY_URL_PROD = 'https://ipnpb.paypal.com/cgi-bin/webscr' | |
VERIFY_URL_TEST = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr' | |
# Switch as appropriate | |
VERIFY_URL = VERIFY_URL_TEST | |
# a POST route for our webhook events | |
@app.post("/") | |
def webhook_handler(req: Request): | |
# verify signature if needed | |
# add logic to handle the request | |
# Add '_notify-validate' parameter | |
params = dict(req.query_params) | |
params['cmd']='_notify-validate' | |
# Post back to PayPal for validation | |
headers = {'content-type': 'application/x-www-form-urlencoded', | |
'user-agent': 'Python-IPN-Verification-Script'} | |
r = requests.post(VERIFY_URL, params=params, headers=headers, verify=True) | |
r.raise_for_status() | |
# Check return message and take action as needed | |
if r.text == 'VERIFIED': | |
"""do magic here""" | |
return "verified" | |
if r.text == 'INVALID': | |
return "invalid" | |
return "nothing to see here" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment