diff --git a/app/Http/Controllers/API/Contact/ContactApiController.php b/app/Http/Controllers/API/Contact/ContactApiController.php index 3a71ae0..4f39678 100644 --- a/app/Http/Controllers/API/Contact/ContactApiController.php +++ b/app/Http/Controllers/API/Contact/ContactApiController.php @@ -2,10 +2,135 @@ namespace App\Http\Controllers\API\Contact; -use App\Http\Controllers\Controller; +use Throwable; +use App\Models\User; +use App\Models\Contact; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; +use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\Auth; class ContactApiController extends Controller { - // + public function getContact() + { + $data = DB::table('contacts') + ->join('users', 'contacts.relasi_kontak', '=', 'users.email') + ->select('contacts.relasi_kontak', 'users.nama_depan', 'users.nama_belakang') + ->where('contacts.pemilik_kontak', '=', Auth::user()->email) + ->paginate(10); + return response()->json($data); + } + + public function store(Request $request) + { + $email_relasi = $request->email; + if ($email_relasi == Auth::user()->email) { + return response()->json([ + 'status' => false, + 'message' => 'Kontak yang ingin didaftarkan tidak boleh sama', + ]); + } + + try{ + DB::beginTransaction(); + + Contact::create([ + 'pemilik_kontak' => Auth::user()->email, + 'relasi_kontak' => $email_relasi, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Kontak berhasil ditambahkan.' + ]); + + }catch(Throwable $e){ + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi error di bagian server atau kontak sudah didaftarkan', + ]); + } + } + + public function destroy(Request $request) + { + try { + DB::beginTransaction(); + + Contact::destroy($request->id); + + DB::commit(); + + return response()->json([ + 'message' => 'Berhasil hapus data', + 'status' => true, + ]); + + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi error di bagian server.', + ]); + } + } + + public function cekEmail(Request $request) + { + $result = User::where('email', $request->email) + ->where('role', 'User') + ->first(); + if ($result) { + if ($result->status == 'Finished') { + return response()->json([ + 'status' => true, + 'message' => $result, + ]); + } else { + return response()->json([ + 'status' => false, + 'message' => 'Akun dengen email ' . $request->email . ' tersedia dan belum diverifikasi', + ]); + } + } else { + return response()->json([ + 'status' => false, + 'message' => 'Akun dengen email ' . $request->email . ' tidak tersedia atau ditolak', + ]); + } + } + + public function detailContact(Request $request){ + $data = Contact::join('users','contacts.relasi_kontak','=','users.email') + ->join('indonesia_villages','users.kode_kelurahan','=','indonesia_villages.code') + ->join('indonesia_districts','indonesia_villages.district_code','=','indonesia_districts.code') + ->join('indonesia_cities','indonesia_districts.city_code','=','indonesia_cities.code') + ->join('indonesia_provinces','indonesia_cities.province_code','=','indonesia_provinces.code') + ->where('contacts.id',$request->id) + ->select( + DB::raw("CONCAT(users.nama_depan,' ',users.nama_belakang) as nama_lengkap"), + 'users.alamat', + 'users.email', + 'users.nohp', + 'indonesia_villages.name as kelurahan', + 'indonesia_districts.name as kecamatan', + 'indonesia_cities.name as kota', + 'indonesia_provinces.name as provinsi' + ) + ->first(); + return response()->json([ + 'data' => $data + ]); + } } diff --git a/app/Http/Controllers/API/Pembeli/PembeliApiController.php b/app/Http/Controllers/API/Pembeli/PembeliApiController.php index bab4e6e..4c447ca 100644 --- a/app/Http/Controllers/API/Pembeli/PembeliApiController.php +++ b/app/Http/Controllers/API/Pembeli/PembeliApiController.php @@ -2,10 +2,726 @@ namespace App\Http\Controllers\API\Pembeli; -use App\Http\Controllers\Controller; +use Throwable; +use Carbon\Carbon; +use App\Models\User; +use App\Models\Refund; +use GuzzleHttp\Client; +use App\Models\Contact; +use App\Models\Setting; +use App\Models\Transaction; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; +use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\Http; +use App\Models\TransactionDescription; class PembeliApiController extends Controller { - // + public function create() + { + $now = Carbon::now(); + $bulan = $now->format('F'); + $tahun = $now->year; + $persentase_keuntungan = Setting::where('status', 'Active') + ->where('bulan', '=', $bulan) + ->where('tahun', '=', $tahun) + ->value('persentase'); + if (is_null($persentase_keuntungan)) { + $persentase_keuntungan = Setting::where('status', 'Active') + ->latest() + ->value('persentase'); + } + return view('user.transaction.pembeli.new-transaction', [ + 'persentase_keuntungan' => $persentase_keuntungan, + ]); + } + + public function store(Request $request) + { + $pembeli = auth()->user()->email; + $penjual = $request->email_penjual; + $nama_barang = $request->nama_barang; + $satuan_barang = $request->satuan_barang; + $deskripsi_transaksi = $request->deskripsi; + $harga_barang = $request->harga_barang; + $jumlah_barang = $request->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'); + $bank_penjual = User::where('email', $penjual)->value('nama_bank'); + $no_rek_penjual = User::where('email', $penjual)->value('no_rek'); + + if ($bank_penjual == '' && $no_rek_penjual == '') { + return response()->json([ + 'status' => false, + 'message' => 'Penjual belum memiliki/memasukan nomor rek bank', + ]); + } + + $alamat = ucwords(strtolower(auth()->user()->alamat)); + + $now = Carbon::now(); + + $persentase_keuntungan = $request->persentase_keuntungan; + + $total_harga = $request->total_harga; + $total_keuntungan = $request->total_keuntungan; + $total_bayar = $request->total_bayar; + + $batas_pembayaran = $now->addDays(1)->toTimeString(); + $batas_konfirmasi_transaksi = $now->addDays(2)->toDateTimeString(); + $batas_pengiriman_barang_awal = $now->addDays(3)->toDateTimeString(); + $batas_pengiriman_barang_akhir = $now->addDays(4)->toDateTimeString(); + + $status = 'created'; + + try { + DB::beginTransaction(); + + $query = Transaction::create([ + '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, + 'nama_bank_penjual' => $bank_penjual, + 'no_rek_penjual' => $no_rek_penjual, + 'status_transaksi' => $status, + 'batas_pembayaran' => $batas_pembayaran, + 'batas_konfirmasi_transaksi' => $batas_konfirmasi_transaksi, + 'batas_pengiriman_barang_awal' => $batas_pengiriman_barang_awal, + 'batas_pengiriman_barang_akhir' => $batas_pengiriman_barang_akhir, + ]); + + $params = [ + 'transaction_details' => [ + 'order_id' => $query->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' => $alamat, + 'city' => auth()->user()->village->district->city->name, + 'country_code' => 'IDN', + ], + ], + 'callbacks' => [ + 'finish' => route('user-pembeli.index'), + ], + 'enabled_payments' => ['credit_card', 'shopeepay', 'gopay', 'other_qris'], + ]; + + // $client = new Client([ + // 'verify' => false, + // ]); + + // $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + // $response = $client->request('POST', 'https://app.sandbox.midtrans.com/snap/v1/transactions', [ + // 'body' => json_encode($params), + // 'headers' => [ + // 'accept' => 'application/json', + // 'authorization' => 'Basic ' . $auth, + // 'content-type' => 'application/json', + // ], + // ]); + + // $result = json_decode($response->getBody(), true); + + // Transaction::where('id', $query->id)->update([ + // 'token' => $result['token'], + // ]); + + $contact = Contact::where('pemilik_kontak', $pembeli) + ->where('relasi_kontak', $penjual) + ->count(); + + if ($contact == 0) { + Contact::create([ + 'pemilik_kontak' => $pembeli, + 'relasi_kontak' => $penjual, + ]); + } + + TransactionDescription::create([ + 'transaction_id' => $query->id, + 'status' => $status, + 'user' => $pembeli, + 'judul' => 'fa fa-plus', + 'background' => 'bg-buyer', + 'deskripsi' => $nama_depan_pembeli . ' telah membuat transaksi baru dengan ' . $nama_penjual, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Berhasil menambahkan transaksi. Silahkan lakukan pembayaran.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal menambahkan transaksi.', + ]); + } + } + + public function finishTransaction(Request $request) + { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'finished', + 'status_pembayaran' => 'settlement', + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'finished', + 'background' => 'bg-buyer', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-check', + 'deskripsi' => 'Pesanan telah diselesaikan oleh ' . auth()->user()->nama_depan . '.', + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Pesanan telah diselesaikan oleh ' . auth()->user()->nama_depan . '.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal update status karena kesalahan server.', + ]); + } + } + + public function payTransaction(Request $request) + { + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->get('https://api.sandbox.midtrans.com/v2/' . $request->id . '/status'); + + $result = json_decode($response->body(), true); + + $code = substr($result['status_code'], 0, 1); + + try { + DB::beginTransaction(); + + if ($code == '4') { + return response()->json([ + 'status' => false, + 'message' => 'Terjadi error di server.', + 'data' => $result, + ]); + } else { + if ($result['transaction_status'] == 'settlement') { + $transaction = 'success'; + } elseif ($result['transaction_status'] == 'capture') { + if ($result['fraud_status'] == 'accept') { + $transaction = 'success'; + } elseif ($result['fraud_status'] == 'challenge') { + $transaction = 'challenge'; + } + } else { + $transaction = 'failure'; + } + + Transaction::where('id', $request->id)->update([ + 'metode_pembayaran' => $result['payment_type'], + 'tanggal_transaksi' => $result['transaction_time'], + 'status_transaksi' => $transaction, + 'status_pembayaran' => $result['transaction_status'], + 'fraud_status' => $result['fraud_status'], + 'signature_key' => $result['signature_key'], + ]); + + if ($transaction == 'success') { + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'success', + 'background' => 'bg-buyer', + 'judul' => 'fas fa-money-bill', + 'deskripsi' => auth()->user()->nama_depan . ' telah sukses melakukan pembayaran. Transaksi diteruskan ke penjual.', + 'user' => auth()->user()->email, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Pembayaran sukses', + ]); + } elseif ($transaction == 'challenge') { + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'challenge', + 'background' => 'bg-primary', + 'judul' => 'fas fa-clock', + 'deskripsi' => 'Transaksi ' . auth()->user()->email . ' terindikasi masalah, tunggu sesaat hingga admin menyetujui pembayaran.', + 'user' => 'admin@example.net', + 'keterangan' => $result['status_message'], + ]); + + DB::commit(); + + return response()->json([ + 'status' => false, + 'message' => 'Pembayaran ditunda hingga disetujui oleh admin.', + ]); + } else { + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'failure', + 'background' => 'bg-primary', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => 'Terjadi kegagalan pembayaran.', + 'user' => 'admin@example.net', + 'keterangan' => $result['status_message'], + ]); + + DB::commit(); + + return response()->json([ + 'status' => false, + 'message' => 'Transaksi pembayaran gagal', + ]); + } + } + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Transaksi pembayaran gagal.', + ]); + } + + return response()->json(); + } + + public function cancelPayment(Request $request) + { + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->post('https://api.sandbox.midtrans.com/v2/' . $request->id . '/cancel'); + + $result = json_decode($response->body(), true); + + if (in_array($result['status_code'], ['412','401'])) { + return response()->json([ + 'status' => false, + 'message' => 'Transaksi gagal.', + 'data' => $result + ]); + } else { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'failure', + 'status_pembayaran' => 'cancel' + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'cancel', + 'background' => 'bg-buyer', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => auth()->user()->nama_depan . ' telah membatalkan transaksi.', + 'user' => auth()->user()->email, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Transaksi berhasil dibatalkan', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Transaksi gagal dibatalkan', + ]); + } + } + } + + public function cancelTransaction(Request $request){ + $transaction = Transaction::where('id', $request->id)->first(); + + $params = [ + 'refund_key' => $request->id . '-ref1', + 'amount' => $transaction->total_bayar, + 'reason' => $request->complaint, + ]; + + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->post('https://api.sandbox.midtrans.com/v2/' . $request->id . '/refund', $params); + + $result = json_decode($response->body(), true); + $code = $result['status_code']; + $code = '200'; + + if ($code == '200') { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'failure', + 'status_pembayaran' => 'refund', + ]); + + Refund::create([ + 'transaction_id' => $request->id, + 'total' => $transaction->total_bayar, + 'due_date' => now(), + 'status' => 'refund', + 'complaint' => $request->complaint, + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'refund', + 'background' => 'bg-seller', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-times', + 'deskripsi' => 'Transaksi dibatalkan oleh ' . auth()->user()->nama_depan . '. Alasan : ' . $request->complaint, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Transaksi telah dibatalkan.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal update status karena kesalahan server.', + ]); + } + } else { + Log::error($result['status_message']); + + return response()->json([ + 'status' => false, + 'message' => 'Transaksi gagal', + ]); + } + } + + public function pendingTransaction(Request $request) + { + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->get('https://api.sandbox.midtrans.com/v2/' . $request->id . '/status'); + + $result = json_decode($response->body(), true); + + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_pembayaran' => $result['transaction_status'], + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Pembayaran di-pending, silahkan masuk lagi dan bayar secepat mungkin.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi error di bagian server.', + ]); + } + } + + public function complaintTransaction($id) + { + return view('user.refund.new-refund', compact('id')); + } + + public function onErrorTransaction(Request $request) + { + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->get('https://api.sandbox.midtrans.com/v2/' . $request->id . '/status'); + + $result = json_decode($response->body(), true); + + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_pembayaran' => $result['transaction_status'], + ]); + + if ($result['transaction_status'] == 'expire') { + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'cancel', + 'background' => 'bg-buyer', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => 'Pembayaran sudah expire', + 'user' => 'admin@example.net', + ]); + + DB::commit(); + + return response()->json([ + 'status' => false, + 'message' => 'Pembayaran sudah expire, silahkan buat transaksi baru.', + ]); + } elseif ($result['transaction'] == 'failure') { + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'failure', + 'background' => 'bg-buyer', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => auth()->user()->nama_depan . ' telah membatalkan transaksi.', + 'user' => 'admin@example.net', + ]); + + DB::commit(); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi kesalahan di server saat pembayaran.', + 'data' => $result, + ]); + } else { + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => $result['transaction_status'], + 'background' => 'bg-primary', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => 'Status tidak diketahui', + 'user' => 'admin@example.net', + 'keterangan' => $result['status_message'], + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Terjadi kesalahan di server', + 'data' => $result, + ]); + } + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi error di bagian server.', + ]); + } + } + + public function onCloseTransaction(Request $request) + { + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->get('https://api.sandbox.midtrans.com/v2/' . $request->id . '/status'); + + $result = json_decode($response->body(), true); + + $status = $result['status_code'] == '404' ? '' : $result['transaction_status']; + + if ($status == '') { + return response()->json([ + 'status' => true, + 'message' => 'Halaman pembayaran telah ditutup. Silahkan lakukan pembayaran lagi.', + ]); + } else { + try { + DB::beginTransaction(); + + if ($result['transaction_status'] == 'expire') { + Transaction::where('id', $request->id)->update([ + 'status_pembayaran' => $result['transaction_status'], + 'status_transaksi' => 'failure', + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'cancel', + 'background' => 'bg-buyer', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => 'Pembayaran sudah expire', + 'user' => 'admin@example.net', + ]); + + DB::commit(); + + return response()->json([ + 'status' => false, + 'message' => 'Pembayaran sudah expire, silahkan buat transaksi baru.', + ]); + } elseif ($result['transaction'] == 'failure') { + Transaction::where('id', $request->id)->update([ + 'status_pembayaran' => $result['transaction_status'], + 'status_transaksi' => 'failure', + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'failure', + 'background' => 'bg-buyer', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => auth()->user()->nama_depan . ' telah membatalkan transaksi.', + 'user' => 'admin@example.net', + ]); + + DB::commit(); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi kesalahan di server saat pembayaran.', + ]); + } else { + Transaction::where('id', $request->id)->update([ + 'status_pembayaran' => $result['transaction_status'], + 'status_transaksi' => 'failure', + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => $result['transaction_status'], + 'background' => 'bg-primary', + 'judul' => 'fas fa-exclamation', + 'deskripsi' => 'Status tidak diketahui.', + 'user' => 'admin@example.net', + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Terjadi kesalahan di server', + ]); + } + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Terjadi error di bagian server.', + 'data' => $result + ]); + } + } + } + + + } diff --git a/app/Http/Controllers/API/Penjual/PenjualApiController.php b/app/Http/Controllers/API/Penjual/PenjualApiController.php index 2fa025a..024544e 100644 --- a/app/Http/Controllers/API/Penjual/PenjualApiController.php +++ b/app/Http/Controllers/API/Penjual/PenjualApiController.php @@ -2,10 +2,219 @@ namespace App\Http\Controllers\API\Penjual; -use App\Http\Controllers\Controller; +use Throwable; +use App\Models\Refund; +use App\Models\transaction; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; +use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\Http; +use App\Models\TransactionDescription; +use Illuminate\Support\Facades\Storage; class PenjualApiController extends Controller { - // + public function acceptTransaction(Request $request) + { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'process', + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'process', + 'background' => 'bg-seller', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-handshake', + 'deskripsi' => 'Transaksi telah diterima oleh ' . auth()->user()->nama_depan, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Transaksi telah diterima. Siapkan pesanan untuk dikirim ke penjual.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal update status karena kesalahan server.', + ]); + } + } + + public function denyTransaction(Request $request) + { + $transaction = Transaction::where('id', $request->id)->first(); + + $params = [ + 'refund_key' => $request->id . '-ref1', + 'amount' => $transaction->total_bayar, + 'reason' => $request->complaint, + ]; + + $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); + + $response = Http::withOptions([ + 'verify' => false, + ]) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Authorization' => "Basic $auth", + ]) + ->post('https://api.sandbox.midtrans.com/v2/' . $request->id . '/refund', $params); + + $result = json_decode($response->body(), true); + $code = $result['status_code']; + $code = '200'; + + if ($code == '200') { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'failure', + 'status_pembayaran' => 'refund', + ]); + + Refund::create([ + 'transaction_id' => $request->id, + 'total' => $transaction->total_bayar, + 'due_date' => now(), + 'status' => 'refund', + 'complaint' => $request->complaint, + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'refund', + 'background' => 'bg-seller', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-times', + 'deskripsi' => 'Transaksi ditolak ' . auth()->user()->nama_depan . ', uang akan dikembalikan ke pembeli. Alasan : ' . $request->complaint, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Transaksi telah ditolak. Uang akan dikirimkan ke pembeli.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal update status karena kesalahan server.', + ]); + } + } else { + Log::error($result['status_message']); + + return response()->json([ + 'status' => false, + 'message' => 'Transaksi gagal', + ]); + } + } + + public function sendingOrder(Request $request) + { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'sending', + ]); + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'sending', + 'background' => 'bg-seller', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-truck-moving', + 'deskripsi' => 'Pesanan telah dikirim oleh ' . auth()->user()->nama_depan . ' dan sedang dalam perjalanan menuju pembeli.', + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Pesanan sedang dikirim dan menuju pembeli.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal update status karena kesalahan server.', + ]); + } + } + + public function sentOrder(Request $request) + { + try { + DB::beginTransaction(); + + Transaction::where('id', $request->transaction_id)->update([ + 'status_transaksi' => 'sent', + ]); + + $bukti_foto = ''; + + if ($request->hasFile('bukti_foto')) { + $file = $request->file('bukti_foto'); + $bukti_foto = time() . '.' . $file->getClientOriginalExtension(); + $path = 'bukti-foto/' . $bukti_foto; + + Storage::disk('public')->put($path, file_get_contents($file)); + } + + TransactionDescription::create([ + 'transaction_id' => $request->transaction_id, + 'status' => 'sent', + 'background' => 'bg-seller', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-check', + 'deskripsi' => 'Pesanan telah sampai di tempat pembeli. Keterangan: ' . $request->keterangan_bukti, + 'bukti_foto' => $bukti_foto, + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Pesanan telah sampai di tempat pembeli.', + ]); + } catch (Throwable $e) { + DB::rollBack(); + + Log::error($e->getMessage()); + + return response()->json([ + 'status' => false, + 'message' => 'Gagal update status karena kesalahan server.', + ]); + } + + return response([ + 'status' => true, + 'message' => 'Sukses kirim data.', + 'data' => $request, + ]); + } + } diff --git a/app/Http/Controllers/API/Refund/RefundApiController.php b/app/Http/Controllers/API/Refund/RefundApiController.php index 8ee5945..2a8f11e 100644 --- a/app/Http/Controllers/API/Refund/RefundApiController.php +++ b/app/Http/Controllers/API/Refund/RefundApiController.php @@ -2,10 +2,97 @@ namespace App\Http\Controllers\API\Refund; -use App\Http\Controllers\Controller; +use Throwable; +use Carbon\Carbon; +use App\Models\Refund; +use App\Models\Transaction; use Illuminate\Http\Request; +use App\Models\RefundDescription; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; +use App\Http\Controllers\Controller; +use App\Models\TransactionDescription; class RefundApiController extends Controller { - // + public function create($id) + { + return view('user.refund.new-refund',['id' => $id]); + } + + public function store(Request $request){ + $now = Carbon::now(); + $due_date = $now->addDays(2)->toDateTimeString(); + + try{ + DB::beginTransaction(); + + $transaction = Transaction::where('id',$request->id)->first(); + + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'refund' + ]); + + $refund = Refund::create([ + 'transaction_id' => $request->id, + 'total' => $transaction->total_harga, + 'due_date' => $due_date, + 'complaint' => $request->complaint + ]); + + if ($request->hasFile('files')) { + $files = $request->file('files'); + foreach ($files as $file) { + $filename = $file->getClientOriginalName(); + $mime = $file->getClientMimeType(); + if (strpos($mime, 'image') !== false) { + $type = 'image'; + $file->storeAs('public/refund-image/', $filename); + } elseif (strpos($mime, 'video') !== false) { + $type = 'video'; + $file->storeAs('public/refund-video/', $filename); + } else { + $type = 'Other'; + } + + RefundDescription::create([ + 'refund_id' => $refund->id, + 'filename' => $filename, + 'type' => $type + ]); + } + } + + TransactionDescription::create([ + 'transaction_id' => $request->id, + 'status' => 'pending', + 'user' => auth()->user()->email, + 'judul' => 'fas fa-clock', + 'background' => 'bg-buyer', + 'deskripsi' => auth()->user()->nama_depan.' mengajukan refund.', + ]); + + DB::commit(); + + return response()->json([ + 'status' => true, + 'message' => 'Permintaan refund anda telah dikirim ke admin untuk direview. Mohon tunggu maksimal 2 hari.', + ]); + }catch(Throwable $e){ + DB::rollback(); + + Log::error($e->getMessage()); + + return response()->json(['success' => false, 'message' => 'Terjadi Kesalahan pada sisi server']); + } + } + + public function show($id){ + $refund = Refund::find($id); + $refundDescription = RefundDescription::where('refund_id',$id)->get(); + return view('user.refund.detail-refund',[ + 'refund' => $refund, + 'descriptions' => $refundDescription + ]); + } } diff --git a/app/Http/Controllers/API/Transaction/TransactionApiController.php b/app/Http/Controllers/API/Transaction/TransactionApiController.php index 909f009..d13991d 100644 --- a/app/Http/Controllers/API/Transaction/TransactionApiController.php +++ b/app/Http/Controllers/API/Transaction/TransactionApiController.php @@ -2,10 +2,28 @@ namespace App\Http\Controllers\API\Transaction; -use App\Http\Controllers\Controller; use Illuminate\Http\Request; +use App\Http\Controllers\Controller; +use App\Models\TransactionDescription; +use App\Models\Transaction; class TransactionApiController extends Controller { - // + public function transactionTracking(Request $request){ + $data = TransactionDescription::where('transaction_id', $request->id)->get(); + + return response()->json([ + 'data' => $data + ]); + } + + public function show($id) + { + return view('user.transaction.pembeli.detail-transaction', [ + 'transaction' => Transaction::findOrFail($id), + 'trackings' => TransactionDescription::where('transaction_id', $id) + ->latest() + ->get(), + ]); + } }