205 lines
6.2 KiB
PHP
205 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
use App\Models\Transaction;
|
|
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;
|
|
|
|
class UserTransactionController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function indexPembeli()
|
|
{
|
|
return view('user.transaction.pembeli.index', [
|
|
'name' => 'npannisa',
|
|
'TransactionUser' => TransactionUser::HistoryTransaction(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function indexPenjual()
|
|
{
|
|
return view('user.transaction.penjual.transaction-penjual', [
|
|
'name' => 'npannisa',
|
|
'TransactionUser' => TransactionUser::HistoryTransaction(),
|
|
]);
|
|
}
|
|
|
|
public function detailTransaction($id)
|
|
{
|
|
return view('user.transaction.pembeli.detail-transaction', [
|
|
'name' => 'npannisa',
|
|
'TransactionUser' => TransactionUser::HistoryTransaction(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function createTransaction(Request $request)
|
|
{
|
|
return view('user.transaction.pembeli.new-transaction', [
|
|
'name' => 'npannisa',
|
|
'TransactionUser' => TransactionUser::HistoryTransaction(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
|
|
$now = Carbon::now();
|
|
$bulan = $now->format('F');
|
|
$tahun = $now->year;
|
|
|
|
// $persentase_keuntungan = Setting::where('status','Active')
|
|
// ->where('bulan','=',$bulan)
|
|
// ->where('tahun','=',$tahun)->get();
|
|
$persentase_keuntungan = floatval('5');
|
|
|
|
$total_harga = $jumlah_barang * $harga_barang;
|
|
$total_keuntungan = $persentase_keuntungan * $total_harga;
|
|
$biaya_admin = 0.1;
|
|
$total_bayar = intval($biaya_admin + $total_keuntungan + $total_harga);
|
|
|
|
$params = [
|
|
'transaction_details' => [
|
|
'order_id' => Uuid::uuid4(),
|
|
'gross_amount' => $total_bayar,
|
|
],
|
|
'item_details' => [
|
|
[
|
|
'id' => $nama_barang.time(),
|
|
'price' => $total_bayar,
|
|
'quantity' => 1,
|
|
'name' => $nama_barang,
|
|
],
|
|
],
|
|
'customer_details' => [
|
|
'firts_name' => $nama_depan_pembeli,
|
|
'last_name' => $nama_belakang_pembeli,
|
|
'email' => $pembeli,
|
|
'phone' => $nohp_pembeli,
|
|
],
|
|
];
|
|
|
|
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;
|
|
$batas_pembayaran = $now->addDays(2)->toDateTimeString();
|
|
$batas_pengiriman_barang = $now->addDays(6)->toDateTimeString();
|
|
$status = 'Pending';
|
|
$query = Transaction::create([
|
|
'id' => Uuid::uuid4(),
|
|
'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,
|
|
'biaya_admin' => $biaya_admin,
|
|
'total_harga' => $total_harga,
|
|
'total_bayar' => $total_bayar,
|
|
'token' => $token,
|
|
'status' => $status,
|
|
'batas_pembayaran' => $batas_pembayaran,
|
|
'batas_pengiriman_barang' => $batas_pengiriman_barang,
|
|
]);
|
|
|
|
return response()->json($query);
|
|
|
|
// $result_api = json_decode($response->body(), true);
|
|
|
|
// $token = '124';
|
|
// $redirect_url = 'haha';
|
|
|
|
// // save to db
|
|
// $payment = new payment;
|
|
// $payment->order_id = $params['transaction_details'][ 'order_id'];
|
|
// $payment->status = 'pending';
|
|
// $payment->price = $request->price;
|
|
// $payment->customer_firts_name = $request->customer_firts_name;
|
|
// $payment->customer_email = $request->customer_email;
|
|
// $payment->item_name = $request->item_name;
|
|
// $payment->checkout_link = $response['redirect_url'];
|
|
// $payment->save();
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
//
|
|
}
|
|
}
|