Skip to content

Instantly share code, notes, and snippets.

@pigeonflight
Created July 6, 2021 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigeonflight/15a679a1715b7def9288eaae887ff3cc to your computer and use it in GitHub Desktop.
Save pigeonflight/15a679a1715b7def9288eaae887ff3cc to your computer and use it in GitHub Desktop.
Fast API with deta.sh and Paypal IPN (first working test)
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