[Python3.8.5] Stripeを試してみたい

Stripe手数料:3.6%
-> smartHR, Booking.com, slack, shopify, freee, cookpad
-> Ruby, Java, Scala and Javascript

### Stripeの特徴
– 初期費用がかからず、事前審査不要
– 少額の場合は他社より料率・振込手数料が安価
– 決済画面への遷移がなくて決済フォームもシンプル
– 顧客へ返金時に返金手数料はかからないものの、元の決済手数料は一切返金なし

### 実装方法
自前でサーバを用意するパターン(custom payment flow)と、ドロップインソリューション(prebuild checkout page)といって Stripeが用意した決済画面を利用する方法がある。

docs: https://stripe.com/docs/payments/accept-a-payment?integration=elements

$ python3 –version
Python 3.8.5
$ pip3 install stripe
$ pip3 install flask

server.py

import os
from flask import Flask, jsonify, request

import stripe

stripe.api_key = 'sk_test_***'

app = Flask(__name__,
			static_url_path='',
			static_folder='.')

YOUR_DOMAIN = '192.168.33.10:5000'

@app.route('/create-session', methods=['POST'])
def create_checkout_session():
	try:
		checkout_session = stripe.checkout.Session.create(
			payment_method_types=['card'],
			line_items=[
				{
					'price_data': {
						'currency': 'usd',
						'unit_amount': 2000,
						'product_data': {
							'name': 'Stubborn Attachments',
							'images': ['https://i.imgur.com/EHyR2nP.png'],
						},
					},
					'quantity': 1,
				},
			],
			mode='payment',
			success_url=YOUR_DOMAIN + '/success.html',
			cancel_url=YOUR_DOMAIN + '/cancel.html',
		)
		return jsonify({'id': checkout_session.id})
	except Exception as e:
		return jsonify(error=str(e)), 403

if __name__ == '__main__':
	app.run(host='0.0.0.0')

success.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Thanks for your order!</title>
	<link rel="stylesheet" href="">
</head>
<body>
	<section>
		<p>
			We appreciate your business! If you have any questions, please email <a href="mailto:info@hpscript.com">info@hpscript.com</a>.
		</p>
	</section>
</body>
</html>

cancel.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Checkout canceled</title>
	<link rel="stylesheet" href="">
</head>
<body>
	<section>
		<p>
			Forgot to add something to your car? Shop around then come back to pay!
		</p>
	</section>
</body>
</html>

checkout.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Buy cool new product</title>
	<link rel="stylesheet" href="">
	<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
	<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
	<section>
		<div class="product">
			<img src="https://i.imgur.com/EHyR2nP.png" alt="The cover of Stubborn Attachments">
			<div class="description">
				<h3>Stubborn Attachments</h3>
				<h5>$20.00</h5>
			</div>
		</div>
		<button id="checkout-button">Checkout</button>
	</section>
</body>
<script type="text/javascript">
	var stripe = Stripe("pk_test_***");
	var checkoutButton = document.getElementById("checkout-button");

	checkoutButton.addEventListener("click", function(){
		fetch("/create-session", {
			method: "POST",
		})
			.then(function (response){
				return response.json();
			})
			.then(function (session){
				return stripe.redirectToCheckout({ sessionId: session.id });
			})
			.then(function(result){
				if(result.error){
					alert(result.error.message);
				}
			})
			.catch(function(error){
				console.error("Error:", error);
			});
	});
</script>
</html>

$ ls
cancel.html checkout.html other server.py style.css success.html

$ python3 server.py

ん? strip.jsはhttpsでないとダメっぽい。。