Guest User

Untitled

a guest
Nov 6th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. import stripe
  2. from flask import Blueprint, flash, redirect, render_template, request, url_for
  3. from flask_login import current_user
  4.  
  5. from app.payment.forms import EmailForm
  6.  
  7. payment = Blueprint('payment', __name__, template_folder='templates')
  8. # import db from  folder in __init__.py.
  9. from app import db
  10. from app.models import Payments, User
  11.  
  12. '''
  13. # This is before the table was created.
  14. products = {
  15.    'donations':
  16.    {
  17.        'name': 'Donation for the site',
  18.        'price': 500, # 500 is = 5.00 , how do I use a counter? Answer turn into a table in a database
  19.    }
  20. }
  21. '''
  22. from app.payment.functions import add_foreign_key
  23. @payment.route('/donations', methods = ['POST', 'GET'])
  24. def donations():
  25.     form = EmailForm()
  26.     if form.validate_on_submit():
  27.         '''
  28.        Start off as a decimal float then you mulitply by 100 to get the cents. An ex int ex .55 then get 55.0,
  29.        then convert from float to int then to string because request.form uses str/strings.
  30.        '''
  31.         flash('price_of_donation_form')
  32.         if not request.form["number"]: # empty form
  33.             error = 'Please type in an amount to donate.'
  34.             return render_template('stripe_payment/donations.html', form=form, title='Give donations', error=error)        
  35.                
  36.         # The reason you do the converting from a decimal float to a int because sql can't store decimals.
  37.         price_of_donation_form = str(int(float(request.form["number"]) *100) ) # Make global variable?
  38.         email_form = form.email.data
  39.         print(email_form)
  40.         add_payment_db = Payments(price_of_donation=price_of_donation_form, item_name='Donate', email=email_form)
  41.         db.session.add(add_payment_db)
  42.         db.session.commit()
  43.  
  44.  
  45.         # Check if the user is logged in. Then check if user_db exists/None equal to None.
  46.         # Then compare the if the logged in user's email matches the  email in the form.
  47.         if current_user.is_authenticated:
  48.             flash('user is logged in')
  49.             user_db = db.session.execute(db.select(User).where(User.email==current_user.email)).scalar_one_or_none()
  50.             if user_db and user_db.email and user_db.email != email_form:
  51.                 flash("The email you type does not match your logged on email. Please use your logged in email.")
  52.                 return redirect(url_for('main.home'))
  53.  
  54.    
  55.         payment_id = add_payment_db.id
  56.         add_foreign_key(email_form)
  57.         # I need to query id because that is the only thing that is unique in the db
  58.         payment_db = db.one_or_404(db.select(Payments).filter_by(id=payment_id))
  59.         # 307 allows the redirects to redirect to a POST request          
  60.         return redirect(url_for('payment.order', payment_db_id=payment_db.id), code=307)
  61.    
  62.     error = None # empty, the if statement won't work  
  63.     return render_template('stripe_payment/donations.html', form=form, title='Give donations', error=error)
  64.  
  65. # Is the route secure with just id?
  66. @payment.route('/order/<payment_db_id>', methods=['POST'])
  67. def order(payment_db_id):
  68.     # I need to query id because that is the only thing that is unique in the db
  69.     payment_db = db.one_or_404(db.select(Payments).filter_by(id=payment_db_id))
  70.     '''
  71.    you can only purchase one product at a time, but since line_items is a list,
  72.    you can select and buy multiple products if you add a shopping cart interface
  73.    '''
  74.     checkout_session = stripe.checkout.Session.create(  
  75.         # The line_items argument specifies the product that the user wishes to buy.
  76.         line_items=[
  77.             {
  78.                 'price_data': {
  79.                     'product_data': {
  80.                         'name': payment_db.item_name,
  81.                     },
  82.                  
  83.                     # automatically converts to decimals/float
  84.                     'unit_amount': payment_db.price_of_donation,
  85.                     'currency': 'usd',
  86.                 },
  87.                 'quantity': 1,
  88.             },
  89.         ],        
  90.         # prefill the email input in the form.
  91.         # I use this so I don't have 2 different emails in 2 different forms.  
  92.         customer_email=payment_db.email,
  93.         # payment_method_types argument allows what payment you want/allow.
  94.         payment_method_types=['card'],
  95.         # mode specifies what type of payment you want. An example is payment is a one time payment.
  96.         mode='payment',
  97.         # stripe will redirect to one of these pages upon the form completion. How?
  98.         success_url=request.host_url + 'order/success',
  99.         cancel_url=request.host_url + 'order/cancel',
  100.     )
  101.     return redirect(checkout_session.url)
  102.  
  103.  
  104.  
  105.  
  106. @payment.route('/order/success')
  107. def success():
  108.     return render_template('success.html')
  109.  
  110.  
  111. @payment.route('/order/cancel')
  112. def cancel():
  113.     # send email
  114.     return render_template('cancel.html')
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.    
Advertisement
Add Comment
Please, Sign In to add comment