ApiClient.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model;
  7. use Magento\Sales\Api\Data\OrderInterface;
  8. use Magento\Store\Model\ScopeInterface;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Psr\Log\LoggerInterface;
  11. use Vertex\Tax\Api\ClientInterface;
  12. use Vertex\Tax\Model\ApiClient\ObjectConverter;
  13. use Vertex\Utility\SoapClientFactory;
  14. /**
  15. * {@inheritdoc}
  16. * @deprecated
  17. */
  18. class ApiClient implements ClientInterface
  19. {
  20. const CONNECTION_TIMEOUT = 12; // seconds
  21. /** @var Config */
  22. private $config;
  23. /** @var ExceptionLogger */
  24. private $logger;
  25. /** @var ObjectConverter */
  26. private $objectConverter;
  27. /** @var RequestLogger */
  28. private $requestLogger;
  29. /** @var SoapClientFactory */
  30. private $soapClientFactory;
  31. /** @var StoreManagerInterface */
  32. private $storeManager;
  33. /**
  34. * @param ExceptionLogger $logger
  35. * @param Config $config
  36. * @param StoreManagerInterface $storeManager
  37. * @param SoapClientFactory $soapClientFactory
  38. * @param RequestLogger $requestLogger
  39. * @param ObjectConverter $objectConverter
  40. */
  41. public function __construct(
  42. ExceptionLogger $logger,
  43. Config $config,
  44. StoreManagerInterface $storeManager,
  45. SoapClientFactory $soapClientFactory,
  46. RequestLogger $requestLogger,
  47. ObjectConverter $objectConverter
  48. ) {
  49. $this->logger = $logger;
  50. $this->config = $config;
  51. $this->storeManager = $storeManager;
  52. $this->soapClientFactory = $soapClientFactory;
  53. $this->requestLogger = $requestLogger;
  54. $this->objectConverter = $objectConverter;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. * @deprecated
  59. */
  60. public function sendApiRequest(array $request, $type, OrderInterface $order = null)
  61. {
  62. $scopeType = ScopeInterface::SCOPE_STORE;
  63. $scopeCode = $this->getStoreId($order);
  64. $apiUrl = $this->config->getVertexHost($scopeCode, $scopeType);
  65. if ($type === 'tax_area_lookup') {
  66. $apiUrl = $this->config->getVertexAddressHost($scopeCode, $scopeType);
  67. }
  68. $apiUrl = $this->getWsdlUrl($apiUrl);
  69. $client = null;
  70. try {
  71. $client = $this->createSoapClient($apiUrl);
  72. $taxResponse = $this->performSoapCall($client, $type, $request);
  73. $taxResponseArray = $this->objectConverter->convertToArray($taxResponse);
  74. } catch (\Exception $e) {
  75. $this->logException($e, $type, $client);
  76. return false;
  77. }
  78. $this->logRequest(
  79. $type,
  80. $client->__getLastRequest(),
  81. $client->__getLastResponse()
  82. );
  83. return $taxResponseArray;
  84. }
  85. /**
  86. * Create a SOAP client for use with the API
  87. *
  88. * Enforces TLSv1.2 with SHA2 on a SOAP 1.1 Call
  89. *
  90. * @param $apiUrl
  91. * @return \SoapClient
  92. */
  93. private function createSoapClient($apiUrl)
  94. {
  95. $soapParams = [
  96. 'trace' => true,
  97. 'soap_version' => SOAP_1_1
  98. ];
  99. $context = [
  100. 'ssl_method' => SOAP_SSL_METHOD_TLS,
  101. 'connection_timeout' => static::CONNECTION_TIMEOUT,
  102. 'stream_context' => $this->createStreamContext(),
  103. ];
  104. $soapParams['stream_context'] = $context; // for TLS 1.2
  105. return $this->soapClientFactory->create($apiUrl, $soapParams);
  106. }
  107. /**
  108. * Create a communication context for the client.
  109. *
  110. * Returns context to properly negotiate on TLS 1.2.
  111. *
  112. * @return resource
  113. */
  114. private function createStreamContext()
  115. {
  116. return stream_context_create(
  117. [
  118. 'ssl' => [
  119. 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
  120. 'ciphers' => 'SHA2',
  121. ]
  122. ]
  123. );
  124. }
  125. /**
  126. * Retrieve the Store ID relevant to the request
  127. *
  128. * @param OrderInterface|null $order
  129. * @return int
  130. */
  131. private function getStoreId(OrderInterface $order = null)
  132. {
  133. return $order !== null ? $order->getStoreId() : $this->storeManager->getStore()->getId();
  134. }
  135. /**
  136. * Retrieve the total tax calculated from a response
  137. *
  138. * @param array $taxResponseArray
  139. * @return float
  140. */
  141. private function getTotalTax($taxResponseArray)
  142. {
  143. $totalTax = 0;
  144. if (isset($taxResponseArray['TotalTax'])) {
  145. $totalTax = $taxResponseArray['TotalTax'];
  146. }
  147. return $totalTax;
  148. }
  149. /**
  150. * Get the WSDL version of an API URL
  151. *
  152. * @param string $apiUrl
  153. * @return string
  154. */
  155. private function getWsdlUrl($apiUrl)
  156. {
  157. if (stripos($apiUrl, 'wsdl') === false) {
  158. $apiUrl .= '?wsdl';
  159. }
  160. return $apiUrl;
  161. }
  162. /**
  163. * Log an exception that occurred during an API request
  164. *
  165. * @param \Throwable $exception
  166. * @param string $type Request Type
  167. * @param \SoapClient|null $client
  168. */
  169. private function logException($exception, $type, \SoapClient $client = null)
  170. {
  171. $this->logger->critical($exception);
  172. $this->logRequest(
  173. $type,
  174. $client !== null ? $client->__getLastRequest() : '',
  175. $client !== null ? $client->__getLastResponse() : ''
  176. );
  177. }
  178. /**
  179. * Log an API Request
  180. *
  181. * @param string $type
  182. * @param string $requestXml
  183. * @param string $responseXml
  184. */
  185. private function logRequest($type, $requestXml, $responseXml)
  186. {
  187. try {
  188. $this->requestLogger->log(
  189. $type,
  190. $requestXml,
  191. $responseXml
  192. );
  193. } catch (\Exception $originalException) {
  194. // Logging Exception
  195. $exception = new \Exception('Failed to log Vertex Request', 0, $originalException);
  196. $this->logger->critical($exception);
  197. }
  198. }
  199. /**
  200. * Perform a SOAP Call given a client and request data and type
  201. *
  202. * Uses type to determine which function to call against SOAP
  203. *
  204. * @param \SoapClient $client
  205. * @param string $type
  206. * @param array $request
  207. * @param string $scopeType
  208. * @param string $scopeCode
  209. * @return mixed
  210. * @throws Exception
  211. * @throws \SoapFault
  212. */
  213. private function performSoapCall(
  214. \SoapClient $client,
  215. $type,
  216. $request
  217. ) {
  218. if ($type === 'tax_area_lookup') {
  219. $taxResponse = $client->LookupTaxAreas60($request);
  220. } elseif (in_array($type, ['quote', 'invoice', 'invoice_refund'])) {
  221. $taxResponse = $client->CalculateTax60($request);
  222. } else {
  223. throw new \Exception('Invalid request type');
  224. }
  225. if ($taxResponse instanceof \SoapFault) {
  226. throw $taxResponse;
  227. }
  228. return $taxResponse;
  229. }
  230. }