public function onCheckout(CartCheckoutRequest $request) { $userInput = ['name' => $request->name, 'email' => $request->email, 'phone' => $request->phone, 'address1' => $request->address1, 'address2' => $request->address2, 'city' => $request->city, 'state' => $request->state, 'country' => $request->country, 'cardName' => $request->cardName, 'cardBrand' => $request->cardBrand, 'cardLastFour' => $request->cardLastFour, 'cardExpiryMonth' => $request->cardExpiryMonth, 'cardExpiryYear' => $request->cardExpiryYear]; $user = $this->user->store($userInput); $transaction = $this->gateway->charge($user, ShoppingCart::total(), $request->token); if (!$transaction) { event(new UserPurchaseWasNotSuccessful($user)); /** * If payment is not successful, delete the user to avoid duplicating his/her * record on the "users" table */ $this->user->delete($user->id); flash()->error('Your card was declined. Please try again.'); return redirect()->back()->withInput(); } $bookingData = ['booking_reference' => bookingReference($transaction), 'paid' => $transaction->paid, 'status' => $transaction->status, 'comments' => '']; $booking = $this->booking->createBooking($user, $bookingData); /** * Attach the selected packages on the bookings table */ $this->booking->attachPackages($booking, ShoppingCart::content()); /** * Delete the Booked items */ ShoppingCart::destroy(); /** * Fire off an event */ event(new UserPurchasedAPackage($user, $booking->booking_reference)); flash()->overlay('You have successfully booked the Package(s).'); return redirect()->route('cart.checkout.success'); }
/** * Execute the job. * * @return void */ public function handle(UserRepositoryInterface $userRepository, BillingGateway $gateway, ShoppingCart $cart) { $data = ['name' => $this->name, 'email' => $this->email, 'phone' => $this->phone, 'address1' => $this->address1, 'address2' => $this->address2, 'city' => $this->city, 'state' => $this->state, 'country' => $this->country]; $user = $userRepository->store($data); $chargeWasSuccessful = $gateway->charge($user, $cart->total(), $this->token); if ($chargeWasSuccessful) { /** * Save the data to "bookings" table */ $booking = $user->bookings()->create(['booking_reference' => bookingReference($chargeWasSuccessful), 'status' => 1, 'comments' => '']); foreach ($cart->content() as $item) { $packageId = $item->options->package->id; $adult_quantity = $item->qty; $child_quantity = $item->options->child_quantity; /** * Save the data to "booking_details" table */ $booking->packages()->attach($packageId, ['adult_quantity' => $adult_quantity, 'child_quantity' => $child_quantity, 'date' => $item->options->date, 'date_submit' => $item->options->date_submit, 'time' => $item->options->time]); } $cart->destroy(); event(new UserPurchasedAPackage($user, $booking->booking_reference)); } else { event(new UserPurchaseWasNotSuccessful($user)); /** * If payment is not successful, delete the user to avoid duplicating his/her * record on the "users" table */ $userRepository->delete($user->id); } }
/** * Execute the job. * * @return void */ public function handle(UserRepositoryInterface $userRepo, BookingRepositoryInterface $bookingRepo, BillingGateway $gateway) { $user = $userRepo->find($this->user_id); $user->cardName = $this->cardName; $user->cardBrand = $this->cardBrand; $user->cardLastFour = $this->cardLastFour; $user->cardExpiryMonth = $this->cardExpiryMonth; $user->cardExpiryYear = $this->cardExpiryYear; $user->save(); $booking = $bookingRepo->findByReference($this->booking_reference); $bookedPackages = $booking->packages; $total = calculateTotalAmount($bookedPackages); $transaction = $gateway->charge($user, $total, $this->token); if ($transaction) { $bookingRepo->update($booking->id, ['booking_reference' => bookingReference($transaction), 'paid' => $transaction->paid, 'status' => $transaction->status, 'comments' => '']); event(new UserPaidTheBooking($user, $this->booking_reference)); } else { event(new UserPurchaseWasNotSuccessful($user)); /** * If payment is not successful, delete the user to avoid duplicating his/her * record on the "users" table */ //$userRepository->delete($user->id); } }
/** * Execute the job. * * @return void */ public function handle(UserRepositoryInterface $userRepo, BillingGateway $gateway, ShoppingCart $cart) { $data = ['name' => $this->name, 'email' => $this->email, 'phone' => $this->phone, 'address1' => $this->address1, 'address2' => $this->address2, 'city' => $this->city, 'state' => $this->state, 'country' => $this->country, 'cardName' => $this->cardName, 'cardBrand' => $this->cardBrand, 'cardLastFour' => $this->cardLastFour, 'cardExpiryMonth' => $this->cardExpiryMonth, 'cardExpiryYear' => $this->cardExpiryYear]; $user = $userRepo->store($data); $transaction = $gateway->charge($user, $cart->total(), $this->token); if (!$transaction) { event(new UserPurchaseWasNotSuccessful($user)); /** * If payment is not successful, delete the user to avoid duplicating his/her * record on the "users" table */ $userRepo->delete($user->id); flash()->error(companyName(), 'Your card was declined.'); return redirect()->back(); } /** * Save the data to "bookings" table */ $booking = $user->bookings()->create(['booking_reference' => bookingReference($transaction), 'paid' => $transaction->paid, 'status' => $transaction->status, 'comments' => '']); foreach ($cart->content() as $item) { $packageId = $item->options->selectedPackage->id; $adult_quantity = $item->qty; $child_quantity = $item->options->child_quantity; /** * Save the data to "booking_details" table */ $booking->packages()->attach($packageId, ['adult_quantity' => $adult_quantity, 'child_quantity' => $child_quantity, 'date' => $item->options->date, 'date_submit' => $item->options->date_submit, 'time' => $item->options->time, 'ticket' => $item->options->ticket]); } $cart->destroy(); event(new UserPurchasedAPackage($user, $booking->booking_reference)); }