PayPalExpressCheckoutRepository.php 4.1 KB

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