我从Paypal获得了IPN脚本并实现了我的代码,当我通过沙箱结账并购买东西时,订单详细信息不会插入到我的数据库中我对Paypal开发人员部分很新,所以我很感激你的帮助。
没有数据插入数据库
如何调试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']!='brad.97@hotmail.com'){
$message= "Investigate why and how the recievers email is wrong email = ".$_POST['reciever_email']."\n\n\n$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']."\n\n\n$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 \n\n\n$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);
}
}
?>
购物车购物车页面
<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" />
<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>
答案 0 :(得分:1)
$ res返回null。我经过多次故障排除后发现,因此永远不会执行对VERIFIED的IF语句测试,这就是为什么你的查询没有被执行。