[+] Stripe first local CLI webhook test

This commit is contained in:
Charles Le Maux 2024-09-23 21:58:06 +02:00
parent f111956e39
commit 1901711d97
2 changed files with 61 additions and 1 deletions

59
finances.py Normal file
View File

@ -0,0 +1,59 @@
from flask import Flask, request, jsonify
import stripe
import os
app = Flask(__name__)
#stripe.api_key = os.getenv("STRIPE_SECRET_KEY")
endpoint_secret = "whsec_2a1cadb771f7acfdeaac6720fdd56d3353cca5d38bdc2ed88336932968531457"
@app.route('/finances', methods=['POST'])
def webhook():
payload = request.get_data(as_text=True)
sig_header = request.headers.get('Stripe-Signature')
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError as e:
print(f"Invalid payload: {e}")
return jsonify({'error': 'Invalid payload'}), 400
except stripe.error.SignatureVerificationError as e:
print(f"Invalid signature: {e}")
return jsonify({'error': 'Invalid signature'}), 400
print(event)
# Handle the event based on its type
match event['type']:
case 'payment_intent.created':
payment_intent = event['data']['object']
print(f"PaymentIntent created: {payment_intent['id']}")
case 'customer.created':
customer = event['data']['object']
print(f"Customer created: {customer['id']}")
case 'payment_intent.succeeded':
payment_intent = event['data']['object']
print(f"PaymentIntent succeeded: {payment_intent['id']}")
case 'charge.succeeded':
charge = event['data']['object']
print(f"Charge succeeded: {charge['id']}")
case 'checkout.session.completed':
session = event['data']['object']
print(f"Checkout session completed: {session['id']}")
case 'charge.failed':
charge = event['data']['object']
print(f"Charge failed: {charge['id']}")
case 'customer.subscription.deleted':
customer = event['data']['object']
print(f"Customer stopped subscription: {customer['id']}")
case 'customer.subscription.created':
customer = event['data']['object']
print(f"Customer started subscription: {customer['id']}")
return jsonify({'status': 'success'}), 200
if __name__ == '__main__':
app.run(port=3400)

View File

@ -11,4 +11,5 @@ firebase-admin~=6.5.0
protobuf~=4.25.3
typing_extensions~=4.9.0
APScheduler~=3.10.4
Werkzeug~=3.0.3
Werkzeug~=3.0.3
stripe~=10.12.0