Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. <form id="wps-bn" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" >
  2.  
  3. <!--Tipo do botão-->
  4.  
  5. <input type="hidden" name="cmd" value="_xclick" />
  6.  
  7. <!--Vendedor e URL de retorno, cancelamento e notificação-->
  8.  
  9. <input type="hidden" name="business" value="atendimento-facilitator@meusite.com" />
  10.  
  11. <input type="hidden" name="return" value="http://meusite/paypal/ipn" />
  12.  
  13. <input type="hidden" name="cancel" value="http://meusite/action/cancelamento" />
  14.  
  15. <input type="hidden" name="notify_url" value="http://meusite/notificacao" />
  16.  
  17. <!--Internacionalização e localização da página de pagamento-->
  18.  
  19. <input type="hidden" name="charset" value="utf-8" />
  20.  
  21. <input type="hidden" name="lc" value="BR" />
  22.  
  23. <input type="hidden" name="country_code" value="BR" />
  24.  
  25. <input type="hidden" name="currency_code" value="BRL" />
  26.  
  27. <!--Informações sobre o produto e seu valor-->
  28.  
  29. <input type="hidden" name="amount" value="12.34" />
  30.  
  31. <input type="hidden" name="item_name" value="Servico" />
  32.  
  33. <input type="hidden" name="quantity" value="1" />
  34.  
  35. <!--Botão para submissão do formulário-->
  36.  
  37. <input type="image" src="https://www.paypalobjects.com/pt_BR/BR/i/btn/btn_buynowCC_LG.gif" border="0" />
  38.  
  39. $total = '150.95'; //VALOR TOTAL DA COMPRA
  40. $id_compra = '123'; //ID DA COMPRA
  41.  
  42. $nvp = array(
  43. 'PAYMENTREQUEST_0_AMT' => $total,
  44. 'PAYMENTREQUEST_0_CURRENCYCODE' => 'BRL',
  45. 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
  46. 'PAYMENTREQUEST_0_CUSTOM' => $id_compra,
  47. 'L_PAYMENTREQUEST_0_NAME0' => 'Pedido número '.$id_compra,
  48. 'L_PAYMENTREQUEST_0_DESC0' => 'Pagamento referente ao pedido '.$id_compra,
  49. 'L_PAYMENTREQUEST_0_AMT0' => $total,
  50. 'L_PAYMENTREQUEST_0_QTY0' => 1,
  51. 'L_PAYMENTREQUEST_0_ITEMAMT' => $total,
  52. // 'RETURNURL' => 'http://beta.socioteca2014.com/advertisement/retorno',
  53. 'RETURNURL' => 'meusite.com/paypal/ipn',
  54. 'CANCELURL' => 'https://www.seusite.com.br/cancelamento.php',
  55. 'METHOD' => 'SetExpressCheckout',
  56. 'VERSION' => '108.0',
  57. 'PWD' => '',
  58. 'USER' => '',
  59. 'SIGNATURE' => '',
  60. 'CMD' => '_xclick'
  61. );
  62.  
  63.  
  64. $curl = curl_init();
  65.  
  66. curl_setopt( $curl , CURLOPT_URL , 'https://api-3t.sandbox.paypal.com/nvp' );
  67. curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
  68. curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
  69. curl_setopt( $curl , CURLOPT_POST , 1 );
  70. curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query( $nvp ) );
  71.  
  72. $response = urldecode( curl_exec( $curl ) );
  73. $responseNvp = array();
  74.  
  75. curl_close( $curl );
  76.  
  77. if ( preg_match_all( '/(?<name>[^=]+)=(?<value>[^&]+)&?/' , $response , $matches ) ) {
  78. foreach ( $matches[ 'name' ] as $offset => $name ) {
  79. $responseNvp[ $name ] = $matches[ 'value' ][ $offset ];
  80. }
  81. }
  82.  
  83. if ( isset( $responseNvp[ 'ACK' ] ) && $responseNvp[ 'ACK' ] == 'Success' ) {
  84. $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
  85. $query = array(
  86. 'cmd' => '_express-checkout',
  87. 'token' => $responseNvp[ 'TOKEN' ]
  88. );
  89.  
  90. forward($paypalURL . '?' . http_build_query( $query ) );
  91. } else {
  92. echo 'Falha na transação';
  93. }
  94.  
  95. $sandbox = true;
  96.  
  97. error_log('IPN triggered: ' . print_r($_POST,1));
  98. $txn = (object) $_POST;
  99.  
  100. // This IPN listener reads IPN msgs sent from PayPal, verifies the msg, and processes
  101. // the data by posting the IPN message fields and values to the browser screen.
  102.  
  103. // read the IPN msg from PayPal and add 'cmd' for your verification request
  104. $req = 'cmd=_notify-validate';
  105.  
  106. // append the IPN msg, in NVP format, to your verification request
  107. foreach ($_POST as $key => $value) {
  108. $value = urlencode(stripslashes($value));
  109. $req .= "&$key=$value";
  110. }
  111.  
  112. // set up the headers for your verification request
  113. // POST your verification requests to PayPal (here, the Sandbox)
  114. $header = "POST /cgi-bin/webscr HTTP/1.0rn";
  115.  
  116. if ($sandbox) {
  117. $header .= "Host: www.sandbox.paypal.com:443rn";
  118. $fsock = 'ssl://www.sandbox.paypal.com';
  119. }
  120. else {
  121. $header .= "Host: ipnpb.paypal.com:443rn"; // endpoint for Live apps
  122. $fsock = 'ssl://ipnpb.paypal.com';
  123. }
  124.  
  125. $header .= "Content-Type: application/x-www-form-urlencodedrn";
  126. $header .= "Content-Length: " . strlen($req) . "rnrn";
  127.  
  128. // open the socket
  129. // $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
  130. $fp = fsockopen ($fsock, 443, $errno, $errstr, 30);
  131.  
  132. if (!$fp) {
  133. // HTTP error
  134. error_log("Paypal IPN ERROR:: HTTP socket error! Unable to open URL");
  135. die('error $fp');
  136. }
  137. else {
  138. // POST verification request
  139. fputs ($fp, $header . $req);
  140. while (!feof($fp)) {
  141. $res = fgets ($fp, 1024);
  142.  
  143. if (strcmp ($res, "VERIFIED") == 0) {
  144. // trigger plugin hook for individual plugins to have an option to save the ipn first
  145. $result = elgg_trigger_plugin_hook('paypal', 'ipn_log', array('txn' => $txn), false);
  146.  
  147. if (!$result) {
  148. // no plugins logged the transaction, log it to the site
  149. //
  150. paypal_log_ipn(elgg_get_logged_in_user_entity(), $txn);
  151. }
  152. }
  153. else if (strcmp ($res, "INVALID") == 0) {
  154.  
  155. // invalid - do nothing for now
  156. // IPN invalid, log for manual investigation
  157. paypal_log_ipn(elgg_get_logged_in_user_entity(), $txn);
  158. }
  159. }
  160.  
  161. fclose ($fp);
  162. }
  163. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement