-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added endpoint for stripe checkout payment
- Loading branch information
1 parent
856236f
commit 9ac8cb0
Showing
12 changed files
with
412 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
alembic/versions/2e1bdb317917_added_models_to_support_billing_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""added models to support billing subscription | ||
Revision ID: 2e1bdb317917 | ||
Revises: 95a09b7f5c2c | ||
Create Date: 2024-08-10 18:53:02.873361 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '2e1bdb317917' | ||
down_revision: Union[str, None] = '95a09b7f5c2c' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### |
44 changes: 44 additions & 0 deletions
44
alembic/versions/5048f25d8274_added_models_to_support_billing_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""added models to support billing subscription | ||
Revision ID: 5048f25d8274 | ||
Revises: ff92a0037698 | ||
Create Date: 2024-08-10 18:15:12.101521 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '5048f25d8274' | ||
down_revision: Union[str, None] = 'ff92a0037698' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('user_subscriptions', | ||
sa.Column('user_id', sa.String(), nullable=False), | ||
sa.Column('plan_id', sa.String(), nullable=False), | ||
sa.Column('active', sa.Boolean(), nullable=True), | ||
sa.Column('start_date', sa.String(), nullable=False), | ||
sa.Column('end_date', sa.String(), nullable=True), | ||
sa.Column('id', sa.String(), nullable=False), | ||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), | ||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), | ||
sa.ForeignKeyConstraint(['plan_id'], ['billing_plans.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_index(op.f('ix_user_subscriptions_id'), 'user_subscriptions', ['id'], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_user_subscriptions_id'), table_name='user_subscriptions') | ||
op.drop_table('user_subscriptions') | ||
# ### end Alembic commands ### |
30 changes: 30 additions & 0 deletions
30
alembic/versions/95a09b7f5c2c_added_models_to_support_billing_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""added models to support billing subscription | ||
Revision ID: 95a09b7f5c2c | ||
Revises: 5048f25d8274 | ||
Create Date: 2024-08-10 18:47:09.891441 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '95a09b7f5c2c' | ||
down_revision: Union[str, None] = '5048f25d8274' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### |
30 changes: 30 additions & 0 deletions
30
alembic/versions/b7e8db61eb86_updated_billing_model_ensuing_that_plan_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""updated billing model ensuing that plan name is unique | ||
Revision ID: b7e8db61eb86 | ||
Revises: 2e1bdb317917 | ||
Create Date: 2024-08-11 09:55:41.558757 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = 'b7e8db61eb86' | ||
down_revision: Union[str, None] = '2e1bdb317917' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_unique_constraint(None, 'billing_plans', ['name']) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(None, 'billing_plans', type_='unique') | ||
# ### end Alembic commands ### |
30 changes: 30 additions & 0 deletions
30
alembic/versions/c9d13f346b57_updated_billing_model_ensuing_that_plan_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""updated billing model ensuing that plan name is unique | ||
Revision ID: c9d13f346b57 | ||
Revises: b7e8db61eb86 | ||
Create Date: 2024-08-11 10:04:26.675695 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = 'c9d13f346b57' | ||
down_revision: Union[str, None] = 'b7e8db61eb86' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from fastapi import APIRouter, Depends, HTTPException, Request | ||
from sqlalchemy.orm import Session | ||
import stripe | ||
from api.v1.services.stripe_payment import stripe_payment_request, update_user_plan | ||
import json | ||
from api.v1.schemas.stripe import PlanUpgradeRequest | ||
from api.db.database import get_db | ||
import os | ||
from api.v1.models.user import User | ||
from api.v1.services.user import user_service | ||
from dotenv import load_dotenv, find_dotenv | ||
|
||
load_dotenv(find_dotenv()) | ||
|
||
stripe.api_key = os.getenv('STRIPE_SECRET_KEY') | ||
endpoint_secret = os.getenv('STRIPE_WEBHOOK_SECRET') | ||
|
||
subscription_ = APIRouter(prefix="/payment", tags=["subscribe-plan"]) | ||
|
||
@subscription_.post("/stripe/upgrade-plan") | ||
def stripe_payment( | ||
plan_upgrade_request: PlanUpgradeRequest, | ||
request: Request, | ||
db: Session = Depends(get_db), | ||
current_user: User = Depends(user_service.get_current_user) | ||
): | ||
return stripe_payment_request(db, plan_upgrade_request.user_id, request, plan_upgrade_request.plan_name) | ||
|
||
@subscription_.get("/stripe/success") | ||
def success_upgrade(): | ||
return {"message" : "Payment successful"} | ||
|
||
@subscription_.get("/stripe/cancel") | ||
def cancel_upgrade(): | ||
return {"message" : "Payment canceled"} | ||
|
||
@subscription_.post("/webhook") | ||
async def webhook_received( | ||
request: Request, | ||
db: Session = Depends(get_db) | ||
): | ||
|
||
payload = await request.body() | ||
event = None | ||
|
||
try: | ||
event = stripe.Event.construct_from(json.loads(payload), stripe.api_key) | ||
except ValueError as e: | ||
print("Invalid payload") | ||
raise HTTPException(status_code=400, detail="Invalid payload") | ||
except stripe.error.SignatureVerificationError as e: | ||
print("Invalid signature") | ||
raise HTTPException(status_code=400, detail="Invalid signature") | ||
|
||
if event["type"] == "checkout.session.completed": | ||
payment = event["data"]["object"] | ||
response_details = { | ||
"amount": payment["amount_total"], | ||
"currency": payment["currency"], | ||
"user_id": payment["metadata"]["user_id"], | ||
"user_email": payment["customer_details"]["email"], | ||
"user_name": payment["customer_details"]["name"], | ||
"order_id": payment["id"] | ||
} | ||
# Save to DB | ||
# Send email in background task | ||
await update_user_plan(db, payment["metadata"]["user_id"], payment["metadata"]["plan_name"]) | ||
return {"message": response_details} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel, Field, validator | ||
|
||
|
||
class PaymentInfo(BaseModel): | ||
card_number: str = Field(..., min_length=16, max_length=16) | ||
exp_month: int | ||
exp_year: int | ||
cvc: str = Field(..., min_length=3, max_length=4) | ||
|
||
@validator('card_number') | ||
def card_number_validator(cls, v): | ||
if not v.isdigit() or len(v) != 16: | ||
raise ValueError('Card number must be 16 digits') | ||
return v | ||
|
||
@validator('cvc') | ||
def cvc_validator(cls, v): | ||
if not v.isdigit() or not (3 <= len(v) <= 4): | ||
raise ValueError('CVC must be 3 or 4 digits') | ||
return v | ||
|
||
|
||
class PlanUpgradeRequest(BaseModel): | ||
user_id: str | ||
plan_name: str | ||
payment_info: Optional[PaymentInfo] = None | ||
|
||
|
Oops, something went wrong.