Builder.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP 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\Kp\Model\Api\Request;
  11. use Klarna\Core\Model\Api\Exception as KlarnaApiException;
  12. use Klarna\Kp\Api\Data\AddressInterface;
  13. use Klarna\Kp\Api\Data\AttachmentInterface;
  14. use Klarna\Kp\Api\Data\CustomerInterface;
  15. use Klarna\Kp\Api\Data\OptionsInterface;
  16. use Klarna\Kp\Api\Data\OrderlineInterface;
  17. use Klarna\Kp\Api\Data\RequestInterface;
  18. use Klarna\Kp\Api\Data\UrlsInterface;
  19. use Klarna\Kp\Model\Api\RequestFactory;
  20. use Magento\Framework\DataObject;
  21. /**
  22. * Class Builder
  23. *
  24. * @package Klarna\Kp\Model\Api\Request
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. * @SuppressWarnings(PHPMD.TooManyFields)
  27. */
  28. class Builder
  29. {
  30. /**
  31. * @var string
  32. */
  33. private $merchant_reference1;
  34. /**
  35. * @var string
  36. */
  37. private $merchant_reference2;
  38. /**
  39. * @var string
  40. */
  41. private $purchase_country;
  42. /**
  43. * @var string
  44. */
  45. private $purchase_currency;
  46. /**
  47. * @var string
  48. */
  49. private $locale;
  50. /**
  51. * @var int
  52. */
  53. private $order_tax_amount = 0;
  54. /**
  55. * @var int
  56. */
  57. private $order_amount = 0;
  58. /**
  59. * @var CustomerInterface
  60. */
  61. private $customer;
  62. /**
  63. * @var AttachmentInterface
  64. */
  65. private $attachment;
  66. /**
  67. * @var AddressInterface
  68. */
  69. private $billing_address;
  70. /**
  71. * @var AddressInterface
  72. */
  73. private $shipping_address;
  74. /**
  75. * @var UrlsInterface
  76. */
  77. private $merchant_urls;
  78. /**
  79. * @var OptionsInterface
  80. */
  81. private $options;
  82. /**
  83. * @var OrderlineInterface[]
  84. */
  85. private $orderlines = [];
  86. /**
  87. * @var RequestFactory
  88. */
  89. private $requestFactory;
  90. /**
  91. * @var AddressFactory
  92. */
  93. private $addressFactory;
  94. /**
  95. * @var AttachmentFactory
  96. */
  97. private $attachmentFactory;
  98. /**
  99. * @var CustomerFactory
  100. */
  101. private $customerFactory;
  102. /**
  103. * @var MerchantUrlsFactory
  104. */
  105. private $urlFactory;
  106. /**
  107. * @var OptionsFactory
  108. */
  109. private $optionsFactory;
  110. /**
  111. * @var OrderlineFactory
  112. */
  113. private $orderlineFactory;
  114. /**
  115. * Builder constructor.
  116. *
  117. * @param RequestFactory $requestFactory
  118. * @param AddressFactory $addressFactory
  119. * @param AttachmentFactory $attachmentFactory
  120. * @param CustomerFactory $customerFactory
  121. * @param MerchantUrlFactory $merchantUrlFactory
  122. * @param OptionsFactory $optionsFactory
  123. * @param OrderlineFactory $orderlineFactory
  124. */
  125. public function __construct(
  126. RequestFactory $requestFactory,
  127. AddressFactory $addressFactory,
  128. AttachmentFactory $attachmentFactory,
  129. CustomerFactory $customerFactory,
  130. MerchantUrlsFactory $urlFactory,
  131. OptionsFactory $optionsFactory,
  132. OrderlineFactory $orderlineFactory
  133. ) {
  134. $this->requestFactory = $requestFactory;
  135. $this->addressFactory = $addressFactory;
  136. $this->attachmentFactory = $attachmentFactory;
  137. $this->customerFactory = $customerFactory;
  138. $this->urlFactory = $urlFactory;
  139. $this->optionsFactory = $optionsFactory;
  140. $this->orderlineFactory = $orderlineFactory;
  141. }
  142. /**
  143. * @param $data
  144. * @return $this
  145. */
  146. public function setAttachment($data)
  147. {
  148. $this->attachment = $this->attachmentFactory->create(['data' => $data]);
  149. return $this;
  150. }
  151. /**
  152. * @param $data
  153. * @return $this
  154. */
  155. public function setBillingAddress($data)
  156. {
  157. $this->billing_address = $this->addressFactory->create(['data' => $data]);
  158. return $this;
  159. }
  160. /**
  161. * @param $data
  162. * @return $this
  163. */
  164. public function setShippingAddress($data)
  165. {
  166. $this->shipping_address = $this->addressFactory->create(['data' => $data]);
  167. return $this;
  168. }
  169. /**
  170. * @param $data
  171. * @return $this
  172. */
  173. public function setMerchantUrls($data)
  174. {
  175. $this->merchant_urls = $this->urlFactory->create(['data' => $data]);
  176. return $this;
  177. }
  178. /**
  179. * @param $data
  180. * @return $this
  181. */
  182. public function setOptions($data)
  183. {
  184. $this->options = $this->optionsFactory->create(['data' => $data]);
  185. return $this;
  186. }
  187. /**
  188. * @param $data
  189. * @return $this
  190. */
  191. public function addOrderlines($data)
  192. {
  193. foreach ($data as $key => $orderLine) {
  194. $this->orderlines[$key] = $this->orderlineFactory->create(['data' => $orderLine]);
  195. }
  196. return $this;
  197. }
  198. /**
  199. * @param DataObject $references
  200. * @return $this
  201. */
  202. public function setMerchantReferences($references)
  203. {
  204. $this->merchant_reference1 = $references->getData('merchant_reference_1');
  205. $this->merchant_reference2 = $references->getData('merchant_reference_2');
  206. return $this;
  207. }
  208. /**
  209. * @return RequestInterface
  210. */
  211. public function getRequest()
  212. {
  213. return $this->requestFactory->create([
  214. 'data' => [
  215. 'purchase_country' => $this->purchase_country,
  216. 'purchase_currency' => $this->purchase_currency,
  217. 'locale' => $this->locale,
  218. 'customer' => $this->customer,
  219. 'options' => $this->options,
  220. 'order_amount' => $this->order_amount,
  221. 'order_tax_amount' => $this->order_tax_amount,
  222. 'order_lines' => $this->orderlines,
  223. 'urls' => $this->merchant_urls,
  224. 'attachment' => $this->attachment,
  225. 'billing_address' => $this->billing_address,
  226. 'shipping_address' => $this->shipping_address,
  227. 'merchant_reference1' => $this->merchant_reference1,
  228. 'merchant_reference2' => $this->merchant_reference2
  229. ]
  230. ]);
  231. }
  232. /**
  233. * @param $data
  234. * @return $this
  235. */
  236. public function setCustomer($data)
  237. {
  238. $this->customer = $this->customerFactory->create(['data' => $data]);
  239. return $this;
  240. }
  241. /**
  242. * @param int $amount
  243. * @return $this
  244. */
  245. public function setOrderAmount($amount)
  246. {
  247. $this->order_amount = $amount;
  248. return $this;
  249. }
  250. /**
  251. * @param int $amount
  252. * @return $this
  253. */
  254. public function setOrderTaxAmount($amount)
  255. {
  256. $this->order_tax_amount = $amount;
  257. return $this;
  258. }
  259. /**
  260. * @param array $requiredAttributes
  261. * @param string $type
  262. * @return $this
  263. * @throws KlarnaApiException
  264. */
  265. public function validate($requiredAttributes, $type)
  266. {
  267. $missingAttributes = [];
  268. foreach ($requiredAttributes as $requiredAttribute) {
  269. if (null === $this->$requiredAttribute) {
  270. $missingAttributes[] = $requiredAttribute;
  271. }
  272. if (is_array($this->$requiredAttribute) && count($this->$requiredAttribute) === 0) {
  273. $missingAttributes[] = $requiredAttribute;
  274. }
  275. }
  276. if (!empty($missingAttributes)) {
  277. throw new KlarnaApiException(
  278. __(
  279. 'Missing required attribute(s) on %1: "%2".',
  280. $type,
  281. implode(', ', $missingAttributes)
  282. )
  283. );
  284. }
  285. $total = 0;
  286. foreach ($this->orderlines as $orderLine) {
  287. $total += (int)$orderLine->getTotal();
  288. }
  289. if ($total !== $this->order_amount) {
  290. throw new KlarnaApiException(
  291. __('Order line totals do not total order_amount - %1 != %2', $total, $this->order_amount)
  292. );
  293. }
  294. return $this;
  295. }
  296. /**
  297. * @return string
  298. */
  299. public function getPurchaseCountry()
  300. {
  301. return $this->purchase_country;
  302. }
  303. /**
  304. * @param string $purchase_country
  305. * @return Builder
  306. */
  307. public function setPurchaseCountry($purchase_country)
  308. {
  309. $this->purchase_country = $purchase_country;
  310. return $this;
  311. }
  312. /**
  313. * @return string
  314. */
  315. public function getLocale()
  316. {
  317. return $this->locale;
  318. }
  319. /**
  320. * @param string $locale
  321. * @return Builder
  322. */
  323. public function setLocale($locale)
  324. {
  325. $this->locale = $locale;
  326. return $this;
  327. }
  328. /**
  329. * @return string
  330. */
  331. public function getPurchaseCurrency()
  332. {
  333. return $this->purchase_currency;
  334. }
  335. /**
  336. * @param string $purchase_currency
  337. * @return Builder
  338. */
  339. public function setPurchaseCurrency($purchase_currency)
  340. {
  341. $this->purchase_currency = $purchase_currency;
  342. return $this;
  343. }
  344. }