'Where can I write server code for stripe payment in Django project?
I am working on React JS to implement stripe payment gateway. I am using Django as a backend Server. In stripe documentation. https://stripe.com/docs/checkout/integration-builder?server=python
there is following code given for implementation
code for react App.js
import React, { useState, useEffect } from "react";
import "./App.css";
const ProductDisplay = () => (
<section>
<div className="product">
<img
src="https://i.imgur.com/EHyR2nP.png"
alt="The cover of Stubborn Attachments"
/>
<div className="description">
<h3>Stubborn Attachments</h3>
<h5>$20.00</h5>
</div>
</div>
<form action="/create-checkout-session" method="POST">
<button type="submit">
Checkout
</button>
</form>
</section>
);
const Message = ({ message }) => (
<section>
<p>{message}</p>
</section>
);
export default function App() {
const [message, setMessage] = useState("");
useEffect(() => {
// Check to see if this is a redirect back from Checkout
const query = new URLSearchParams(window.location.search);
if (query.get("success")) {
setMessage("Order placed! You will receive an email confirmation.");
}
if (query.get("canceled")) {
setMessage(
"Order canceled -- continue to shop around and checkout when you're ready."
);
}
}, []);
return message ? (
<Message message={message} />
) : (
<ProductDisplay />
);
}
code for python server.py
"""
server.py
Stripe Sample.
Python 3.6 or newer required.
"""
import os
from flask import Flask, redirect, request
import stripe
# This is your real test secret API key.
stripe.api_key = 'my key'
app = Flask(__name__,
static_url_path='',
static_folder='public')
YOUR_DOMAIN = 'http://localhost:3000/checkout'
@app.route('/create-checkout-session', methods=['POST'])
def create_checkout_session():
try:
checkout_session = stripe.checkout.Session.create(
line_items=[
{
# TODO: replace this with the `price` of the product you want to sell
'price': '{{PRICE_ID}}',
'quantity': 1,
},
],
payment_method_types=[
'card',
],
mode='payment',
success_url=YOUR_DOMAIN + '?success=true',
cancel_url=YOUR_DOMAIN + '?canceled=true',
)
except Exception as e:
return str(e)
return redirect(checkout_session.url, code=303)
if __name__ == '__main__':
app.run(port=4242)
I can't understand where to write this server.py code in my django project. any help will be appreciated. My django project hierarchy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|