| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 | <?phpnamespace App\Http\Controllers\Front;use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface;use App\Shop\Cart\Requests\CartCheckoutRequest;use App\Shop\Carts\Repositories\Interfaces\CartRepositoryInterface;use App\Shop\Carts\Requests\PayPalCheckoutExecutionRequest;use App\Shop\Carts\Requests\StripeExecutionRequest;use App\Shop\Couriers\Repositories\Interfaces\CourierRepositoryInterface;use App\Shop\Customers\Customer;use App\Shop\Customers\Repositories\CustomerRepository;use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface;use App\Shop\Orders\Repositories\Interfaces\OrderRepositoryInterface;use App\Shop\PaymentMethods\Paypal\Exceptions\PaypalRequestError;use App\Shop\PaymentMethods\Paypal\Repositories\PayPalExpressCheckoutRepository;use App\Shop\PaymentMethods\Stripe\Exceptions\StripeChargingErrorException;use App\Shop\PaymentMethods\Stripe\StripeRepository;use App\Shop\Products\Repositories\Interfaces\ProductRepositoryInterface;use App\Shop\Products\Transformations\ProductTransformable;use App\Shop\Shipping\ShippingInterface;use Exception;use App\Http\Controllers\Controller;use Gloudemans\Shoppingcart\Facades\Cart;use Illuminate\Http\Request;use Illuminate\Support\Collection;use Illuminate\Support\Facades\Log;use PayPal\Exception\PayPalConnectionException;class CheckoutController extends Controller{    use ProductTransformable;    /**     * @var CartRepositoryInterface     */    private $cartRepo;    /**     * @var CourierRepositoryInterface     */    private $courierRepo;    /**     * @var AddressRepositoryInterface     */    private $addressRepo;    /**     * @var CustomerRepositoryInterface     */    private $customerRepo;    /**     * @var ProductRepositoryInterface     */    private $productRepo;    /**     * @var OrderRepositoryInterface     */    private $orderRepo;    /**     * @var PayPalExpressCheckoutRepository     */    private $payPal;    /**     * @var ShippingInterface     */    private $shippingRepo;    public function __construct(        CartRepositoryInterface $cartRepository,        CourierRepositoryInterface $courierRepository,        AddressRepositoryInterface $addressRepository,        CustomerRepositoryInterface $customerRepository,        ProductRepositoryInterface $productRepository,        OrderRepositoryInterface $orderRepository,        ShippingInterface $shipping    ) {        $this->cartRepo = $cartRepository;        $this->courierRepo = $courierRepository;        $this->addressRepo = $addressRepository;        $this->customerRepo = $customerRepository;        $this->productRepo = $productRepository;        $this->orderRepo = $orderRepository;        $this->payPal = new PayPalExpressCheckoutRepository;        $this->shippingRepo = $shipping;    }    /**     * Display a listing of the resource.     *     * @param Request $request     *     * @return \Illuminate\Http\Response     */    public function index(Request $request)    {        $products = $this->cartRepo->getCartItems();        $customer = $request->user();        $rates = null;        $shipment_object_id = null;        if (env('ACTIVATE_SHIPPING') == 1) {            $shipment = $this->createShippingProcess($customer, $products);            if (!is_null($shipment)) {                $shipment_object_id = $shipment->object_id;                $rates = $shipment->rates;            }        }        // Get payment gateways        $paymentGateways = collect(explode(',', config('payees.name')))->transform(function ($name) {            return config($name);        })->all();        $billingAddress = $customer->addresses()->first();        return view('front.checkout', [            'customer' => $customer,            'billingAddress' => $billingAddress,            'addresses' => $customer->addresses()->get(),            'products' => $this->cartRepo->getCartItems(),            'subtotal' => $this->cartRepo->getSubTotal(),            'tax' => $this->cartRepo->getTax(),            'total' => $this->cartRepo->getTotal(2),            'payments' => $paymentGateways,            'cartItems' => $this->cartRepo->getCartItemsTransformed(),            'shipment_object_id' => $shipment_object_id,            'rates' => $rates        ]);    }    /**     * Checkout the items     *     * @param CartCheckoutRequest $request     *     * @return \Illuminate\Http\RedirectResponse     * @throws \App\Shop\Addresses\Exceptions\AddressNotFoundException     * @throws \App\Shop\Customers\Exceptions\CustomerPaymentChargingErrorException     * @codeCoverageIgnore     */    public function store(CartCheckoutRequest $request)    {        $shippingFee = 0;        switch ($request->input('payment')) {            case 'paypal':                return $this->payPal->process($shippingFee, $request);                break;            case 'stripe':                $details = [                    'description' => 'Stripe payment',                    'metadata' => $this->cartRepo->getCartItems()->all()                ];                $customer = $this->customerRepo->findCustomerById(auth()->id());                $customerRepo = new CustomerRepository($customer);                $customerRepo->charge($this->cartRepo->getTotal(2, $shippingFee), $details);                break;            default:        }    }    /**     * Execute the PayPal payment     *     * @param PayPalCheckoutExecutionRequest $request     * @return \Illuminate\Http\RedirectResponse     */    public function executePayPalPayment(PayPalCheckoutExecutionRequest $request)    {        try {            $this->payPal->execute($request);            $this->cartRepo->clearCart();            return redirect()->route('checkout.success');        } catch (PayPalConnectionException $e) {            throw new PaypalRequestError($e->getData());        } catch (Exception $e) {            throw new PaypalRequestError($e->getMessage());        }    }    /**     * @param StripeExecutionRequest $request     * @return \Stripe\Charge     */    public function charge(StripeExecutionRequest $request)    {        try {            $customer = $this->customerRepo->findCustomerById(auth()->id());            $stripeRepo = new StripeRepository($customer);            $stripeRepo->execute(                $request->all(),                Cart::total(),                Cart::tax()            );            return redirect()->route('checkout.success')->with('message', 'Stripe payment successful!');        } catch (StripeChargingErrorException $e) {            Log::info($e->getMessage());            return redirect()->route('checkout.index')->with('error', 'There is a problem processing your request.');        }    }    /**     * Cancel page     *     * @param Request $request     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View     */    public function cancel(Request $request)    {        return view('front.checkout-cancel', ['data' => $request->all()]);    }    /**     * Success page     *     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View     */    public function success()    {        return view('front.checkout-success');    }    /**     * @param Customer $customer     * @param Collection $products     *     * @return mixed     */    private function createShippingProcess(Customer $customer, Collection $products)    {        $customerRepo = new CustomerRepository($customer);        if ($customerRepo->findAddresses()->count() > 0 && $products->count() > 0) {            $this->shippingRepo->setPickupAddress();            $deliveryAddress = $customerRepo->findAddresses()->first();            $this->shippingRepo->setDeliveryAddress($deliveryAddress);            $this->shippingRepo->readyParcel($this->cartRepo->getCartItems());            return $this->shippingRepo->readyShipment();        }    }}
 |