传递和解析PayPal IPN自定义字段

use*_*359 9 php

我已经设置了PayPal IPN文件.当用户在站点并按提交时,有关事务的详细信息将上载到数据库.相关ID通过PayPal作为自定义字段发送.当付款完成IPN用于更新数据库作为基于id的交易完成.

一切都很好.

然而,这是棘手的一点.我还需要更新另一个表 - 折扣/优惠券代码db.更新基于输入的代码以及代码仍可使用的次数.基本上,如果它是50次,使用一次后db将更新为49.所以我需要传递代码和剩余的使用允许所以可以说更新表代码= XXXX(更新新值49等).

我可以弄清楚如何在自定义字段中传递所有这些值,但无法解决如何再次解析它们?阅读将它们与以下内容分开,但需要一些以前做过的人的建议.

这就是目前IPN细节的回归:

$ custom = $ _POST ['custom'];

谢谢.

Ani*_*nil 13

我最近做了这个,
将你的paypal自定义字段发送到数据,就像在自定义字段中一样,使用分隔符来分割数据.在下面的示例中,使用"|"分割值,您可以使用charset中可用的任何字符.

$member_id = 1;
$some_other_id = 2;
<input type="hidden" name="custom" value="<?php echo $member_id.'|'.$some_other_id ?>"/>
Run Code Online (Sandbox Code Playgroud)

这将输出:

<input type="hidden" name="custom" value="1|2"/>
Run Code Online (Sandbox Code Playgroud)

当您从paypal(IPN响应)过程中获取信息时,它就像这样:

$ids = explode('|', $_POST['custom']); // Split up our string by '|'
// Now $ids is an array containing your 2 values in the order you put them.
$member_id = $ids[0]; // Our member id was the first value in the hidden custom field
$some_other_ud = $ids[1]; // some_other_id was the second value in our string.
Run Code Online (Sandbox Code Playgroud)

基本上,我们发送一个字符串,其中包含我们选择paypal的自定义分隔符,paypal将在IPN响应中将其返回给我们.然后我们需要将其拆分(使用explode()函数),然后用它做你想做的事情.

当您从数据库中获取值时,使用常规方法选择它,然后使用以下方法将其减1:

$val_from_db--; // Thats it!, Takes the current number, and minus 1 from it.
Run Code Online (Sandbox Code Playgroud)


小智 9

这扩展了JustAnil的解决方案.

HTML:

<input type="hidden" name="custom" value="some-id=1&some-type=2&some-thing=xyz"/>
Run Code Online (Sandbox Code Playgroud)

你的IPN脚本看起来像这样:

<?php
    parse_str($_POST['custom'],$_CUSTOMPOST);

    echo $_CUSTOMPOST['some-id'];
    echo $_CUSTOMPOST['some-type'];
    echo $_CUSTOMPOST['some-price'];
?>
Run Code Online (Sandbox Code Playgroud)

您可能需要仔细检查parse_str是否对结果数组元素执行urldecode.