/src/Resource/Orders.php

https://gitlab.com/fabiorf/moip-sdk-php · PHP · 507 lines · 219 code · 67 blank · 221 comment · 2 complexity · 5121fb7cbeb91d9354a9fed8ae57a5ff MD5 · raw file

  1. <?php
  2. namespace Moip\Resource;
  3. use ArrayIterator;
  4. use stdClass;
  5. class Orders extends MoipResource
  6. {
  7. /**
  8. * @const strign
  9. */
  10. const PATH = 'orders';
  11. /**
  12. * Defines what kind of payee as pripmary.
  13. *
  14. * @const strign
  15. */
  16. const RECEIVER_TYPE_PRIMARY = 'PRIMARY';
  17. /**
  18. * Defines what kind of payee as secundary.
  19. *
  20. * @const strign
  21. */
  22. const RECEIVER_TYPE_SECONDARY = 'SECONDARY';
  23. /**
  24. * Currency used in the application.
  25. *
  26. * @const strign
  27. */
  28. const AMOUNT_CURRENCY = 'BRL';
  29. /**
  30. * @var \Moip\Resource\Orders
  31. **/
  32. private $orders;
  33. /**
  34. * Adds a new item to order.
  35. *
  36. * @param string $product Name of the product.
  37. * @param int $quantity Product Quantity.
  38. * @param string $detail Additional product description.
  39. * @param intefer $price Initial value of the item.
  40. *
  41. * @return $this
  42. */
  43. public function addItem($product, $quantity, $detail, $price)
  44. {
  45. $item = new stdClass();
  46. $item->product = $product;
  47. $item->quantity = $quantity;
  48. $item->detail = $detail;
  49. $item->price = $price;
  50. $this->data->items[] = $item;
  51. return $this;
  52. }
  53. /**
  54. * Adds a new receiver to order.
  55. *
  56. * @param string $moipAccount Id MoIP MoIP account that will receive payment values.
  57. * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY.
  58. *
  59. * @return $this
  60. */
  61. public function addReceiver($moipAccount, $type = self::RECEIVER_TYPE_PRIMARY)
  62. {
  63. $receiver = new stdClass();
  64. $receiver->moipAccount = new stdClass();
  65. $receiver->moipAccount->id = $moipAccount;
  66. $receiver->type = $type;
  67. $this->data->receivers[] = $receiver;
  68. return $this;
  69. }
  70. /**
  71. * Initialize necessary used in some functions.
  72. */
  73. protected function initialize()
  74. {
  75. $this->data = new stdClass();
  76. $this->data->ownId = null;
  77. $this->data->amount = new stdClass();
  78. $this->data->amount->currency = self::AMOUNT_CURRENCY;
  79. $this->data->items = [];
  80. $this->data->receivers = [];
  81. }
  82. /**
  83. * Initialize necessary used in some functions.
  84. */
  85. private function initializeSubtotals()
  86. {
  87. if (!isset($this->data->subtotals)) {
  88. $this->data->subtotals = new stdClass();
  89. }
  90. }
  91. /**
  92. * Mount the structure of order.
  93. *
  94. * @param \stdClass $response
  95. *
  96. * @return Orders Response order.
  97. */
  98. protected function populate(stdClass $response)
  99. {
  100. $this->orders = clone $this;
  101. $this->orders->data->id = $response->id;
  102. $this->orders->data->amount->total = $response->amount->total;
  103. $this->orders->data->amount->fees = $response->amount->fees;
  104. $this->orders->data->amount->refunds = $response->amount->refunds;
  105. $this->orders->data->amount->liquid = $response->amount->liquid;
  106. $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers;
  107. $this->orders->data->amount->subtotals = $response->amount->subtotals;
  108. $customer = new Customer($this->moip);
  109. $customer->populate($response->customer);
  110. $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class);
  111. $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class);
  112. $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class);
  113. $this->orders->data->events = $this->structure($response, Event::PATH, Event::class);
  114. $this->orders->data->items = $response->items;
  115. $this->orders->data->receivers = $response->receivers;
  116. $this->orders->data->createdAt = $response->createdAt;
  117. $this->orders->data->status = $response->status;
  118. $this->orders->data->_links = $response->_links;
  119. return $this->orders;
  120. }
  121. /**
  122. * Structure resource.
  123. *
  124. * @param stdClass $response
  125. * @param string $resource
  126. * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class
  127. *
  128. * @return array
  129. */
  130. private function structure(stdClass $response, $resource, $class)
  131. {
  132. $structures = [];
  133. foreach ($response->$resource as $responseResource) {
  134. $structure = new $class($this->orders->moip);
  135. $structure->populate($responseResource);
  136. $structures[] = $structure;
  137. }
  138. return $structures;
  139. }
  140. /**
  141. * Create a new order in MoIP.
  142. *
  143. * @return stdClass
  144. */
  145. public function create()
  146. {
  147. return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
  148. }
  149. /**
  150. * Get an order in MoIP.
  151. *
  152. * @param string $id Id MoIP order id
  153. *
  154. * @return stdClass
  155. */
  156. public function get($id)
  157. {
  158. return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id));
  159. }
  160. /**
  161. * Get MoIP order id.
  162. *
  163. * @return strign
  164. */
  165. public function getId()
  166. {
  167. return $this->getIfSet('id');
  168. }
  169. /**
  170. * Get own request id. external reference.
  171. *
  172. * @return string
  173. */
  174. public function getOwnId()
  175. {
  176. return $this->getIfSet('ownId');
  177. }
  178. /**
  179. * Get total value of order.
  180. *
  181. * @return int|float
  182. */
  183. public function getAmountTotal()
  184. {
  185. return $this->getIfSet('total', $this->data->amount);
  186. }
  187. /**
  188. * Get total value of MoIP rate.
  189. *
  190. * @return int|float
  191. */
  192. public function getAmountFees()
  193. {
  194. return $this->getIfSet('feed', $this->data->amount);
  195. }
  196. /**
  197. * Get total amount of refunds.
  198. *
  199. * @return int|float
  200. */
  201. public function getAmountRefunds()
  202. {
  203. return $this->getIfSet('refunds', $this->data->amount);
  204. }
  205. /**
  206. * Get net total value.
  207. *
  208. * @return int|float
  209. */
  210. public function getAmountLiquid()
  211. {
  212. return $this->getIfSet('liquid', $this->data->amount);
  213. }
  214. /**
  215. * Get sum of amounts received by other recipients. Used in Marketplaces.
  216. *
  217. * @return int|float
  218. */
  219. public function getAmountOtherReceivers()
  220. {
  221. return $this->getIfSet('otherReceivers', $this->data->amount);
  222. }
  223. /**
  224. * Get currency used in the application. Possible values: BRL.
  225. *
  226. * @return string
  227. */
  228. public function getCurrenty()
  229. {
  230. return $this->getIfSet('currency', $this->data->amount);
  231. }
  232. /**
  233. * Get greight value of the item will be added to the value of the items.
  234. *
  235. * @return int|float
  236. */
  237. public function getSubtotalShipping()
  238. {
  239. $this->initializeSubtotals();
  240. return $this->getIfSet('shipping', $this->data->amount->subtotals);
  241. }
  242. /**
  243. * Get Additional value to the item will be added to the value of the items.
  244. *
  245. * @return int|float
  246. */
  247. public function getSubtotalAddition()
  248. {
  249. $this->initializeSubtotals();
  250. return $this->getIfSet('addition', $this->data->amount->subtotals);
  251. }
  252. /**
  253. * Get discounted value of the item will be subtracted from the total value of the items.
  254. *
  255. * @return int|float
  256. */
  257. public function getSubtotalDiscount()
  258. {
  259. $this->initializeSubtotals();
  260. return $this->getIfSet('discount', $this->data->amount->subtotals);
  261. }
  262. /**
  263. * Get summing the values of all items.
  264. *
  265. * @return int|float
  266. */
  267. public function getSubtotalItems()
  268. {
  269. $this->initializeSubtotals();
  270. return $this->getIfSet('items', $this->data->amount->subtotals);
  271. }
  272. /**
  273. * Ger structure item information request.
  274. *
  275. * @return \ArrayIterator
  276. */
  277. public function getItemIterator()
  278. {
  279. return new ArrayIterator($this->data->items);
  280. }
  281. /**
  282. * Get Customer associated with the request.
  283. *
  284. * @return \Moip\Resource\Customer
  285. */
  286. public function getCustomer()
  287. {
  288. return $this->data->customer;
  289. }
  290. /**
  291. * Get payments associated with the request.
  292. *
  293. * @return ArrayIterator
  294. */
  295. public function getPaymentIterator()
  296. {
  297. return new ArrayIterator($this->data->payments);
  298. }
  299. /**
  300. * Get recipient structure of payments.
  301. *
  302. * @return ArrayIterator
  303. */
  304. public function getReceiverIterator()
  305. {
  306. return new ArrayIterator($this->data->receivers);
  307. }
  308. /**
  309. * Get releases associated with the request.
  310. *
  311. * @return ArrayIterator
  312. */
  313. public function getEventIterator()
  314. {
  315. return new ArrayIterator($this->data->events);
  316. }
  317. /**
  318. * Get repayments associated with the request.
  319. *
  320. * @return ArrayIterator
  321. */
  322. public function getRefundIterator()
  323. {
  324. return new ArrayIterator($this->data->refunds);
  325. }
  326. /**
  327. * Get order status.
  328. * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
  329. *
  330. * @return string
  331. */
  332. public function getStatus()
  333. {
  334. return $this->getIfSet('status');
  335. }
  336. /**
  337. * Get date of resource creation.
  338. *
  339. * @return \DateTime
  340. */
  341. public function getCreatedAt()
  342. {
  343. return $this->getIfSet('createdAt');
  344. }
  345. /**
  346. * Get updated resource.
  347. *
  348. * @return \DateTime
  349. */
  350. public function getUpdatedAt()
  351. {
  352. return $this->getIfSet('updatedAt');
  353. }
  354. /**
  355. * Get hypermedia link structure (HATEOAS) resource Orders.
  356. *
  357. * @return \stdClass
  358. */
  359. public function getLinks()
  360. {
  361. return $this->getIfSet('_links');
  362. }
  363. /**
  364. * Structure of payment.
  365. *
  366. * @return \Moip\Resource\Payment
  367. */
  368. public function payments()
  369. {
  370. $payment = new Payment($this->moip);
  371. $payment->setOrder($this);
  372. return $payment;
  373. }
  374. /**
  375. * Structure of refund.
  376. *
  377. * @return \Moip\Resource\Refund
  378. */
  379. public function refunds()
  380. {
  381. $refund = new Refund($this->moip);
  382. $refund->setOrder($this);
  383. return $refund;
  384. }
  385. /**
  386. * Set additional value to the item will be added to the value of the items.
  387. *
  388. * @param int|float $value additional value to the item.
  389. */
  390. public function setAddition($value)
  391. {
  392. $this->data->subtotals->addition = (float) $value;
  393. return $this;
  394. }
  395. /**
  396. * Set customer associated with the order.
  397. *
  398. * @param \Moip\Resource\Customer $customer customer associated with the request.
  399. */
  400. public function setCustomer(Customer $customer)
  401. {
  402. $this->data->customer = $customer;
  403. return $this;
  404. }
  405. /**
  406. * Set discounted value of the item will be subtracted from the total value of the items.
  407. *
  408. * @param int|float $value discounted value.
  409. */
  410. public function setDiscont($value)
  411. {
  412. $this->data->subtotals->discont = (float) $value;
  413. return $this;
  414. }
  415. /**
  416. * Set own request id. external reference.
  417. *
  418. * @param string $ownId external reference.
  419. */
  420. public function setOwnId($ownId)
  421. {
  422. $this->data->ownId = $ownId;
  423. return $this;
  424. }
  425. /**
  426. * Set shipping Amount.
  427. *
  428. * @param float $value shipping Amount.
  429. *
  430. * @return $this
  431. */
  432. public function setShippingAmount($value)
  433. {
  434. if (!isset($this->data->amount->subtotals)) {
  435. $this->data->amount->subtotals = new stdClass();
  436. }
  437. $this->data->amount->subtotals->shipping = (float) $value;
  438. return $this;
  439. }
  440. }