Jul-03-2019, 08:40 AM
I am building an integration with Stripe by following the examples in its documentation, but I can't understand the part of creating a Charge for more than one product.
I was looking all over Stripe's docs and was searching for any articles / forums about the similar issue but was unable to find anything. I'd be very grateful to either some links to the articles on this matter or any tips to help me to understand how to solve it.
Here's a server side code:
If there are some more code details you require to help me to solve this issue, please ask, I'll update my post.
I was looking all over Stripe's docs and was searching for any articles / forums about the similar issue but was unable to find anything. I'd be very grateful to either some links to the articles on this matter or any tips to help me to understand how to solve it.
Here's a server side code:
@app.route("/checkout", methods=["GET", "POST"]) def checkout(): if request.method == "POST": # Process a JSON string with a checkout information: # { item_id: item_quantity, ... } # Build the SQL query based on it items = {} shopping_cart = request.form["cart_checkout"] shopping_cart = shopping_cart.lstrip("{") shopping_cart = shopping_cart.rstrip("}") shopping_cart = shopping_cart.split(",") sqlQuery = "SELECT * FROM Items WHERE item_id IN (" for KeyValPair in shopping_cart: Key = KeyValPair.split(":")[0] Key = Key.strip('"') sqlQuery = sqlQuery + Key + "," Value = KeyValPair.split(":")[1] items[Key] = Value sqlQuery = sqlQuery.rstrip(",") sqlQuery = sqlQuery + ") ORDER BY item_id ASC" cart_items = sql_select(sqlQuery) # Add a column about the quantity of items for item in cart_items: item["quantity"] = items[item["item_id"]] # Build a Stripe checkout list line_items_list = [] for item in cart_items: line_item = {} line_item["name"] = item["item_name"] line_item["description"] = item["item_description"] line_item["amount"] = item["price"] line_item["currency"] = "usd" line_item["quantity"] = item["quantity"] line_items_list.append(dict(line_item)) stripe_session = stripe.checkout.Session.create( submit_type="pay", payment_method_types=["card"], line_items=line_items_list, success_url='https://example.com/success', cancel_url='https://example.com/cancel', ) return render_template("checkout.html", stripe_id=stripe_session.id, stripe_pk=stripe_keys["PUBLIC_KEY"]) return redirect("/")And here's a part of HTML template:
<form action="/checkout" method="post" id="form_checkout" onsubmit="return cart_info()" ... <input type="hidden" name="cart_checkout" id="checkout_info" value="{{ cart_checkout }}"> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="{{ stripe_pk }}" data-name="Company Name" data-image="https://stripe.com/img/documentation/checkout/marketplace.png" data-description="A description of the product or service being purchased" data-amount="999" data-shipping-address="true" data-zip-code="true" data-allow-remember-me="true" data-panel-label="Pay" data-label="Checkout" data-locale="auto"> </script> </form>If I do a simple example of Charge from Stripe docs, like this:
@app.route('/charge', methods=['POST']) def charge(): # Amount in cents amount = 500 customer = stripe.Customer.create( email='[email protected]', source=request.form['stripeToken'] ) charge = stripe.Charge.create( customer=customer.id, amount=amount, currency='usd', description='Flask Charge' ) return render_template('charge.html', amount=amount)Then I can create without any issues a successful test Charge, it displays with the success label in my Stripe's dashboard. But it just a charge for a single product. The issue is, if I use
stripe.checkout.Session.create(), Stripe dashboard properly creates a record of an incomplete transaction from this Checkout session with the selected list of items, but I've no idea how to proceed from there to finalise the Charge for them.
If there are some more code details you require to help me to solve this issue, please ask, I'll update my post.