main: Added test stripe verification
All checks were successful
Build Docker / Build Bot (push) Successful in 20s
Build Docker / Build Master (push) Successful in 32s

This commit is contained in:
Nathan Woodburn 2023-08-24 16:41:38 +10:00
parent d4fd470564
commit 230dc2a12a
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 28 additions and 13 deletions

View File

@ -234,20 +234,34 @@ def tlsa():
@app.route('/stripe', methods=['POST']) @app.route('/stripe', methods=['POST'])
def stripe(): def stripeapi():
# Log all requests
print(request.json)
# Log for docker
print(request.json, flush=True) print(request.json, flush=True)
# Get API header print(request.headers, flush=True)
api_key = request.headers.get('key') payload = request.data
if api_key == None: stripe.api_key = os.getenv('STRIPE_SECRET')
return jsonify({'error': 'Invalid API key', 'success': 'false'}) endpoint_secret = os.getenv('STRIPE_ENDPOINT_SECRET')
if api_key != os.getenv('STRIPE_KEY'): sig_header = request.headers.get('HTTP_STRIPE_SIGNATURE')
return jsonify({'error': 'Invalid API key', 'success': 'false'}) events = None
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError as e:
# Invalid payload
return jsonify({'success': 'false'})
except stripe.error.SignatureVerificationError as e:
return jsonify({'success': 'false'})
# Handle the event
if event.type == 'payment_intent.succeeded':
payment_intent = event.data.object # contains a stripe.PaymentIntent
print('PaymentIntent was successful!', flush=True)
elif event.type == 'payment_method.attached':
payment_method = event.data.object # contains a stripe.PaymentMethod
print('PaymentMethod was attached to a Customer!', flush=True)
# ... handle other event types
else:
print('Unhandled event type {}'.format(event.type))
return jsonify({'success': 'true'}) return jsonify({'success': 'true'})

View File

@ -2,3 +2,4 @@ python-dotenv
requests requests
flask flask
jsonify jsonify
stripe