Created
October 11, 2020 05:41
-
-
Save bibhas/aa4bf369df48d21d0a9ba722d4a60212 to your computer and use it in GitHub Desktop.
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="v2-full">Buy MacUpdater 2 Full</button> | |
<!-- | |
<button id="v2pro-full">Buy MacUpdater 2 Pro Full</button> | |
<button id="v2-upgrade">Buy MacUpdater 2 Upgrade</button> | |
<button id="v2pro-upgrade-1x">Buy MacUpdater 2 Pro Upgrade from 1.x</button> | |
<button id="v2pro-upgrade-nonpro">Buy MacUpdater 2 Pro Upgrade from Non-Pro</button> | |
--> | |
</div> | |
<script type="text/javascript"> | |
var stripe = Stripe('pk_test_vG8eGfEu2D6JiZuGxEw8Y3zv00WXeDFDrB'); | |
function hookupButton(identifier) { | |
var checkoutButton = document.getElementById(identifier); | |
checkoutButton.addEventListener('click', function() { | |
// Create a new Checkout Session using the server-side endpoint you | |
// created in step 3. | |
fetch('/create-checkout-session/' + identifier, { | |
method: 'POST', | |
}) | |
.then(function(response) { | |
return response.json(); | |
}) | |
.then(function(session) { | |
return stripe.redirectToCheckout({ | |
sessionId: session.id | |
}); | |
}) | |
.then(function(result) { | |
// If `redirectToCheckout` fails due to a browser or network | |
// error, you should display the localized error message to your | |
// customer using `error.message`. | |
if (result.error) { | |
alert(result.error.message); | |
} | |
}) | |
.catch(function(error) { | |
console.error('Error:', error); | |
}); | |
}); | |
} | |
hookupButton("v2-full"); | |
/* | |
hookupButton("v2pro-full"); | |
hookupButton("v2-upgrade"); | |
hookupButton("v2pro-upgrade-1x"); | |
hookupButton("v2pro-upgrade-nonpro"); | |
*/ | |
</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
# 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_CU04Wp7KPmkOONt9w1kUx3mI00kAwNzPOu' # Secret Key | |
@app.route('/') | |
def index(): | |
return render_template('checkout.html') | |
products = { | |
# MacUpdater 2 Full | |
"v2-full" : ["prod_I8RENjGcYHtkBF"], | |
# MacUpdater 2 Pro Full | |
"v2pro-full" : ["prod_I8RFKjs1N9FFLm"], | |
# MacUpdater 2 Upgrade | |
"v2-upgrade" : ["prod_I8woOP1eNfNKAY"], | |
# MacUpdater 2 Pro Upgrade from 1.x | |
"v2pro-upgrade-1x" : ["prod_I8wqId1ZE1tZZZ"], | |
# MacUpdater 2 Pro Upgrade from Non-Pro | |
"v2pro-upgrade-nonpro" : ["prod_I8wtiDSH3Qo2os"] | |
} | |
""" | |
lutt = { | |
# MacUpdater 2 Full | |
"v2-full" : ["price_1HYALcBmh0wcPuUpgQ7RF0mO", "price_1HYALcBmh0wcPuUpgwuM1wRf"], | |
# MacUpdater 2 Pro Full | |
"v2pro-full" : ["price_1HYAMXBmh0wcPuUpaT4RPWIy", "price_1HYAMXBmh0wcPuUp4N0RNGHJ"], | |
# MacUpdater 2 Upgrade | |
"v2-upgrade" : ["price_1HYeuuBmh0wcPuUpmuLJbg4d", "price_1HYeuuBmh0wcPuUpB6HWeTz1"], | |
# MacUpdater 2 Pro Upgrade from 1.x | |
"v2pro-upgrade-1x" : ["price_1HYewZBmh0wcPuUpJhYvW455", "price_1HYewZBmh0wcPuUpdEvXUvxw"], | |
# MacUpdater 2 Pro Upgrade from Non-Pro | |
"v2pro-upgrade-nonpro" : ["price_1HYezSBmh0wcPuUp2yMfNVUb", "price_1HYezSBmh0wcPuUpapNYRVfB"] | |
} | |
""" | |
@app.route('/create-checkout-session/<identifier>', methods=['POST']) | |
def create_checkout_session(identifier): | |
product = products[identifier] | |
line_items = [] | |
for pid in product: | |
line_items.append({ | |
'price' : 'price_1HYALcBmh0wcPuUpgQ7RF0mO', | |
'quantity': 1, | |
'description' : "<Fill out this description>", | |
'dynamic_tax_rates' : [ | |
'txr_1HTtFRBmh0wcPuUpAP7C9M6t', | |
'txr_1HTtFlBmh0wcPuUpscJ621Vk', | |
'txr_1HTtGPBmh0wcPuUpaU4PcrKw' | |
] | |
}) | |
session = stripe.checkout.Session.create( | |
line_items = line_items, | |
payment_method_types = ['card'], | |
mode='payment', | |
locale='en', | |
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