| |
|
| | "use server"; |
| |
|
| | import type { CoinPurchaseInput } from "@/lib/schemas"; |
| | import { getLoggedInUser } from "./auth"; |
| | import { getDb } from "@/lib/mongodb"; |
| | import type { PaymentTransaction } from "@/lib/types"; |
| | import { ObjectId } from "mongodb"; |
| | import { revalidatePath } from "next/cache"; |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | export async function initiateCoinPurchase( |
| | data: CoinPurchaseInput |
| | ): Promise<{ success: boolean; message: string; transactionReference?: string; paymentLink?: string; gateway?: 'paystack' | 'flutterwave'}> { |
| | const user = await getLoggedInUser(); |
| | if (!user) { |
| | return { success: false, message: "User not authenticated." }; |
| | } |
| |
|
| | const transactionReference = `anitadeploy_${user._id}_${new ObjectId().toString()}`; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | console.log("Initiating coin purchase:", data); |
| | console.log("Generated Transaction Reference:", transactionReference); |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | try { |
| | const db = await getDb(); |
| | const transaction: Omit<PaymentTransaction, '_id'> = { |
| | userId: user._id, |
| | packageId: data.package, |
| | coinsPurchased: data.coinsToCredit, |
| | amountPaid: data.amountInSelectedCurrency, |
| | currency: data.currency, |
| | paymentGateway: data.paymentGateway, |
| | transactionReference, |
| | status: 'pending', |
| | createdAt: new Date(), |
| | updatedAt: new Date(), |
| | }; |
| | await db.collection<PaymentTransaction>('payment_transactions').insertOne(transaction); |
| |
|
| | } catch (dbError) { |
| | console.error("DB Error storing pending transaction:", dbError); |
| | return { success: false, message: "Failed to record transaction. Please try again."}; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | if (data.paymentGateway === "paystack") { |
| | |
| | return { |
| | success: true, |
| | message: `Paystack payment initiated (simulation). Ref: ${transactionReference}. Redirecting...`, |
| | transactionReference, |
| | paymentLink: `https://checkout.paystack.com/success?trxref=${transactionReference}&reference=${transactionReference}`, |
| | gateway: 'paystack' |
| | }; |
| | } else if (data.paymentGateway === "flutterwave") { |
| | |
| | return { |
| | success: true, |
| | message: `Flutterwave payment initiated (simulation). Ref: ${transactionReference}. Redirecting...`, |
| | transactionReference, |
| | paymentLink: `https://ravemodal-dev.herokuapp.com/v3/hosted/pay/successful?tx_ref=${transactionReference}`, |
| | gateway: 'flutterwave' |
| | }; |
| | } |
| |
|
| | return { success: false, message: "Invalid payment gateway selected (simulation)." }; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | export async function verifyPaymentAndAwardCoins( |
| | gateway: 'paystack' | 'flutterwave', |
| | reference: string, |
| | gatewayResponse?: any |
| | ): Promise<{ success: boolean; message: string }> { |
| | console.log(`Verifying payment for ${gateway} with reference ${reference}`); |
| | console.log("Gateway response (simulated or actual):", gatewayResponse); |
| |
|
| | const db = await getDb(); |
| | const transactionsCollection = db.collection<PaymentTransaction>("payment_transactions"); |
| | const usersCollection = db.collection("users"); |
| |
|
| | const transaction = await transactionsCollection.findOne({ transactionReference: reference, status: 'pending' }); |
| |
|
| | if (!transaction) { |
| | console.error(`Transaction not found or not pending for reference: ${reference}`); |
| | return { success: false, message: "Transaction not found or already processed." }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | const isPaymentSuccessful = true; |
| |
|
| | if (isPaymentSuccessful) { |
| | const userUpdateResult = await usersCollection.updateOne( |
| | { _id: transaction.userId }, |
| | { $inc: { coins: transaction.coinsPurchased } } |
| | ); |
| |
|
| | if (userUpdateResult.modifiedCount > 0) { |
| | await transactionsCollection.updateOne( |
| | { _id: transaction._id }, |
| | { $set: { status: 'successful', updatedAt: new Date(), gatewayResponse: gatewayResponse || { simulated: true } } } |
| | ); |
| | revalidatePath("/dashboard"); |
| | revalidatePath("/dashboard/buy-coins"); |
| | return { success: true, message: `Payment successful! ${transaction.coinsPurchased} coins added to your account.` }; |
| | } else { |
| | await transactionsCollection.updateOne( |
| | { _id: transaction._id }, |
| | { $set: { status: 'failed', updatedAt: new Date(), gatewayResponse: { error: "Failed to update user coins."} } } |
| | ); |
| | return { success: false, message: "Payment verified but failed to update coin balance." }; |
| | } |
| | } else { |
| | await transactionsCollection.updateOne( |
| | { _id: transaction._id }, |
| | { $set: { status: 'failed', updatedAt: new Date(), gatewayResponse: gatewayResponse || { simulated_failure: true } } } |
| | ); |
| | return { success: false, message: "Payment verification failed." }; |
| | } |
| | } |
| |
|