Created
October 11, 2020 06:18
-
-
Save bibhas/66d9bea899bfd23a30e1bbe15a35335f to your computer and use it in GitHub Desktop.
Stripe support request code
This file contains hidden or 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
<!-- Taken from Stripe's docs (https://stripe.com/docs/payments/accept-a-payment?integration=elements) --> | |
<html> | |
<head> | |
<title>Buy cool new product</title> | |
<script src="https://js.stripe.com/v3/"></script> | |
<style> | |
button { | |
display: block; | |
margin-bottom: 10px; | |
margin-left: 5px; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<button id="buy-button">Buy now</button> | |
</div> | |
<script type="text/javascript"> | |
var stripe = Stripe('pk_test_oGWsGWpekijJnvnQesjOY6Lk'); | |
var checkoutButton = document.getElementById("buy-button"); | |
checkoutButton.addEventListener('click', function() { | |
fetch('/create-checkout-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> | |
</body> | |
</html> |
This file contains hidden or 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
[2020-10-11 12:01:58,865] ERROR in app: Exception on /create-checkout-session/ [POST] | |
Traceback (most recent call last): | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 2447, in wsgi_app | |
response = self.full_dispatch_request() | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1952, in full_dispatch_request | |
rv = self.handle_user_exception(e) | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1821, in handle_user_exception | |
reraise(exc_type, exc_value, tb) | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1950, in full_dispatch_request | |
rv = self.dispatch_request() | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1936, in dispatch_request | |
return self.view_functions[rule.endpoint](**req.view_args) | |
File "server.py", line 35, in create_checkout_session | |
cancel_url='https://example.com/cancel' # TODO | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/stripe/api_resources/abstract/createable_api_resource.py", line 22, in create | |
response, api_key = requestor.request("post", url, params, headers) | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/stripe/api_requestor.py", line 122, in request | |
resp = self.interpret_response(rbody, rcode, rheaders) | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/stripe/api_requestor.py", line 373, in interpret_response | |
self.handle_error_response(rbody, rcode, resp.data, rheaders) | |
File "/Users/bibhas/Library/Python/2.7/lib/python/site-packages/stripe/api_requestor.py", line 152, in handle_error_response | |
raise err | |
InvalidRequestError: Request req_F6pQyaIPr7wTTM: Only a single currency can be used with any price or price_data object in line_items. You provided: ["usd", "eur"]. | |
127.0.0.1 - - [11/Oct/2020 12:01:58] "POST /create-checkout-session/ HTTP/1.1" 500 - |
This file contains hidden or 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
# server.py | |
# Taken from Stripe's docs (https://stripe.com/docs/payments/accept-a-payment?integration=elements) | |
import os | |
import stripe | |
from flask import Flask, jsonify, render_template | |
app = Flask(__name__) | |
stripe.api_key = 'sk_test_Om252hAlZ2NHZGnwrzWde6pt' # Secret Key | |
@app.route('/') | |
def index(): | |
return render_template('checkout.html') | |
@app.route('/create-checkout-session/', methods=['POST']) | |
def create_checkout_session(): | |
line_items = [{ | |
'price' : 'price_1HaxmZFH3hEpbPLWWRSfxgyW', | |
'quantity': 1, | |
'description' : "Base License", | |
}, | |
{ | |
'price' : 'price_1HaxmZFH3hEpbPLWC5bu1qCo', | |
'quantity': 1, | |
'description' : "Base License 2", | |
}] | |
session = stripe.checkout.Session.create( | |
line_items = line_items, | |
payment_method_types = ['card'], | |
mode='payment', | |
locale='de', | |
allow_promotion_codes=True, | |
success_url='https://example.com/success', # TODO | |
cancel_url='https://example.com/cancel' # TODO | |
) | |
return jsonify(id=session.id) | |
if __name__== '__main__': | |
app.run(host='0.0.0.0', port=80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment