简体   繁体   English

付款重定向至成功网址后,贝宝IPN无法正常工作

[英]PayPal IPN not working after payment redirect to success URL

I am working on localhost. 我在本地主机上工作。 I have successfully implemented Payza on my website and not I have headed towards integrating Paypal too. 我已经在我的网站上成功实现了Payza,但我也没有打算整合Paypal。 I downloaded a code from some website (don't remember from where) and tried testing it on localhost as received before I could modify and implement it on my website as per my requirement. 我从某个网站(不记得从哪里)下载了代码,并尝试在收到的本地主机上对其进行测试,然后才能根据需要在自己的网站上进行修改和实现。 The code seems to work on all the steps rather than receiving the IPN data sent to my URL. 该代码似乎适用于所有步骤,而不是接收发送到我的URL的IPN数据。 Since I am using localhost for development, I am using ngrok for testing purposes (to run my localhost on internet). 由于我使用本地主机进行开发,因此我将ngrok用于测试目的(以便在Internet上运行本地主机)。 Below are the two pages. 以下是两个页面。

index.php 的index.php

<?php
$data=array(
'merchant_email'=>'shubhamjha900@gmail.com',
'product_name'=>'Demo Product',
'amount'=>5.50,
'currency_code'=>'USD',
'thanks_page'=>'http://229a1c8a.ngrok.io/sites/paypal3/success.php',
'notify_url'=>'http://229a1c8a.ngrok.io/sites/paypal3/ipn.php',
'cancel_url'=>'http://229a1c8a.ngrok.io/sites/paypal3/cancel.php',
'paypal_mode'=>true
);
if(isset($_POST['pay_now'])){
echo '<link rel="stylesheet" type="text/css" href="style.css" />';
echo '<div class="wait">PayPal is processing the payment, please wait...</div>';
echo '<div class="loader">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>';
echo infotutsPaypal($data);
}else{
?>
<html>
        <title>Integrate PayPal Payment gateway in php - InfoTuts</title>
        <head>
        <link rel="stylesheet" type="text/css" href="style.css" />
        </head>
        <div id="mhead"><h2>Integrate PayPal Payment gateway in PHP - <span class="red">Info</span>Tuts</h2></div>
        <div id="product">
        <form id='paypal-info' method='post' action='#'>
        <label>Product Name : <?php echo $data['product_name']; ?></label></br>
        <label>Product Price : <?php echo $data['amount'].''.$data['currency_code']; ?></label>
        <input type='submit' name='pay_now' id='pay_now' value='Pay' />
        </form>
        </div>
</html>
<?php
}
    function infotutsPaypal( $data) {

            define( 'SSL_URL', 'https://www.paypal.com/cgi-bin/webscr' );
            define( 'SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr' );

            $action = '';
            //Is this a test transaction?
            $action = ($data['paypal_mode']) ? SSL_SAND_URL : SSL_URL;

            $form = '';

            $form .= '<form name="frm_payment_method" action="' . $action . '" method="post">';
            $form .= '<input type="hidden" name="business" value="' . $data['merchant_email'] . '" />';
            // Instant Payment Notification & Return Page Details /
            $form .= '<input type="hidden" name="notify_url" value="' . $data['notify_url'] . '" />';
            $form .= '<input type="hidden" name="cancel_return" value="' . $data['cancel_url'] . '" />';
            $form .= '<input type="hidden" name="return" value="' . $data['thanks_page'] . '" />';
            $form .= '<input type="hidden" name="rm" value="2" />';
            // Configures Basic Checkout Fields -->
            $form .= '<input type="hidden" name="lc" value="" />';
            $form .= '<input type="hidden" name="no_shipping" value="1" />';
            $form .= '<input type="hidden" name="no_note" value="1" />';
            // <input type="hidden" name="custom" value="localhost" />-->
            $form .= '<input type="hidden" name="currency_code" value="' . $data['currency_code'] . '" />';
            $form .= '<input type="hidden" name="page_style" value="paypal" />';
            $form .= '<input type="hidden" name="charset" value="utf-8" />';
            $form .= '<input type="hidden" name="item_name" value="' . $data['product_name'] . '" />';
            $form .= '<input type="hidden" value="_xclick" name="cmd"/>';
            $form .= '<input type="hidden" name="amount" value="' . $data['amount'] . '" />';

            $form .= '</form>';
            $form .= '<script>';
            $form .= 'setTimeout("document.frm_payment_method.submit()", 0);';
            $form .= '</script>';
            return $form;
        }

ipn.php ipn.php

<?php
class PayPal_IPN {
  function infotuts_ipn($im_debut_ipn) {
      define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
      define('SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
      $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
      if (!preg_match('/paypal\.com$/', $hostname)) {
          $ipn_status = 'Validation post isn\'t from PayPal';
          if ($im_debut_ipn == true) {
                // mail test
          }
          return false;
      }

    // parse the paypal URL
      $paypal_url = ($_REQUEST['test_ipn'] == 1) ? SSL_SAND_URL : SSL_P_URL;
      $url_parsed = parse_url($paypal_url);

    $post_string = '';
      foreach ($_REQUEST as $field => $value) {
          $post_string .= $field . '=' . urlencode(stripslashes($value)) . '&';
      }
      $post_string.="cmd=_notify-validate"; // append ipn command
      // get the correct paypal url to post request to
      $paypal_mode_status = $im_debut_ipn; //get_option('im_sabdbox_mode');
      if ($paypal_mode_status == true)
          $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
      else
          $fp = fsockopen('ssl://www.paypal.com', "443", $err_num, $err_str, 60);

      $ipn_response = '';

      if(!$fp){
        // could not open the connection.  If loggin is on, the error message
        // will be in the log.
        $ipn_status = "fsockopen error no. $err_num: $err_str";
        if ($im_debut_ipn == true) {
            echo 'fsockopen fail';
        }
        return false;
      }else{
        // Post the data back to paypal
        fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
        fputs($fp, "Host: $url_parsed[host]\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($post_string) . "\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $post_string . "\r\n\r\n");
        // loop through the response from the server and append to variable
        while (!feof($fp)) {
            $ipn_response .= fgets($fp, 1024);
        }
        fclose($fp); // close connection
      }
      // Invalid IPN transaction.  Check the $ipn_status and log for details.
      if (!preg_match("/VERIFIED/s", $ipn_response)) {
          $ipn_status = 'IPN Validation Failed';
          if ($im_debut_ipn == true) {
              echo 'Validation fail';
              print_r($_REQUEST);
          }
          return false;
      }else{
          $ipn_status = "IPN VERIFIED";
          if ($im_debut_ipn == true) {
              echo 'SUCCESS';
        }
      return true;
      }
  }
    function ipn_response($request){
    mail("shubhamjha1000@gmail.com","My subject",print_r($request,true));
    $im_debut_ipn=true;
     if ($this->infotuts_ipn($im_debut_ipn)) {
             // if paypal sends a response code back let's handle it
       if ($im_debut_ipn == true) {
            $sub = 'PayPal IPN Debug Email Main';
            $msg = print_r($request, true);
            $aname = 'infotuts';
          //mail send
        }
                // process the membership since paypal gave us a valid +
        $this->insert_data($request);
      }
    }
  function issetCheck($post,$key){
    if(isset($post[$key])){
      $return=$post[$key];
    }else{
      $return='';
    }
    return $return;
  }
  function insert_data($request){
    require_once('dbconnect.php');
    $post=$request;
    $item_name=$this->issetCheck($post,'item_name');
    $amount=$this->issetCheck($post,'mc_gross');
    $currency=$this->issetCheck($post,'mc_currency');
    $payer_email=$this->issetCheck($post,'payer_email');
    $first_name=$this->issetCheck($post,'first_name');
    $last_name=$this->issetCheck($post,'last_name');
    $country=$this->issetCheck($post,'residence_country');
    $txn_id=$this->issetCheck($post,'txn_id');
    $txn_type=$this->issetCheck($post,'txn_type');
    $payment_status=$this->issetCheck($post,'payment_status');
    $payment_type=$this->issetCheck($post,'payment_type');
    $payer_id=$this->issetCheck($post,'payer_id');
    $create_date=date('Y-m-d H:i:s');
    $payment_date=date('Y-m-d H:i:s');

    mysqli_query($con,"INSERT INTO infotuts_transection_tbl(item_name,payer_email,first_name,last_name,amount,currency,country,txn_id,txn_type,payer_id,payment_status,payment_type,create_date,payment_date)
    VALUES('$item_name','$payer_email','$first_name','$last_name','$amount','$currency','$country','$txn_id','$txn_type','$payer_id','$payment_status','$payment_type','$create_date','$payment_date')");
    mysqli_close($con);
  }
}
$obj = New PayPal_IPN();
$obj->ipn_response($_REQUEST);
?>

IPN will not receive in localhost, it will receive only in domain or live site. IPN将不会在本地主机中接收,它将仅在域或活动站点中接收。 if you want to check IPN code, 如果要检查IPN代码,

  1. login in to sandbox.paypal.com 登录到sandbox.paypal.com
  2. go IPN history ,click any one history , you will get IPN full url. 进入IPN历史记录,单击任何一个历史记录,您将获得IPN完整URL。
  3. using postman send request to your localhost. 使用邮递员将请求发送到您的本地主机。 Now you can validate IPN response 现在您可以验证IPN响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM