Request.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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;
  11. use Klarna\Kp\Api\Data\AddressInterface;
  12. use Klarna\Kp\Api\Data\AttachmentInterface;
  13. use Klarna\Kp\Api\Data\CustomerInterface;
  14. use Klarna\Kp\Api\Data\OptionsInterface;
  15. use Klarna\Kp\Api\Data\OrderlineInterface;
  16. use Klarna\Kp\Api\Data\RequestInterface;
  17. use Klarna\Kp\Api\Data\UrlsInterface;
  18. /**
  19. * Class Request
  20. *
  21. * @package Klarna\Kp\Model\Api
  22. * @SuppressWarnings(PHPMD.TooManyFields)
  23. */
  24. class Request implements RequestInterface
  25. {
  26. /**
  27. * @var string
  28. */
  29. private $merchant_reference1;
  30. /**
  31. * @var string
  32. */
  33. private $merchant_reference2;
  34. /**
  35. * @var OptionsInterface
  36. */
  37. private $options;
  38. /**
  39. * @var CustomerInterface
  40. */
  41. private $customer;
  42. /**
  43. * @var UrlsInterface
  44. */
  45. private $urls;
  46. /**
  47. * @var string
  48. */
  49. private $purchase_country;
  50. /**
  51. * @var string
  52. */
  53. private $purchase_currency;
  54. /**
  55. * @var string
  56. */
  57. private $locale;
  58. /**
  59. * @var AddressInterface
  60. */
  61. private $billing_address;
  62. /**
  63. * @var AddressInterface
  64. */
  65. private $shipping_address;
  66. /**
  67. * @var int
  68. */
  69. private $order_tax_amount;
  70. /**
  71. * @var OrderlineInterface[]
  72. */
  73. private $order_lines;
  74. /**
  75. * @var AttachmentInterface
  76. */
  77. private $attachment;
  78. /**
  79. * @var int
  80. */
  81. private $order_amount;
  82. /**
  83. * @var string
  84. */
  85. private $design;
  86. /**
  87. * @var array
  88. */
  89. private $custom_payment_methods;
  90. /**
  91. * Request constructor.
  92. *
  93. * @param array $data
  94. */
  95. public function __construct($data = [])
  96. {
  97. foreach ($data as $key => $value) {
  98. if (property_exists($this, $key)) {
  99. $this->$key = $value;
  100. }
  101. }
  102. }
  103. /**
  104. * Used for storing merchant's internal order number or other reference (max 255 characters).
  105. *
  106. * @param string $reference
  107. */
  108. public function setMerchantReference1($reference)
  109. {
  110. $this->merchant_reference1 = $reference;
  111. }
  112. /**
  113. * Used for storing merchant's internal order number or other reference (max 255 characters).
  114. *
  115. * @param string $reference
  116. */
  117. public function setMerchantReference2($reference)
  118. {
  119. $this->merchant_reference2 = $reference;
  120. }
  121. /**
  122. * @param OptionsInterface $options
  123. */
  124. public function setOptions(OptionsInterface $options)
  125. {
  126. $this->options = $options;
  127. }
  128. /**
  129. * Information about the liable customer of the order.
  130. *
  131. * @param CustomerInterface $customer
  132. */
  133. public function setCustomer(CustomerInterface $customer)
  134. {
  135. $this->customer = $customer;
  136. }
  137. /**
  138. * @param UrlsInterface $urls
  139. */
  140. public function setMerchantUrls(UrlsInterface $urls)
  141. {
  142. $this->urls = $urls;
  143. }
  144. /**
  145. * ISO 3166 alpha-2 => Purchase country
  146. *
  147. * @param string $purchaseCountry
  148. */
  149. public function setPurchaseCountry($purchaseCountry)
  150. {
  151. $this->purchase_country = $purchaseCountry;
  152. }
  153. /**
  154. * ISO 4217 => Purchase currency.
  155. *
  156. * @param string $purchaseCurrency
  157. */
  158. public function setPurchaseCurrency($purchaseCurrency)
  159. {
  160. $this->purchase_currency = $purchaseCurrency;
  161. }
  162. /**
  163. * RFC 1766 => Customer's locale.
  164. *
  165. * @param string $locale
  166. */
  167. public function setLocale($locale)
  168. {
  169. $this->locale = $locale;
  170. }
  171. /**
  172. * The billing address.
  173. *
  174. * @param AddressInterface $billingAddress
  175. */
  176. public function setBillingAddress(AddressInterface $billingAddress)
  177. {
  178. $this->billing_address = $billingAddress;
  179. }
  180. /**
  181. * The shipping address.
  182. *
  183. * @param AddressInterface $shippingAddress
  184. */
  185. public function setShippingAddress(AddressInterface $shippingAddress)
  186. {
  187. $this->shipping_address = $shippingAddress;
  188. }
  189. /**
  190. * The total tax amount of the order. Implicit decimal (eg 1000 instead of 10.00)
  191. *
  192. * @param int $orderTaxAmount
  193. */
  194. public function setOrderTaxAmount($orderTaxAmount)
  195. {
  196. $this->order_tax_amount = $orderTaxAmount;
  197. }
  198. /**
  199. * The applicable order lines.
  200. *
  201. * @param array|OrderlineInterface[] $orderlines
  202. */
  203. public function setOrderLines(array $orderlines)
  204. {
  205. $this->order_lines = $orderlines;
  206. }
  207. /**
  208. * Add an orderline to the request.
  209. *
  210. * @param OrderlineInterface $orderline
  211. */
  212. public function addOrderLine(OrderlineInterface $orderline)
  213. {
  214. $this->order_lines[] = $orderline;
  215. }
  216. /**
  217. * Container for optional merchant specific data.
  218. *
  219. * @param AttachmentInterface $attachment
  220. */
  221. public function setAttachment(AttachmentInterface $attachment)
  222. {
  223. $this->attachment = $attachment;
  224. }
  225. /**
  226. * Total amount of the order, including tax and any discounts. Implicit decimal (eg 1000 instead of 10.00)
  227. *
  228. * @param int $orderAmount
  229. */
  230. public function setOrderAmount($orderAmount)
  231. {
  232. $this->order_amount = $orderAmount;
  233. }
  234. /**
  235. * Used to load a specific design for the credit form
  236. *
  237. * @param string $design
  238. */
  239. public function setDesign($design)
  240. {
  241. $this->design = $design;
  242. }
  243. /**
  244. * An optional list of merchant triggered payment methods
  245. *
  246. * @param array $paymentMethods
  247. */
  248. public function setCustomPaymentMethods(array $paymentMethods)
  249. {
  250. $this->custom_payment_methods = $paymentMethods;
  251. }
  252. /**
  253. * Add a merchant triggered payment method to request
  254. *
  255. * @param string $paymentMethod
  256. */
  257. public function addCustomPaymentMethods($paymentMethod)
  258. {
  259. $this->custom_payment_methods[] = $paymentMethod;
  260. }
  261. /**
  262. * Generate array object needed for API call
  263. *
  264. * @return array
  265. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  266. */
  267. public function toArray()
  268. {
  269. $request = [
  270. 'purchase_country' => $this->purchase_country,
  271. 'purchase_currency' => $this->purchase_currency,
  272. 'locale' => $this->locale,
  273. 'order_amount' => $this->order_amount,
  274. 'order_tax_amount' => $this->order_tax_amount,
  275. 'order_lines' => null,
  276. 'billing_address' => null,
  277. 'shipping_address' => null,
  278. 'customer' => null,
  279. 'merchant_urls' => null,
  280. 'merchant_reference1' => $this->merchant_reference1,
  281. 'merchant_reference2' => $this->merchant_reference2,
  282. 'design' => $this->design,
  283. 'options' => null,
  284. 'custom_payment_methods' => $this->custom_payment_methods,
  285. 'attachment' => null
  286. ];
  287. if (null !== $this->billing_address) {
  288. $request['billing_address'] = $this->billing_address->toArray();
  289. }
  290. if (null !== $this->shipping_address) {
  291. $request['shipping_address'] = $this->shipping_address->toArray();
  292. }
  293. if (null !== $this->customer) {
  294. $request['customer'] = $this->customer->toArray();
  295. }
  296. if (null !== $this->urls) {
  297. $request['merchant_urls'] = $this->urls->toArray();
  298. }
  299. if (null !== $this->attachment) {
  300. $request['attachment'] = $this->attachment->toArray();
  301. }
  302. if (null !== $this->order_lines) {
  303. $request['order_lines'] = [];
  304. foreach ($this->order_lines as $line) {
  305. $request['order_lines'][] = $line->toArray();
  306. }
  307. }
  308. if (null !== $this->options) {
  309. $request['options'] = $this->options->toArray();
  310. }
  311. return array_filter($request, function ($value) {
  312. if ($value === null) {
  313. return false;
  314. }
  315. if (is_array($value) && count($value) === 0) {
  316. return false;
  317. }
  318. return true;
  319. });
  320. }
  321. }