/com/paypal/api/ExpressCheckout.php

https://github.com/br-paypaldev/paypal-refund · PHP · 203 lines · 107 code · 33 blank · 63 comment · 15 complexity · 3d88561e472cfb0faaca718543bd43c1 MD5 · raw file

  1. <?php
  2. /**
  3. * Pacote de integração com PayPal
  4. * @package com.paypal.api
  5. */
  6. namespace com\paypal\api;
  7. use \InvalidArgumentException;
  8. use \RuntimeException;
  9. use \UnexpectedValueException;
  10. use com\paypal\http\HTTPConnection;
  11. use com\paypal\http\HTTPRequest;
  12. class ExpressCheckout {
  13. /**
  14. * HOST da API do PayPal, utilizada em produção.
  15. */
  16. const API_HOST = 'api-3t.paypal.com';
  17. /**
  18. * Versão da API.
  19. */
  20. const API_VERSION = '72.0';
  21. /**
  22. * HOST da API no Sandbox PayPal, utilizada durante os testes.
  23. */
  24. const SANDBOX_HOST = 'api-3t.sandbox.paypal.com';
  25. /**
  26. * @var array
  27. */
  28. private $params = array();
  29. /**
  30. * @var boolean
  31. */
  32. private $sandbox;
  33. /**
  34. * Constroi o objeto de integração com PayPal Express Checkout especificando
  35. * a versão da API que será utilizada.
  36. * @param string $user Usuário da API
  37. * @param string $pswd Senha do usuário da API
  38. * @param string $signature Assinatura da API
  39. */
  40. public function __construct( $user , $pswd , $signature ) {
  41. $this->sandbox( false );
  42. $this->setParam( 'USER' , $user );
  43. $this->setParam( 'PWD' , $pswd );
  44. $this->setParam( 'SIGNATURE' , $signature );
  45. $this->setParam( 'VERSION' , ExpressCheckout::API_VERSION );
  46. }
  47. /**
  48. * Operação DoExpressCheckout da API PayPal Express Checkout
  49. * @param string $token
  50. * @param string $payerId
  51. * @return ExpressCheckout
  52. */
  53. public function doExpressCheckout( $token , $payerId ) {
  54. $this->setParam( 'METHOD' , 'DoExpressCheckoutPayment' );
  55. $this->setParam( 'TOKEN' , $token );
  56. $this->setParam( 'PAYERID' , $payerId );
  57. return $this;
  58. }
  59. /**
  60. * Executa a operação
  61. * @return array Resposta no formato NVP.
  62. * @throws RuntimeException
  63. */
  64. public function execute() {
  65. $httpConnection = new HTTPConnection();
  66. if ( $this->sandbox ) {
  67. $host = ExpressCheckout::SANDBOX_HOST;
  68. } else {
  69. $host = ExpressCheckout::API_HOST;
  70. }
  71. $httpConnection->initialize( $host , true );
  72. foreach ( $this->params as $name => $value ) {
  73. $httpConnection->setParam( $name , $value );
  74. }
  75. $httpResponse = $httpConnection->execute( '/nvp' , HTTPRequest::POST );
  76. if ( $httpResponse->getStatusCode() == 200 ) {
  77. $nvp = array();
  78. $matches = array();
  79. if ( preg_match_all(
  80. '/(?<name>[^\=]+)\=(?<value>[^&]+)&?/',
  81. $httpResponse->getContent(),
  82. $matches ) ) {
  83. foreach ( $matches[ 'name' ] as $offset => $name ) {
  84. $nvp[ $name ] = urldecode(
  85. $matches[ 'value' ][ $offset ]
  86. );
  87. }
  88. if ( isset( $nvp[ 'ACK' ] ) ) {
  89. if ( $nvp[ 'ACK' ] == 'Failure' ) {
  90. $e = new RuntimeException(
  91. 'Falha integração com PayPal'
  92. );
  93. foreach ( $nvp as $name => $value ) {
  94. if ( substr( $name , 0 , 13 ) == 'L_LONGMESSAGE' ) {
  95. $o = substr( $name , 13 );
  96. $code = 0;
  97. if ( isset( $nvp[ 'L_ERRORCODE' . $o ] ) ) {
  98. $code = $nvp[ 'L_ERRORCODE' . $o ];
  99. }
  100. $c = new RuntimeException( $value , $code , $e );
  101. $e = $c;
  102. }
  103. }
  104. throw $e;
  105. } else {
  106. return $nvp;
  107. }
  108. }
  109. }
  110. throw new RuntimeException( 'Falha na recepção dos dados do PayPal' );
  111. } else {
  112. throw new RuntimeException( 'Falha na comunicação com o PayPal' );
  113. }
  114. }
  115. /**
  116. * Operação GetExpressCheckoutDetails da API PayPal ExpressCheckout
  117. * @param string $token
  118. * @return ExpressCheckout
  119. */
  120. public function getExpressCheckoutDetails( $token ) {
  121. $this->setParam( 'METHOD' , 'GetExpressCheckoutDetails' );
  122. $this->setParam( 'TOKEN' , $token );
  123. return $this;
  124. }
  125. /**
  126. * Define se as requisições serão feitas no PayPal Sandbox ou em produção.
  127. * @param boolean $sandbox
  128. * @return com\paypal\api\ExpressCheckout
  129. */
  130. public function sandbox( $sandbox = true ) {
  131. $this->sandbox = !!$sandbox;
  132. return $this;
  133. }
  134. /**
  135. * Operação RefundTransaction da API PayPal
  136. * @param string $transactionId
  137. * @param string $refundType
  138. */
  139. public function refundTransaction( $transactionId , $refundType = 'Full' ) {
  140. $this->setParam( 'METHOD' , 'RefundTransaction' );
  141. $this->setParam( 'TRANSACTIONID' , $transactionId );
  142. $this->setParam( 'REFUNDTYPE' , $transactionId );
  143. return $this;
  144. }
  145. /**
  146. * Operação SetExpressCheckout da API PayPal Express Checkout
  147. * @param string $returnURL
  148. * @param string $cancelURL
  149. * @throws InvalidArgumentException
  150. */
  151. public function setExpressCheckout( $returnURL , $cancelURL ) {
  152. if ( filter_var( $returnURL , FILTER_VALIDATE_URL ) &&
  153. filter_var( $cancelURL , FILTER_VALIDATE_URL ) ) {
  154. $this->setParam( 'METHOD' , 'SetExpressCheckout' );
  155. $this->setParam( 'RETURNURL' , $returnURL );
  156. $this->setParam( 'CANCELURL' , $cancelURL );
  157. return $this;
  158. } else {
  159. throw new InvalidArgumentException( 'URL inválida' );
  160. }
  161. }
  162. /**
  163. * Define um parâmetro que será enviado na requisição
  164. * @param string $name
  165. * @param string $value
  166. */
  167. public function setParam( $name , $value ) {
  168. $this->params[ $name ] = $value;
  169. }
  170. }