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\Orders\Repositories\Interfaces\OrderRepositoryInterface; |
14
|
|
|
use App\Shop\PaymentMethods\Paypal\Exceptions\PaypalRequestError; |
15
|
|
|
use App\Shop\PaymentMethods\Paypal\Repositories\PayPalExpressCheckoutRepository; |
16
|
|
|
use App\Shop\PaymentMethods\Stripe\Exceptions\StripeChargingErrorException; |
17
|
|
|
use App\Shop\PaymentMethods\Stripe\StripeRepository; |
18
|
|
|
use App\Shop\Products\Repositories\Interfaces\ProductRepositoryInterface; |
19
|
|
|
use App\Shop\Products\Transformations\ProductTransformable; |
20
|
|
|
use Exception; |
21
|
|
|
use App\Http\Controllers\Controller; |
22
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart; |
23
|
|
|
use Illuminate\Http\Request; |
24
|
|
|
use Illuminate\Support\Facades\Log; |
25
|
|
|
use PayPal\Exception\PayPalConnectionException; |
26
|
|
|
|
27
|
|
|
class CheckoutController extends Controller |
28
|
|
|
{ |
29
|
|
|
use ProductTransformable; |
30
|
|
|
|
31
|
|
|
private $cartRepo; |
32
|
|
|
private $courierRepo; |
33
|
|
|
private $addressRepo; |
34
|
|
|
private $customerRepo; |
35
|
|
|
private $productRepo; |
36
|
|
|
private $orderRepo; |
37
|
|
|
private $courierId; |
38
|
|
|
private $payPal; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
CartRepositoryInterface $cartRepository, |
42
|
|
|
CourierRepositoryInterface $courierRepository, |
43
|
|
|
AddressRepositoryInterface $addressRepository, |
44
|
|
|
CustomerRepositoryInterface $customerRepository, |
45
|
|
|
ProductRepositoryInterface $productRepository, |
46
|
|
|
OrderRepositoryInterface $orderRepository |
47
|
|
|
) { |
48
|
|
|
$this->cartRepo = $cartRepository; |
49
|
|
|
$this->courierRepo = $courierRepository; |
50
|
|
|
$this->addressRepo = $addressRepository; |
51
|
|
|
$this->customerRepo = $customerRepository; |
52
|
|
|
$this->productRepo = $productRepository; |
53
|
|
|
$this->orderRepo = $orderRepository; |
54
|
|
|
|
55
|
|
|
$payPalRepo = new PayPalExpressCheckoutRepository(); |
56
|
|
|
$this->payPal = $payPalRepo; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Display a listing of the resource. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Http\Response |
63
|
|
|
*/ |
64
|
|
|
public function index() |
65
|
|
|
{ |
66
|
|
|
$customer = $this->customerRepo->findCustomerById($this->loggedUser()->id); |
67
|
|
|
|
68
|
|
|
$this->courierId = request()->session()->get('courierId', 1); |
69
|
|
|
$courier = $this->courierRepo->findCourierById($this->courierId); |
70
|
|
|
|
71
|
|
|
$shippingCost = $this->cartRepo->getShippingFee($courier); |
72
|
|
|
|
73
|
|
|
$addressId = request()->session()->get('addressId', 1); |
74
|
|
|
$paymentId = request()->session()->get('paymentName', 'paypal'); |
75
|
|
|
|
76
|
|
|
// Get payees |
77
|
|
|
$paymentMethods = config('payees.name'); |
78
|
|
|
$payees = explode(',', $paymentMethods); |
79
|
|
|
|
80
|
|
|
$paymentGateways = collect($payees)->transform(function ($name) { |
81
|
|
|
return config($name); |
82
|
|
|
})->filter()->all(); |
83
|
|
|
|
84
|
|
|
$courier = $this->courierRepo->findCourierById(1); |
85
|
|
|
$shippingFee = $this->cartRepo->getShippingFee($courier); |
86
|
|
|
|
87
|
|
|
return view('front.checkout', [ |
|
|
|
|
88
|
|
|
'customer' => $customer, |
89
|
|
|
'addresses' => $customer->addresses()->get(), |
90
|
|
|
'products' => $this->cartRepo->getCartItems(), |
91
|
|
|
'subtotal' => $this->cartRepo->getSubTotal(), |
92
|
|
|
'shipping' => $shippingCost, |
93
|
|
|
'tax' => $this->cartRepo->getTax(), |
94
|
|
|
'total' => $this->cartRepo->getTotal(2, $shippingCost), |
95
|
|
|
'couriers' => $this->courierRepo->listCouriers(), |
96
|
|
|
'selectedCourier' => $this->courierId, |
97
|
|
|
'selectedAddress' => $addressId, |
98
|
|
|
'selectedPayment' => $paymentId, |
99
|
|
|
'payments' => $paymentGateways, |
100
|
|
|
'cartItems' => $this->cartRepo->getCartItemsTransformed(), |
101
|
|
|
'shippingFee' => $shippingFee |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checkout the items |
107
|
|
|
* |
108
|
|
|
* @param CartCheckoutRequest $request |
109
|
|
|
* |
110
|
|
|
* @return \Illuminate\Http\RedirectResponse |
111
|
|
|
* @codeCoverageIgnore |
112
|
|
|
* @throws \App\Shop\Customers\Exceptions\CustomerPaymentChargingErrorException |
113
|
|
|
*/ |
114
|
|
|
public function store(CartCheckoutRequest $request) |
115
|
|
|
{ |
116
|
|
|
$courier = $this->courierRepo->findCourierById($request->input('courier')); |
117
|
|
|
$shippingFee = $this->cartRepo->getShippingFee($courier); |
118
|
|
|
|
119
|
|
|
switch ($request->input('payment')) { |
120
|
|
|
case 'paypal': |
121
|
|
|
return $this->payPal->process($courier, $request); |
122
|
|
|
break; |
|
|
|
|
123
|
|
|
case 'stripe': |
124
|
|
|
|
125
|
|
|
$details = [ |
126
|
|
|
'description' => 'Stripe payment', |
127
|
|
|
'metadata' => $this->cartRepo->getCartItems()->all() |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
$customer = $this->customerRepo->findCustomerById(auth()->id()); |
131
|
|
|
$customerRepo = new CustomerRepository($customer); |
132
|
|
|
$customerRepo->charge($this->cartRepo->getTotal(2, $shippingFee), $details); |
133
|
|
|
break; |
134
|
|
|
default: |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Execute the PayPal payment |
140
|
|
|
* |
141
|
|
|
* @param PayPalCheckoutExecutionRequest $request |
142
|
|
|
* @return \Illuminate\Http\RedirectResponse |
143
|
|
|
*/ |
144
|
|
|
public function executePayPalPayment(PayPalCheckoutExecutionRequest $request) |
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
$this->payPal->execute($request); |
148
|
|
|
$this->cartRepo->clearCart(); |
149
|
|
|
|
150
|
|
|
return redirect()->route('checkout.success'); |
151
|
|
|
} catch (PayPalConnectionException $e) { |
152
|
|
|
throw new PaypalRequestError($e->getData()); |
153
|
|
|
} catch (Exception $e) { |
154
|
|
|
throw new PaypalRequestError($e->getMessage()); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param StripeExecutionRequest $request |
160
|
|
|
* @return \Stripe\Charge |
161
|
|
|
*/ |
162
|
|
|
public function charge(StripeExecutionRequest $request) |
163
|
|
|
{ |
164
|
|
|
try { |
165
|
|
|
$customer = $this->customerRepo->findCustomerById(auth()->id()); |
166
|
|
|
$stripeRepo = new StripeRepository($customer); |
167
|
|
|
|
168
|
|
|
$stripeRepo->execute( |
169
|
|
|
$request->all(), |
170
|
|
|
Cart::total(), |
171
|
|
|
Cart::tax() |
172
|
|
|
); |
173
|
|
|
return redirect()->route('checkout.success')->with('message', 'Stripe payment successful!'); |
|
|
|
|
174
|
|
|
} catch (StripeChargingErrorException $e) { |
175
|
|
|
Log::info($e->getMessage()); |
176
|
|
|
return redirect()->route('checkout.index')->with('error', 'There is a problem processing your request.'); |
|
|
|
|
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
|
|
|
|