PaypalExpress.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Shop\PaymentMethods\Paypal;
  3. use App\Shop\Addresses\Address;
  4. use App\Shop\Carts\ShoppingCart;
  5. use App\Shop\PaymentMethods\Paypal\Exceptions\PaypalRequestError;
  6. use Illuminate\Support\Collection;
  7. use PayPal\Api\Amount;
  8. use PayPal\Api\Details;
  9. use PayPal\Api\InvoiceAddress;
  10. use PayPal\Api\Item;
  11. use PayPal\Api\ItemList;
  12. use PayPal\Api\Payer;
  13. use PayPal\Api\Payment;
  14. use PayPal\Api\PaymentExecution;
  15. use PayPal\Api\RedirectUrls;
  16. use PayPal\Api\ShippingAddress;
  17. use PayPal\Api\Transaction;
  18. use PayPal\Exception\PayPalConnectionException;
  19. use PayPal\Auth\OAuthTokenCredential;
  20. use PayPal\Rest\ApiContext;
  21. /**
  22. * Class PaypalExpress
  23. * @package App\Shop\PaymentMethods\Paypal
  24. * @codeCoverageIgnore
  25. *
  26. * @todo Make a test for this
  27. */
  28. class PaypalExpress
  29. {
  30. private $apiContext;
  31. private $payer;
  32. private $amount;
  33. private $transactions = [];
  34. private $itemList;
  35. private $others;
  36. public function __construct($clientId, $clientSecret, $mode)
  37. {
  38. $apiContext = new ApiContext(
  39. new OAuthTokenCredential($clientId, $clientSecret)
  40. );
  41. $apiContext->setConfig(
  42. array(
  43. 'mode' => $mode,
  44. 'log.LogEnabled' => env('APP_DEBUG'),
  45. 'log.FileName' => storage_path('logs/paypal.log'),
  46. 'log.LogLevel' => env('APP_LOG_LEVEL'),
  47. 'cache.enabled' => true,
  48. 'cache.FileName' => storage_path('logs/paypal.cache'),
  49. 'http.CURLOPT_SSLVERSION' => CURL_SSLVERSION_TLSv1
  50. )
  51. );
  52. $this->apiContext = $apiContext;
  53. }
  54. /**
  55. * Returns the Paypal API Context
  56. *
  57. * @return ApiContext
  58. */
  59. public function getApiContext()
  60. {
  61. return $this->apiContext;
  62. }
  63. public function setPayer()
  64. {
  65. $payer = new Payer();
  66. $payer->setPaymentMethod('paypal');
  67. $this->payer = $payer;
  68. }
  69. /**
  70. * @param Collection $products
  71. */
  72. public function setItems(Collection $products)
  73. {
  74. $items = [];
  75. foreach ($products as $product) {
  76. $item = new Item();
  77. $item->setName($product->name)
  78. ->setDescription($product->description)
  79. ->setQuantity($product->qty)
  80. ->setCurrency(ShoppingCart::$defaultCurrency)
  81. ->setPrice($product->price);
  82. $items[] = $item;
  83. }
  84. $itemList = new ItemList();
  85. $itemList->setItems($items);
  86. $this->itemList = $itemList;
  87. }
  88. /**
  89. * @param $subtotal
  90. * @param int $tax
  91. * @param $shipping
  92. */
  93. public function setOtherFees($subtotal, $tax = 0, $shipping)
  94. {
  95. $details = new Details();
  96. $details->setTax($tax)
  97. ->setSubtotal($subtotal)
  98. ->setShipping($shipping);
  99. $this->others = $details;
  100. }
  101. /**
  102. * @param $amt
  103. */
  104. public function setAmount($amt)
  105. {
  106. $amount = new Amount();
  107. $amount->setCurrency(ShoppingCart::$defaultCurrency)
  108. ->setTotal($amt)
  109. ->setDetails($this->others);
  110. $this->amount = $amount;
  111. }
  112. public function setTransactions()
  113. {
  114. $transaction = new Transaction();
  115. $transaction->setAmount($this->amount)
  116. ->setItemList($this->itemList)
  117. ->setDescription('Payment via Paypal')
  118. ->setInvoiceNumber(uniqid());
  119. $this->transactions = $transaction;
  120. }
  121. /**
  122. * @param string $returnUrl
  123. * @param string $cancelUrl
  124. *
  125. * @return Payment
  126. */
  127. public function createPayment(string $returnUrl, string $cancelUrl)
  128. {
  129. $payment = new Payment();
  130. $payment->setIntent('sale')
  131. ->setPayer($this->payer)
  132. ->setTransactions([$this->transactions]);
  133. $redirectUrls = new RedirectUrls();
  134. $redirectUrls
  135. ->setReturnUrl($returnUrl)
  136. ->setCancelUrl($cancelUrl);
  137. $payment->setRedirectUrls($redirectUrls);
  138. try {
  139. return $payment->create($this->apiContext);
  140. } catch (PayPalConnectionException $e) {
  141. throw new PaypalRequestError($e->getData());
  142. }
  143. }
  144. /**
  145. * @param string $payerID
  146. * @return PaymentExecution
  147. */
  148. public function setPayerId(string $payerID)
  149. {
  150. $execution = new PaymentExecution();
  151. $execution->setPayerId($payerID);
  152. return $execution;
  153. }
  154. /**
  155. * @param Address $address
  156. * @return InvoiceAddress
  157. */
  158. public function setBillingAddress(Address $address)
  159. {
  160. $billingAddress = new InvoiceAddress();
  161. $billingAddress->line1 = $address->address_1;
  162. $billingAddress->line2 = $address->address_2;
  163. $billingAddress->city = $address->city;
  164. $billingAddress->state = $address->state_code;
  165. $billingAddress->postal_code = $address->zip;
  166. $billingAddress->country_code = $address->country->iso;
  167. return $billingAddress;
  168. }
  169. /**
  170. * @param Address $address
  171. * @return ShippingAddress
  172. */
  173. public function setShippingAddress(Address $address)
  174. {
  175. $shipping = new ShippingAddress();
  176. $shipping->line1 = $address->address_1;
  177. $shipping->line2 = $address->address_2;
  178. $shipping->city = $address->city;
  179. $shipping->state = $address->state_code;
  180. $shipping->postal_code = $address->zip;
  181. $shipping->country_code = $address->country->iso;
  182. return $shipping;
  183. }
  184. }