Advertisement
ronikuchan

tarik dana

May 3rd, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. public function tarikDanaWallet(Request $request)
  2. {
  3. $validator = Validator::make($request->all(), [
  4. 'amount' => 'required',
  5. ]);
  6. if ($validator->fails()) {
  7. return response()->json([
  8. 'success' => false,
  9. 'messages' => 'Please fill in the blank !',
  10. 'data' => $validator->errors(),
  11. ], 403);
  12. }
  13.  
  14. $getUser = Auth::User();
  15.  
  16. if ($request->amount > $getUser->uang) {
  17. return response()->json([
  18. 'success' => false,
  19. 'messages' => 'Nominal Terlalu Besar !',
  20. 'data' => [],
  21. ], 403);
  22. }
  23.  
  24. // $getUser->uang = $getUser->uang - $request->amount; // ketika verifiasi/done saja oleh cfo (roni)
  25. // $getUser->save();
  26.  
  27. // get saldo akhir transaksi/withdraw terakhir
  28. $transaksiSaldoAkhir = TransaksiKeuangan::where('user_id', $getUser->_id)->where('tag', 'WITHDRAW WALLET')->orderBy('created_at', 'desc')->first();
  29. if ($transaksiSaldoAkhir) {
  30. $uangAkhir = $transaksiSaldoAkhir->saldo_akhir;
  31. } else {
  32. $uangAkhir = $getUser->uang;
  33. }
  34.  
  35. // insert ke transaksi keuangan
  36. $HistoryBonusMitra = TransaksiKeuangan::create([
  37. 'user_id' => Auth::id(),
  38. 'tanggal_transaksi' => date('Y-m-d'),
  39. 'tipe' => "OUT",
  40. 'nominal' => (int) $request->amount,
  41. 'saldo_akhir' => (int) $uangAkhir - $request->amount,
  42. 'tag' => "WITHDRAW WALLET",
  43. 'keterangan' => "Penarikan Dompet Mitra",
  44. 'status' => "PENGAJUAN",
  45. ]);
  46.  
  47. if ($HistoryBonusMitra) {
  48. // Mail::send(new \App\Mail\SendNotifications(Auth::User(), 'mitraWithDraw', $HistoryBonusMitra));
  49. }
  50. if (!empty($getUser)) {
  51. return response()->json([
  52. 'success' => true,
  53. 'messages' => 'Tarik Dana Berhasil !',
  54. 'data' => [
  55. 'id' => $getUser->_id,
  56. 'nama' => $getUser->name,
  57. 'nominal' => $getUser->uang,
  58. ],
  59. ], 200);
  60. } else {
  61. return response()->json([
  62. 'success' => false,
  63. 'messages' => 'User Not Found !',
  64. 'data' => null,
  65. ], 403);
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement