2024-09-23 19:58:06 +00:00
|
|
|
import stripe
|
2024-09-25 16:30:36 +00:00
|
|
|
from flask import Flask, request, jsonify
|
2024-09-23 19:58:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
# Handle the event based on its type
|
|
|
|
match event['type']:
|
|
|
|
case 'customer.subscription.deleted':
|
|
|
|
customer = event['data']['object']
|
2024-09-25 16:30:36 +00:00
|
|
|
print(event)
|
2024-09-23 19:58:06 +00:00
|
|
|
print(f"Customer stopped subscription: {customer['id']}")
|
|
|
|
case 'customer.subscription.created':
|
2024-09-25 16:30:36 +00:00
|
|
|
print(event)
|
|
|
|
customer = event['data']['object']
|
|
|
|
print(f"Customer started subscription: {customer['id']}")
|
|
|
|
case 'customer.subscription.updated':
|
|
|
|
print(event)
|
2024-09-23 19:58:06 +00:00
|
|
|
customer = event['data']['object']
|
|
|
|
print(f"Customer started subscription: {customer['id']}")
|
|
|
|
|
|
|
|
return jsonify({'status': 'success'}), 200
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(port=3400)
|