Request.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Hostedpro;
  7. use Magento\Customer\Helper\Address;
  8. use Magento\Framework\DataObject;
  9. use Magento\Framework\Locale\Resolver;
  10. use Magento\Payment\Helper\Formatter;
  11. use Magento\Paypal\Model\Hostedpro;
  12. use Magento\Sales\Model\Order;
  13. use Magento\Tax\Helper\Data;
  14. /**
  15. * Website Payments Pro Hosted Solution request model to get token.
  16. *
  17. * @author Magento Core Team <core@magentocommerce.com>
  18. */
  19. class Request extends DataObject
  20. {
  21. use Formatter;
  22. /**
  23. * Request's order model
  24. *
  25. * @var \Magento\Sales\Model\Order
  26. */
  27. protected $order;
  28. /**
  29. * Request's Hosted Pro payment method model
  30. *
  31. * @var \Magento\Paypal\Model\Hostedpro
  32. */
  33. protected $paymentMethod;
  34. /**
  35. * Name format for button variables
  36. *
  37. * @var string
  38. */
  39. protected $buttonVarFormat = 'L_BUTTONVAR%d';
  40. /**
  41. * Request Parameters which dont have to wrap as button vars
  42. *
  43. * @var string[]
  44. */
  45. protected $notButtonVars = ['METHOD', 'BUTTONCODE', 'BUTTONTYPE'];
  46. /**
  47. * Customer address
  48. *
  49. * @var \Magento\Customer\Helper\Address
  50. */
  51. protected $customerAddress = null;
  52. /**
  53. * Tax data
  54. *
  55. * @var \Magento\Tax\Helper\Data
  56. */
  57. protected $taxData;
  58. /**
  59. * Locale Resolver
  60. *
  61. * @var \Magento\Framework\Locale\Resolver
  62. */
  63. protected $localeResolver;
  64. /**
  65. * @param \Magento\Framework\Locale\Resolver $localeResolver
  66. * @param \Magento\Customer\Helper\Address $customerAddress
  67. * @param \Magento\Tax\Helper\Data $taxData
  68. * @param array $data
  69. */
  70. public function __construct(
  71. Resolver $localeResolver,
  72. Address $customerAddress,
  73. Data $taxData,
  74. array $data = []
  75. ) {
  76. $this->customerAddress = $customerAddress;
  77. $this->localeResolver = $localeResolver;
  78. $this->taxData = $taxData;
  79. parent::__construct($data);
  80. }
  81. /**
  82. * Build and return request array from object data
  83. *
  84. * @return array
  85. */
  86. public function getRequestData()
  87. {
  88. $requestData = [];
  89. if (!empty($this->_data)) {
  90. // insert params to request as additional button variables,
  91. // except special params from _notButtonVars list
  92. $i = 0;
  93. foreach ($this->_data as $key => $value) {
  94. if (in_array($key, $this->notButtonVars)) {
  95. $requestData[$key] = $value;
  96. } else {
  97. $varKey = sprintf($this->buttonVarFormat, $i);
  98. $requestData[$varKey] = $key . '=' . $value;
  99. $i++;
  100. }
  101. }
  102. }
  103. return $requestData;
  104. }
  105. /**
  106. * Append payment data to request
  107. *
  108. * @param \Magento\Paypal\Model\Hostedpro $paymentMethod
  109. * @return $this
  110. */
  111. public function setPaymentMethod($paymentMethod)
  112. {
  113. $this->paymentMethod = $paymentMethod;
  114. $requestData = $this->getPaymentData($paymentMethod);
  115. $this->addData($requestData);
  116. return $this;
  117. }
  118. /**
  119. * Append order data to request
  120. *
  121. * @param \Magento\Sales\Model\Order $order
  122. * @return $this
  123. */
  124. public function setOrder(Order $order)
  125. {
  126. $this->order = $order;
  127. $requestData = $this->getOrderData($order);
  128. $this->addData($requestData);
  129. return $this;
  130. }
  131. /**
  132. * Add amount data to request
  133. *
  134. * @access public
  135. * @param \Magento\Sales\Model\Order $order
  136. * @return $this
  137. */
  138. public function setAmount(Order $order)
  139. {
  140. $this->addData($this->getAmountData($order));
  141. return $this;
  142. }
  143. /**
  144. * Calculate amount for order
  145. * @param \Magento\Sales\Model\Order $order
  146. * @return array
  147. * @throws \Exception
  148. */
  149. protected function getAmountData(Order $order)
  150. {
  151. // if tax is included - need add to request only total amount
  152. if ($this->taxData->getConfig()->priceIncludesTax()) {
  153. return $this->getTaxableAmount($order);
  154. } else {
  155. return $this->getNonTaxableAmount($order);
  156. }
  157. }
  158. /**
  159. * Get payment amount data with excluded tax
  160. * @param \Magento\Sales\Model\Order $order
  161. * @return array
  162. */
  163. private function getNonTaxableAmount(Order $order)
  164. {
  165. // PayPal denied transaction with 0 amount
  166. $subtotal = $order->getBaseSubtotal() ? : $order->getPayment()->getBaseAmountAuthorized();
  167. return [
  168. 'subtotal' => $this->formatPrice($subtotal),
  169. 'total' => $this->formatPrice($order->getPayment()->getBaseAmountAuthorized()),
  170. 'tax' => $this->formatPrice($order->getBaseTaxAmount()),
  171. 'shipping' => $this->formatPrice($order->getBaseShippingAmount()),
  172. 'discount' => $this->formatPrice(abs($order->getBaseDiscountAmount()))
  173. ];
  174. }
  175. /**
  176. * Get order amount data with included tax
  177. * @param \Magento\Sales\Model\Order $order
  178. * @return array
  179. */
  180. private function getTaxableAmount(Order $order)
  181. {
  182. $amount = $this->formatPrice($order->getPayment()->getBaseAmountAuthorized());
  183. return [
  184. 'amount' => $amount,
  185. 'subtotal' => $amount // subtotal always is required
  186. ];
  187. }
  188. /**
  189. * Get payment request data as array
  190. *
  191. * @param \Magento\Paypal\Model\Hostedpro $paymentMethod
  192. * @return array
  193. */
  194. protected function getPaymentData(Hostedpro $paymentMethod)
  195. {
  196. $request = [
  197. 'paymentaction' => strtolower($paymentMethod->getConfigData('payment_action')),
  198. 'notify_url' => $paymentMethod->getNotifyUrl(),
  199. 'cancel_return' => $paymentMethod->getCancelUrl(),
  200. 'return' => $paymentMethod->getReturnUrl(),
  201. 'lc' => \Locale::getRegion($this->localeResolver->getLocale()),
  202. 'template' => 'mobile-iframe',
  203. 'showBillingAddress' => 'false',
  204. 'showShippingAddress' => 'true',
  205. 'showBillingEmail' => 'false',
  206. 'showBillingPhone' => 'false',
  207. 'showCustomerName' => 'false',
  208. 'showCardInfo' => 'true',
  209. 'showHostedThankyouPage' => 'false',
  210. ];
  211. return $request;
  212. }
  213. /**
  214. * Get order request data as array
  215. *
  216. * @param \Magento\Sales\Model\Order $order
  217. * @return array
  218. */
  219. protected function getOrderData(Order $order)
  220. {
  221. $request = [
  222. 'invoice' => $order->getIncrementId(),
  223. 'address_override' => 'true',
  224. 'currency_code' => $order->getBaseCurrencyCode(),
  225. 'buyer_email' => $order->getCustomerEmail(),
  226. ];
  227. // append to request billing address data
  228. if ($billingAddress = $order->getBillingAddress()) {
  229. $request = array_merge($request, $this->getAddress($billingAddress, 'billing'));
  230. }
  231. // append to request shipping address data
  232. if ($shippingAddress = $order->getShippingAddress()) {
  233. $request = array_merge($request, $this->getAddress($shippingAddress));
  234. }
  235. return $request;
  236. }
  237. /**
  238. * Export address data to request
  239. *
  240. * @param DataObject $address
  241. * @param string $type
  242. * @return array
  243. */
  244. protected function getAddress(DataObject $address, $type = '')
  245. {
  246. $type = !empty($type) ? $type . '_' : '';
  247. $request = [
  248. $type . 'first_name' => $address->getFirstname(),
  249. $type . 'last_name' => $address->getLastname(),
  250. $type . 'city' => $address->getCity(),
  251. $type . 'state' => $this->getRegion($address),
  252. $type . 'zip' => $address->getPostcode(),
  253. $type . 'country' => $address->getCountryId(),
  254. ];
  255. $streets = $this->getAddressStreets($address);
  256. $request[$type . 'address1'] = $streets[0];
  257. $request[$type . 'address2'] = $streets[1];
  258. return $request;
  259. }
  260. /**
  261. * Export region code from address data
  262. *
  263. * @param DataObject $address
  264. * @return string
  265. */
  266. protected function getRegion(DataObject $address)
  267. {
  268. // get region code, otherwise - region, otherwise - city
  269. return $address->getRegionCode() ?: ($address->getRegion() ?: $address->getCity());
  270. }
  271. /**
  272. * Export streets from address data
  273. *
  274. * @param DataObject $address
  275. * @return array
  276. */
  277. protected function getAddressStreets(DataObject $address)
  278. {
  279. $street1 = '';
  280. $street2 = '';
  281. $data = $this->customerAddress->convertStreetLines($address->getStreet(), 2);
  282. if (!empty($data[0])) {
  283. $street1 = $data[0];
  284. }
  285. if (!empty($data[1])) {
  286. $street2 = $data[1];
  287. }
  288. return [$street1, $street2];
  289. }
  290. }