Ordermanagement.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * This file is part of the Klarna Order Management module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Ordermanagement\Model\Api\Rest\Service;
  11. use Klarna\Core\Api\ServiceInterface;
  12. use Klarna\Core\Helper\VersionInfo;
  13. use Magento\Framework\DataObject;
  14. /**
  15. * Class Ordermanagement
  16. *
  17. * @package Klarna\Ordermanagement\Model\Api\Rest\Service
  18. * @SuppressWarnings(PHPMD.TooManyPublicMethods)
  19. */
  20. class Ordermanagement
  21. {
  22. const API_VERSION = 'v1';
  23. /**
  24. * @var ServiceInterface
  25. */
  26. private $service;
  27. /**
  28. * Initialize class
  29. *
  30. * @param ServiceInterface $service
  31. * @param VersionInfo $versionInfo
  32. */
  33. public function __construct(
  34. ServiceInterface $service,
  35. VersionInfo $versionInfo
  36. ) {
  37. $this->service = $service;
  38. $version = sprintf(
  39. '%s;Core/%s',
  40. $versionInfo->getVersion('Klarna_Ordermanagement'),
  41. $versionInfo->getVersion('Klarna_Core')
  42. );
  43. $mageMode = $versionInfo->getMageMode();
  44. $mageVersion = $versionInfo->getMageEdition() . ' ' . $versionInfo->getMageVersion();
  45. $mageInfo = "Magento {$mageVersion} {$mageMode} mode";
  46. $this->service->setUserAgent('Magento2_OM', $version, $mageInfo);
  47. $this->service->setHeader('Accept', '*/*');
  48. }
  49. /**
  50. * Setup connection based on store config
  51. *
  52. * @param string $user
  53. * @param string $password
  54. * @param string $url
  55. */
  56. public function resetForStore($user, $password, $url)
  57. {
  58. $this->service->connect($user, $password, $url);
  59. }
  60. /**
  61. * Used by merchants to acknowledge the order.
  62. *
  63. * Merchants will receive the order confirmation push until the order has been acknowledged.
  64. *
  65. * @param $orderId
  66. *
  67. * @return array
  68. */
  69. public function acknowledgeOrder($orderId)
  70. {
  71. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/acknowledge";
  72. return $this->service->makeRequest($url, '', ServiceInterface::POST, $orderId);
  73. }
  74. /**
  75. * Get the current state of an order
  76. *
  77. * @param $orderId
  78. *
  79. * @return array
  80. */
  81. public function getOrder($orderId)
  82. {
  83. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}";
  84. return $this->service->makeRequest($url, '', ServiceInterface::GET, $orderId);
  85. }
  86. /**
  87. * Update the total order amount of an order, subject to a new customer credit check.
  88. *
  89. * The updated amount can optionally be accompanied by a descriptive text and new order lines. Supplied order lines
  90. * will replace the existing order lines. If no order lines are supplied in the call, the existing order lines will
  91. * be deleted. The updated 'order_amount' must not be negative, nor less than current 'captured_amount'. Currency
  92. * is inferred from the original order.
  93. *
  94. * @param string $orderId
  95. * @param array $data
  96. *
  97. * @return array
  98. */
  99. public function updateOrderItems($orderId, $data)
  100. {
  101. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/authorization";
  102. return $this->service->makeRequest($url, $data, ServiceInterface::PATCH, $orderId);
  103. }
  104. /**
  105. * Extend the order's authorization by default period according to merchant contract.
  106. *
  107. * @param string $orderId
  108. *
  109. * @return array
  110. */
  111. public function extendAuthorization($orderId)
  112. {
  113. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/extend-authorization-time";
  114. return $this->service->makeRequest($url, '', ServiceInterface::POST, $orderId);
  115. }
  116. /**
  117. * Update one or both merchant references. To clear a reference, set its value to "" (empty string).
  118. *
  119. * @param string $orderId
  120. * @param string $merchantReference1
  121. * @param string $merchantReference2
  122. *
  123. * @return array
  124. */
  125. public function updateMerchantReferences($orderId, $merchantReference1, $merchantReference2 = null)
  126. {
  127. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/merchant-references";
  128. $data = [
  129. 'merchant_reference1' => $merchantReference1
  130. ];
  131. if ($merchantReference2 !== null) {
  132. $data['merchant_reference2'] = $merchantReference2;
  133. }
  134. return $this->service->makeRequest($url, $data, ServiceInterface::PATCH, $orderId);
  135. }
  136. /**
  137. * Update billing and/or shipping address for an order, subject to customer credit check.
  138. * Fields can be updated independently. To clear a field, set its value to "" (empty string).
  139. *
  140. * Mandatory fields can not be cleared
  141. *
  142. * @param string $orderId
  143. * @param array $data
  144. *
  145. * @return array
  146. */
  147. public function updateAddresses($orderId, $data)
  148. {
  149. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/customer-details";
  150. return $this->service->makeRequest($url, $data, ServiceInterface::PATCH, $orderId);
  151. }
  152. /**
  153. * Add shipping info to capture
  154. *
  155. * @param string $orderId
  156. * @param string $captureId
  157. * @param array $data
  158. * @return array
  159. */
  160. public function addShippingInfo($orderId, $captureId, $data)
  161. {
  162. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/captures/{$captureId}/shipping-info";
  163. return $this->service->makeRequest($url, $data, ServiceInterface::POST, $orderId);
  164. }
  165. /**
  166. * Cancel an authorized order. For a cancellation to be successful, there must be no captures on the order.
  167. * The authorized amount will be released and no further updates to the order will be allowed.
  168. *
  169. * @param $orderId
  170. *
  171. * @return array
  172. */
  173. public function cancelOrder($orderId)
  174. {
  175. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/cancel";
  176. return $this->service->makeRequest($url, '', ServiceInterface::POST, $orderId);
  177. }
  178. /**
  179. * Capture the supplied amount. Use this call when fulfillment is completed, e.g. physical goods are being shipped
  180. * to the customer.
  181. * 'captured_amount' must be equal to or less than the order's 'remaining_authorized_amount'.
  182. * Shipping address is inherited from the order. Use PATCH method below to update the shipping address of an
  183. * individual capture. The capture amount can optionally be accompanied by a descriptive text and order lines for
  184. * the captured items.
  185. *
  186. * @param $orderId
  187. * @param $data
  188. *
  189. * @return array
  190. * @throws \Klarna\Core\Model\Api\Exception
  191. */
  192. public function captureOrder($orderId, $data)
  193. {
  194. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/captures";
  195. return $this->service->makeRequest($url, $data, ServiceInterface::POST, $orderId);
  196. }
  197. /**
  198. * Retrieve a capture
  199. *
  200. * @param $orderId
  201. * @param $captureId
  202. *
  203. * @return array
  204. */
  205. public function getCapture($orderId, $captureId)
  206. {
  207. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/captures/{$captureId}";
  208. return $this->service->makeRequest($url, '', ServiceInterface::GET, $orderId);
  209. }
  210. /**
  211. * Appends new shipping info to a capture.
  212. *
  213. * @param $orderId
  214. * @param $captureId
  215. * @param $data
  216. *
  217. * @return array
  218. */
  219. public function addShippingDetailsToCapture($orderId, $captureId, $data)
  220. {
  221. // @codingStandardsIgnoreLine
  222. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/captures/{$captureId}/shipping-info";
  223. return $this->service->makeRequest($url, $data, ServiceInterface::POST, $orderId);
  224. }
  225. /**
  226. * Update the billing address for a capture. Shipping address can not be updated.
  227. * Fields can be updated independently. To clear a field, set its value to "" (empty string).
  228. *
  229. * Mandatory fields can not be cleared,
  230. *
  231. * @param $orderId
  232. * @param $captureId
  233. * @param $data
  234. *
  235. * @return array
  236. */
  237. public function updateCaptureBillingAddress($orderId, $captureId, $data)
  238. {
  239. // @codingStandardsIgnoreLine
  240. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/captures/{$captureId}/customer-details";
  241. return $this->service->makeRequest($url, $data, ServiceInterface::PATCH, $orderId);
  242. }
  243. /**
  244. * Trigger a new send out of customer communication., typically a new invoice, for a capture.
  245. *
  246. * @param $orderId
  247. * @param $captureId
  248. *
  249. * @return array
  250. */
  251. public function resendOrderInvoice($orderId, $captureId)
  252. {
  253. // @codingStandardsIgnoreLine
  254. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/captures/{$captureId}/trigger-send-out";
  255. return $this->service->makeRequest($url, '', ServiceInterface::POST, $orderId);
  256. }
  257. /**
  258. * Refund an amount of a captured order. The refunded amount will be credited to the customer.
  259. * The refunded amount must not be higher than 'captured_amount'.
  260. * The refunded amount can optionally be accompanied by a descriptive text and order lines.
  261. *
  262. * @param $orderId
  263. * @param $data
  264. *
  265. * @return array
  266. */
  267. public function refund($orderId, $data)
  268. {
  269. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/refunds";
  270. return $this->service->makeRequest($url, $data, ServiceInterface::POST, $orderId);
  271. }
  272. /**
  273. * Signal that there is no intention to perform further captures.
  274. *
  275. * @param $orderId
  276. *
  277. * @return array
  278. */
  279. public function releaseAuthorization($orderId)
  280. {
  281. $url = "/ordermanagement/" . self::API_VERSION . "/orders/{$orderId}/release-remaining-authorization";
  282. return $this->service->makeRequest($url, '', ServiceInterface::POST, $orderId);
  283. }
  284. /**
  285. * Get resource id from Location URL
  286. *
  287. * This assumes the ID is the last url path
  288. *
  289. * @param string|array|DataObject $location
  290. *
  291. * @return string
  292. */
  293. public function getLocationResourceId($location)
  294. {
  295. if ($location instanceof DataObject) {
  296. $responseObject = $location->getResponseObject();
  297. $location = $responseObject['headers']['Location'];
  298. }
  299. if (is_array($location)) {
  300. $location = array_shift($location);
  301. }
  302. $location = rtrim($location, '/');
  303. $locationArr = explode('/', $location);
  304. return array_pop($locationArr);
  305. }
  306. }