chore: trial subscription endpoint update (#839)

* fix: updated trial subscription endpoint

* chore: trial subscription

---------

Co-authored-by: pablohashescobar <nikhilschacko@gmail.com>
This commit is contained in:
guru_sainath
2024-08-14 21:31:40 +05:30
committed by GitHub
parent 3796766487
commit 6d8649fdbf
6 changed files with 82 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ from .views import (
WorkspaceLicenseRefreshEndpoint,
WorkspaceLicenseSyncEndpoint,
WorkspaceFreeTrialEndpoint,
WorkspaceTrialUpgradeEndpoint,
)
urlpatterns = [
@@ -68,6 +69,11 @@ urlpatterns = [
path(
"workspaces/<str:slug>/trial-subscriptions/",
WorkspaceFreeTrialEndpoint.as_view(),
name="license-refresh",
name="trial-subscriptions",
),
path(
"workspaces/<str:slug>/trial-subscriptions/upgrade/",
WorkspaceTrialUpgradeEndpoint.as_view(),
name="trial-upgrade",
),
]

View File

@@ -9,6 +9,7 @@ from .payment import (
PaymentLinkEndpoint,
WebsitePaymentLinkEndpoint,
WorkspaceFreeTrialEndpoint,
WorkspaceTrialUpgradeEndpoint,
)
from .subscription import SubscriptionEndpoint, UpgradeSubscriptionEndpoint
from .feature_flag import FeatureFlagProxyEndpoint

View File

@@ -260,3 +260,60 @@ class WorkspaceFreeTrialEndpoint(BaseAPIView):
{"error": "error fetching payment link"},
status=status.HTTP_400_BAD_REQUEST,
)
class WorkspaceTrialUpgradeEndpoint(BaseAPIView):
permission_classes = [
WorkspaceOwnerPermission,
]
def post(self, request, slug):
try:
# Get the product_id and price_id
price_id = request.data.get("price_id", False)
# Check if product_id and price_id are present
if not price_id:
return Response(
{"error": "price_id is required"},
status=status.HTTP_400_BAD_REQUEST,
)
# Get the workspace
workspace = Workspace.objects.get(slug=slug)
# Check if the payment server base url is present
if settings.PAYMENT_SERVER_BASE_URL:
response = requests.post(
f"{settings.PAYMENT_SERVER_BASE_URL}/api/trial-subscriptions/upgrade/",
headers={
"content-type": "application/json",
"x-api-key": settings.PAYMENT_SERVER_AUTH_TOKEN,
},
json={
"workspace_id": str(workspace.id),
"stripe_price_id": price_id,
},
)
# Check if the response is successful
response.raise_for_status()
# Convert the response to json
response = response.json()
return Response(response, status=status.HTTP_200_OK)
else:
return Response(
{"error": "error fetching product details"},
status=status.HTTP_400_BAD_REQUEST,
)
except requests.exceptions.RequestException as e:
if e.response.status_code == 400:
return Response(
e.response.json(),
status=status.HTTP_400_BAD_REQUEST,
)
log_exception(e)
return Response(
{"error": "error fetching payment link"},
status=status.HTTP_400_BAD_REQUEST,
)

View File

@@ -152,7 +152,7 @@ export const ProPlanCloudUpgradeModal: FC<ProPlanCloudUpgradeModalProps> = (prop
};
// handling the payment link when the free trial is enabled
const handleSubscriptionPageRedirection = () => {
const handleSubscriptionPageRedirection = (priceId: string) => {
if (!workspaceSlug) return;
if (!isAdmin) {
@@ -166,10 +166,10 @@ export const ProPlanCloudUpgradeModal: FC<ProPlanCloudUpgradeModalProps> = (prop
setLoading(true);
paymentService
.getWorkspaceSubscriptionPageLink(workspaceSlug.toString())
.modifyTrailSubscription(workspaceSlug.toString(), { price_id: priceId })
.then((response) => {
if (response.url) {
window.open(response.url, "_blank");
if (response.session_url) {
window.open(response.session_url, "_blank");
}
})
.catch(() => {
@@ -267,7 +267,7 @@ export const ProPlanCloudUpgradeModal: FC<ProPlanCloudUpgradeModalProps> = (prop
features={PRO_PLAN_FEATURES_MAP}
isLoading={isLoading}
handlePaymentLink={(priceId: string) =>
isInTrailPeriod ? handleSubscriptionPageRedirection() : handlePaymentLink(priceId)
isInTrailPeriod ? handleSubscriptionPageRedirection(priceId) : handlePaymentLink(priceId)
}
yearlyPlanOnly={yearlyPlan}
trialLoader={trialLoader}

View File

@@ -72,4 +72,15 @@ export class PaymentService extends APIService {
throw error?.response?.data;
});
}
async modifyTrailSubscription(
workspaceSlug: string,
payload: { price_id: string }
): Promise<{ session_url: string }> {
return this.post(`/api/payments/workspaces/${workspaceSlug}/trial-subscriptions/upgrade/`, payload)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}
}

View File

@@ -36,6 +36,7 @@ export class WorkspaceSubscriptionStore implements IWorkspaceSubscriptionStore {
toggleProPlanModal: action,
fetchWorkspaceSubscribedPlan: action,
refreshWorkspaceSubscribedPlan: action,
freeTrialSubscription: action,
});
}