Spaces:
Paused
Paused
Commit ·
4369d38
1
Parent(s): 345c00f
payment
Browse files- src/database/model.py +40 -2
- src/main.py +38 -1
- src/test.py +14 -0
src/database/model.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# coding: utf-8
|
|
|
|
| 3 |
import math
|
| 4 |
import os
|
| 5 |
from contextlib import contextmanager
|
|
@@ -21,7 +22,12 @@ from sqlalchemy.orm import relationship, sessionmaker
|
|
| 21 |
|
| 22 |
from config import FREE_DOWNLOAD
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
Base = declarative_base()
|
|
@@ -57,7 +63,16 @@ class Payment(Base):
|
|
| 57 |
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 58 |
method = Column(String(50), nullable=False)
|
| 59 |
amount = Column(Float, nullable=False)
|
| 60 |
-
status = Column(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
| 62 |
|
| 63 |
user = relationship("User", back_populates="payments")
|
|
@@ -181,3 +196,26 @@ def reset_free():
|
|
| 181 |
for user in users:
|
| 182 |
user.free = FREE_DOWNLOAD
|
| 183 |
session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# coding: utf-8
|
| 3 |
+
import logging
|
| 4 |
import math
|
| 5 |
import os
|
| 6 |
from contextlib import contextmanager
|
|
|
|
| 22 |
|
| 23 |
from config import FREE_DOWNLOAD
|
| 24 |
|
| 25 |
+
|
| 26 |
+
class PaymentStatus:
|
| 27 |
+
PENDING = "pending"
|
| 28 |
+
COMPLETED = "completed"
|
| 29 |
+
FAILED = "failed"
|
| 30 |
+
REFUNDED = "refunded"
|
| 31 |
|
| 32 |
|
| 33 |
Base = declarative_base()
|
|
|
|
| 63 |
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 64 |
method = Column(String(50), nullable=False)
|
| 65 |
amount = Column(Float, nullable=False)
|
| 66 |
+
status = Column(
|
| 67 |
+
Enum(
|
| 68 |
+
PaymentStatus.PENDING,
|
| 69 |
+
PaymentStatus.COMPLETED,
|
| 70 |
+
PaymentStatus.FAILED,
|
| 71 |
+
PaymentStatus.REFUNDED,
|
| 72 |
+
),
|
| 73 |
+
nullable=False,
|
| 74 |
+
)
|
| 75 |
+
transaction_id = Column(String(100))
|
| 76 |
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
| 77 |
|
| 78 |
user = relationship("User", back_populates="payments")
|
|
|
|
| 196 |
for user in users:
|
| 197 |
user.free = FREE_DOWNLOAD
|
| 198 |
session.commit()
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def credit_account(who, total_amount, transaction, method="stripe"):
|
| 202 |
+
with session_manager() as session:
|
| 203 |
+
user = session.query(User).filter(User.user_id == who).first()
|
| 204 |
+
if user:
|
| 205 |
+
dollar = total_amount / 100
|
| 206 |
+
price = int(os.getenv("TOKEN_PRICE")) # per one dollar
|
| 207 |
+
user.paid += int(dollar * price)
|
| 208 |
+
logging.info("user %d credited with %d tokens, payment:$%.2f", who, user.paid, dollar)
|
| 209 |
+
session.add(
|
| 210 |
+
Payment(
|
| 211 |
+
method=method,
|
| 212 |
+
amount=total_amount,
|
| 213 |
+
status=PaymentStatus.COMPLETED,
|
| 214 |
+
transaction_id=transaction,
|
| 215 |
+
user_id=user.id,
|
| 216 |
+
)
|
| 217 |
+
)
|
| 218 |
+
session.commit()
|
| 219 |
+
return user.free, user.paid
|
| 220 |
+
|
| 221 |
+
return None, None
|
src/main.py
CHANGED
|
@@ -7,8 +7,8 @@
|
|
| 7 |
|
| 8 |
__author__ = "Benny <benny.think@gmail.com>"
|
| 9 |
|
| 10 |
-
import contextlib
|
| 11 |
import logging
|
|
|
|
| 12 |
import re
|
| 13 |
import threading
|
| 14 |
import time
|
|
@@ -36,6 +36,7 @@ from config import (
|
|
| 36 |
BotText,
|
| 37 |
)
|
| 38 |
from database.model import (
|
|
|
|
| 39 |
get_format_settings,
|
| 40 |
get_free_quota,
|
| 41 |
get_paid_quota,
|
|
@@ -144,6 +145,42 @@ def ping_handler(client: Client, message: types.Message):
|
|
| 144 |
thread.start()
|
| 145 |
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
@app.on_message(filters.command(["stats"]))
|
| 148 |
def stats_handler(client: Client, message: types.Message):
|
| 149 |
chat_id = message.chat.id
|
|
|
|
| 7 |
|
| 8 |
__author__ = "Benny <benny.think@gmail.com>"
|
| 9 |
|
|
|
|
| 10 |
import logging
|
| 11 |
+
import os
|
| 12 |
import re
|
| 13 |
import threading
|
| 14 |
import time
|
|
|
|
| 36 |
BotText,
|
| 37 |
)
|
| 38 |
from database.model import (
|
| 39 |
+
credit_account,
|
| 40 |
get_format_settings,
|
| 41 |
get_free_quota,
|
| 42 |
get_paid_quota,
|
|
|
|
| 145 |
thread.start()
|
| 146 |
|
| 147 |
|
| 148 |
+
@app.on_message(filters.command(["buy"]))
|
| 149 |
+
def buy(client: Client, message: types.Message):
|
| 150 |
+
chat_id = message.chat.id
|
| 151 |
+
logging.info("Generating invoice for %s", chat_id)
|
| 152 |
+
price = os.getenv("TOKEN_PRICE")
|
| 153 |
+
client.send_invoice(
|
| 154 |
+
chat_id,
|
| 155 |
+
f"{price} permanent download",
|
| 156 |
+
"Please make a payment via Stripe",
|
| 157 |
+
f"vip-{chat_id}",
|
| 158 |
+
"USD",
|
| 159 |
+
[types.LabeledPrice(label="VIP", amount=100)],
|
| 160 |
+
provider_token=os.getenv("PROVIDER_TOKEN"),
|
| 161 |
+
protect_content=True,
|
| 162 |
+
start_parameter="no-forward",
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
@app.on_pre_checkout_query()
|
| 167 |
+
def pre_checkout(client: Client, query: types.PreCheckoutQuery):
|
| 168 |
+
client.answer_pre_checkout_query(query.id, ok=True)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
@app.on_message(filters.successful_payment)
|
| 172 |
+
def successful_payment(client: Client, message: types.Message):
|
| 173 |
+
who = message.chat.id
|
| 174 |
+
amount = message.successful_payment.total_amount # in cents
|
| 175 |
+
ch = message.successful_payment.provider_payment_charge_id
|
| 176 |
+
free, paid = credit_account(who, amount, ch)
|
| 177 |
+
if paid > 0:
|
| 178 |
+
message.reply_text(f"Payment successful! You now have {free} free and {paid} paid quota.")
|
| 179 |
+
else:
|
| 180 |
+
message.reply_text("Something went wrong. Please contact the admin.")
|
| 181 |
+
message.delete()
|
| 182 |
+
|
| 183 |
+
|
| 184 |
@app.on_message(filters.command(["stats"]))
|
| 185 |
def stats_handler(client: Client, message: types.Message):
|
| 186 |
chat_id = message.chat.id
|
src/test.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# ytdlbot - test.py
|
| 5 |
+
|
| 6 |
+
import yt_dlp
|
| 7 |
+
|
| 8 |
+
url = "https://www.youtube.com/watch?v=e19kTVgb2c8"
|
| 9 |
+
opts = {
|
| 10 |
+
"cookiefile": "cookies.txt",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 14 |
+
ydl.download([url])
|