Paypal html按钮自定义字段限制

时间:2015-11-25 18:43:26

标签: php paypal paypal-ipn php-5.4

Paypal html按钮的自定义字段限制为256个字符。有没有办法增加这个限制,或者是否有我可以使用的其他字段(如custom1,custom2,other)等......

谢谢

2 个答案:

答案 0 :(得分:3)

不是通过自定义字段发送整个数据,而是将数据保存在数据库中,并发送记录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文件中,您收集产品信息,将订单保存到数据库,并生成通常包含在按钮表单字段中的贝宝数据

//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));

答案 1 :(得分:0)

你可以使用这些:

<input type="hidden" name="on0" value="Size">
<input type="hidden" name="on1" value="Position">

第一个选项字段名称和标签。 os0变量包含此选项字段的相应值。例如,如果on0是size,则os0可能很大。

第二个选项字段名称和标签。 os1变量包含此选项字段的相应值。例如,如果on1是彩色,则os1可以是蓝色。 您可以通过递增选项名称索引(on0到on6)来指定最多7个选项字段名称(6个带有“订阅”按钮)。

Optional for Buy Now, Add to Cart, Subscribe, Automatic Billing, and Installment Plan buttons
Not used with Donate or Buy Gift Certificate buttons