forked from stripe-samples/accept-a-payment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
115 lines (93 loc) · 4.04 KB
/
server.py
File metadata and controls
115 lines (93 loc) · 4.04 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /usr/bin/env python3.6
import stripe
import json
import os
from flask import Flask, render_template, jsonify, request, send_from_directory
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
# For sample support and debugging, not required for production:
stripe.set_app_info(
'stripe-samples/accept-a-payment/custom-payment-flow',
version='0.0.2',
url='https://github.com/stripe-samples')
stripe.api_version = '2020-08-27'
stripe.api_key = os.getenv('STRIPE_SECRET_KEY')
static_dir = str(os.path.abspath(os.path.join(__file__ , "..", os.getenv("STATIC_DIR"))))
app = Flask(__name__, static_folder=static_dir, static_url_path="", template_folder=static_dir)
@app.route('/', methods=['GET'])
def get_root():
return render_template('index.html')
@app.route('/config', methods=['GET'])
def get_config():
return jsonify({'publishableKey': os.getenv('STRIPE_PUBLISHABLE_KEY')})
@app.route('/create-payment-intent', methods=['POST'])
def create_payment():
data = json.loads(request.data)
# Each payment method type has support for different currencies. In order to
# support many payment method types and several currencies, this server
# endpoint accepts both the payment method type and the currency as
# parameters.
#
# Some example payment method types include `card`, `ideal`, and `alipay`.
payment_method_type = data['paymentMethodType']
currency = data['currency']
# Create a PaymentIntent with the amount, currency, and a payment method type.
#
# See the documentation [0] for the full list of supported parameters.
#
# [0] https://stripe.com/docs/api/payment_intents/create
params = {
'payment_method_types': [payment_method_type],
'amount': 1999,
'currency': currency
}
# If this is for an ACSS payment, we add payment_method_options
# to create the Mandate. This is not required if you're not accepting
# ACSS (Pre-authorized debit in Canada).
if payment_method_type == 'acss_debit':
params['payment_method_options'] = {
'acss_debit': {
'mandate_options': {
'payment_schedule': 'sporadic',
'transaction_type': 'personal'
}
}
}
try:
intent = stripe.PaymentIntent.create(**params)
# Send PaymentIntent details to the front end.
return jsonify({'clientSecret': intent.client_secret})
except stripe.error.StripeError as e:
return jsonify({'error': {'message': str(e)}}), 400
except Exception as e:
return jsonify({'error': {'message': str(e)}}), 400
@app.route('/webhook', methods=['POST'])
def webhook_received():
# You can use webhooks to receive information about asynchronous payment events.
# For more about our webhook events check out https://stripe.com/docs/webhooks.
webhook_secret = os.getenv('STRIPE_WEBHOOK_SECRET')
request_data = json.loads(request.data)
if webhook_secret:
# Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured.
signature = request.headers.get('stripe-signature')
try:
event = stripe.Webhook.construct_event(
payload=request.data, sig_header=signature, secret=webhook_secret)
data = event['data']
except Exception as e:
return e
# Get the type of webhook event sent - used to check the status of PaymentIntents.
event_type = event['type']
else:
data = request_data['data']
event_type = request_data['type']
data_object = data['object']
if event_type == 'payment_intent.succeeded':
print('💰 Payment received!')
# Fulfill any orders, e-mail receipts, etc
# To cancel the payment you will need to issue a Refund (https://stripe.com/docs/api/refunds)
elif event_type == 'payment_intent.payment_failed':
print('❌ Payment failed.')
return jsonify({'status': 'success'})
if __name__ == '__main__':
app.run(port=4242, debug=True)