在 PayPal 按钮单击上解析表单数据?
Posted
技术标签:
【中文标题】在 PayPal 按钮单击上解析表单数据?【英文标题】:Parse form data on PayPal Button click? 【发布时间】:2017-06-29 12:44:01 【问题描述】:我有一堆数据通过隐藏的输入字段发送到贝宝网关。但是,我想将表单中的数据解析到我的数据库中,同时将用户发送到 PayPal 支付系统。我有以下 php 块来回显 PayPal 按钮,但是在包含“type='submit'”输入字段来解析表单数据时遇到了麻烦。一旦用户单击 PAyPal 按钮,表单数据就不会发送到我的数据库。用户单击 PayPal 按钮后,如何将表单数据发送到我的数据库?
<?php
//////////////////////////////////////////////////////////////////////////////
// Section 5 (render the cart for the user to view on the page) //
//////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$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 PayPal Checkout Button
$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="hyperesale@gmail.com">';
//Start the For Each loop
$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;
setlocale(LC_MONETARY, "en_US");
$pricetotal = money_format("%10.2n", $pricetotal);
//Dynamic Checkout Btn 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 style="border-top: none">';
$cartOutput .= '<td><img src="inventory_images/' . $item_id . '.jpg" /><br /><a href="product.php?id=' . $item_id . '">' . $product_name . '</a></td>';
$cartOutput .= '</tr>';
$i++;
setlocale(LC_MONETARY, "en_US");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px; color: black' align='right'><strong>Cart Total : ".$cartTotal." USD </strong></div>";
//Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.hyperesale.com/storescripts/paypalipn.php">
<input type="hidden" name="return" value="https://www.hyperesale.com/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.hyperesale.com/home/index.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" name="submit" >
</form>
';
?>
<?php
// Parse the form data and add inventory item to the system
if(isset($_POST['fName']))
$id = mysql_real_escape_string($_POST['id']);
$fName = mysql_real_escape_string($_POST['fName']);
$lName = mysql_real_escape_string($_POST['lName']);
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zipCode = mysql_real_escape_string($_POST['zipCode']);
$pNumber = mysql_real_escape_string($_POST['pNumber']);
$SHIPPINGfName = mysql_real_escape_string($_POST['SHIPPINGfName']);
$SHIPPINGlName = mysql_real_escape_string($_POST['SHIPPINGlName']);
$SHIPPINGaddress = mysql_real_escape_string($_POST['SHIPPINGaddress']);
$SHIPPINGcity = mysql_real_escape_string($_POST['SHIPPINGcity']);
$SHIPPINGstate = mysql_real_escape_string($_POST['SHIPPINGstate']);
$SHIPPINGzipCode = mysql_real_escape_string($_POST['SHIPPINGzipCode']);
$SHIPPINGpNumber = mysql_real_escape_string($_POST['SHIPPINGpNumber']);
$cardType = mysql_real_escape_string($_POST['cardType']);
$cardName = mysql_real_escape_string($_POST['cardName']);
$cardNumber = mysql_real_escape_string($_POST['cardNumber']);
$expireDate = mysql_real_escape_string($_POST['expireDate']);
$securityCode = mysql_real_escape_string($_POST['securityCode']);
$email = mysql_real_escape_string($_POST['email']);
//Add this product into the database now
$sql = mysql_query("INSERT INTO checkoutInfo (id, fName, lName, address, city, state, zipCode, pNumber, SHIPPINGfName, SHIPPINGlName, SHIPPINGaddress, SHIPPINGcity, SHIPPINGstate, SHIPPINGzipCode, SHIPPINGpNumber, cardType, cardName, cardNumber, expireDate, securityCode, email)
VALUES('$id','$fName','$lName','$address','$city','$state','$zipCode','$pNumber','$SHIPPINGfName','$SHIPPINGlName','$SHIPPINGaddress','$SHIPPINGcity','$SHIPPINGstate','$SHIPPINGzipCode','$SHIPPINGpNumber','$cardType','$cardName','$cardNumber','$expireDate','$securityCode','$email'") or die (mysql_error());
$pid = mysql_insert_id();
?>
【问题讨论】:
【参考方案1】:我不确定您的代码中发生了什么,因为您没有显示 $_POST['cardNumber'] 等的来源。但是无论如何,您不能同时发布到贝宝服务器和您的服务器来更新您的数据库。您可以执行以下操作:
1) POST 到服务器上的脚本,在该脚本中验证数据并更新数据库,并输出包含上述表单的页面并在加载时提交表单,例如 onload="javascript:document.myform.submit ()"
2) 当用户点击一个按钮时,做一些 AJAX 事情来 POST 到你的服务器,然后提交表单。
让我知道这是否有意义。
【讨论】:
以上是关于在 PayPal 按钮单击上解析表单数据?的主要内容,如果未能解决你的问题,请参考以下文章
在 wordpress 网站中提交带有 paypal 按钮的表单数据