Created
January 7, 2023 13:42
-
-
Save BennyThink/d3a0de89d21ccb955636e0ffba0f9ea1 to your computer and use it in GitHub Desktop.
stripe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Payment status</title> | |
</head> | |
<body> | |
Your payment is {{redirect_status}}. | |
<br> | |
<br> | |
<table border="1"> | |
<tr> | |
{% for key in data.keys() %} | |
<td>{{key}}</td> | |
{% endfor %} | |
</tr> | |
<tr> | |
{% for value in data.values() %} | |
<td>{{value}}</td> | |
{% endfor %} | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Checkout</title> | |
<script src="https://js.stripe.com/v3/"></script> | |
</head> | |
<body> | |
<input id="pk" value="{{pk}}" hidden> | |
<input id="clientSecret" value="{{secret}}" hidden> | |
<input id="method" value="{{method}}" hidden> | |
<div id="error-message"></div> | |
<img id="wechat-qrcode"> | |
</body> | |
<script> | |
const pk = document.getElementById("pk").value | |
const clientSecret = document.getElementById("clientSecret").value | |
const method = document.getElementById("method").value | |
let error = null; | |
const stripe = Stripe(pk); | |
if (method === "wechat_pay") { | |
const {error, paymentIntent} = stripe.confirmWechatPayPayment( | |
clientSecret, | |
{ | |
payment_method_options: { | |
wechat_pay: { | |
client: 'web', | |
}, | |
} | |
}, | |
); | |
} else if (method === "alipay") { | |
error = stripe.confirmAlipayPayment(clientSecret, { | |
// Return URL where the customer should be redirected after the authorization | |
return_url: `${window.location.origin}/callback`, | |
}); | |
} else { | |
alert("Invalid payment method") | |
} | |
if (error) { | |
// Inform the customer that there was an error. | |
const errorElement = document.getElementById('error-message'); | |
errorElement.textContent = error.message; | |
} | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Checkout</title> | |
</head> | |
<body> | |
<form method="post" action="/checkout"> | |
<h1>Choose payment method and amount</h1> | |
<select name="method"> | |
<option value="alipay">Alipay</option> | |
<option value="wechat_pay">WeChat</option> | |
</select> | |
<input type="text" name="amount">元 | |
<button id="payment-button"> | |
Submit Payment | |
</button> | |
</form> | |
</body> | |
<script> | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import stripe | |
import time | |
from flask import Flask, request, render_template | |
logging.basicConfig(level=logging.INFO) | |
stripe.api_key = "sk_test9lxNH" | |
app = Flask(__name__) | |
pk = "pk_test_OuQ" | |
@app.route('/') | |
def index(): | |
return render_template("index.html") | |
@app.route('/checkout', methods=['POST']) | |
def checkout(): | |
method = request.form['method'] | |
# unit is 分 | |
amount = int(float(request.form['amount']) * 100) | |
# create payment intent | |
logging.info("method: %s, amount: %s", method, amount) | |
intent = stripe.PaymentIntent.create(payment_method_types=[method], amount=amount, currency="cny") | |
secret = intent.client_secret | |
return render_template('checkout.html', pk=pk, secret=secret, method=method) | |
def ts_to_date(ts): | |
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts)) | |
@app.route('/callback') | |
def callback(): | |
payment_intent = request.args.get("payment_intent") | |
redirect_status = request.args.get("redirect_status") | |
data = stripe.PaymentIntent.retrieve(payment_intent) | |
display_info = { | |
"amount": data["amount"] / 100, | |
"currency": data["currency"], | |
"created": ts_to_date(data["created"]), | |
"payment_method": data["payment_method_types"], | |
} | |
return render_template('callback.html', redirect_status=redirect_status, data=display_info) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment