SHARE
TWEET

CheckoutController

a guest Feb 18th, 2020 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Transaction;
  6. use App\TransactionDetail;
  7. use App\TravelPackage;
  8. use Carbon\Carbon;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11.  
  12. class CheckoutController extends Controller
  13. {
  14.     public function index(Request $request, $id)
  15.     {
  16.         $item = Transaction::with(['details', 'travel_package', 'user'])->findOrFail($id);
  17.  
  18.         return view('pages.checkout', [
  19.             'item' => $item
  20.         ]);
  21.     }
  22.  
  23.     public function process(Request $request, $id)
  24.     {
  25.         $travel_package = TravelPackage::findOrFail($id);
  26.  
  27.         $transaction = Transaction::create([
  28.             'travel_packages_id' => $id,
  29.             'users_id' => Auth::user()->id,
  30.             'additional_visa' => 0,
  31.             'transaction_total' => $travel_package->price,
  32.             'transaction_status' => 'IN_CART'
  33.         ]);
  34.  
  35.         TransactionDetail::create([
  36.             'transactions_id' => $transaction->id,
  37.             'username' => Auth::user()->username,
  38.             'nationality' => 'ID',
  39.             'is_visa' => false,
  40.             'doe_passport' => Carbon::now()->addYears(5)
  41.         ]);
  42.  
  43.         return redirect()->route('checkout', $transaction->id);
  44.     }
  45.  
  46.     public function remove(Request $request, $detail_id)
  47.     {
  48.         $item = TransactionDetail::findorFail($detail_id);
  49.  
  50.         $transaction = Transaction::with(['details', 'travel_package'])
  51.             ->findOrFail($item->transactions_id);
  52.  
  53.         if ($item->is_visa) {
  54.             $transaction->transaction_total -= 190;
  55.             $transaction->additional_visa -= 190;
  56.         }
  57.  
  58.         $transaction->transaction_total -= $transaction->travel_package->price;
  59.  
  60.         $transaction->save();
  61.         $item->delete();
  62.  
  63.         return redirect()->route('checkout', $item->transactions_id);
  64.     }
  65.  
  66.     public function create(Request $request, $id)
  67.     {
  68.         $request->validate([
  69.             'username' => 'required|string|exists:users,username',
  70.             'is_visa' => 'required|boolean',
  71.             'doe_passport' => 'required',
  72.         ]);
  73.  
  74.         $data = $request->all();
  75.         $data['transactions_id'] = $id;
  76.  
  77.         TransactionDetail::create($data);
  78.  
  79.         $transaction = Transaction::with(['travel_package'])->find($id);
  80.  
  81.         if ($request->is_visa) {
  82.             $transaction->transaction_total += 190;
  83.             $transaction->additional_visa += 190;
  84.         }
  85.  
  86.         $transaction->transaction_total += $transaction->travel_package->price;
  87.  
  88.         $transaction->save();
  89.  
  90.         return redirect()->route('checkout', $id);
  91.     }
  92.  
  93.     public function success(Request $request, $id)
  94.     {
  95.         $transaction = Transaction::with(['details', 'travel-package.galleries', 'user'])->findOrFail($id);
  96.         $transaction->transaction_status = 'PENDING';
  97.  
  98.         $transaction->save();
  99.  
  100.         //kirim email ke user eticketnya
  101.         Mail::to($transaction->user)->send(
  102.             new TransactionSuccess($transaction)
  103.         );
  104.  
  105.         return view('pages.success');
  106.     }
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top