/vendor/PagSeguroLibrary/domain/PaymentRequest.class.php

https://github.com/madeinnordeste/kohana-pagseguro · PHP · 433 lines · 189 code · 38 blank · 206 comment · 37 complexity · f10650e59cb2ba15d39c5b5cd938b769 MD5 · raw file

  1. <?php if (!defined('PAGSEGURO_LIBRARY')) { die('No direct script access allowed'); }
  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. * Represents a payment request
  18. */
  19. class PaymentRequest {
  20. /**
  21. * Party that will be sending the money
  22. */
  23. private $sender;
  24. /**
  25. * Payment currency
  26. */
  27. private $currency;
  28. /**
  29. * Products/items in this payment request
  30. */
  31. private $items;
  32. /**
  33. * Uri to where the PagSeguro payment page should redirect the user after the payment information is processed.
  34. * Typically this is a confirmation page on your web site.
  35. */
  36. private $redirectURL;
  37. /**
  38. * Extra amount to be added to the transaction total
  39. *
  40. * This value can be used to add an extra charge to the transaction
  41. * or provide a discount in the case ExtraAmount is a negative value.
  42. */
  43. private $extraAmount;
  44. /**
  45. * Reference code
  46. *
  47. * Optional. You can use the reference code to store an identifier so you can
  48. * associate the PagSeguro transaction to a transaction in your system.
  49. */
  50. private $reference;
  51. /**
  52. * Shipping information associated with this payment request
  53. */
  54. private $shipping;
  55. /**
  56. * How long this payment request will remain valid, in seconds.
  57. *
  58. * Optional. After this payment request is submitted, the payment code returned
  59. * will remain valid for the period specified here.
  60. */
  61. private $maxAge;
  62. /**
  63. * How many times the payment redirect uri returned by the payment web service can be accessed.
  64. *
  65. * Optional. After this payment request is submitted, the payment redirect uri returned by
  66. * the payment web service will remain valid for the number of uses specified here.
  67. */
  68. private $maxUses;
  69. /**
  70. * @return the sender
  71. *
  72. * Party that will be sending the Uri to where the PagSeguro payment page should redirect the user after the payment information is processed.
  73. * money
  74. */
  75. public function getSender() {
  76. return $this->sender;
  77. }
  78. /**
  79. * Sets the Sender, party that will be sending the money
  80. * @param String $name
  81. * @param String $email
  82. * @param String $areaCode
  83. * @param String $number
  84. */
  85. public function setSender($name, $email = null, $areaCode = null, $number = null) {
  86. $param = $name;
  87. if (is_array($param)) {
  88. $this->sender = new Sender($param);
  89. } elseif($param instanceof Sender) {
  90. $this->sender = $param;
  91. } else {
  92. $sender = new Sender();
  93. $sender->setName($param);
  94. $sender->setEmail($email);
  95. $sender->setPhone(new Phone($areaCode, $number));
  96. $this->sender = $sender;
  97. }
  98. }
  99. /**
  100. * Sets the name of the sender, party that will be sending the money
  101. * @param String $senderName
  102. */
  103. public function setSenderName($senderName) {
  104. if ($this->sender == null) {
  105. $this->sender = new Sender();
  106. }
  107. $this->sender->setName($senderName);
  108. }
  109. /**
  110. * Sets the name of the sender, party that will be sending the money
  111. * @param String $senderEmail
  112. */
  113. public function setSenderEmail($senderEmail) {
  114. if ($this->sender == null) {
  115. $this->sender = new Sender();
  116. }
  117. $this->sender->setEmail($senderEmail);
  118. }
  119. /**
  120. * Sets the Sender phone number, phone of the party that will be sending the money
  121. *
  122. * @param areaCode
  123. * @param number
  124. */
  125. public function setSenderPhone($areaCode, $number = null) {
  126. $param = $areaCode;
  127. if ($this->sender == null) {
  128. $this->sender = new Sender();
  129. }
  130. if ($param instanceof Phone) {
  131. $this->sender->setPhone($param);
  132. } else {
  133. $this->sender->setPhone(new Phone($param, $number));
  134. }
  135. }
  136. /**
  137. * @return the currency
  138. * Example: BRL
  139. */
  140. public function getCurrency() {
  141. return $this->currency;
  142. }
  143. /**
  144. * Sets the currency
  145. * @param String $currency
  146. */
  147. public function setCurrency($currency) {
  148. $this->currency = $currency;
  149. }
  150. /**
  151. * @return the items/products list in this payment request
  152. */
  153. public function getItems() {
  154. return $this->items;
  155. }
  156. /**
  157. * Sets the items/products list in this payment request
  158. * @param array $items
  159. */
  160. public function setItems(Array $items) {
  161. if (is_array($items)) {
  162. $i = Array();
  163. foreach ($items as $key => $item) {
  164. if ($item instanceof Item) {
  165. $i[$key] = $item;
  166. } else if (is_array($item)) {
  167. $i[$key] = new Item($item);
  168. }
  169. }
  170. }
  171. $this->items = $i;
  172. }
  173. /**
  174. * Adds a new product/item in this payment request
  175. *
  176. * @param String $id
  177. * @param String $description
  178. * @param String $quantity
  179. * @param String $amount
  180. * @param String $weight
  181. * @param String $shippingCost
  182. */
  183. public function addItem($id, $description = null, $quantity = null, $amount = null, $weight = null, $shippingCost = null) {
  184. $param = $id;
  185. if ($this->items == null) {
  186. $this->items = Array();
  187. }
  188. if (is_array($param)) {
  189. array_push($this->items, new Item($param));
  190. } else if ($param instanceof Item) {
  191. array_push($this->items, $param);
  192. } else {
  193. $item = new Item();
  194. $item->setId($param);
  195. $item->setDescription($description);
  196. $item->setQuantity($quantity);
  197. $item->setAmount($amount);
  198. $item->setWeight($weight);
  199. $item->setShippingCost($shippingCost);
  200. array_push($this->items, $item);
  201. }
  202. }
  203. /**
  204. * Uri to where the PagSeguro payment page should redirect the user after the payment information is processed.
  205. * Typically this is a confirmation page on your web site.
  206. *
  207. * @return the redirectURL
  208. */
  209. public function getRedirectURL() {
  210. return $this->redirectURL;
  211. }
  212. /**
  213. * Sets the redirect URL
  214. *
  215. * Uri to where the PagSeguro payment page should redirect the user after the payment information is processed.
  216. * Typically this is a confirmation page on your web site.
  217. *
  218. * @param String $redirectURL
  219. */
  220. public function setRedirectURL($redirectURL) {
  221. $this->redirectURL = $redirectURL;
  222. }
  223. /**
  224. * This value can be used to add an extra charge to the transaction
  225. * or provide a discount in the case ExtraAmount is a negative value.
  226. *
  227. * @return the extra amount
  228. */
  229. public function getExtraAmount() {
  230. return $this->extraAmount;
  231. }
  232. /**
  233. * Sets the extra amount
  234. * This value can be used to add an extra charge to the transaction
  235. * or provide a discount in the case <b>extraAmount</b> is a negative value.
  236. *
  237. * @param extraAmount
  238. */
  239. public function setExtraAmount($extraAmount) {
  240. $this->extraAmount = $extraAmount;
  241. }
  242. /**
  243. * @return the reference of this payment request
  244. */
  245. public function getReference() {
  246. return $this->reference;
  247. }
  248. /**
  249. * Sets the reference of this payment request
  250. * @param reference
  251. */
  252. public function setReference($reference) {
  253. $this->reference = $reference;
  254. }
  255. /**
  256. * @return the shipping information for this payment request
  257. * @see Shipping
  258. */
  259. public function getShipping() {
  260. return $this->shipping;
  261. }
  262. /**
  263. * Sets the shipping information for this payment request
  264. * @param Shipping $address
  265. * @param ShippingType $type
  266. */
  267. public function setShipping($address, $type = null) {
  268. $param = $address;
  269. if ($param instanceof Shipping) {
  270. $this->shipping = $param;
  271. } else {
  272. $shipping = new Shipping();
  273. if (is_array($param)) {
  274. $shipping->setAddress(new Address($param));
  275. } else if ($param instanceof Address) {
  276. $shipping->setAddress($param);
  277. }
  278. if ($type) {
  279. if ($type instanceof ShippingType) {
  280. $shipping->setType($type);
  281. } else {
  282. $shipping->setType(new ShippingType($type));
  283. }
  284. }
  285. $this->shipping = $shipping;
  286. }
  287. }
  288. /**
  289. * Sets the shipping address for this payment request
  290. * @param String $postalCode
  291. * @param String $street
  292. * @param String $number
  293. * @param String $complement
  294. * @param String $district
  295. * @param String $city
  296. * @param String $state
  297. * @param String $country
  298. */
  299. public function setShippingAddress($postalCode = null, $street = null, $number = null, $complement = null, $district = null, $city = null, $state = null, $country = null) {
  300. $param = $postalCode;
  301. if ($this->shipping == null) {
  302. $this->shipping = new Shipping();
  303. }
  304. if (is_array($param)) {
  305. $this->shipping->setAddress(new Address($param));
  306. } elseif ($param instanceof Address) {
  307. $this->shipping->setAddress($param);
  308. } else {
  309. $address = new Address();
  310. $address->setPostalCode($postalCode);
  311. $address->setStreet($street);
  312. $address->setNumber($number);
  313. $address->setComplement($complement);
  314. $address->setDistrict($district);
  315. $address->setCity($city);
  316. $address->setState($state);
  317. $address->setCountry($country);
  318. $this->shipping->setAddress($address);
  319. }
  320. }
  321. /**
  322. * Sets the shipping type for this payment request
  323. * @param ShippingType $type
  324. */
  325. public function setShippingType($type) {
  326. $param = $type;
  327. if ($this->shipping == null) {
  328. $this->shipping = new Shipping();
  329. }
  330. if ($param instanceof ShippingType) {
  331. $this->shipping->setType($param);
  332. } else {
  333. $this->shipping->setType(new ShippingType($param));
  334. }
  335. }
  336. /**
  337. * @return the max age of this payment request
  338. *
  339. * After this payment request is submitted, the payment code returned
  340. * will remain valid for the period specified.
  341. */
  342. public function getMaxAge() {
  343. return $this->maxAge;
  344. }
  345. /**
  346. * Sets the max age of this payment request
  347. * After this payment request is submitted, the payment code returned
  348. * will remain valid for the period specified here.
  349. *
  350. * @param maxAge
  351. */
  352. public function setMaxAge($maxAge) {
  353. $this->maxAge = $maxAge;
  354. }
  355. /**
  356. * After this payment request is submitted, the payment redirect uri returned by
  357. * the payment web service will remain valid for the number of uses specified here.
  358. *
  359. * @return the max uses configured for this payment request
  360. */
  361. public function getMaxUses() {
  362. return $this->maxUses;
  363. }
  364. /**
  365. * Sets the max uses of this payment request
  366. *
  367. * After this payment request is submitted, the payment redirect uri returned by
  368. * the payment web service will remain valid for the number of uses specified here.
  369. *
  370. * @param maxUses
  371. */
  372. public function setMaxUses($maxUses) {
  373. $this->maxUses = $maxUses;
  374. }
  375. /**
  376. * Calls the PagSeguro web service and register this request for payment
  377. *
  378. * @param Credentials $credentials
  379. * @return The URL to where the user needs to be redirected to in order to complete the payment process
  380. */
  381. public function register(Credentials $credentials) {
  382. return PaymentService::createCheckoutRequest($credentials, $this);
  383. }
  384. /**
  385. * @return a string that represents the current object
  386. */
  387. public function toString(){
  388. $email = $this->sender ? $this->sender->getEmail() : "null";
  389. return "PaymentRequest(Reference=".$this->reference.", SenderEmail=".$email.")";
  390. }
  391. }
  392. ?>