【问题标题】:Integrate PayPal集成贝宝
【发布时间】:2011-11-03 13:21:32
【问题描述】:

我已成功集成 PayPal。一切正常。但我希望我的表单在成功付款后重定向到我的网站。另一个问题:如何获得 PayPal 的回复? 这是我的 PayPal 表格。谢谢。

   `<form action="https://sandbox.paypal.com/cgi-bin/webscr" method="post">  
    <input type="hidden" name="business" value="savife_1314264698_biz@gmail.com ">  
    <input type="hidden" name="cmd" value="_xclick-subscriptions">  
    <input type="hidden" name="item_name" value="Alice's Weekly Digest">  
    <input type="hidden" name="item_number" value="DIG Weekly">  
    <input type="hidden" name="currency_code" value="USD">  
    <input type="hidden" name="notify_url" value="http://localhost/check.php">
    <input type="hidden" name="return" value="http://google.co.in">
    <input type="hidden" name="a3" value="5.00">  
    <input type="hidden" name="p3" value="1">  
    <input type="hidden" name="t3" value="M">  
    
    <!-- Display the payment button. -->  
    <input type="image" name="submit" border="0" src="btn_subscribe_LG.gif" alt="PayPal - The safer, easier way to pay online">  
    <img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >  
</form>

【问题讨论】:

    标签: html redirect paypal


    【解决方案1】:

    只需通过这个:

    您需要的是PAYPAL IPN,即付款完成后发送到用户网站的即时付款通知。

    https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro

    或者您也可以在购物车中设置 IPN 的路径,paypal 将发送如下通知:

    <input type="hidden" name="notify_url" value="site.com/ipn/index.php" />
    <input type="hidden" name="return"     value="site.com/ipn/index.php"/>
    

    但您需要在 paypal 上登录您的商家帐户以手动指定 ipn 和自动返回 url。在个人资料选项卡下的网站支付偏好和即时支付通知部分。

    以下我添加了成功付款后发送到您网站的示例 ipn 代码:

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    
    foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
    }
    
    // post back to PayPal system to validate
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
    
    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
    
    if (!$fp) {
    // HTTP ERROR
    } else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {
    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
    }
    else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
    }
    }
    fclose ($fp);
    }
    ?>
    

    您需要在通知 url 页面上添加此代码。您在购物车的通知 url 字段中提到的返回页面,即

    <input type="hidden" name="notify_url" value="site.com/ipn/index.php" />
    

    将此代码放入site.com/ipn/index.php

    让我们以您的购物车为例:

    <form action="https://sandbox.paypal.com/cgi-bin/webscr" method="post">  
        <input type="hidden" name="business" value="savife_1314264698_biz@gmail.com ">  
        <input type="hidden" name="cmd" value="_xclick-subscriptions">  
        <input type="hidden" name="item_name" value="Alice's Weekly Digest">  
        <input type="hidden" name="item_number" value="DIG Weekly">  
        <input type="hidden" name="currency_code" value="USD">  
        <input type="hidden" name="notify_url" value="http://localhost/check.php">
        <input type="hidden" name="return" value="http://google.co.in">
        <input type="hidden" name="a3" value="5.00">  
        <input type="hidden" name="p3" value="1">  
        <input type="hidden" name="t3" value="M">  
    
        <!-- Display the payment button. -->  
        <input type="image" name="submit" border="0" src="btn_subscribe_LG.gif" alt="PayPal - The safer, easier way to pay online">  
        <img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >  
    </form>
    

    在这种情况下,paypal 将发送 ipn 的通知 url 是 http://localhost/check.php

    所以将该代码放在check.php 页面中。收到 ipn 后,您可以进一步使用它来输入您的数据库等。

    或访问此链接以了解 paypal ipn 侦听器的概述:

    https://cms.paypal.com/uk/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNImplementation

    希望这会有所帮助。

    【讨论】:

    • 请查看我更新的答案。我已经提到了在哪里添加所有 ipn 侦听器代码。
    【解决方案2】:

    付款后,您可以使用 paypal 的 notify_url 向您网站上的脚本发送 IPN 响应。使用此隐藏字段。

    <input type="hidden" name="notify_url" value="yourwebsite.com/paypal/process" />
    

    现在当用户付款时,paypal 会向您的网站发送一个 IPN。

    对于退货,您可以使用:

    <input type="hidden" name="return" value="path.to/return/website/"/> // After successful payment
    <input type="hidden" name="cancel_return" value=""/> // If user cancels where to redirect to.
    

    【讨论】:

      【解决方案3】:

      你可以像这样设置paypal notify_url(IPN Response url)和成功url:

      <input type="hidden" name="notify_url" value="http://localhost/demo/payment.php/ipn_response">                                            
      <input type="hidden" name="return" value="http://localhost/demo/payment_successful">
      

      获取 Paypal 响应 paypal.php

           function ipn($ipn_data) {
      
          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 ($ipn_data == true) {
                  //You can send email as well
              }
              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 = $ipn_data; //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.sandbox.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 ($ipn_data == 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 ($ipn_data == true) {
                  echo 'Validation fail';
                  print_r($_REQUEST);
              }
              return false;
          } else {
              $ipn_status = "IPN VERIFIED";
              if ($ipn_data == true) {
                  echo 'SUCCESS';
                  print_r($_REQUEST);
              }
              return true;
          }
      }
      
      function ipn_response() {
          //mail("sobhagya1411@gmail.com","My subject",print_r($request,true));
          $ipn_data = true;
          if ($this->ipn($_REQUEST)) {
              $this->insert_data($_REQUEST);
          }
      }
      
      function issetCheck($post, $key) {
          if (isset($post[$key])) {
              $return = $post[$key];
          } else {
              $return = '';
          }
          return $return;
      }
      
      
       public function insert_data(){
      
           $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');
      
          $paypal_sql = "INSERT INTO ipn_data_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')";
          mysql_query($paypal_sql); 
      
      }
      

      【讨论】:

        猜你喜欢
        • 2011-08-01
        • 2016-08-18
        • 2013-05-13
        • 2011-05-24
        • 2013-07-10
        • 2015-09-08
        • 2012-10-05
        • 2012-03-13
        • 2012-05-05
        相关资源
        最近更新 更多