Passing and Parse PayPal IPN Custom field

I have setup a PayPal IPN file. When the user is at the site and press submit details about the transaction is uploaded to the db. The relevant id is sent via PayPal as the custom field. When payment complete IPN used to update DB as transaction completed based on id.

All is fine.

However, this is the tricky bit. I also need to update another table – a discount/coupon code db. The update is based on the code entered and also the number of times the code can still be used. Basically if it was 50 times, after used once the db would be updated with 49. So I need to pass the code and the remaining uses allowed so can say update table where code = XXXX (update new value of 49 etc).

I can work out how to pass all these values in the custom field, but cannot work out how to parse them out again? Read about separating them with : etc, but need some advice from someone who has done before.

This is how IPN details currently comes back:

$custom = $_POST[‘custom’];

Thank you.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I did just this recently,
Send your paypal custom field to data as your would, inside that custom field, use a separator to split your data.
In the below example, the values are split using a “|”, you can use any character you want available in your charset.

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

This will output:

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

When you get the information from paypal (the IPN response) process it like so:

$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.

So basically, we send a string with a custom delimiter that we choose to paypal, paypal will return it to us in the IPN response. We then need to split it up (using the explode() function) and then do what you would like with it.

When you get your value from the database your select it using normal methods, then just decrement it by 1 using:

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

Solution 2

This expands on JustAnil’s solution.


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

and your IPN script would look something like this:


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

You may want to double check that parse_str performs urldecode on resulting array elements.

Solution 3

Here is an example using JSON:

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

Then on the other side:

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

You can also parse associative arrays too:

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

Then on the other side:

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

There’s a nice symmetry when using JSON to parse the data.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply