servii-backend/finances.py
2024-09-23 21:58:06 +02:00

59 lines
2.1 KiB
Python

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)