-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.py
More file actions
69 lines (56 loc) · 2.13 KB
/
Copy pathpayment.py
File metadata and controls
69 lines (56 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, request, jsonify
from flask_cors import CORS
import stripe
import os
app = Flask(__name__)
CORS(app)
# Set your Stripe secret key
stripe.api_key = os.getenv('STRIPE_SECRET_KEY', 'sk_test_YOUR_STRIPE_SECRET_KEY')
@app.route('/create-payment-intent', methods=['POST'])
def create_payment_intent():
try:
data = request.get_json()
amount = data['amount']
currency = data.get('currency', 'ngn')
description = data.get('description', 'Payment')
payment_intent = stripe.PaymentIntent.create(
amount=int(amount * 100), # Convert to kobo for NGN
currency=currency,
description=description,
payment_method_types=['card'],
)
return jsonify({
'clientSecret': payment_intent.client_secret,
'id': payment_intent.id,
})
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/confirm-payment', methods=['POST'])
def confirm_payment():
try:
data = request.get_json()
payment_intent_id = data['paymentIntentId']
payment_method_id = data['paymentMethodId']
confirmed = stripe.PaymentIntent.confirm(
payment_intent_id,
payment_method={'id': payment_method_id}
)
return jsonify({'status': confirmed.status})
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/webhook', methods=['POST'])
def webhook():
payload = request.get_data()
sig_header = request.headers.get('stripe-signature')
endpoint_secret = 'whsec_YOUR_WEBHOOK_SECRET' # Replace with your webhook secret
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
if event['type'] == 'payment_intent.succeeded':
print('Payment successful:', event['data']['object'])
return jsonify({'received': True})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=3000)