dashboard-admin/app/Http/Controllers/User/UserTransactionController.php

281 lines
9.1 KiB
PHP

<?php
namespace App\Http\Controllers\User;
use App\Models\Transaction;
use App\Models\TransactionDescription;
use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use App\Models\TransactionUser;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Ramsey\Uuid\Uuid;
use Midtrans\Config;
use Midtrans\Snap;
use Midtrans\Transaction as Trans;
class UserTransactionController extends Controller
{
/**
* Display a listing of the resource.
*/
public function indexPembeli()
{
return view('user.transaction.pembeli.index', [
'transactions' => Transaction::where('pembeli', Auth::user()->email)
->latest()
->get(),
]);
// dd(Transaction::where('pembeli',Auth::user()->email)->get());
}
/**
* Display a listing of the resource.
*/
public function indexPenjual()
{
return view('user.transaction.penjual.index', [
'transactions' => Transaction::where('penjual', Auth::user()->email)->get(),
]);
}
public function detailTransaction($id)
{
return view('user.transaction.pembeli.detail-transaction', [
'transaction' => Transaction::findOrFail($id),
'trackings' => TransactionDescription::where('order_id', $id)->get(),
]);
// dd(Transaction::findOrFail($id));
}
/**
* Show the form for creating a new resource.
*/
public function createTransaction(Request $request)
{
return view('user.transaction.pembeli.new-transaction');
}
/**
* Store a newly created resource in storage.
*/
public function invoiceTransaction($id)
{
return view('user.transaction.pembeli.invoice-transaction', [
'name' => 'npannisa',
'TransactionUser' => TransactionUser::HistoryTransaction(),
]);
}
public function storeTransaction(Request $request)
{
$pembeli = Auth::user()->email;
$penjual = $request->get('email_penjual');
$nama_barang = $request->get('nama_barang');
$satuan_barang = $request->get('satuan_barang');
$deskripsi_transaksi = $request->get('deskripsi');
$harga_barang = $request->get('harga_barang');
$jumlah_barang = $request->get('jumlah_barang');
$nama_depan_pembeli = Auth::user()->nama_depan;
$nama_belakang_pembeli = Auth::user()->nama_belakang;
$nohp_pembeli = Auth::user()->nohp;
$nama_penjual = User::where('email', $penjual)->value('nama_depan');
$alamat = ucwords(strtolower(Auth::user()->alamat));
$id = Uuid::uuid4();
$now = Carbon::now()->tz('Asia/Jakarta');
$bulan = $now->format('F');
$tahun = $now->year;
// $persentase_keuntungan = Setting::where('status','Active')
// ->where('bulan','=',$bulan)
// ->where('tahun','=',$tahun)->get();
$persentase_keuntungan = $request->get('persentase_keuntungan');
$total_harga = $request->get('total_harga');
$total_keuntungan = $request->get('total_keuntungan');
$total_bayar = $request->get('total_bayar');
$batas_pembayaran = $now->addDays(1)->toDateTimeString();
$batas_pengiriman_barang_awal = $now->addDays(2)->toDateTimeString();
$batas_pengiriman_barang_akhir = $now->addDays(5)->toDateTimeString();
$params = [
'transaction_details' => [
'order_id' => $id,
'gross_amount' => $total_bayar,
],
'item_details' => [
[
'id' => $nama_barang . time(),
'price' => $harga_barang,
'quantity' => $jumlah_barang,
'name' => $nama_barang,
],
[
'id' => 'BA01',
'price' => $total_keuntungan,
'quantity' => 1,
'name' => 'Biaya Admin',
],
],
'customer_details' => [
'firts_name' => $nama_depan_pembeli,
'last_name' => $nama_belakang_pembeli,
'email' => $pembeli,
'phone' => $nohp_pembeli,
'billing' => [
'first_name' => $nama_depan_pembeli,
'last_name' => $nama_belakang_pembeli,
'email' => $pembeli,
'phone' => $nohp_pembeli,
'address' => Auth::user()->alamat,
'city' => Auth::user()->village->district->city->name,
'country_code' => 'IDN',
],
],
'callbacks' => [
'finish' => 'http://127.0.0.1:8000/user/user-transaction-pembeli/',
],
'expiry' => [
'start_time' => $now->format('Y-m-d H:i:s P'),
'unit' => 'hours',
'duration' => 24,
],
];
Config::$serverKey = 'SB-Mid-server-8rydZAwKoWuoQ6g_3ot0-K7p';
Config::$isProduction = false;
// Set sanitization on (default)
Config::$isSanitized = true;
// Set 3DS transaction for credit card to true
Config::$is3ds = true;
$snap_token = Snap::getSnapToken($params);
$token = $snap_token;
$status = 'pending';
$query = Transaction::create([
'id' => $id,
'pembeli' => $pembeli,
'penjual' => $penjual,
'nama_barang' => $nama_barang,
'deskripsi_transaksi' => $deskripsi_transaksi,
'satuan_barang' => $satuan_barang,
'harga_barang' => $harga_barang,
'jumlah_barang' => $jumlah_barang,
'persentase_keuntungan' => $persentase_keuntungan,
'total_keuntungan' => $total_keuntungan,
'total_harga' => $total_harga,
'total_bayar' => $total_bayar,
'token' => $token,
'status' => $status,
'batas_pembayaran' => $batas_pembayaran,
'batas_pengiriman_barang_awal' => $batas_pengiriman_barang_awal,
'batas_pengiriman_barang_akhir' => $batas_pengiriman_barang_akhir,
]);
if ($query) {
TransactionDescription::create([
'order_id' => $id,
'status' => $status,
'user' => $pembeli,
'judul' => 'fa fa-plus',
'background' => 'bg-buyer',
'deskripsi' => $nama_depan_pembeli . ' telah membuat transaksi baru dengan ' . $nama_penjual,
]);
return response()->json([
'status' => true,
'message' => 'Berhasil menambahkan transaksi. Silahkan lakukan pembayaran.',
]);
} else {
return response()->json([
'status' => false,
'message' => 'Gagal menambahkan transaksi.',
]);
}
}
public function updateStatusTransaction(Request $request)
{
}
public function acceptTransaction(Request $request)
{
}
public function paymentTransaction(Request $request, $id)
{
Config::$serverKey = 'SB-Mid-server-8rydZAwKoWuoQ6g_3ot0-K7p';
Config::$isProduction = false;
// Set sanitization on (default)
Config::$isSanitized = true;
// Set 3DS transaction for credit card to true
Config::$is3ds = true;
$payment = Trans::status($id);
$result = json_decode(json_encode($payment), true);
$query = Transaction::where('id', $id)->update([
'acquire' => $result['acquirer'],
'currency' => $result['currency'],
'fraud_status' => $result['fraud_status'],
'issuer' => $result['issuer'],
'merchant_id' => $result['merchant_id'],
'metode_pembayaran' => $result['payment_type'],
'tanggal_pembayaran' => $result['settlement_time'],
'signature_key' => $result['signature_key'],
'status' => $result['transaction_status'],
'tipe_transaction' => $result['transaction_type'],
'status_code' => $result['status_code'],
]);
if ($query) {
return response()->json([
'status' => true,
'message' => 'Pembayaran sukses. Proses akan dilanjutkan ke penjual',
]);
} else {
return response()->json([
'status' => false,
'message' => 'Pembayaran gagal',
]);
}
}
/**
* Display the specified resource.
*/
public function show(Transaction $transaction)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Transaction $transaction)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Transaction $transaction)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Transaction $transaction)
{
//
}
}