[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でないとダメっぽい。。

[paypal] APIを使ってみる

Basic Integrationに沿ってやっていきます。
https://developer.paypal.com/docs/checkout/integrate/#

– Render the PayPal Smart Payment Buttons
– Set up a one-time transaction and immediately capture the funds into your account
– Test the button and take it live on your site

まず前提として、paypalアカウントが必要。
paypalアカウントでdeveloperサイトにログインする。
https://developer.paypal.com/developer/applications

create appとすると、paypal account, client ID, secretが生成されるのでメモしておく。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script
    src="https://www.paypal.com/sdk/js?client-id=${clientId}"></script>
    <div id="paypal-button-container"></div>
    <script>
		paypal.Buttons().render('#paypal-button-container');
    </script>
</body>
</html>

$ python3 -m http.server 8000

やばっ

valueは$USっぽいです。

<script>
		paypal.Buttons({
			createOrder: function(data, actions){
				return actions.order.create({
					purchase_units: [{
						amount: {
							value: '0.01'
						}
					}]
				});
			},
			onApprove: function(data, actions){
				return actions.order.capture().then(function(details){
					alert('Transaction completed by ' + details.payer.name.given_name);
				});
			}
		}).render('#paypal-button-container');
    </script>

productionではbusinessアカウントが必要っぽい。
$0.01がカゴに入ってます。

なるほど、JSで管理するんやな、日本はpaypalは普及してませんが、イメージは掴めました。

アプリのシリアルコード問題

– appleがappのプレゼントとしてシリアルコードの配布を禁止
– それによって、事前登録によるアイテム配布などができなくなった
– 事の発端はモンストのシリアルコード配布終了

うーん、奥が深い

SLA (Service Level Agreement)

SLA stands for Service Level Agreement, which means agreement service level, service quality assurance, etc. on the level of service(definitions, ranges, contents, achievement targets, etc.) tied between providers and their users it will be. This is an indication of how much quality can be guaranteed to the contractor by the service provider.

SLAs are often used mainly for communication service, cloud services, rental servers, etc. We define guarantee items such as definition of service, communication speed, upper limit of usage stop time, etc. When the quality of service falls below the guaranteed value of SLA, reduction of usage fee etc will be done.

In many cases, this reduction is often limited to the usage fee, and it does not include lost profits etc. that the service could not be used properly. In principle, the specified item can be quantitatively measured, and the upper limit, the lower limit, the average, etc. are expressed numerically, and the measurement method etc. are also defined at the same time.

As a user, it is an index to measure the availability and stability of the service, so it is a good idea to check it when selecting a service. Although the SLA values presented by each company vary, it can be seen that there are large differences when looking at “stop time”.