Skip to content

Commit

Permalink
Add shards webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed Sep 24, 2020
1 parent 32f8b0d commit 1ad88cd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"request": "launch",
"program": "${workspaceRoot}/launcher.py",
"console": "internalConsole"
},
{
"name": "Run Server",
"type": "python",
"request": "launch",
"program": "${workspaceRoot}/server.py",
"console": "internalConsole"
}
]
}
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ humanfriendly = "^8.2"
unidecode = "^1.1.1"
discord-flags = "^2.1.1"
quart = "^0.13.1"
stripe = "^2.51.0"

[tool.poetry.dev-dependencies]
black = {version = "^20.8b1", allow-prereleases = true}
Expand Down
42 changes: 41 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@
from datetime import datetime, timedelta
from functools import wraps

import stripe
from discord.ext.ipc import Client
from motor.motor_asyncio import AsyncIOMotorClient
from quart import Quart, request

loop = asyncio.get_event_loop()
# Constants

purchase_amounts = {
500: 500,
1000: 1100,
2000: 2420,
4000: 5324,
}

# Setup

stripe.api_key = os.getenv("STRIPE_KEY")
endpoint_secret = os.getenv("STRIPE_WEBHOOK_SECRET")

app = Quart(__name__)
web_ipc = Client(secret_key=os.getenv("SECRET_KEY"))

loop = asyncio.get_event_loop()
db = AsyncIOMotorClient(os.getenv("DATABASE_URI"), io_loop=loop)[
os.getenv("DATABASE_NAME")
]
Expand Down Expand Up @@ -188,5 +202,31 @@ async def dbl():
return "Success", 200


@app.route("/purchase", methods=["POST"])
async def purchase():
try:
event = stripe.Webhook.construct_event(
await request.get_data(),
request.headers["Stripe-Signature"],
endpoint_secret,
)
except ValueError as e:
return "Invalid Payload", 400
except stripe.error.SignatureVerificationError as e:
return "Invalid Signature", 400

if event.type != "payment_intent.succeeded":
return "Invalid Event", 400

session = event.data.object
uid = int(session["metadata"]["id"])
amount = session["amount"]
shards = purchase_amounts[amount]

await db.member.update_one({"_id": uid}, {"$inc": {"premium_balance": shards}})

return "Success", 200


if __name__ == "__main__":
app.run(loop=loop)

0 comments on commit 1ad88cd

Please sign in to comment.