diff --git a/finances.py b/finances.py new file mode 100644 index 0000000..6ab7d5f --- /dev/null +++ b/finances.py @@ -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) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 01721e4..2fab202 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +Werkzeug~=3.0.3 +stripe~=10.12.0 \ No newline at end of file