mirror of
https://github.com/TitanEmbeds/Titan.git
synced 2025-06-16 19:35:24 +02:00
Paypal (#41)
* Implement Titan Tokens clientside * Titan Tokens can be modified in the admin panel
This commit is contained in:
committed by
GitHub
parent
d363b66799
commit
8bc95f7b0e
@ -1,10 +1,12 @@
|
||||
from flask import Blueprint, request, redirect, jsonify, abort, session, url_for, render_template
|
||||
from flask import current_app as app
|
||||
from config import config
|
||||
from titanembeds.decorators import discord_users_only
|
||||
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, Cosmetics, UserCSS
|
||||
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, Cosmetics, UserCSS, set_titan_token, get_titan_token
|
||||
from titanembeds.oauth import authorize_url, token_url, make_authenticated_session, get_current_authenticated_user, get_user_managed_servers, check_user_can_administrate_guild, check_user_permission, generate_avatar_url, generate_guild_icon_url, generate_bot_invite_url
|
||||
import time
|
||||
import datetime
|
||||
import paypalrestsdk
|
||||
|
||||
user = Blueprint("user", __name__)
|
||||
|
||||
@ -70,7 +72,10 @@ def dashboard():
|
||||
css_list = None
|
||||
if cosmetics and cosmetics.css:
|
||||
css_list = db.session.query(UserCSS).filter(UserCSS.user_id == session['user_id']).all()
|
||||
return render_template("dashboard.html.j2", servers=guilds, icon_generate=generate_guild_icon_url, cosmetics=cosmetics, css_list=css_list)
|
||||
tokens = get_titan_token(session["user_id"])
|
||||
if tokens == -1:
|
||||
tokens = 0
|
||||
return render_template("dashboard.html.j2", servers=guilds, icon_generate=generate_guild_icon_url, cosmetics=cosmetics, css_list=css_list, tokens=tokens)
|
||||
|
||||
@user.route("/custom_css/new", methods=["GET"])
|
||||
@discord_users_only()
|
||||
@ -322,3 +327,69 @@ def revoke_unauthenticated_user():
|
||||
abort(409)
|
||||
db_user.revokeUser()
|
||||
return ('', 204)
|
||||
|
||||
@user.route('/donate', methods=["GET"])
|
||||
@discord_users_only()
|
||||
def donate_get():
|
||||
return render_template('donate.html.j2')
|
||||
|
||||
def get_paypal_api():
|
||||
return paypalrestsdk.Api({
|
||||
'mode': 'sandbox' if app.config["DEBUG"] else 'live',
|
||||
'client_id': config["paypal-client-id"],
|
||||
'client_secret': config["paypal-client-secret"]})
|
||||
|
||||
@user.route('/donate', methods=['POST'])
|
||||
@discord_users_only()
|
||||
def donate_post():
|
||||
donation_amount = request.form.get('amount')
|
||||
if not donation_amount:
|
||||
abort(402)
|
||||
|
||||
donation_amount = "{0:.2f}".format(float(donation_amount))
|
||||
payer = {"payment_method": "paypal"}
|
||||
items = [{"name": "TitanEmbeds Donation",
|
||||
"price": donation_amount,
|
||||
"currency": "USD",
|
||||
"quantity": "1"}]
|
||||
amount = {"total": donation_amount,
|
||||
"currency": "USD"}
|
||||
description = "Donate and support TitanEmbeds development."
|
||||
redirect_urls = {"return_url": url_for('user.donate_confirm', success="true", _external=True),
|
||||
"cancel_url": url_for('index', _external=True)}
|
||||
payment = paypalrestsdk.Payment({"intent": "sale",
|
||||
"payer": payer,
|
||||
"redirect_urls": redirect_urls,
|
||||
"transactions": [{"item_list": {"items":
|
||||
items},
|
||||
"amount": amount,
|
||||
"description":
|
||||
description}]}, api=get_paypal_api())
|
||||
if payment.create():
|
||||
for link in payment.links:
|
||||
if link['method'] == "REDIRECT":
|
||||
return redirect(link["href"])
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@user.route("/donate/confirm")
|
||||
@discord_users_only()
|
||||
def donate_confirm():
|
||||
if not request.args.get('success'):
|
||||
return redirect(url_for('index'))
|
||||
payment = paypalrestsdk.Payment.find(request.args.get('paymentId'), api=get_paypal_api())
|
||||
if payment.execute({"payer_id": request.args.get('PayerID')}):
|
||||
trans_id = str(payment.transactions[0]["related_resources"][0]["sale"]["id"])
|
||||
amount = float(payment.transactions[0]["amount"]["total"])
|
||||
tokens = int(amount * 100)
|
||||
action = "PAYPAL {}".format(trans_id)
|
||||
set_titan_token(session["user_id"], tokens, action)
|
||||
return redirect(url_for('user.donate_thanks', transaction=trans_id))
|
||||
else:
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@user.route("/donate/thanks")
|
||||
@discord_users_only()
|
||||
def donate_thanks():
|
||||
tokens = get_titan_token(session["user_id"])
|
||||
transaction = request.args.get("transaction")
|
||||
return render_template("donate_thanks.html.j2", tokens=tokens, transaction=transaction)
|
Reference in New Issue
Block a user