2014-12-03 103 views
0

我从PayPal得到了IPN脚本,并实现了我的代码,当我通过沙箱签出和购买东西时,订单详细信息不会插入到我的数据库中。对于Paypal开发人员部分,我很新颖我感谢您的帮助。数据PayPal IPN数据不进入数据库

没有被插入到数据库

如何调试的IPN脚本?

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\r\n";     // HTTP POST request 
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

    // 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']!='[email protected]'){ 
     $message= "Investigate why and how the recievers email is wrong email = ".$_POST['reciever_email']."\n\n\n$req"; 
     mail("[email protected]","Recievers email is incorrect",$message,"From: [email protected]"); 
     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']."\n\n\n$req"; 
     mail("[email protected]","Recievers email is incorrect",$message,"From: [email protected]"); 
     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 \n\n\n$req"; 
     mail("[email protected]","Duplicate txnID in the IPN system",$message,"From: [email protected]"); 
     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 = "[email protected]"; 
     $mail_To  = "[email protected]"; 
     $mail_Subject = "INVALID IPN"; 
     $mail_Body = "IPN failed to insert data".$req; 

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

?> 

购物车购物车页面

<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="[email protected]">'; 
      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

$水库返回null。经过很多故障排除之后,我发现这样做,所以对VERIFIED的IF语句测试从未执行,这就是为什么您的查询不会被执行。