feat: Implement JWT authentication for client API and add transaction verification endpoint.
This commit is contained in:
@@ -11,6 +11,7 @@ use App\Models\Log;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log as FacadesLog;
|
||||
|
||||
class TransactionController extends Controller
|
||||
{
|
||||
@@ -55,6 +56,12 @@ class TransactionController extends Controller
|
||||
$vnp_TxnRef = $data['vnp_TxnRef'] ?? null;
|
||||
$vnp_Amount = $data['vnp_Amount'] ?? 0;
|
||||
|
||||
FacadesLog::info('VNPay callback received', [
|
||||
'vnp_TxnRef' => $vnp_TxnRef,
|
||||
'vnp_Amount' => $vnp_Amount,
|
||||
'user' => $request->user() ? $request->user()->id : 'not authenticated',
|
||||
]);
|
||||
|
||||
$transaction = Transaction::where('trans', 'DEP' . $vnp_TxnRef)->first();
|
||||
|
||||
if (!$transaction) {
|
||||
@@ -100,6 +107,12 @@ class TransactionController extends Controller
|
||||
|
||||
$verify = $this->vnpayService->verifyPayment($data);
|
||||
|
||||
FacadesLog::info('VNPay callback debug', [
|
||||
'isValid' => $verify['isValid'],
|
||||
'isSuccess' => $verify['isSuccess'] ?? false,
|
||||
'responseCode' => $data['vnp_ResponseCode'] ?? 'N/A',
|
||||
]);
|
||||
|
||||
if (!$verify['isValid']) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
@@ -137,10 +150,24 @@ class TransactionController extends Controller
|
||||
$transaction->update(['status' => TransactionStatus::COMPLETED->value]);
|
||||
|
||||
$customer = Customer::find($transaction->customer_id);
|
||||
|
||||
FacadesLog::info('Customer update debug', [
|
||||
'customer_id' => $transaction->customer_id,
|
||||
'customer_found' => $customer ? true : false,
|
||||
'transaction_amount' => $transaction->amount,
|
||||
'current_total' => $customer->total ?? 'N/A',
|
||||
'current_credit' => $customer->credit ?? 'N/A',
|
||||
]);
|
||||
|
||||
$customer->total += $transaction->amount;
|
||||
$customer->credit += $transaction->amount / 100;
|
||||
$customer->save();
|
||||
|
||||
FacadesLog::info('Customer after update', [
|
||||
'new_total' => $customer->total,
|
||||
'new_credit' => $customer->credit,
|
||||
]);
|
||||
|
||||
Log::create([
|
||||
'agent' => 'api',
|
||||
'client_ip' => $request->ip(),
|
||||
@@ -206,4 +233,39 @@ class TransactionController extends Controller
|
||||
'data' => $transaction,
|
||||
]);
|
||||
}
|
||||
|
||||
public function verifyPayment(Request $request)
|
||||
{
|
||||
$transRef = $request->query('vnp_TxnRef');
|
||||
|
||||
if (!$transRef) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Thiếu tham số vnp_TxnRef',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$transaction = Transaction::where('trans', 'DEP' . $transRef)->first();
|
||||
|
||||
if (!$transaction) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Giao dịch không tồn tại',
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Lấy thông tin giao dịch thành công',
|
||||
'data' => [
|
||||
'trans' => $transaction->trans,
|
||||
'amount' => $transaction->amount,
|
||||
'amount_vnp' => $transaction->amount * 100,
|
||||
'status' => $transaction->status,
|
||||
'status_label' => TransactionStatus::from($transaction->status)->label(),
|
||||
'created_at' => $transaction->created_at,
|
||||
'updated_at' => $transaction->updated_at,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user