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.

229 lines
8.0 KiB
PHTML

<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\DepositHistory;
1 year ago
use App\Models\DepositLocation;
use App\Models\Setting;
use App\Services\GeneralService;
use App\Services\MidtransService;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
class DepositController extends Controller
{
public function index(Request $request)
{
$histories = DepositHistory::where('customer_id', auth()->id())
->orderBy('updated_at', 'desc')
->orderBy('is_valid', 'desc')
->where('type', DepositHistory::TYPE_DEPOSIT);
$start_date = now()->startOfMonth();
$end_date = now()->endOfMonth();
if ($request->startDate != '' && $request->endDate != '') {
$start_date = Carbon::parse($request->startDate);
$end_date = Carbon::parse($request->endDate);
}
$histories->whereBetween('created_at', [$start_date, $end_date]);
return inertia('Deposit/Index', [
'histories' => $histories->paginate(20),
'_start_date' => $start_date->format('m/d/Y'),
1 year ago
'_end_date' => $end_date->format('m/d/Y'),
]);
}
1 year ago
public function create(Request $request)
{
1 year ago
$customer = $request->user('customer');
1 year ago
if (! $customer->allow_transaction) {
1 year ago
return redirect()->back()
1 year ago
->with('message', ['type' => 'error', 'message' => 'akun anda dibekukan tidak dapat melakukan transaksi']);
1 year ago
}
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, Setting::PAYMENT_CASH_DEPOSIT]),
],
]);
DB::beginTransaction();
$deposit = DepositHistory::make([
'customer_id' => auth()->id(),
'debit' => $request->amount,
'payment_channel' => $request->payment,
'type' => DepositHistory::TYPE_DEPOSIT,
]);
if (in_array($request->payment, [Setting::PAYMENT_MANUAL, Setting::PAYMENT_CASH_DEPOSIT])) {
$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('transactions.deposit.show', ['deposit' => $deposit->id, 'direct' => 'true']);
}
public function show(Request $request, DepositHistory $deposit)
{
return inertia('Deposit/Detail', [
1 year ago
'deposit' => $deposit->load(['account', 'depositLocation']),
'accounts' => Account::get(),
1 year ago
'deposit_locations' => DepositLocation::get(),
'midtrans_client_key' => Setting::getByKey('MIDTRANS_CLIENT_KEY'),
'is_production' => app()->isProduction(),
'direct' => $request->direct,
'bank_admin_fee' => Setting::getByKey('ADMINFEE_MANUAL_TRANSFER'),
1 year ago
'cash_admin_fee' => Setting::getByKey('ADMINFEE_CASH_DEPOSIT'),
1 year ago
'back' => $request->back ?? 'transactions.deposit.index',
]);
}
public function update(Request $request, DepositHistory $deposit)
{
$request->validate([
'image' => 'required|image',
]);
1 year ago
if ($deposit->payment_channel == Setting::PAYMENT_CASH_DEPOSIT) {
$request->validate(['deposit_location_id' => 'required|exists:deposit_locations,id']);
}
if ($deposit->payment_channel == Setting::PAYMENT_MANUAL) {
$request->validate(['account_id' => 'required|exists:accounts,id']);
}
$file = $request->file('image');
$file->store('uploads', 'public');
1 year ago
$deposit->update([
'image_prove' => $file->hashName('uploads'),
'account_id' => $request->account_id,
1 year ago
'deposit_location_id' => $request->deposit_location_id,
'is_valid' => DepositHistory::STATUS_WAIT_APPROVE,
]);
$deposit->create_notification();
1 year ago
$deposit->create_notification_repayment();
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
1 year ago
if ($deposit->is_valid == DepositHistory::STATUS_VALID) {
return redirect()->route('transactions.deposit.show', ['deposit' => $deposit->id]);
}
$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) {
1 year ago
if ($deposit->type == DepositHistory::TYPE_REPAYMENT) {
$paylater = $deposit->paylater;
$paylater->update([
'credit' => $deposit->debit,
'is_valid' => $deposit->is_valid,
]);
$paylater->update_customer_paylater();
}
if ($deposit->type == DepositHistory::TYPE_DEPOSIT) {
$deposit->update_customer_balance();
}
1 year ago
}
DB::commit();
return redirect()->route('transactions.deposit.show', ['deposit' => $deposit->id]);
}
public function mindtrans_notification(Request $request)
{
info('mindtrans_notification', ['request boyd' => $request->input()]);
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->payment_type,
]);
if ($request->transaction_status == 'settlement' || $request->transaction_status == 'capture') {
1 year ago
$deposit->fill(['is_valid' => DepositHistory::STATUS_VALID]);
if ($deposit->type == DepositHistory::TYPE_REPAYMENT) {
$paylater = $deposit->paylater;
$paylater->update([
'credit' => $deposit->debit,
'is_valid' => $deposit->is_valid,
]);
$paylater->update_customer_paylater();
}
if ($deposit->type == DepositHistory::TYPE_DEPOSIT) {
$deposit->update_customer_balance();
$deposit->create_notification();
$deposit->create_notification_user();
}
} elseif ($request->transaction_status == 'pending') {
1 year ago
$deposit->fill(['is_valid' => DepositHistory::STATUS_WAIT_PAYMENT]);
} else {
1 year ago
$deposit->fill(['is_valid' => DepositHistory::STATUS_INVALID]);
}
$deposit->save();
}
DB::commit();
return response()->json([
'status' => 'ok',
'order' => $deposit,
]);
}
}