def charge_up_front(): jamla = get_jamla() jamlaApp = Jamla() jamlaApp.load(jamla=jamla) charge = {} charge["amount"] = session["upfront_cost"] charge["currency"] = "GBP" sid = session["sid"] db = get_db() res = db.execute("SELECT * FROM person p WHERE p.sid = ?", (sid, )).fetchone() try: stripe.api_key = jamla["payment_providers"]["stripe"]["secret_key"] customer = stripe.Customer.create(email=res["email"], source=request.form["stripeToken"]) charge = stripe.Charge.create( customer=customer.id, amount=charge["amount"], currency=charge["currency"], description="Subscribie", ) except stripe.error.AuthenticationError as e: return str(e) if jamlaApp.requires_subscription(session["package"]): return redirect(url_for("views.establish_mandate")) else: return redirect( url_for("views.thankyou", _scheme="https", _external=True))
def establish_mandate(): jamla = get_jamla() jamlaApp = Jamla() jamlaApp.load(jamla=jamla) if jamlaApp.has_connected("gocardless") is False: dashboard_url = url_for("admin.dashboard") return """<h1>Shop not set-up yet</h1> The shop owner first needs to login to their <a href="{}">dahboard</a>, and connect GoCardless to their shop. Once this has been completed, you will be able to order. """.format(dashboard_url) # lookup the customer with sid and get their relevant details sid = session["sid"] db = get_db() res = db.execute("SELECT * FROM person p WHERE p.sid = ?", (sid, )).fetchone() logger.info("Person lookup: %s", res) # validate that hasInstantPaid is true for the customer gocclient = gocardless_pro.Client( access_token=jamlaApp.get_secret("gocardless", "access_token"), environment=jamla["payment_providers"]["gocardless"]["environment"], ) description = " ".join([jamla["company"]["name"], session["package"]])[0:100] redirect_flow = gocclient.redirect_flows.create( params={ "description": description, "session_token": sid, "success_redirect_url": current_app.config["SUCCESS_REDIRECT_URL"], "prefilled_customer": { "given_name": res["given_name"], "family_name": res["family_name"], "address_line1": res["address_line1"], "city": res["city"], "postal_code": res["postal_code"], "email": res["email"], }, }) # Hold on to this ID - we'll need it when we # "confirm" the dedirect flow later print("ID: {} ".format(redirect_flow.id)) print("URL: {} ".format(redirect_flow.redirect_url)) # Check if we're inside an iframe, if yes redirect to pop-up # Issue https://github.com/Subscribie/subscribie/issues/128 if request.args.get('inside_iframe', 'False') == "True": inside_iframe = True return render_template("iframe_new_window_redirect.html", redirect_url=redirect_flow.redirect_url, jamla=jamla) return '<a href="{}" target="_blank">Continue</a>'.format( redirect_flow.redirect_url) else: return redirect(redirect_flow.redirect_url)
def getPathTitle(path): """Return page title of a given path, or None""" db = get_db() res = db.execute('SELECT title FROM module_seo_page_title WHERE path = ?', (path, )).fetchone() if res is None: return '' else: title = res[0] return title
def load_logged_in_user(): user_id = session.get("user_id") if user_id is None: g.user = None else: g.user = ( get_db() .execute("SELECT email, active FROM user WHERE email = ?", (user_id,)) .fetchone() )
def generate_login_url(email): db = get_db() result = db.execute("SELECT COUNT(*) FROM user WHERE email=?", (email,)).fetchone() if result is False: return "Invalid valid user" # Generate login token login_token = urlsafe_b64encode(os.urandom(24)).decode("utf-8") # Insert login token into db db.execute( """ UPDATE user SET login_token= ? WHERE email= ? """, (login_token, email) ) db.commit() login_url = "".join([request.host_url, "auth/login/", login_token]) print("One-time login url: {}".format(login_url)) return login_url
def do_login(login_token): if len(login_token) < 10: return "Invalid token" # Try to get email from login_token db = get_db() error = None user = db.execute( "SELECT email FROM user WHERE login_token=?", (login_token,) ).fetchone() if user is None: error = "Incorrect login token" if error is None: session.clear() session["user_id"] = user["email"] # Invaldate previous token new_login_token = urlsafe_b64encode(os.urandom(24)) db.execute( "UPDATE user SET login_token=? WHERE login_token=?", (new_login_token, login_token), ) db.commit() return redirect(url_for("admin.dashboard"))
def store_customer(): form = CustomerForm() if form.validate(): given_name = form.data["given_name"] family_name = form.data["family_name"] address_line_one = form.data["address_line_one"] city = form.data["city"] postcode = form.data["postcode"] email = form.data["email"] mobile = form.data["mobile"] now = datetime.datetime.now() # Store customer in session sid = session["sid"] # Store email in session session["email"] = email # Store plan in session jamlaApp = Jamla() jamla = get_jamla() jamlaApp.load(jamla=jamla) if jamlaApp.sku_exists(request.args.get("plan")): wants = request.args.get("plan") session["plan"] = wants db = get_db() db.execute( "INSERT INTO person VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ( sid, now, given_name, family_name, address_line_one, city, postcode, email, mobile, wants, "null", "null", False, ), ) db.commit() # Store note to seller in session if there is one note_to_seller = form.data["note_to_seller"] session["note_to_seller"] = note_to_seller if jamlaApp.requires_instantpayment(session["package"]): return redirect( url_for( "views.up_front", _scheme="https", _external=True, sid=sid, package=wants, fname=given_name, )) if jamlaApp.requires_subscription(session["package"]): # Check if in iframe if form.data["is_iframe"] == "True": insideIframe = True else: insideIframe = False return redirect( url_for("views.establish_mandate", inside_iframe=insideIframe)) return redirect( url_for("views.thankyou", _scheme="https", _external=True)) else: return "Oops, there was an error processing that form, please go back and try again."