Files
portal-api/app/Http/Controllers/frontend/RechargeController.php
T
2025-07-24 16:18:27 +07:00

128 lines
4.2 KiB
PHP

<?php
namespace App\Http\Controllers\frontend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Recharge\RechargeRequest;
use App\Http\Services\VNPayService;
use App\Jobs\Payment\GetPaymentMessage;
use App\Jobs\Payment\GetTransactionPayment;
use App\Models\Customer;
use App\Models\Payment;
use App\Models\Transaction;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
class RechargeController extends Controller
{
protected $vnpayService;
public function __construct(
VNPayService $vnPayService
)
{
parent::__construct();
$this->vnpayService = $vnPayService;
}
public function index()
{
$title = 'Yêu cầu nạp tiền vào tài khoản';
$payment = Payment::whereNot('id', 1)->select('id', 'name')->get();
$breadcrumbs = $this->crums->add(__('Dashboard'), route('home'))->add(__('Nạp tiền'));
return view('frontend.payment.index', compact('title', 'payment', 'breadcrumbs'));
}
public function paymentProcess(RechargeRequest $request)
{
$auth = customer();
if (!$auth) {
return redirect()->route('auth.index')->withErrors('Vui lòng đăng nhập');
}
// Chuẩn bị dữ liệu cho VNPAY
$paymentData = [
'amount' => $request->money,
'language' => 'vn',
];
$paymentUrl = $this->vnpayService->createPaymentUrl($paymentData);
return redirect($paymentUrl);
}
public function paymentReturn(Request $request)
{
$result = $this->vnpayService->verifyPayment($request->all());
$data = $result['data'] ?? [];
if (!empty($data) && ($data['vnp_TransactionStatus'] ?? '') === '00') {
$amount = ($data['vnp_Amount'] ?? 0) / 100;
$customerId = customer()->id;
// Lưu vào bảng transaction
Transaction::create([
'trans' => $data['vnp_TxnRef'] ?? 'unknown',
'amount' => $amount,
'customer_id' => $customerId,
]);
// Cộng vào credit của Customer
$customer = Customer::find($customerId);
if ($customer) {
$customer->increment('credit', $amount);
$customer->increment('total', $amount);
}
dispatch(new GetPaymentMessage($customerId, $amount))
->delay(now()->addSeconds(10));
return redirect()->route('recharge.index')->with('success', 'Thanh toán thành công, tài khoản đã được cộng tiền!');
}
return redirect()->route('recharge.index')->withErrors('Thanh toán thất bại hoặc bị hủy.');
}
public function show(Request $request)
{
$amount = (int)$request->amount;
if ($amount < 200000) {
return back()->withErrors('Yêu cầu không hợp lệ!');
}
Cookie::queue(Cookie::forget('start_checking'));
$memo = "NapTienUser" . customer()->id;
$payment = Payment::find($request->payment);
if ($payment->type == 1 && !session()->has('start_checking')) {
\session()->put('start_checking', true);
$time = Carbon::now()->addMinutes(5)->timestamp;
dispatch(new GetTransactionPayment($memo, $time, customer()->id))->delay(now()->addSeconds(30));
}
$qr = "https://img.vietqr.io/image/$payment->bankCode-$payment->accountNo-qr_only.png?amount=$amount&addInfo=$memo&accountName=$payment->bankName";
$title = "Kiểm tra thanh toán";
$breadcrumbs = $this->crums->add(__('Dashboard'), route('home'))->add(__('Nạp tiền'), route('recharge.index'))->add(__('Kiểm tra thanh toán'));
return view('frontend.payment.view', compact('title', 'qr', 'amount', 'payment', 'memo', 'breadcrumbs'));
}
public function HistoryRecharge()
{
$title = 'Lịch sử giao dịch';
$breadcrumbs = $this->crums->add(__('Dashboard'), route('home'))->add(__('Lịch sử giao dịch'));
return view('frontend.transaction', compact('title', 'breadcrumbs'));
}
public function CallbackResponse(Request $request)
{
\session()->forget('start_checking');
return redirect()->route('home');
}
}