Merge pull request #13 from i701/feat/device-payments
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 5m22s

feat/device payments
This commit is contained in:
Abdulla Aidhaan
2025-07-06 22:41:28 +05:00
committed by GitHub
8 changed files with 83 additions and 72 deletions

View File

@ -179,24 +179,16 @@ export async function cancelTopup({ id }: { id: string }) {
export async function cancelPayment({ id }: { id: string }) {
const session = await getServerSession(authOptions);
const response = await fetch(
`${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/delete/`,
`${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/cancel/`,
{
method: "DELETE",
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${session?.apiToken}`,
},
},
);
if (!response.ok) {
const errorData = (await response.json()) as ApiError;
const errorMessage =
errorData.message || errorData.detail || "An error occurred.";
const error = new Error(errorMessage);
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
throw error;
}
return { message: "Payment successfully canceled." };
return handleApiResponse<Payment>(response, "cancelPayment");
}
type UpdatePayment = {

View File

@ -1,11 +1,12 @@
import { redirect } from "next/navigation";
import { getPayment, getProfile } from "@/actions/payment";
import CancelPaymentButton from "@/components/billing/cancel-payment-button";
import ExpiryCountDown from "@/components/billing/expiry-time-countdown";
import ClientErrorMessage from "@/components/client-error-message";
import DevicesToPay from "@/components/devices-to-pay";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { tryCatch } from "@/utils/tryCatch";
import { redirect } from "next/navigation";
export default async function PaymentPage({
params,
}: {
@ -40,13 +41,24 @@ export default async function PaymentPage({
{payment?.paid ? "Paid" : "Pending"}
</Button>
{!payment.paid && (
<CancelPaymentButton paymentId={paymentId} />
payment.is_expired ? (
<Button
disabled
className="rounded-md opacity-100! uppercase font-semibold text-red-500 bg-red-500/20"
>
Payment Expired
</Button>
) : (
<CancelPaymentButton paymentId={paymentId} />
)
)}
</div>
</div>
{!payment.paid && (
<ExpiryCountDown expiryLabel="Payment" expiresAt={payment.expires_at} />
)}
<div
id="user-filters"
id="user-device-payments"
className="pb-4 gap-4 flex sm:flex-row flex-col items-start justify-start"
>
<DevicesToPay

View File

@ -56,7 +56,7 @@ export default async function TopupPage({
</div>
</div>
{!topup.paid && (
<ExpiryCountDown expiresAt={topup.expires_at} />
<ExpiryCountDown expiryLabel="Top up" expiresAt={topup.expires_at} />
)}
<div
id="user-topup-details"

View File

@ -1,11 +1,11 @@
"use client";
import { cancelPayment } from "@/actions/payment";
import { tryCatch } from "@/utils/tryCatch";
import { Loader2, Trash2 } from "lucide-react";
import { useRouter } from "next/navigation";
import React from "react";
import { toast } from "sonner";
import { cancelPayment } from "@/actions/payment";
import { tryCatch } from "@/utils/tryCatch";
import { Button } from "../ui/button";
export default function CancelPaymentButton({
@ -17,12 +17,16 @@ export default function CancelPaymentButton({
<Button
onClick={async () => {
setLoading(true);
const [error, x] = await tryCatch(cancelPayment({ id: paymentId }));
console.log(x);
const [error, payment] = await tryCatch(cancelPayment({ id: paymentId }));
console.log(payment);
if (error) {
toast.error(error.message);
setLoading(false);
} else {
toast.success("Payment cancelled successfully!", {
description: `Your payment of ${payment?.amount} MVR has been cancelled.`,
closeButton: true,
});
router.replace("/devices");
}
}}

View File

@ -15,7 +15,7 @@ const HumanizeTimeLeft = (seconds: number) => {
return `${minutes}m ${remainingSeconds}s`
}
export default function ExpiryCountDown({ expiresAt }: { expiresAt: string }) {
export default function ExpiryCountDown({ expiresAt, expiryLabel }: { expiresAt: string, expiryLabel: string }) {
const [timeLeft, setTimeLeft] = useState(calculateTimeLeft(expiresAt))
const [mounted, setMounted] = useState(false)
const router = useRouter()
@ -47,7 +47,7 @@ export default function ExpiryCountDown({ expiresAt }: { expiresAt: string }) {
{timeLeft ? (
<span>Time left: {HumanizeTimeLeft(timeLeft)}</span>
) : (
<span>Top up has expired. Please make another topup to add balance to your wallet.</span>
<span>{expiryLabel} has expired.</span>
)}
{timeLeft > 0 && (
<Progress className='absolute bottom-0 left-0 right-0' color='#f49b5b' value={timeLeft / 600 * 100} />

View File

@ -59,7 +59,7 @@ export async function PaymentsTable({
<TableRow>
<TableHead>Details</TableHead>
<TableHead>Duration</TableHead>
<TableHead>Status</TableHead>
<TableHead>Amount</TableHead>
</TableRow>
</TableHeader>
@ -84,6 +84,8 @@ export async function PaymentsTable({
month: "short",
day: "2-digit",
year: "numeric",
minute: "2-digit",
hour: "2-digit",
},
)}
</span>
@ -98,16 +100,6 @@ export async function PaymentsTable({
View Details
</Button>
</Link>
<Badge
className={cn(
payment?.paid
? "text-green-500 bg-green-500/20"
: "text-yellow-500 bg-yellow-500/20",
)}
variant={payment.paid ? "outline" : "secondary"}
>
{payment.paid ? "Paid" : "Unpaid"}
</Badge>
</div>
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border">
<h3 className="text-sm font-medium">Devices</h3>
@ -124,9 +116,30 @@ export async function PaymentsTable({
</div>
</div>
</TableCell>
<TableCell className="font-medium">
{payment.number_of_months} Months
</TableCell>
<TableCell>
<span className="font-semibold pr-2">
{payment.paid ? (
<Badge
className={cn(
payment.status === "PENDING"
? "bg-yellow-100 text-yellow-700 dark:bg-yellow-700 dark:text-yellow-100"
: "bg-green-100 dark:bg-green-700"
)}
variant="outline"
>
{payment.status}
</Badge>
) : payment.is_expired ? (
<Badge>Expired</Badge>
) : (
<Badge variant="outline">{payment.status}</Badge>
)}
</span>
</TableCell>
<TableCell>
<span className="font-semibold pr-2">
{payment.amount.toFixed(2)}
@ -185,6 +198,8 @@ function MobilePaymentDetails({ payment }: { payment: Payment }) {
month: "short",
day: "2-digit",
year: "numeric",
minute: "2-digit",
hour: "2-digit",
})}
</span>
</div>
@ -198,16 +213,6 @@ function MobilePaymentDetails({ payment }: { payment: Payment }) {
View Details
</Button>
</Link>
<Badge
className={cn(
payment?.paid
? "text-green-500 bg-green-500/20"
: "text-yellow-500 bg-yellow-500/20",
)}
variant={payment.paid ? "outline" : "secondary"}
>
{payment.paid ? "Paid" : "Unpaid"}
</Badge>
</div>
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border">
<h3 className="text-sm font-medium">Devices</h3>
@ -226,9 +231,29 @@ function MobilePaymentDetails({ payment }: { payment: Payment }) {
</span>
<Separator className="my-2" />
<h3 className="text-sm font-medium">Amount</h3>
<span className="text-sm text-muted-foreground">
{payment.amount.toFixed(2)} MVR
</span>
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">
{payment.amount.toFixed(2)} MVR
</span>
<span className="font-semibold pr-2">
{payment.paid ? (
<Badge
className={cn(
payment.status === "PENDING"
? "bg-yellow-100 text-yellow-700 dark:bg-yellow-700 dark:text-yellow-100"
: "bg-green-100 dark:bg-green-700"
)}
variant="outline"
>
{payment.status}
</Badge>
) : payment.is_expired ? (
<Badge>Expired</Badge>
) : (
<Badge variant="secondary">{payment.status}</Badge>
)}
</span>
</div>
</div>
</div>
</div>

View File

@ -103,19 +103,6 @@ export async function TopupsTable({
View Details
</Button>
</Link>
{!topup.is_expired &&
topup.status !== "CANCELLED" && (
<Badge
className={cn(
topup?.paid
? "text-green-500 bg-green-500/20"
: "text-yellow-500 bg-yellow-500/20",
)}
variant={topup.paid ? "outline" : "secondary"}
>
{topup.paid ? "Paid" : "Unpaid"}
</Badge>
)}
</div>
</div>
</TableCell>
@ -203,18 +190,6 @@ function MobileTopupDetails({ topup }: { topup: Topup }) {
View Details
</Button>
</Link>
{!topup.is_expired && topup.status !== "CANCELLED" && (
<Badge
className={cn(
topup?.paid
? "text-green-500 bg-green-500/20"
: "text-yellow-500 bg-yellow-500/20",
)}
variant={topup.paid ? "outline" : "secondary"}
>
{topup.paid ? "Paid" : "Unpaid"}
</Badge>
)}
</div>
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border flex justify-between items-center">

View File

@ -81,9 +81,12 @@ export interface Payment {
paid: boolean;
paid_at: string | null;
method: string;
expires_at: string | null;
expires_at: string;
is_expired: boolean;
created_at: string;
updated_at: string;
status: "CANCELLED" | "PENDING" | "PAID";
mib_reference: string | null;
user: number;
}