Passing and Parse Paypal Ipn Custom Field

Passing and Parse PayPal IPN Custom field

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.

Paypal IPN custom variables

You've got three main options:

  1. Use the standard custom hidden field to store all the data you need in one string. You could use JSON format or some equivalent. The variable size is limited to 256 characters, which sounds like it might be enough.

  2. Store a unique id in the custom field, and use this id to look up the other data that you've stored in your database.

  3. There are also additional option fields you can use as suggested here and explained by PayPal here:

You could possibly also use some of the standard PayPal fields for purposes other than that for which they were intended (i.e., to hide your data), but this is highly dubious and entirely unnecessary, I would imagine.

See this answer.

Paypal IPN process more than one custom variable

As I (and many others I imagine) am also facing this problem, I thought I'd share some solutions I came across.

This one raised in the PayPal Community, suggests the use of the Option Variables which appear to offer a key/value pair implementation to facilitate up to 99 vars (for the record I have not actually tried this).

The most commonly accepted solution (which I also favor) is to add all of your data to a DB record, then use the custom var to store your record ID, which can obviously be used later (e.g. via IPN) to retrieve your data.

Where to enter paypal IPN url and how to pass custom data?

Q1: IPN is deprecated. You should be using webhooks, such as CHECKOUT.ORDER.APPROVED

Q2: There does not appear to be any specific way to pass custom data.

Option 1:

What I ended up doing was using the invoice_id field in purchase_units. I tested and found that I could pass a reasonably long string of characters and they did not have to be unique. It seems a bit of a hack but it works.

When you implement the CHECKOUT.ORDER.APPROVED webhook, you can then parse the data in purchase_unit invoice_id.

Option 2:

Another idea I considered which doesn't need to hack the invoice_id, is to send data to the server using the details from the capture event in the javascript, store that custom information, along with the order id in the database, and then when the webhook occurs, cross-match the order id to the saved one to retrieve the information.

I prefer option 1, but your needs may vary.

It's pretty indicative though of how poorly designed the whole PayPal API is. There are huge gaps in the implementation, very little documentation, and zero support.

I had to implement Stripe as well. The entire implementation took me 3 days. The same PayPal implementation took me over 3 weeks! The lack of documentation, having to constantly just try things out, and the inconsistency of the sandbox implementation were real time killers.

Pass Custom data in PayPal form

There is character sending limitation in paypal so one thing you can do when the paypal form is created at that time you can insert all records in temporary table and you only pass that record id in custom field and after payment will be success you can fetch that data from db and insert original data entry.

Passing array through PayPal IPN

$_POST['custom'] is returning a value that is the String version of the array. I.e the same as if you did echo array(...). $_POST['custom'] will always be a string, which is why you are getting A when you do $custom[0].

When setting the value of the custom element you most likely want to format it in a way that you then parse the data when you receive it back from PayPal.

You could potentially use JSON as the format, or have a look at this SO solution for other options.

Using JSON the implementation would be:

<?php
$arr = array($website_ref, $user_id, $item1, $item2, $item3, $item4);
$data = json_encode($arr);
?>
<input type="hidden" name="custom" value="<?= $data ?>">

Then in IPN.php:

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

$website_ref = $custom[0];
$user_id = $custom[1];
$item1 = $custom[2];
$item2 = $custom[3];
$item3 = $custom[4];
$item4 = $custom[5];


Related Topics



Leave a reply



Submit