Skip to content

Commit

Permalink
Merge pull request #904 from MikeSoft007/feat/stripe_checkout
Browse files Browse the repository at this point in the history
bugfix: update endpoint response and stripe urls
  • Loading branch information
CodewithSegNet authored Aug 16, 2024
2 parents de41edb + 6e716b8 commit 424dc05
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
15 changes: 15 additions & 0 deletions api/utils/success_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ def auth_response(status_code: int, message: str, access_token: str, data: Optio
response_data["data"] = data

return JSONResponse(status_code=status_code, content=jsonable_encoder(response_data))


def fail_response(status_code: int, message: str, data: Optional[dict] = None):
'''Returns a JSON response for success responses'''

response_data = {
"status_code": status_code,
"success": False,
"message": message
}

if data is not None:
response_data["data"] = data

return JSONResponse(status_code=status_code, content=jsonable_encoder(response_data))
10 changes: 8 additions & 2 deletions api/v1/routes/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
from api.v1.schemas.stripe import PlanUpgradeRequest
from typing import List
from api.db.database import get_db
from typing import Optional, Dict, Any
from fastapi.responses import JSONResponse
from fastapi.responses import RedirectResponse
import os
from api.utils.success_response import success_response
from api.utils.success_response import success_response, fail_response
from api.v1.models.user import User
from api.v1.services.user import user_service
from fastapi.encoders import jsonable_encoder
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())
Expand All @@ -20,6 +24,7 @@

subscription_ = APIRouter(prefix="/payment", tags=["subscribe-plan"])


@subscription_.post("/stripe/upgrade-plan")
def stripe_payment(
plan_upgrade_request: PlanUpgradeRequest,
Expand All @@ -36,6 +41,7 @@ def success_upgrade():

@subscription_.get("/stripe/cancel")
def cancel_upgrade():

return success_response(status_code=status.HTTP_200_OK, message="Payment intent canceled")


Expand Down Expand Up @@ -86,7 +92,7 @@ async def get_organisations_with_users_and_plans(db: Session = Depends(get_db),
try:
data = fetch_all_organisations_with_users_and_plans(db)
if not data:
raise HTTPException(status_code=404, detail="No data found")
return fail_response(status_code=404, message="No data found")
return success_response(
status_code=status.HTTP_200_OK,
message='billing details successfully retrieved',
Expand Down
16 changes: 9 additions & 7 deletions api/v1/services/stripe_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import select, join
from fastapi.encoders import jsonable_encoder
from api.utils.success_response import success_response
from api.utils.success_response import success_response, fail_response
import os
from fastapi import HTTPException, status, Request
from datetime import datetime, timedelta
Expand All @@ -34,20 +34,22 @@ def get_plan_by_name(db: Session, plan_name: str):

def stripe_payment_request(db: Session, user_id: str, request: Request, plan_name: str):

base_url = request.base_url
# base_url = request.base_url
# base_urls = str(request.url.scheme) + "://" + str(request.url.netloc)

success_url = f"{base_url}api/v1/payment/stripe/success"
cancel_url = f"{base_url}api/v1/payment/stripe/cancel"
base_urls = "https://anchor-python.teams.hng.tech/"
success_url = f"{base_urls}payment" + "/success?session_id={CHECKOUT_SESSION_ID}"
cancel_url = f"{base_urls}payment/cancel"

user = db.query(User).filter(User.id == user_id).first()

if not user:
raise HTTPException(status_code=404, detail="User not found")
return fail_response(status_code=404, message="User not found")

plan = get_plan_by_name(db, plan_name)

if not plan:
raise HTTPException(status_code=404, detail="Plan not found")
return fail_response(status_code=404, message="Plan not found")

if plan.name != "Free":
try:
Expand Down Expand Up @@ -99,7 +101,7 @@ def stripe_payment_request(db: Session, user_id: str, request: Request, plan_nam
raise HTTPException(status_code=500, detail=f"Payment failed: {str(e)}")

else:
raise HTTPException(status_code=400, detail="No payment is required for the Free plan")
return fail_response(status_code=400, message="No payment is required for the Free plan")


def convert_duration_to_timedelta(duration: str) -> timedelta:
Expand Down
Empty file added test_case1.py
Empty file.

0 comments on commit 424dc05

Please sign in to comment.