Test Failed
Push — master ( 55431f...64d5a8 )
by Jeff
04:55
created

CheckoutController::executePayPalPayment()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 7
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface;
6
use App\Shop\Cart\Requests\CartCheckoutRequest;
7
use App\Shop\Carts\Repositories\Interfaces\CartRepositoryInterface;
8
use App\Shop\Carts\Requests\PayPalCheckoutExecutionRequest;
9
use App\Shop\Carts\Requests\StripeExecutionRequest;
10
use App\Shop\Couriers\Repositories\Interfaces\CourierRepositoryInterface;
11
use App\Shop\Customers\Repositories\CustomerRepository;
12
use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface;
13
use App\Shop\OrderDetails\Repositories\Interfaces\OrderProductRepositoryInterface;
14
use App\Shop\Orders\Repositories\Interfaces\OrderRepositoryInterface;
15
use App\Shop\PaymentMethods\Paypal\Exceptions\PaypalRequestError;
16
use App\Shop\PaymentMethods\Paypal\Repositories\PayPalExpressCheckoutRepository;
17
use App\Shop\PaymentMethods\Stripe\Exceptions\StripeChargingErrorException;
18
use App\Shop\PaymentMethods\Stripe\StripeRepository;
19
use App\Shop\Products\Repositories\Interfaces\ProductRepositoryInterface;
20
use App\Shop\Products\Transformations\ProductTransformable;
21
use Exception;
22
use App\Http\Controllers\Controller;
23
use Gloudemans\Shoppingcart\Facades\Cart;
24
use Illuminate\Http\Request;
25
use Illuminate\Support\Facades\Log;
26
use PayPal\Exception\PayPalConnectionException;
27
28
class CheckoutController extends Controller
29
{
30
    use ProductTransformable;
31
32
    private $cartRepo;
33
    private $courierRepo;
34
    private $addressRepo;
35
    private $customerRepo;
36
    private $productRepo;
37
    private $orderRepo;
38
    private $courierId;
39
    private $orderProductRepo;
40
    private $payPal;
41
42
    public function __construct(
43
        CartRepositoryInterface $cartRepository,
44
        CourierRepositoryInterface $courierRepository,
45
        AddressRepositoryInterface $addressRepository,
46
        CustomerRepositoryInterface $customerRepository,
47
        ProductRepositoryInterface $productRepository,
48
        OrderRepositoryInterface $orderRepository,
49
        OrderProductRepositoryInterface $orderProductRepository
50
    ) {
51
        $this->cartRepo = $cartRepository;
52
        $this->courierRepo = $courierRepository;
53
        $this->addressRepo = $addressRepository;
54
        $this->customerRepo = $customerRepository;
55
        $this->productRepo = $productRepository;
56
        $this->orderRepo = $orderRepository;
57
        $this->orderProductRepo = $orderProductRepository;
58
59
        $payPalRepo = new PayPalExpressCheckoutRepository();
60
        $this->payPal = $payPalRepo;
61
    }
62
63
    /**
64
     * Display a listing of the resource.
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function index()
69
    {
70
        $customer = $this->customerRepo->findCustomerById($this->loggedUser()->id);
71
72
        $this->courierId = request()->session()->get('courierId', 1);
73
        $courier = $this->courierRepo->findCourierById($this->courierId);
74
75
        $shippingCost = $this->cartRepo->getShippingFee($courier);
76
77
        $addressId = request()->session()->get('addressId', 1);
78
        $paymentId = request()->session()->get('paymentName', 'paypal');
79
80
        // Get payees
81
        $paymentMethods = config('payees.name');
82
        $payees = explode(',', $paymentMethods);
83
84
        $paymentGateways = collect($payees)->transform(function ($name) {
85
            return config($name);
86
        })->filter()->all();
87
88
        $courier = $this->courierRepo->findCourierById(1);
89
        $shippingFee = $this->cartRepo->getShippingFee($courier);
90
91
        return view('front.checkout', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('front.check...gFee' => $shippingFee)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
92
            'customer' => $customer,
93
            'addresses' => $customer->addresses()->get(),
94
            'products' => $this->cartRepo->getCartItems(),
95
            'subtotal' => $this->cartRepo->getSubTotal(),
96
            'shipping' => $shippingCost,
97
            'tax' => $this->cartRepo->getTax(),
98
            'total' => $this->cartRepo->getTotal(2, $shippingCost),
99
            'couriers' => $this->courierRepo->listCouriers(),
100
            'selectedCourier' => $this->courierId,
101
            'selectedAddress' => $addressId,
102
            'selectedPayment' => $paymentId,
103
            'payments' => $paymentGateways,
104
            'cartItems' => $this->cartRepo->getCartItemsTransformed(),
105
            'shippingFee' => $shippingFee
106
        ]);
107
    }
108
109
    /**
110
     * Checkout the items
111
     *
112
     * @param CartCheckoutRequest $request
113
     * @return \Illuminate\Http\RedirectResponse
114
     * @codeCoverageIgnore
115
     */
116
    public function store(CartCheckoutRequest $request)
117
    {
118
        $courier = $this->courierRepo->findCourierById($request->input('courier'));
119
        $shippingFee = $this->cartRepo->getShippingFee($courier);
120
121
        switch ($request->input('payment')) {
122
            case 'paypal':
123
                return $this->payPal->process($courier, $request);
124
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
125
            case 'stripe':
126
127
                $details = [
128
                    'description' => 'Stripe payment',
129
                    'metadata' => $this->cartRepo->getCartItems()->all()
130
                ];
131
132
                $customerRepo = new CustomerRepository($this->loggedUser());
133
                $customerRepo->charge($this->cartRepo->getTotal(2, $shippingFee), $details);
134
                break;
135
            default:
136
        }
137
    }
138
139
    /**
140
     * Execute the PayPal payment
141
     *
142
     * @param PayPalCheckoutExecutionRequest $request
143
     * @return \Illuminate\Http\RedirectResponse
144
     */
145
    public function executePayPalPayment(PayPalCheckoutExecutionRequest $request)
146
    {
147
        try {
148
            $this->payPal->execute($request);
149
            $this->cartRepo->clearCart();
150
151
            return redirect()->route('checkout.success');
152
        } catch (PayPalConnectionException $e) {
153
            throw new PaypalRequestError($e->getData());
154
        } catch (Exception $e) {
155
            throw new PaypalRequestError($e->getMessage());
156
        }
157
    }
158
159
    /**
160
     * @param StripeExecutionRequest $request
161
     * @return \Stripe\Charge
162
     */
163
    public function charge(StripeExecutionRequest $request)
164
    {
165
        try {
166
            $customer = auth()->user();
167
            $stripeRepo = new StripeRepository($customer);
168
169
            return $stripeRepo->execute(
170
                $request->all(),
171
                Cart::total(),
172
                Cart::tax()
173
            );
174
        } catch (StripeChargingErrorException $e) {
175
            Log::info($e->getMessage());
176
            return redirect()->route('checkout.index')->with('error', 'There is a problem processing your request.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...cessing your request.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Stripe\Charge.
Loading history...
177
        }
178
    }
179
180
    /**
181
     * Cancel page
182
     *
183
     * @param Request $request
184
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
185
     */
186
    public function cancel(Request $request)
187
    {
188
        return view('front.checkout-cancel', ['data' => $request->all()]);
189
    }
190
191
    /**
192
     * Success page
193
     *
194
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
195
     */
196
    public function success()
197
    {
198
        return view('front.checkout-success');
199
    }
200
}
201