1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Shop\PaymentMethods\Paypal\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Shop\Addresses\Address; |
6
|
|
|
use App\Shop\Addresses\Repositories\AddressRepository; |
7
|
|
|
use App\Shop\Carts\Repositories\CartRepository; |
8
|
|
|
use App\Shop\Carts\Requests\PayPalCheckoutExecutionRequest; |
9
|
|
|
use App\Shop\Carts\ShoppingCart; |
10
|
|
|
use App\Shop\Checkout\CheckoutRepository; |
11
|
|
|
use App\Shop\Couriers\Courier; |
12
|
|
|
use App\Shop\PaymentMethods\Payment; |
13
|
|
|
use App\Shop\PaymentMethods\Paypal\Exceptions\PaypalRequestError; |
14
|
|
|
use App\Shop\PaymentMethods\Paypal\PaypalExpress; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use PayPal\Exception\PayPalConnectionException; |
17
|
|
|
use PayPal\Api\Payment as PayPalPayment; |
18
|
|
|
use Ramsey\Uuid\Uuid; |
19
|
|
|
|
20
|
|
|
class PayPalExpressCheckoutRepository implements PayPalExpressCheckoutRepositoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
private $payPal; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* PayPalExpressCheckoutRepository constructor. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$payment = new Payment(new PaypalExpress( |
33
|
|
|
config('paypal.client_id'), |
34
|
|
|
config('paypal.client_secret'), |
35
|
|
|
config('paypal.mode'), |
36
|
|
|
config('paypal.api_url') |
37
|
|
|
)); |
38
|
|
|
|
39
|
|
|
$this->payPal = $payment->init(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function getApiContext() |
46
|
|
|
{ |
47
|
|
|
return $this->payPal; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Courier $courier |
52
|
|
|
* @param Request $request |
53
|
|
|
* @return \Illuminate\Http\RedirectResponse |
54
|
|
|
*/ |
55
|
|
|
public function process(Courier $courier, Request $request) |
56
|
|
|
{ |
57
|
|
|
$cartRepo = new CartRepository(new ShoppingCart()); |
58
|
|
|
$items = $cartRepo->getCartItemsTransformed(); |
59
|
|
|
|
60
|
|
|
$addressRepo = new AddressRepository(new Address()); |
61
|
|
|
|
62
|
|
|
$this->payPal->setPayer(); |
63
|
|
|
$this->payPal->setItems($items); |
64
|
|
|
$this->payPal->setOtherFees( |
65
|
|
|
$cartRepo->getSubTotal(), |
66
|
|
|
$cartRepo->getTax(), |
67
|
|
|
$cartRepo->getShippingFee($courier) |
68
|
|
|
); |
69
|
|
|
$this->payPal->setAmount($cartRepo->getTotal(2, $cartRepo->getShippingFee($courier))); |
|
|
|
|
70
|
|
|
$this->payPal->setTransactions(); |
71
|
|
|
|
72
|
|
|
$billingAddress = $addressRepo->findAddressById($request->input('billing_address')); |
73
|
|
|
$this->payPal->setBillingAddress($billingAddress); |
74
|
|
|
|
75
|
|
|
if ($request->has('shipping_address')) { |
76
|
|
|
$shippingAddress = $addressRepo->findAddressById($request->input('shipping_address')); |
77
|
|
|
$this->payPal->setShippingAddress($shippingAddress); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$response = $this->payPal->createPayment( |
82
|
|
|
route('checkout.execute', $request->except('_token')), |
83
|
|
|
route('checkout.cancel') |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$redirectUrl = config('app.url'); |
87
|
|
|
if ($response) { |
88
|
|
|
$redirectUrl = $response->links[1]->href; |
89
|
|
|
} |
90
|
|
|
return redirect()->to($redirectUrl); |
91
|
|
|
} catch (PayPalConnectionException $e) { |
92
|
|
|
throw new PaypalRequestError($e->getMessage()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Request $request |
98
|
|
|
*/ |
99
|
|
|
public function execute(Request $request) |
100
|
|
|
{ |
101
|
|
|
$payment = PayPalPayment::get($request->input('paymentId'), $this->payPal->getApiContext()); |
102
|
|
|
$execution = $this->payPal->setPayerId($request->input('PayerID')); |
103
|
|
|
$trans = $payment->execute($execution, $this->payPal->getApiContext()); |
104
|
|
|
|
105
|
|
|
$cartRepo = new CartRepository(new ShoppingCart); |
106
|
|
|
$transactions = $trans->getTransactions(); |
107
|
|
|
|
108
|
|
|
foreach ($transactions as $transaction) { |
109
|
|
|
$checkoutRepo = new CheckoutRepository; |
110
|
|
|
$checkoutRepo->buildCheckoutItems([ |
111
|
|
|
'reference' => Uuid::uuid4()->toString(), |
112
|
|
|
'courier_id' => $request->input('courier'), |
113
|
|
|
'customer_id' => auth()->user()->id, |
114
|
|
|
'address_id' => $request->input('billing_address'), |
115
|
|
|
'order_status_id' => 1, |
116
|
|
|
'payment' => $request->input('payment'), |
117
|
|
|
'discounts' => 0, |
118
|
|
|
'total_products' => $cartRepo->getSubTotal(), |
119
|
|
|
'total' => $cartRepo->getTotal(), |
120
|
|
|
'total_paid' => $transaction->getAmount()->getTotal(), |
121
|
|
|
'tax' => $cartRepo->getTax() |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$cartRepo->clearCart(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|