Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form id="wps-bn" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" >
- <!--Tipo do botão-->
- <input type="hidden" name="cmd" value="_xclick" />
- <!--Vendedor e URL de retorno, cancelamento e notificação-->
- <input type="hidden" name="business" value="atendimento-facilitator@meusite.com" />
- <input type="hidden" name="return" value="http://meusite/paypal/ipn" />
- <input type="hidden" name="cancel" value="http://meusite/action/cancelamento" />
- <input type="hidden" name="notify_url" value="http://meusite/notificacao" />
- <!--Internacionalização e localização da página de pagamento-->
- <input type="hidden" name="charset" value="utf-8" />
- <input type="hidden" name="lc" value="BR" />
- <input type="hidden" name="country_code" value="BR" />
- <input type="hidden" name="currency_code" value="BRL" />
- <!--Informações sobre o produto e seu valor-->
- <input type="hidden" name="amount" value="12.34" />
- <input type="hidden" name="item_name" value="Servico" />
- <input type="hidden" name="quantity" value="1" />
- <!--Botão para submissão do formulário-->
- <input type="image" src="https://www.paypalobjects.com/pt_BR/BR/i/btn/btn_buynowCC_LG.gif" border="0" />
- $total = '150.95'; //VALOR TOTAL DA COMPRA
- $id_compra = '123'; //ID DA COMPRA
- $nvp = array(
- 'PAYMENTREQUEST_0_AMT' => $total,
- 'PAYMENTREQUEST_0_CURRENCYCODE' => 'BRL',
- 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
- 'PAYMENTREQUEST_0_CUSTOM' => $id_compra,
- 'L_PAYMENTREQUEST_0_NAME0' => 'Pedido número '.$id_compra,
- 'L_PAYMENTREQUEST_0_DESC0' => 'Pagamento referente ao pedido '.$id_compra,
- 'L_PAYMENTREQUEST_0_AMT0' => $total,
- 'L_PAYMENTREQUEST_0_QTY0' => 1,
- 'L_PAYMENTREQUEST_0_ITEMAMT' => $total,
- // 'RETURNURL' => 'http://beta.socioteca2014.com/advertisement/retorno',
- 'RETURNURL' => 'meusite.com/paypal/ipn',
- 'CANCELURL' => 'https://www.seusite.com.br/cancelamento.php',
- 'METHOD' => 'SetExpressCheckout',
- 'VERSION' => '108.0',
- 'PWD' => '',
- 'USER' => '',
- 'SIGNATURE' => '',
- 'CMD' => '_xclick'
- );
- $curl = curl_init();
- curl_setopt( $curl , CURLOPT_URL , 'https://api-3t.sandbox.paypal.com/nvp' );
- curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
- curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
- curl_setopt( $curl , CURLOPT_POST , 1 );
- curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query( $nvp ) );
- $response = urldecode( curl_exec( $curl ) );
- $responseNvp = array();
- curl_close( $curl );
- if ( preg_match_all( '/(?<name>[^=]+)=(?<value>[^&]+)&?/' , $response , $matches ) ) {
- foreach ( $matches[ 'name' ] as $offset => $name ) {
- $responseNvp[ $name ] = $matches[ 'value' ][ $offset ];
- }
- }
- if ( isset( $responseNvp[ 'ACK' ] ) && $responseNvp[ 'ACK' ] == 'Success' ) {
- $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
- $query = array(
- 'cmd' => '_express-checkout',
- 'token' => $responseNvp[ 'TOKEN' ]
- );
- forward($paypalURL . '?' . http_build_query( $query ) );
- } else {
- echo 'Falha na transação';
- }
- $sandbox = true;
- error_log('IPN triggered: ' . print_r($_POST,1));
- $txn = (object) $_POST;
- // This IPN listener reads IPN msgs sent from PayPal, verifies the msg, and processes
- // the data by posting the IPN message fields and values to the browser screen.
- // read the IPN msg from PayPal and add 'cmd' for your verification request
- $req = 'cmd=_notify-validate';
- // append the IPN msg, in NVP format, to your verification request
- foreach ($_POST as $key => $value) {
- $value = urlencode(stripslashes($value));
- $req .= "&$key=$value";
- }
- // set up the headers for your verification request
- // POST your verification requests to PayPal (here, the Sandbox)
- $header = "POST /cgi-bin/webscr HTTP/1.0rn";
- if ($sandbox) {
- $header .= "Host: www.sandbox.paypal.com:443rn";
- $fsock = 'ssl://www.sandbox.paypal.com';
- }
- else {
- $header .= "Host: ipnpb.paypal.com:443rn"; // endpoint for Live apps
- $fsock = 'ssl://ipnpb.paypal.com';
- }
- $header .= "Content-Type: application/x-www-form-urlencodedrn";
- $header .= "Content-Length: " . strlen($req) . "rnrn";
- // open the socket
- // $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
- $fp = fsockopen ($fsock, 443, $errno, $errstr, 30);
- if (!$fp) {
- // HTTP error
- error_log("Paypal IPN ERROR:: HTTP socket error! Unable to open URL");
- die('error $fp');
- }
- else {
- // POST verification request
- fputs ($fp, $header . $req);
- while (!feof($fp)) {
- $res = fgets ($fp, 1024);
- if (strcmp ($res, "VERIFIED") == 0) {
- // trigger plugin hook for individual plugins to have an option to save the ipn first
- $result = elgg_trigger_plugin_hook('paypal', 'ipn_log', array('txn' => $txn), false);
- if (!$result) {
- // no plugins logged the transaction, log it to the site
- //
- paypal_log_ipn(elgg_get_logged_in_user_entity(), $txn);
- }
- }
- else if (strcmp ($res, "INVALID") == 0) {
- // invalid - do nothing for now
- // IPN invalid, log for manual investigation
- paypal_log_ipn(elgg_get_logged_in_user_entity(), $txn);
- }
- }
- fclose ($fp);
- }
- exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement