dspld86684
2014-12-03 11:06 阅读 71
已采纳

Paypal IPN数据未输入数据库

I got the IPN script from Paypal and implemented my code and when I checkout and purchase something through the sandbox the order details don't insert into my database I'm quite new to Paypal developers section so I appreciate your help.

None of the data gets inserted to the database

How do i debug the IPN script?

IPN SCRIPT

<?php

   // Send an empty HTTP 200 OK response to acknowledge receipt of the notification 
   header('HTTP/1.1 200 OK'); 

   // Build the required acknowledgement message out of the notification just received
  $req = 'cmd=_notify-validate';               // Add 'cmd=_notify-validate' to beginning of the acknowledgement

  foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
    $value = urlencode(stripslashes($value));  // Encode these values
    $req  .= "&$key=$value";                   // Add the NV pairs to the acknowledgement
  }

   // Set up the acknowledgement request headers
  $header  = "POST /cgi-bin/webscr HTTP/1.1
";                    // HTTP POST request
  $header .= "Content-Type: application/x-www-form-urlencoded
";
  $header .= "Content-Length: " . strlen($req) . "

";

  // Open a socket for the acknowledgement request
  $fp = fsockopen('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

  // Send the HTTP POST request back to PayPal for validation
  fputs($fp, $header . $req);

   while (!feof($fp)) {                     // While not EOF
    $res = fgets($fp, 1024);               // Get the acknowledgement response
    if (strcmp ($res, "VERIFIED") == 0) {  // Response contains VERIFIED - process notification

      // Authentication protocol is complete - OK to process notification contents

      // Possible processing steps for a payment include the following:

      // Check that receiver_email is your Primary PayPal email
      if($_POST['reciever_email']!='brad.97@hotmail.com'){
        $message= "Investigate why and how the recievers email is wrong email = ".$_POST['reciever_email']."


$req";  
        mail("brad.96@hotmail.com","Recievers email is incorrect",$message,"From: brad.96@hotmail.com");
        exit();
      }

      // Check that the payment_status is Completed
      if($_POST['payment_status']!="Completed"){
        $message= "Investigate why and how the payment status was not completed = ".$_POST['reciever_email']."


$req";  
        mail("brad.96@hotmail.com","Recievers email is incorrect",$message,"From: brad.96@hotmail.com");
        exit();
      }
      // Check that txn_id has not been previously processed
      $this_txn = $_POST['txn_id'];
      $sql = mysqli_query("SELECT invoiceID FROM transactions WHERE txn_ID='$this_txn' LIMIT 1");
      $numRows = mysqli_num_rows($sql);
      if($numRows > 0){
        $message= "Duplicate transaction ID occured so the IPN script was killed 


$req";  
        mail("brad.96@hotmail.com","Duplicate txnID in the IPN system",$message,"From: brad.96@hotmail.com");
        exit();
      }
      // Check that payment_amount/payment_currency are correct


      // Process payment
       // Assign payment notification values to local variables
  $payment_status   = $_POST['payment_status'];
  $payment_amount   = $_POST['mc_gross'];
  $txn_id           = $_POST['txn_id'];
  $reciever_email   = $_POST['reciever_email'];
  $payment_date     = $_POST['payment_date'];
  $payment_date     = $_POST['payment_date']; 
  $payer_id         = $_POST['payer_id'];
  $payer_email      = $_POST['payer_email'];
  $payer_firstName  = $_POST['first_name'];
  $payer_lastName   = $_POST['last_name'];
  $address          = $_POST['address_street'];
  $address_zip      = $_POST['address_zip'];
  $address_suburb   = $_POST['address_city'];
  $address_state    = $_POST['address_state'];
  $address_country  = $_POST['address_country'];

  $sql ="INSERT INTO customers(customerID,firstName,lastName,email,address,suburb,state,postCode,country,instructions)
values('$payer_ID','$payer_firstName','$payer_lastName','$payer_email','$address','$address_suburb','$address_state''$address_zip','$address_country')";
$result = mysqli_query($con, $sql) or die(mysqli_error($con)); //run the query


  $sql="INSERT INTO invoice(customerID,total,paymentStatus,dateTime,account,txnID)
  VALUES ('$payer_id','$payment_amount','$payment_status','$payment_date','$reciever_email','$txn_id')";
  $result = mysqli_query($con, $sql) or die(mysqli_error($con)); //run the query

  /*Dont worry about this section below as it is not completed */

  $invoiceID=mysqli_insert_id($con); //retrieve the last generated automatic ID
  $productID=1;
  $quantity=1;
  $sql="INSERT INTO invoice_products(invoiceID, productID, quantity)
  VALUES ('$invoiceID',$productID,$quantity)";
  $result = mysqli_query($con, $sql) or die(mysqli_error($con)); //run the query
  unset($_SESSION['cart']); //unset the 'cart' session when the order is completed 
    } 
    else if (strcmp ($res, "INVALID") == 0) { 
    //Response contains INVALID - reject notification

      // Authentication protocol is complete - begin error handling

      // Send an email announcing the IPN message is INVALID
      $mail_From    = "IPN@example.com";
      $mail_To      = "brad.96@hotmail.com";
      $mail_Subject = "INVALID IPN";
      $mail_Body    = "IPN failed to insert data".$req;

      mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
    }
  }

 ?>

Shopping cart cart page

    <div class="table-responsive">
<table class="table table-bordered">

<?php 
    if((isset($_SESSION['cart'])) && count($_SESSION['cart'])>0)  
        {
            echo '<form method="post" action="https://sandbox.paypal.com/au/cgi-bin/webscr">';
            echo '<input type="hidden" name="cmd" value="_cart">';
            echo '<input type="hidden" name="upload" value="1">';
            echo '<input type="hidden" name="business" value="brad.97@hotmail.com">';
            echo '<thead>';
            echo '<tr>';
            echo '<td><strong>Product image</strong></td>';
            echo '<td><strong>Product name</strong></td>';
            echo '<td><strong>Price</strong></td>';
            echo '<td><strong>Quantity</strong></td>';
            echo '<td><strong>Subtotal</strong></td>';
            echo '</tr>';
            echo '</thead>';

    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){ //for each product in the cart get the following
    $pid=$_SESSION['cart'][$i]['productID'];
    $q=$_SESSION['cart'][$i]['qty'];
    $pname=get_product_name($pid);
    $price=get_price($pid);
    $image=get_product_image($pid);
    if($q==0) continue;
    $product_id_array = $pid.'-'.$q ;
?>

<tr>
    <td class="fixedWidth text-center">
    <img src="../images/products/<?php echo $image;?>" class="product-image" alt="<?php echo $pname;?>"/>
    </td>

    <td>
    <h4><?php echo $pname?></h4>
    </td>

    <td>
    <h4>$<?php echo(number_format((get_price($pid)), 2, '.', ''))?></h4>
    </td>

    <td>
    <h4><?php echo $q?></h4>
    </td> 

    <td>
    <h4>$<?php echo(number_format((get_price($pid)*$q), 2, '.', ''))?></h4>
    </td>
</tr>

<?php 
    $x=$i+1;
    echo '<input type="hidden" name="item_name_'.$x.'" value="'.$pname.'">';
    echo '<input type="hidden" name="quantity_'.$x.'" value="'.$q.'">';
    echo '<input type="hidden" name="amount_'.$x.'" value="'.$price.'">';
    }

?>

<tr>
<td colspan="5"><div class="pull-right"><h3 class="h3-padding">Order Total: $ <?php
echo(number_format((get_order_total()), 2, '.', ''))?></h3></div></td>
</tr>
<tr>
<td colspan="5">
<div class="pull-right">
<input type="hidden" name="custom" value="<?php echo $product_id_array; ?>" />
<input type="hidden" name="image_url" value="http://localhost/websitename/images/logo.jpg" />
<input type="hidden" name="notify_url" value="http://localhost/websitename/pages/myipn.php" />
<input type="hidden" name="return" value="http://localhost/websitename/pages/checkoutcomplete.php" />
<input type="hidden" name="cancel_return" value="http://localhost/websitename/pages/shoppingcart.php" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="cbt" value="Return to store">
<input type="hidden" name="currency_code" value="AUD">
<input type="button" value="Return to Cart" onclick="window.location='shoppingcart.php'" class="btn btn-default" />&nbsp;
<input type="image" src="../images/paypalBtn.jpg" name="submit" class="btn" alt="Make payments with PayPal - it's fast, free and secure!" />
</form>
</div>
</td> 
</tr>

<?php
    }

else {
        echo "<tr><td>There are no items in your shopping cart!</td>";
     }
?>


</table>
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongwei3172 dongwei3172 2015-01-14 01:39

    $res returns null. I found that out after much troubleshooting, so the IF statement testing for the VERIFIED is never executed thats why your queries arent being executed.

    点赞 评论 复制链接分享

相关推荐