Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing and Parse PayPal IPN Custom field

Tags:

php

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.

like image 819
user718359 Avatar asked Sep 30 '11 11:09

user718359


People also ask

What does preparing PayPal IPN mean?

Instant Payment Notification (IPN) is a message service that notifies you of events related to PayPal transactions. You can use IPN messages to automate back-office and administrative functions, such as fulfilling orders, tracking customers, or providing status and other transaction-related information.

How does PayPal IPN work?

Instant Payment Notification (IPN) is a message service that automatically notifies merchants of events related to PayPal transactions. Merchants can use it to automate back-office and administrative functions, including automatically fulfilling orders and providing customers with order status.

How do I enable IPN in PayPal?

Click the settings icon at the top of your PayPal account page and then click Account Settings. On the Notifications page, click the Update link for the Instant payment notifications item. Click Choose IPN Settings to specify your listener's URL and activate the listener.


3 Answers

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.
like image 196
Anil Avatar answered Oct 22 '22 20:10

Anil


This expands on JustAnil's solution.

HTML:

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

and your IPN script would look something like this:

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

    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.

like image 20
DanR Avatar answered Oct 22 '22 19:10

DanR


Here is an example using JSON:

<?php
  $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:

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

like image 42
br3nt Avatar answered Oct 22 '22 19:10

br3nt