2011-09-30 60 views
9

我已經設置了PayPal IPN文件。當用戶在網站上時,按提交交易的詳細信息上傳到數據庫。相關的ID通過PayPal作爲自定義字段發送。當付款完成時,IPN用於更新數據庫作爲基於ID完成的事務。傳遞和解析PayPal IPN自定義字段

一切都很好。

但是,這是一個棘手的問題。我還需要更新另一個表 - 折扣/優惠券代碼分貝。更新基於輸入的代碼以及代碼仍可使用的次數。基本上,如果它是50次,使用後一旦數據庫將更新爲49.所以我需要通過代碼,並允許其餘的使用,因此可以說更新表,其中代碼= XXXX(更新新值49等)。

我可以解決如何在自定義字段中傳遞所有這些值,但無法解決如何再次解析它們?閱讀關於將它們分離的內容:等等,但需要以前完成的人的建議。

這是IPN如何詳細信息目前回來:

$定製= $ _ POST [ '定製'];

謝謝。

+0

找到了這一位代碼.. – user718359

+0

$ temp = $ _POST ['custom']; // &和IP&visitorId&attributionInfo 列表($ ,$ ,$ custIP,$ visitorId,$ attributionInfo)=爆炸( 「&」,$溫度,5); – user718359

回答

13

我最近剛剛做到了這一點,
將您的貝寶自定義字段按照您的願望發送到數據中,在該自定義字段中使用分隔符來分割數據。 在下面的例子中,值使用「|」分開,您可以使用字符集中需要的任何字符。

$member_id = 1; 
$some_other_id = 2; 
<input type="hidden" name="custom" value="<?php echo $member_id.'|'.$some_other_id ?>"/> 

這將輸出:

<input type="hidden" name="custom" value="1|2"/> 

當你收到來自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. 

因此,基本上,我們發送一個字符串我們選擇貝寶的自定義分隔符,貝寶將在IPN響應中將其返回給我們。然後我們需要分解它(使用explode()函數),然後按照你的意願去做。

當您選擇它使用普通方法從數據庫中獲取你的價值,那麼就由1減使用它:

$val_from_db--; // Thats it!, Takes the current number, and minus 1 from it. 
+0

謝謝..現在就試試這個.. – user718359

+0

這是不是正確?應該是-1;而不是 - ;?? $ val_from_db--; – user718359

+0

完美的作品! (除了$ val_from_db部分,我還沒有測試或整理出來..現在工作.. – user718359

10

它擴展了JustAnil的解決方案。

HTML:

<input type="hidden" name="custom" value="some-id=1&some-type=2&some-thing=xyz"/> 

和您的IPN腳本將是這個樣子:

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

    echo $_CUSTOMPOST['some-id']; 
    echo $_CUSTOMPOST['some-type']; 
    echo $_CUSTOMPOST['some-price']; 
?> 

您可能要仔細檢查parse_str上導致數組元素進行urldecode。

+0

這會中斷返回的NVP值,因爲它將以「&custom = some-id = 1&some-type = 2&some-thing = xyz」的形式返回。 –

+0

這樣會更好另一個分隔符,例如|或,在自定義字段中,然後使用爆炸來返回一個數組。 –

+0

非常優雅的解決方案。它像一個魅力 – MrD

0

下面是一個例子使用JSON

<?php 
    $arr = array($member_id, $coupon); 
    $data = json_encode($arr); 
?> 
<input type="hidden" name="custom" value="<?= $data ?>"/> 

然後在另一側上:

$custom = json_decode($_POST['custom'], true); 
$member_id = $custom[0]; 
$coupon = $custom[1]; 

也可以解析關聯數組太:

<?php 
    $arr = array('id' => $member_id, 'coupon' => $coupon); 
    $data = json_encode($arr); 
?> 
<input type="hidden" name="custom" value="<?= $data ?>"/> 

然後在其他方:

$custom = json_decode($_POST['custom'], true); 
$member_id = $custom['id']; 
$coupon = $custom['coupon']; 

當使用JSON解析數據時,有一個很好的對稱性。