-
Notifications
You must be signed in to change notification settings - Fork 11.7k
feat: Implement variable pricing calculation engine #23737 #23853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
dharanidharansr
wants to merge
7
commits into
calcom:main
from
dharanidharansr:feature/variable-pricing
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bfcff17
feat: Implement variable pricing calculation engine
dharanidharansr af768fe
Fix ESLint warnings in variable pricing implementation
dharanidharansr 6ce90af
Add implementation plan to fully address linked issues
dharanidharansr 9288b83
Fixed linting issues in variable pricing implementation
dharanidharansr 1ebd75e
Fix VariablePricingModal.tsx based on code review feedback
dharanidharansr 0af0317
Fix VariablePricingModal.tsx based on code review feedback3
dharanidharansr 01c16c5
Fix VariablePricingModal.tsx based on code review feedback and add ST…
dharanidharansr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,149 @@ | ||
| import { useMemo, useState } from "react"; | ||
| import type { FormValues } from "react-hook-form"; | ||
| import { useFormContext } from "react-hook-form"; | ||
|
|
||
| import { useLocale } from "@calcom/lib/hooks/useLocale"; | ||
| import { trpc } from "@calcom/trpc/react"; | ||
| import { Button, Spinner } from "@calcom/ui"; | ||
|
|
||
| interface VariablePricingPaymentProps { | ||
| eventTypeId: number; | ||
| bookingForm: FormValues; | ||
| onSuccess: (priceId: string, price: number, currency: string, metadata: Record<string, string>) => void; | ||
| onError: (error: Error) => void; | ||
| } | ||
|
|
||
| export function VariablePricingPayment({ | ||
| eventTypeId, | ||
| bookingForm, | ||
| onSuccess, | ||
| onError, | ||
| }: VariablePricingPaymentProps) { | ||
| const { t } = useLocale(); | ||
| const [isCalculating, setIsCalculating] = useState(true); | ||
| const [isCreatingPrice, setIsCreatingPrice] = useState(false); | ||
| const [calculatedPrice, setCalculatedPrice] = useState<{ | ||
| price: number; | ||
| currency: string; | ||
| breakdown: Array<{ description: string; amount: number; type: string }>; | ||
| } | null>(null); | ||
|
|
||
| const formValues = useFormContext<FormValues>()?.getValues() || {}; | ||
|
|
||
| // Get the stripeAccountId from the event type | ||
| const { data: eventType } = trpc.viewer.eventTypes.get.useQuery({ | ||
| id: eventTypeId, | ||
| }); | ||
|
|
||
| const stripeAppData = useMemo(() => { | ||
| if (!eventType?.metadata?.apps?.stripe) return null; | ||
| return eventType.metadata.apps.stripe; | ||
| }, [eventType]); | ||
|
|
||
| const stripeAccountId = stripeAppData?.stripe_user_id; | ||
|
|
||
| // Calculate the price based on form values | ||
| const { isLoading } = trpc.viewer.eventTypes.pricing.calculatePrice.useQuery( | ||
| { | ||
| eventTypeId, | ||
| formValues, | ||
| }, | ||
| { | ||
| enabled: !!eventTypeId, | ||
| onSuccess: (data) => { | ||
| setCalculatedPrice({ | ||
| price: data.totalPrice, | ||
| currency: data.currency, | ||
| breakdown: data.breakdown, | ||
| }); | ||
| setIsCalculating(false); | ||
| }, | ||
| onError: (err) => { | ||
| setIsCalculating(false); | ||
| onError(new Error(err.message)); | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| // Get or create a Stripe price ID for this booking | ||
| const createPriceMutation = trpc.viewer.payments.stripe.calculateAndCreatePrice.useMutation({ | ||
| onSuccess: (data) => { | ||
| setIsCreatingPrice(false); | ||
| onSuccess(data.priceId, data.price, data.currency, data.metadata); | ||
| }, | ||
| onError: (err) => { | ||
| setIsCreatingPrice(false); | ||
| onError(new Error(err.message)); | ||
| }, | ||
| }); | ||
|
|
||
| // Handle clicking the pay button | ||
| const handlePayClick = () => { | ||
| if (!calculatedPrice || !stripeAccountId) return; | ||
|
|
||
| setIsCreatingPrice(true); | ||
|
|
||
| // Create a Stripe price object for this booking | ||
| createPriceMutation.mutate({ | ||
| eventTypeId, | ||
| formValues, | ||
| duration: bookingForm.duration || 30, | ||
| startTime: bookingForm.startTime, | ||
| endTime: bookingForm.endTime, | ||
| stripeAccountId, | ||
| }); | ||
| }; | ||
|
|
||
| // Format price for display | ||
| const formatPrice = (amount: number, currency: string) => { | ||
| return new Intl.NumberFormat("en-US", { | ||
| style: "currency", | ||
| currency: currency, | ||
| minimumFractionDigits: 2, | ||
| }).format(amount / 100); | ||
| }; | ||
|
|
||
| if (isCalculating || isLoading) { | ||
| return ( | ||
| <div className="flex flex-col items-center py-8"> | ||
| <Spinner /> | ||
| <p className="mt-4 text-sm text-gray-500">{t("calculating_price")}</p> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| if (!calculatedPrice) { | ||
| return <div className="p-4 text-center text-red-500">{t("error_calculating_price")}</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="space-y-4 p-4"> | ||
| <h3 className="text-lg font-medium">{t("booking_summary")}</h3> | ||
|
|
||
| <div className="rounded-md border p-4"> | ||
| <div className="divide-y"> | ||
| {calculatedPrice.breakdown.map((item, index) => ( | ||
| <div key={index} className="flex justify-between py-2"> | ||
| <span className="text-gray-700"> | ||
| {item.description || (item.type === "base" ? t("base_price") : t("adjustment"))} | ||
| </span> | ||
| <span className="font-medium"> | ||
| {item.type === "discount" ? "-" : ""} | ||
| {formatPrice(item.amount, calculatedPrice.currency)} | ||
| </span> | ||
| </div> | ||
| ))} | ||
|
|
||
| <div className="flex justify-between py-2 font-bold"> | ||
| <span>{t("total")}</span> | ||
| <span>{formatPrice(calculatedPrice.price, calculatedPrice.currency)}</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <Button color="primary" className="w-full" loading={isCreatingPrice} onClick={handlePayClick}> | ||
| {t("continue_to_payment")} | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.