如何显示以前通过PayPal为每个客户下达的订单

时间:2014-06-24 17:04:06

标签: php mysql paypal

我正在做我的毕业设计电子商务网站http://www.fieldhockey-world.co.uk,我希望用户能够通过PayPal查看他们以前的订单。我设法集成PayPal,但我尝试从 用户 创建 ForeignKey 交易 但我无法获取数据,因为用户被重定向到PayPal网站购买商品。我的数据库表如下: 交易表

  1. id int(11) PK
    product_id_array varchar(255)
    payer_email varchar(255)
    first_name varchar(255)
    last_name varchar(255)
    payment_date varchar(255)
    mc_gross varchar(255)
    payment_currency varchar(255)
    txn_id varchar(255) UNIQUE
    receiver_email varchar(255)
    payment_type varchar(255)
    payment_status varchar(255)
    txn_type varchar(255)
    payer_status varchar(255)
    address_street varchar(255)
    address_city varchar(255)
    address_state varchar(255)
    address_zip varchar(255)
    address_country varchar(255)
    address_status varchar(255)
    notify_version varchar(255)
    verify_sign varchar(255)
    payer_id varchar(255)
    mc_currency varchar(255)
    mc_fee varchar(255)
  2. **

    1. USERS TABLE

      user_id int(11) PK
      用户名 varchar(32)
      密码 varchar(32)
      first_name varchar(32)
      last_name varchar(32)
      电子邮件 varchar(80) UNIQUE email_code varchar(32)
      有效 int(11)
      type int(11 ** )**
      date_of_birth 日期
      street_address varchar(80)
      city varchar(80)
      post_code varchar(6)

    2. 如果您有任何想法,请告诉我。谢谢!

      MY_IPN 代码

      <?php
      
      
      // Check to see there are posted variables coming into the script
      if ($_SERVER['REQUEST_METHOD'] != "POST") die ("No Post Variables");
      // Initialize the $req variable and add CMD key value pair
      $req = 'cmd=_notify-validate';
      // Read the post from PayPal
      foreach ($_POST as $key => $value) {
          $value = urlencode(stripslashes($value));
          $req .= "&$key=$value";
      }
      // Now Post all of that back to PayPal's server using curl, and validate everything with PayPal
      // We will use CURL instead of PHP for this for a more universally operable script (fsockopen has issues on some environments)
      //$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
      $url = "https://www.paypal.com/cgi-bin/webscr";
      $curl_result=$curl_err='';
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req)));
      curl_setopt($ch, CURLOPT_HEADER , 0);   
      curl_setopt($ch, CURLOPT_VERBOSE, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_TIMEOUT, 30);
      $curl_result = @curl_exec($ch);
      $curl_err = curl_error($ch);
      curl_close($ch);
      
      $req = str_replace("&", "\n", $req);  // Make it a nice list in case we want to email it to ourselves for reporting
      
      // Check that the result verifies
      if (strpos($curl_result, "VERIFIED") !== false) {
          $req .= "\n\nPaypal Verified OK";
      } else {
          $req .= "\n\nData NOT verified from Paypal!";
          mail("bogomilpavlov@yahoo.com", "IPN interaction not verified", "$req", "From: 
          bogomilpavlov@yahoo.com" );
          exit();
      }
      
      $txn_id = $_POST['txn_id'];
      $payer_email = $_POST['payer_email'];
      $custom = $_POST['custom'];
      $first_name = $_POST['first_name'];
      $last_name = $_POST['last_name'];
      $payment_date = $_POST['payment_date'];
      $mc_gross = $_POST['mc_gross'];
      $payment_currency = $_POST['payment_currency'];
      $payment_type = $_POST['payment_type'];
      $payment_status = $_POST['payment_status'];
      $txn_type = $_POST['txn_type'];
      $payer_status = $_POST['payer_status'];
      $address_street = $_POST['address_street'];
      $address_city = $_POST['address_city'];
      $address_state = $_POST['address_state'];
      $address_zip = $_POST['address_zip'];
      $address_country = $_POST['address_country'];
      $address_status = $_POST['address_status'];
      $notify_version = $_POST['notify_version'];
      $verify_sign = $_POST['verify_sign'];
      $payer_id = $_POST['payer_id'];
      $mc_currency = $_POST['mc_currency'];
      $mc_fee = $_POST['mc_fee']; 
      
      // Place the transaction into the database
      $sql = mysql_query("INSERT INTO transactions (product_id_array, payer_email, first_name, last_name, payment_date, mc_gross, payment_currency, txn_id, receiver_email, payment_type, payment_status, txn_type, payer_status, address_street, address_city, address_state, address_zip, address_country, address_status, notify_version, verify_sign, payer_id, mc_currency, mc_fee) 
         VALUES('$custom','$payer_email','$first_name','$last_name','$payment_date','$mc_gross','$payment_currency','$txn_id','$receiver_email','$payment_type','$payment_status','$txn_type','$payer_status','$address_street','$address_city','$address_state','$address_zip','$address_country','$address_status','$notify_version','$verify_sign','$payer_id','$mc_currency','$mc_fee')") or die ("unable to execute the query");
      
      mysql_close();
      // Mail yourself the details
      mail("bogomilpavlov93@gmail.com", "NORMAL IPN RESULT!", $req, "From: bogomilpavlov@yahoo.com");
      ?>
      

      CART 代码

      <?php 
      // Connect to the MySQL database 
      include("storescripts/init.php");
      protect_page();
      include("includes/template_head.php");
      
      
      session_start(); // Start session first thing in script
      
      // Script Error Reporting (cart error!!!!)
      //error_reporting(E_ALL);
      //ini_set('display_errors', '1');
      
      ?>
      <?php 
      //Section 1 (if user attempts to add something to the cart from the product page)
      if (isset($_POST['pid'])) {
          $pid = $_POST['pid'];
          $wasFound = false;
          $i = 0;
          // If the cart session variable is not set or cart array is empty
          if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
              // RUN IF THE CART IS EMPTY OR NOT SET
              $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
          } else {
              // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
              foreach ($_SESSION["cart_array"] as $each_item) { 
                    $i++;
                    while (list($key, $value) = each($each_item)) {
                        if ($key == "item_id" && $value == $pid) {
                            // That item is in cart already so let's adjust its quantity using array_splice()
                            array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                            $wasFound = true;
                        } // close if condition
                    } // close while loop
                 } // close foreach loop
                 if ($wasFound == false) {
                     array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
                 }
          }
          //header("location: cart.php"); 
      
      }
      ?>
      <?php 
      //Section 2 (if user chooses to empty their shopping cart)
      if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
          unset($_SESSION["cart_array"]);
      }
      ?>
      <?php 
      //Section 3 (if user chooses to adjust item quantity)
      if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
          // execute some code
          $item_to_adjust = $_POST['item_to_adjust'];
          $quantity = $_POST['quantity'];
          $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
          if ($quantity >= 100) { $quantity = 99; }
          if ($quantity < 1) { $quantity = 1; }
          if ($quantity == "") { $quantity = 1; }
          $i = 0;
          foreach ($_SESSION["cart_array"] as $each_item) { 
                    $i++;
                    while (list($key, $value) = each($each_item)) {
                        if ($key == "item_id" && $value == $item_to_adjust) {
                            // That item is in cart already so let's adjust its quantity using array_splice()
                            array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                        } // close if condition
                    } // close while loop
          } // close foreach loop
      }
      ?>
      <?php 
      //Section 4 (if user wants to remove an item from cart)
      if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
          // Access the array and run code to remove that array index
          $key_to_remove = $_POST['index_to_remove'];
          if (count($_SESSION["cart_array"]) <= 1) {
              unset($_SESSION["cart_array"]);
          } else {
              unset($_SESSION["cart_array"]["$key_to_remove"]);
              sort($_SESSION["cart_array"]);
          }
      }
      ?>
      <?php 
      //Section 5  (render the cart for the user to view on the page)
      $cartOutput = "";
      $cartTotal = "";
      global $cartTotal;
      $pp_checkout_btn = '';
      $product_id_array = '';
      if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
          $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
      } else {
      
      
          // Start the For Each loop
      $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
          <input type="hidden" name="cmd" value="_cart">
          <input type="hidden" name="upload" value="1">
          <input type="hidden" name="business" value="bogomilpavlov@yahoo.com">';
          $i = 0; 
          foreach ($_SESSION["cart_array"] as $each_item) { 
              $item_id = $each_item['item_id'];
              $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
              while ($row = mysql_fetch_array($sql)) {
                  $product_name = $row["product_name"];
                  $price = $row["price"];
                  $details = $row["details"];
              }
              $pricetotal = $price * $each_item['quantity'];
      
              $cartTotal = $pricetotal + $cartTotal;
      
              $pricetotal = money_format("%10.2n", $pricetotal);
              // dynamic checkout buttom assembly
              $x = $i + 1;
              $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
              <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
              <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
              // Create the product array variable
              $product_id_array .= "$item_id-".$each_item['quantity'].",";
      
              // Dynamic table row assembly
              $cartOutput .= "<tr>";
              $cartOutput .= '<td align="center" bgcolor="#F9F9F9"><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="100" height="80" border="1" align="center" /></td>';
              $cartOutput .= '<td bgcolor="#F9F9F9">' . $details . '</td>';
              $cartOutput .= '<td bgcolor="#F9F9F9">&pound;' . $price . '</td>';
              $cartOutput .= '<td bgcolor="#F9F9F9"><form action="cart.php" method="post">
              <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
              <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
              <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
              </form></td>';
              ;
              $cartOutput .= '<td bgcolor="#F9F9F9">&pound;' . $pricetotal . '</td>';
              $cartOutput .= '<td bgcolor="#F9F9F9"><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
              $cartOutput .= '</tr>';
              $i++; 
          } 
      
          $cartTotal = money_format("%10.2n", $cartTotal);
      
          //finish paypal checkout system
          $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
          <input type="hidden" name="notify_url" value="https://www.fieldhockey-world.co.uk/storescripts/my_ipn.php">
          <input type="hidden" name="return" value="https://www.fieldhockey-world.co.uk/checkout_complete.php">
          <input type="hidden" name="rm" value="2">
          <input type="hidden" name="cbt" value="Return to The Store">
          <input type="hidden" name="cancel_return" value="https://www.fieldhockey-world.co.uk/paypal_cancel.php">
          <input type="hidden" name="lc" value="GB">
          <input type="hidden" name="currency_code" value="GBP">
          <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/x-click-but6.gif" name="submit" alt="Make payments with PayPal -     it\'s fast, free and secure!" >
          </form>';
      
      }
      
      ?>
      

      这有点长,但这就是我所拥有的......

0 个答案:

没有答案