You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.6 KiB
PHTML

<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\Account;
1 year ago
use App\Models\Customer;
use App\Models\DepositHistory;
use App\Models\Setting;
use App\Services\GeneralService;
use App\Services\MidtransService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class DepositController extends Controller
{
public function index()
{
$histories = DepositHistory::where('customer_id', auth()->id())
->orderBy('updated_at', 'desc')
->orderBy('is_valid', 'desc');
return inertia('Deposit/Index', [
'histories' => $histories->paginate(20),
]);
}
public function create()
{
return inertia('Deposit/Topup', [
'payments' => GeneralService::getEnablePayment(),
]);
}
public function store(Request $request)
{
$request->validate([
'amount' => 'required|numeric|min:10000',
'payment' => [
'required',
Rule::in([Setting::PAYMENT_MANUAL, Setting::PAYMENT_MIDTRANS]),
],
]);
DB::beginTransaction();
$deposit = DepositHistory::make([
'customer_id' => auth()->id(),
'debit' => $request->amount,
'description' => 'Top Up #' . Str::random(5),
'payment_channel' => $request->payment,
]);
if ($request->payment == Setting::PAYMENT_MANUAL) {
$deposit->is_valid = DepositHistory::STATUS_WAIT_UPLOAD;
$deposit->save();
}
if ($request->payment == Setting::PAYMENT_MIDTRANS) {
$deposit->is_valid = DepositHistory::STATUS_WAIT_PAYMENT;
$deposit->save();
$token = (new MidtransService($deposit, Setting::getByKey('MIDTRANS_SERVER_KEY')))->getSnapToken();
$deposit->update(['payment_token' => $token]);
}
$deposit->create_notification();
DB::commit();
return redirect()->route('customer.deposit.show', ['deposit' => $deposit->id, 'direct' => 'true']);
}
public function show(Request $request, DepositHistory $deposit)
{
return inertia('Deposit/Detail', [
'deposit' => $deposit,
'accounts' => Account::get(),
'midtrans_client_key' => Setting::getByKey('MIDTRANS_CLIENT_KEY'),
'is_production' => app()->isProduction(),
'direct' => $request->direct,
]);
}
public function update(Request $request, DepositHistory $deposit)
{
$request->validate([
'account_id' => 'required|exists:accounts,id',
'image' => 'required|image',
]);
$file = $request->file('image');
$file->store('uploads', 'public');
$deposit->update([
'image_prove' => $file->hashName('uploads'),
'account_id' => $request->account_id,
'is_valid' => DepositHistory::STATUS_WAIT_APPROVE,
]);
$deposit->create_notification();
session()->flash('message', ['type' => 'success', 'message' => 'Upload berhasil, silahkan tunggu untuk approve']);
}
public function midtrans_payment(Request $request, DepositHistory $deposit)
{
DB::beginTransaction();
1 year ago
$transaction_status = $request->result['transaction_status'];
if ($transaction_status == 'settlement' || $transaction_status == 'capture') {
$is_valid = DepositHistory::STATUS_VALID;
} elseif ($transaction_status == 'pending') {
$is_valid = DepositHistory::STATUS_WAIT_PAYMENT;
} else {
$is_valid = DepositHistory::STATUS_INVALID;
}
1 year ago
$deposit->update([
'is_valid' => $is_valid,
'payment_response' => json_encode($request->result),
'payment_type' => $request->result['payment_type'],
]);
1 year ago
if ($is_valid == DepositHistory::STATUS_VALID) {
$deposit->update_customer_balance();
$customer = Customer::find($deposit->customer_id);
$customer->repayPaylater($deposit);
}
DB::commit();
return redirect()->route('customer.deposit.show', ['deposit' => $deposit->id]);
}
public function mindtrans_notification(Request $request)
{
DB::beginTransaction();
$deposit = DepositHistory::where('id', $request->order_id)->first();
if ($deposit != null && $deposit->is_valid != DepositHistory::STATUS_VALID) {
$deposit->fill([
'payment_response' => json_encode($request->all()),
'payment_type' => $request->result['payment_type'],
]);
if ($request->transaction_status == 'settlement' || $request->transaction_status == 'capture') {
$deposit->fill(['payment_status' => DepositHistory::STATUS_VALID]);
$deposit->update_customer_balance();
1 year ago
$customer = Customer::find($deposit->customer_id);
$customer->repayPaylater($deposit);
$deposit->create_notification();
$deposit->create_notification_user();
} elseif ($request->transaction_status == 'pending') {
$deposit->fill(['payment_status' => DepositHistory::STATUS_WAIT_PAYMENT]);
} else {
$deposit->fill(['payment_status' => DepositHistory::STATUS_INVALID]);
}
$deposit->save();
}
DB::commit();
return response()->json([
'status' => 'ok',
'order' => $deposit,
]);
}
}