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

/PagSeguro/Controller/Component/CheckoutComponent.php

http://github.com/ftgoncalves/pagseguro
PHP | 211 lines | 96 code | 27 blank | 88 comment | 7 complexity | b8598d48f459fa9595ac79edf8d0fbb4 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin de integração com a API do PagSeguro e CakePHP.
  4. *
  5. * PHP versions 5+
  6. * Copyright 2010-2011, Felipe Theodoro Gonçalves, (http://ftgoncalves.com.br)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @author Felipe Theodoro Gonçalves
  12. * @link https://github.com/ftgoncalves/pagseguro/
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. * @version 1.0
  15. */
  16. App::uses('HttpSocket', 'Network/Http');
  17. class CheckoutComponent extends Component {
  18. /**
  19. *
  20. * Instancia do Controller
  21. * @var Object
  22. */
  23. public $Controller = null;
  24. /**
  25. *
  26. * Dominio do webserver do PagSeguro
  27. * @var String
  28. */
  29. private $__URI = 'ws.pagseguro.uol.com.br';
  30. /**
  31. *
  32. * Endereço do ws PagSeguro v2
  33. * @var String
  34. */
  35. private $__path = '/v2/checkout/';
  36. /**
  37. *
  38. * Charset
  39. * @var String
  40. */
  41. public $charset = 'UTF-8';
  42. /**
  43. *
  44. * Endereço para redirecionamento para o checkout do PagSeguro
  45. * @var String
  46. */
  47. private $__redirect = 'https://pagseguro.uol.com.br/v2/checkout/payment.html?code=';
  48. /**
  49. *
  50. * Atributo contendo as configurações do pagseguro
  51. * @var array
  52. */
  53. public $config = array(
  54. 'currency' => 'BRL'
  55. );
  56. /**
  57. *
  58. * Referencia da transação
  59. * @var array
  60. */
  61. private $reference = array();
  62. /**
  63. *
  64. * Dados do endereço
  65. * @var array
  66. */
  67. private $shippingAddress = array();
  68. /**
  69. *
  70. * Dados do cliente
  71. * @var array
  72. */
  73. private $shippingCustomer = array();
  74. /**
  75. *
  76. * Dados dos itens da compra
  77. * @var array
  78. */
  79. private $cart = array();
  80. /**
  81. * Tipos de frete
  82. */
  83. private $typeFreight = array(
  84. 'PAC' => 1,
  85. 'SEDEX' => 2,
  86. null => 3
  87. );
  88. private $type = array(
  89. 'shippingType' => 3
  90. );
  91. private $count = 1;
  92. /**
  93. *
  94. * Methodo para setar as configurações defaults do pagseguro
  95. * @param Object $controller
  96. */
  97. public function startup(&$Controller) {
  98. $this->Controller = $Controller;
  99. if ((Configure::read('PagSeguro') != false || Configure::read('PagSeguro') != null) && is_array(Configure::read('PagSeguro')))
  100. $this->config = array_merge($this->config, Configure::read('pag_seguro'));
  101. }
  102. /**
  103. *
  104. * Força configurações em tempo de execução
  105. * @param array $config
  106. */
  107. public function setConfig($email, $token, $currency = 'BRL') {
  108. $this->config = array(
  109. 'email' => $email,
  110. 'token' => $token,
  111. 'currency' => $currency
  112. );
  113. }
  114. public function setReference($id) {
  115. $this->reference = array('reference' => $id);
  116. }
  117. public function addItem($id, $name, $amount, $weight, $quantity = 1) {
  118. $this->cart = array_merge($this->cart, array(
  119. 'itemId' . $this->count => $id,
  120. 'itemDescription' . $this->count => $name,
  121. 'itemAmount' . $this->count => str_replace(',', '', number_format($amount, 2)),
  122. 'itemWeight' . $this->count => $weight,
  123. 'itemQuantity' . $this->count => $quantity
  124. ));
  125. $this->count++;
  126. }
  127. public function setShippingAddress($zip, $address, $number, $completion, $neighborhood, $city, $estate, $country) {
  128. $this->shippingAddress = array(
  129. 'shippingAddressStreet' => $address,
  130. 'shippingAddressNumber' => $number,
  131. 'shippingAddressDistrict' => $neighborhood,
  132. 'shippingAddressPostalCode' => $zip,
  133. 'shippingAddressCity' => $city,
  134. 'shippingAddressState' => $estate,
  135. 'shippingAddressCountry' => $country
  136. );
  137. }
  138. public function setCustomer($email, $name, $areaCode, $phoneNumber) {
  139. $this->shippingCustomer = array(
  140. 'senderName' => $name,
  141. 'senderAreaCode' => $areaCode,
  142. 'senderPhone' => $phoneNumber,
  143. 'senderEmail' => $email,
  144. );
  145. }
  146. public function setShippingType($type) {
  147. if (isset($this->typeFreight[$type]))
  148. $this->type = array('shippingType' => $this->typeFreight[$type]);
  149. }
  150. /**
  151. *
  152. * Finaliza a compra.
  153. * Recebe o codigo para redirecionamento ou erro.
  154. */
  155. public function finalize() {
  156. $http = new HttpSocket();
  157. $return = $http->request(array(
  158. 'method' => 'POST',
  159. 'uri' => array(
  160. 'scheme' => 'https',
  161. 'host' => $this->__URI,
  162. 'path' => $this->__path
  163. ),
  164. 'header' => array(
  165. 'Content-Type' => "application/x-www-form-urlencoded; charset={$this->charset}"
  166. ),
  167. 'body' => array_merge($this->reference, $this->config, $this->type, $this->cart, $this->shippingAddress, $this->shippingCustomer)
  168. ));
  169. return $this->__response($return);
  170. }
  171. /**
  172. *
  173. * Recebe o Xml com os dados redirecionamento ou erros. Iniciando o redirecionamento
  174. * @param String $res
  175. * @return array
  176. */
  177. private function __response($data) {
  178. App::uses('Xml', 'Utility');
  179. $response = Xml::toArray(Xml::build($data['body']));
  180. if (isset($response['checkout']))
  181. $this->Controller->redirect($this->__redirect . $response['checkout']['code'], null, false);
  182. return $response;
  183. }
  184. }