PageRenderTime 86ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/PagSeguroLibrary/parser/PagSeguroTransactionParser.class.php

https://gitlab.com/maickonrangel/HelpRpg.br
PHP | 400 lines | 242 code | 78 blank | 80 comment | 72 complexity | 476a5d6d14b22552a26d9af5d8545b4b MD5 | raw file
  1. <?php
  2. /*
  3. * ***********************************************************************
  4. Copyright [2011] [PagSeguro Internet Ltda.]
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. * ***********************************************************************
  15. */
  16. /**
  17. * Class PagSeguroTransactionParser
  18. */
  19. class PagSeguroTransactionParser extends PagSeguroServiceParser
  20. {
  21. /**
  22. * @param $str_xml
  23. * @return PagSeguroTransactionSearchResult
  24. */
  25. public static function readSearchResult($str_xml)
  26. {
  27. $parser = new PagSeguroXmlParser($str_xml);
  28. $data = $parser->getResult('transactionSearchResult');
  29. $searchResutlt = new PagSeguroTransactionSearchResult();
  30. if (isset($data['totalPages'])) {
  31. $searchResutlt->setTotalPages($data['totalPages']);
  32. }
  33. if (isset($data['date'])) {
  34. $searchResutlt->setDate($data['date']);
  35. }
  36. if (isset($data['resultsInThisPage'])) {
  37. $searchResutlt->setResultsInThisPage($data['resultsInThisPage']);
  38. }
  39. if (isset($data['currentPage'])) {
  40. $searchResutlt->setCurrentPage($data['currentPage']);
  41. }
  42. if (isset($data['transactions']) && is_array($data['transactions'])) {
  43. $transactions = array();
  44. if (isset($data['transactions']['transaction'][0])) {
  45. $i = 0;
  46. foreach ($data['transactions']['transaction'] as $key => $value) {
  47. $transactions[$i++] = self::parseTransactionSummary($value);
  48. }
  49. } else {
  50. $transactions[0] = self::parseTransactionSummary($data['transactions']['transaction']);
  51. }
  52. $searchResutlt->setTransactions($transactions);
  53. }
  54. return $searchResutlt;
  55. }
  56. /**
  57. * @param $str_xml
  58. * @return PagSeguroTransaction
  59. */
  60. public static function readTransaction($str_xml)
  61. {
  62. // Parser
  63. $parser = new PagSeguroXmlParser($str_xml);
  64. // <transaction>
  65. $data = $parser->getResult('transaction');
  66. $transaction = new PagSeguroTransaction();
  67. // <transaction> <lastEventDate>
  68. if (isset($data["lastEventDate"])) {
  69. $transaction->setLastEventDate($data["lastEventDate"]);
  70. }
  71. // <transaction> <date>
  72. if (isset($data["date"])) {
  73. $transaction->setDate($data["date"]);
  74. }
  75. // <transaction> <code>
  76. if (isset($data["code"])) {
  77. $transaction->setCode($data["code"]);
  78. }
  79. // <transaction> <reference>
  80. if (isset($data["reference"])) {
  81. $transaction->setReference($data["reference"]);
  82. }
  83. // <transaction> <type>
  84. if (isset($data["type"])) {
  85. $transaction->setType(new PagSeguroTransactionType($data["type"]));
  86. }
  87. // <transaction> <status>
  88. if (isset($data["status"])) {
  89. $transaction->setStatus(new PagSeguroTransactionStatus($data["status"]));
  90. }
  91. if (isset($data["paymentMethod"]) && is_array($data["paymentMethod"])) {
  92. // <transaction> <paymentMethod>
  93. $paymentMethod = new PagSeguroPaymentMethod();
  94. // <transaction> <paymentMethod> <type>
  95. if (isset($data["paymentMethod"]['type'])) {
  96. $paymentMethod->setType(new PagSeguroPaymentMethodType($data["paymentMethod"]['type']));
  97. }
  98. // <transaction> <paymentMethod> <code>
  99. if (isset($data["paymentMethod"]['code'])) {
  100. $paymentMethod->setCode(new PagSeguroPaymentMethodCode($data["paymentMethod"]['code']));
  101. }
  102. $transaction->setPaymentMethod($paymentMethod);
  103. }
  104. // <transaction> <grossAmount>
  105. if (isset($data["grossAmount"])) {
  106. $transaction->setGrossAmount($data["grossAmount"]);
  107. }
  108. // <transaction> <discountAmount>
  109. if (isset($data["discountAmount"])) {
  110. $transaction->setDiscountAmount($data["discountAmount"]);
  111. }
  112. // <transaction> <feeAmount>
  113. if (isset($data["feeAmount"])) {
  114. $transaction->setFeeAmount($data["feeAmount"]);
  115. }
  116. // <transaction> <netAmount>
  117. if (isset($data["netAmount"])) {
  118. $transaction->setNetAmount($data["netAmount"]);
  119. }
  120. // <transaction> <extraAmount>
  121. if (isset($data["extraAmount"])) {
  122. $transaction->setExtraAmount($data["extraAmount"]);
  123. }
  124. // <transaction> <installmentCount>
  125. if (isset($data["installmentCount"])) {
  126. $transaction->setInstallmentCount($data["installmentCount"]);
  127. }
  128. if (isset($data["items"]['item']) && is_array($data["items"]['item'])) {
  129. $items = array();
  130. $i = 0;
  131. if (isset($data["items"]['item'][0])) {
  132. foreach ($data["items"]['item'] as $key => $value) {
  133. $item = self::parseTransactionItem($value);
  134. $items[$i] = $item;
  135. $i++;
  136. }
  137. } else {
  138. $items[0] = self::parseTransactionItem($data["items"]['item']);
  139. }
  140. // <transaction> <items>
  141. $transaction->setItems($items);
  142. }
  143. if (isset($data["sender"])) {
  144. // <transaction> <sender>
  145. $sender = new PagSeguroSender();
  146. // <transaction> <sender> <name>
  147. if (isset($data["sender"]["name"])) {
  148. $sender->setName($data["sender"]["name"]);
  149. }
  150. // <transaction> <sender> <email>
  151. if (isset($data["sender"]["email"])) {
  152. $sender->setEmail($data["sender"]["email"]);
  153. }
  154. if (isset($data["sender"]["phone"])) {
  155. // <transaction> <sender> <phone>
  156. $phone = new PagSeguroPhone();
  157. // <transaction> <sender> <phone> <areaCode>
  158. if (isset($data["sender"]["phone"]["areaCode"])) {
  159. $phone->setAreaCode($data["sender"]["phone"]["areaCode"]);
  160. }
  161. // <transaction> <sender> <phone> <number>
  162. if (isset($data["sender"]["phone"]["number"])) {
  163. $phone->setNumber($data["sender"]["phone"]["number"]);
  164. }
  165. $sender->setPhone($phone);
  166. }
  167. // <transaction><sender><documents>
  168. if (isset($data['sender']['documents']) && is_array($data['sender']['documents'])) {
  169. $documents = $data['sender']['documents'];
  170. if (count($documents) > 0) {
  171. foreach ($documents as $document) {
  172. $sender->addDocument($document['type'], $document['value']);
  173. }
  174. }
  175. }
  176. $transaction->setSender($sender);
  177. }
  178. if (isset($data["shipping"]) && is_array($data["shipping"])) {
  179. // <transaction> <shipping>
  180. $shipping = new PagSeguroShipping();
  181. // <transaction> <shipping> <type>
  182. if (isset($data["shipping"]["type"])) {
  183. $shipping->setType(new PagSeguroShippingType($data["shipping"]["type"]));
  184. }
  185. // <transaction> <shipping> <cost>
  186. if (isset($data["shipping"]["cost"])) {
  187. $shipping->setCost($data["shipping"]["cost"]);
  188. }
  189. if (isset($data["shipping"]["address"]) && is_array($data["shipping"]["address"])) {
  190. // <transaction> <shipping> <address>
  191. $address = new PagSeguroAddress();
  192. // <transaction> <shipping> <address> <street>
  193. if (isset($data["shipping"]["address"]["street"])) {
  194. $address->setStreet($data["shipping"]["address"]["street"]);
  195. }
  196. // <transaction> <shipping> <address> <number>
  197. if (isset($data["shipping"]["address"]["number"])) {
  198. $address->setNumber($data["shipping"]["address"]["number"]);
  199. }
  200. // <transaction> <shipping> <address> <complement>
  201. if (isset($data["shipping"]["address"]["complement"])) {
  202. $address->setComplement($data["shipping"]["address"]["complement"]);
  203. }
  204. // <transaction> <shipping> <address> <city>
  205. if (isset($data["shipping"]["address"]["city"])) {
  206. $address->setCity($data["shipping"]["address"]["city"]);
  207. }
  208. // <transaction> <shipping> <address> <state>
  209. if (isset($data["shipping"]["address"]["state"])) {
  210. $address->setState($data["shipping"]["address"]["state"]);
  211. }
  212. // <transaction> <shipping> <address> <district>
  213. if (isset($data["shipping"]["address"]["district"])) {
  214. $address->setDistrict($data["shipping"]["address"]["district"]);
  215. }
  216. // <transaction> <shipping> <address> <postalCode>
  217. if (isset($data["shipping"]["address"]["postalCode"])) {
  218. $address->setPostalCode($data["shipping"]["address"]["postalCode"]);
  219. }
  220. // <transaction> <shipping> <address> <country>
  221. if (isset($data["shipping"]["address"]["country"])) {
  222. $address->setCountry($data["shipping"]["address"]["country"]);
  223. }
  224. $shipping->setAddress($address);
  225. }
  226. // <transaction> <shipping>
  227. $transaction->setShipping($shipping);
  228. }
  229. return $transaction;
  230. }
  231. /**
  232. * @param $data
  233. * @return PagSeguroItem
  234. */
  235. private static function parseTransactionItem($data)
  236. {
  237. // <transaction> <items> <item>
  238. $item = new PagSeguroItem();
  239. // <transaction> <items> <item> <id>
  240. if (isset($data["id"])) {
  241. $item->setId($data["id"]);
  242. }
  243. // <transaction> <items> <item> <description>
  244. if (isset($data["description"])) {
  245. $item->setDescription($data["description"]);
  246. }
  247. // <transaction> <items> <item> <quantity>
  248. if (isset($data["quantity"])) {
  249. $item->setQuantity($data["quantity"]);
  250. }
  251. // <transaction> <items> <item> <amount>
  252. if (isset($data["amount"])) {
  253. $item->setAmount($data["amount"]);
  254. }
  255. // <transaction> <items> <item> <weight>
  256. if (isset($data["weight"])) {
  257. $item->setWeight($data["weight"]);
  258. }
  259. return $item;
  260. }
  261. /**
  262. * @param $data
  263. * @return PagSeguroTransactionSummary
  264. */
  265. private static function parseTransactionSummary($data)
  266. {
  267. $transactionSummary = new PagSeguroTransactionSummary();
  268. if (isset($data['type'])) {
  269. $transactionSummary->setType(new PagSeguroTransactionType($data['type']));
  270. }
  271. if (isset($data['code'])) {
  272. $transactionSummary->setCode($data['code']);
  273. }
  274. if (isset($data['reference'])) {
  275. $transactionSummary->setReference($data['reference']);
  276. }
  277. if (isset($data['date'])) {
  278. $transactionSummary->setDate($data['date']);
  279. }
  280. if (isset($data['lastEventDate'])) {
  281. $transactionSummary->setLastEventDate($data['lastEventDate']);
  282. }
  283. if (isset($data['grossAmount'])) {
  284. $transactionSummary->setGrossAmount($data['grossAmount']);
  285. }
  286. if (isset($data['status'])) {
  287. $transactionSummary->setStatus(new PagSeguroTransactionStatus($data['status']));
  288. }
  289. if (isset($data['netAmount'])) {
  290. $transactionSummary->setNetAmount($data['netAmount']);
  291. }
  292. if (isset($data['discountAmount'])) {
  293. $transactionSummary->setDiscountAmount($data['discountAmount']);
  294. }
  295. if (isset($data['feeAmount'])) {
  296. $transactionSummary->setFeeAmount($data['feeAmount']);
  297. }
  298. if (isset($data['extraAmount'])) {
  299. $transactionSummary->setExtraAmount($data['extraAmount']);
  300. }
  301. if (isset($data['lastEvent'])) {
  302. $transactionSummary->setLastEventDate($data['lastEvent']);
  303. }
  304. if (isset($data['paymentMethod'])) {
  305. $paymentMethod = new PagSeguroPaymentMethod();
  306. if (isset($data['paymentMethod']['type'])) {
  307. $paymentMethod->setType(new PagSeguroPaymentMethodType($data['paymentMethod']['type']));
  308. }
  309. if (isset($data['paymentMethod']['code'])) {
  310. $paymentMethod->setCode(new PagSeguroPaymentMethodCode($data['paymentMethod']['code']));
  311. }
  312. $transactionSummary->setPaymentMethod($paymentMethod);
  313. }
  314. if (isset($data["recoveryCode"])) {
  315. $transactionSummary->setRecoveryCode($data["recoveryCode"]);
  316. }
  317. return $transactionSummary;
  318. }
  319. }