Paypal html 按钮的自定义 字段有 256 个字符的限制。有没有办法增加该限制或是否有其他我可以使用的字段(如 custom1、custom2、其他)等...
谢谢
最佳答案
不是通过自定义字段发送全部数据,而是将数据保存在数据库中,然后发送记录 ID。在 ipn/cancel 上,检索 ID 并更新/删除记录。
为此,首先您需要更改按钮代码以发布到您自己网站上的 php 文件而不是 paypal,因此常规按钮代码:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="seller@designerfotos.com">
<input type="hidden" name="item_name" value="hat">
<input type="hidden" name="item_number" value="123">
<input type="hidden" name="amount" value="15.00">
<input type="image" name="submit" border="0"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
alt="PayPal - The safer, easier way to pay online">
</form>
变成:
<form action="buttonhandler.php" method="post">
<input type="hidden" name="item_number" value="123">
<input type="image" name="submit" border="0"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
alt="PayPal - The safer, easier way to pay online">
</form>
请注意,缺少一些字段 - cmd、business、item_name 和 amount,因为我们将在 php 中生成这些字段。
您可以在按钮 html 中定义金额,但最好在您的数据库中定义它,然后您可以自动拒绝用户支付错误金额的订单(通过摆弄发送到 paypal 的数据 -他们目前可以用你的普通 html 按钮系统做的事情)。
在 php 文件中,您收集产品信息,将订单保存到数据库,并生成通常包含在按钮表单字段中的 paypal 数据
//buttonhandler.php
$item_number = $_POST['item_number'];
//get item name, price from DB
//Note made up ORM code here for brevity -
//use whatever db acccess method you usually do:
$item = Items::getOne($item_number);
//save order in db, and retrieve order id. You can save whatever you need into the order,
//this is a simple example that just takes item number, amount and timestamp
Orders::add($item->number, $item->amount, time());
$orderId = Orders::lastInsertId();
//create paypal data
$paypalData=array(
'business'=>'seller@designerfotos.com',
'cmd'=>'_xclick',
'notify_url'=>'http://yoursite.com/1hd-ff-ipn.php', //call this something random, you dont want it getting hit by web bots
'return'=>'http://yoursite.com/thanks-for-your-order.php',
'cancel_return'=>'http://yoursite.com/cancel.php?orderid=' . $orderId,
'amount'=>$item->amount,
'currency_code'=>'GBP',
'item_number'=>$item->number,
'item_name'=>$item->name,
'custom'=>$orderId
);
//build a query string and redirect to paypal
$query_string = http_build_query($paypalData);
header("Location: https://www.paypal.com/cgi-bin/webscr?" . $query_string);
//done
die();
现在您可以在 ipn 脚本中根据 orderid 交叉检查价格:
//1hd-ff-ipn.php
$order = Orders::getOne($_POST['custom']);
if ($_POST['mc_gross'] != $order->amount) {
//price mismatch, handle accordingly
}
//more checks here as required, then
$order->paymentStatus = 'complete';
$order->save();
并在您的取消页面中删除订单
//cancel.php
Orders::delete($_GET['orderid');
?>
<h1>Sorry you cancelled</h1>
您还可以每小时/每天/任何时间运行一个 crom 来处理放弃的订单
//cron.php
//delete pending older than 1 day,
Orders::deleteWhere('status = ? and ordered_on <?','pending', time() - (24 * 60 * 60));
关于php - Paypal html按钮自定义字段限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924175/