- 
- 
- 
This commit is contained in:
2026-05-18 18:31:16 +07:00
parent b599d01eea
commit c4efe4453b
36 changed files with 3057 additions and 1493 deletions
+27 -32
View File
@@ -2,30 +2,12 @@
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { tripService } from "@/server/services/trip.service";
import { paymentService } from "@/server/services/payment.service";
import { bookingService } from "@/server/services/booking.service";
import { refundService } from "@/server/services/refund.service";
import { absoluteUrl } from "@/lib/site";
import { revalidatePath } from "next/cache";
export async function markParticipantPaidAction(tripId: string) {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { error: "Kamu harus login terlebih dahulu" };
}
try {
await tripService.markParticipantPayment(tripId, session.user.id);
revalidatePath(`/trips/${tripId}`);
revalidatePath("/trips");
revalidatePath("/");
revalidatePath("/profile");
return { success: true };
} catch (err) {
return { error: (err as Error).message };
}
}
export type StartMidtransResponse =
| { error: string }
| {
@@ -33,6 +15,7 @@ export type StartMidtransResponse =
snapToken: string;
snapJsUrl: string;
clientKey: string;
orderId: string;
};
/**
@@ -58,39 +41,51 @@ export async function startMidtransPaymentAction(
const result = await paymentService.startMidtransPayment(
booking.id,
session.user.id
session.user.id,
{ finishUrl: absoluteUrl(`/trips/${tripId}/payment`) }
);
return {
success: true,
snapToken: result.snapToken,
snapJsUrl: result.snapJsUrl,
clientKey: result.clientKey,
orderId: result.orderId,
};
} catch (err) {
return { error: (err as Error).message };
}
}
export async function confirmParticipantPaymentAction(
tripId: string,
participantId: string
) {
/**
* Tarik status terkini dari Midtrans untuk satu order, lalu sinkron ke DB.
* Dipakai oleh payment page saat user kembali dari Snap (redirect bawa
* `?order_id=...`), dan oleh `MidtransPayButton` di callback `onSuccess`/
* `onPending`/`onClose` agar UI ter-update tanpa menunggu webhook.
*/
export async function reconcileMidtransPaymentAction(orderId: string) {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { error: "Kamu harus login terlebih dahulu" };
}
if (!orderId || typeof orderId !== "string") {
return { error: "order_id tidak valid" };
}
try {
await tripService.confirmParticipantPayment(
tripId,
participantId,
const result = await paymentService.reconcileFromGateway(
orderId,
session.user.id
);
revalidatePath(`/trips/${tripId}`);
revalidatePath("/trips");
revalidatePath("/");
revalidatePath("/profile");
return { success: true };
if (!result.ok) {
if (result.reason === "forbidden") {
return { error: "Order ini bukan milikmu" };
}
if (result.reason === "not_found") {
return { error: "Order tidak ditemukan" };
}
return { error: "Status pembayaran tidak cocok dengan tagihan" };
}
return { success: true as const, status: result.status };
} catch (err) {
return { error: (err as Error).message };
}