44

Ive been using express checkout API to convert people's accounts on my site to premium accounts after paying. The only problem with it is that it doesn't send the user back to the site until they click the button to return, and it updates their permission when that happens. About 40% of the users don't seem to do that.... so their accounts never get credited after payment.

Although paypal does an instant post-back upon the successful payment, I was never able to make it actually update the user's account right away, since I cant get it to send back some sort of informational that would identify the user that just completed the payment. I could only do that when you are sent back to the site, which sends the transaction ID, that I logged with a post-back. It searches for it, and grants permission if it was found int he DB.

Is there a way to submit some sort of a variable to paypal, that it will then post back to me? Something like &user_id=123, which would make it very handly to update the user's permission.

8 Answers8

35

Iten_number hidden variable don't work in my application. But i found that custom hidden field works fine. Just add this field to the form, generated by paypal: <input type="hidden" name="custom" value="YOUR VALUE FROM DB"/>. After, you can read this value to identify, for example, what product have been purchased. (Java code): String custom = request.getParameter("custom");

dsplatonov
  • 745
  • 1
  • 8
  • 16
  • 3
    Best answer! In PHP you then can read this field easily with `$_POST['custom']` from your ipn script. – Avatar Feb 16 '14 at 10:38
  • @dsplatonov We can read it on which page ? I mean the return url or the notify_url – Nehal Nov 10 '15 at 10:31
  • hi,do you know how can I set the value ‘’custom‘’, in java sdk when executed greement? – wawa Jan 13 '17 at 09:19
  • @wawa, Here I got stuck in the same place. I want to set custom value in executed agreement and invoke IPN Listener. Did you found some way? – Mdumanoj May 15 '17 at 02:19
  • 1
    @KaiNoack I tried with another name than `custom`: ``, but nothing arrived to the IPN with `$_POST['my_id']` :( So probably `name="custom"` is the only name which works. – Basj Aug 05 '17 at 21:24
  • 1
    Just saying, although this is highly upvoted (and I left it alone) the "custom" value will actually come back as "cm=". There was probably a change in 2016 that did this, hence all answers prior say it comes back as "custom" but all answers after say it comes back as "cm". – Colin Oct 25 '18 at 04:24
  • 1
    @Colin `custom` variable is working fine, it's not returned as `cm` – Mostafa Aug 07 '20 at 21:11
28

Yes, if you send item_number, the IPN notification will include that when it posts back to you. I record a unique ID in the database when the user starts the payment process, and include that when sending them to PayPal. When the IPN comes in, that unique ID matches up with the record in the database, giving me all the info I need.

Edit Re your comment:

I expect there's a code example somewhere on the site linked above, but basically in my case I'm using a form that I POST to https://www.paypal.com/cgi-bin/webscr. Within that form are various hidden fields documented in the IPN stuff (cmd for what command to perform, business to specify your business ID, item_name for a nice description in the PayPal UI, item_number for the item number I mentioned above, etc., etc.). When IPN posts back to your IPN address, it includes various fields (such as payment_status — kind of important! &mdash and the item_number you fed in when posting to them).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Is there a code example of this? How do I pass the item # to it? –  May 17 '10 at 07:21
  • @Yegor: Updated with a bit more information. I'm not a PayPal integration expert by *any* means, but hopefully that's useful. – T.J. Crowder May 17 '10 at 08:50
  • Where would I put that variable? Does it go in a hidden field? My buttons ahve no variables in them, its all stored at paypal. I just have a hosted_button_id hidden field which tells it which button to use. –  May 17 '10 at 19:00
  • @Yegor: Yes, you'd do it as a hidden field, or as a field on the query string of the URL, etc. -- just as with sending any other field from the client to the server. – T.J. Crowder May 23 '10 at 16:44
  • 4
    I think item_number is not appropriate for such use. item_number must be an internal id of your products database and you should not modify. To pass back values you can use optional fields, on0, on1... and get their values in IPN post back. – jlgsoftware Sep 30 '16 at 07:31
  • @jlgsoftware: What makes you say that? Can you back it up with a reference to documentation? I was happily doing this for several years without any trouble at all. (I can't say whether PayPal has changed in the six years since this answer, as I haven't personally had to do a PP integration since and haven't had to look this closely at any done by my staff.) – T.J. Crowder Sep 30 '16 at 07:37
  • 1
    It's a architecture design problem. You can use item_number for this purpose but I think it's not correct. item_number value should be your unique product id, always the same. In this link you can see "Pass-through" variables. https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/ . I think it's better use custom or invoice varibles, or optionals fields, to solve this question. – jlgsoftware Sep 30 '16 at 12:10
  • @jlgsoftware: Your *opinion* is just that; the PayPal documentation simply says *"Pass-through variable for you to track product or service purchased or the contribution made. The value you specify is passed back to you upon payment completion. This variable is required if you want PayPal to track inventory or track profit and loss for the item the button sells."* So what someone does with that is entirely up to them. Frankly, it makes no sense to comment on a hypothetical design. In my design `item_number` was exactly the right thing. YMMV, but that doesn't make my design "inappropriate." – T.J. Crowder Sep 30 '16 at 12:14
  • your desing may be appropiate, your use of item_number variable no. Paypal says "to track product or service purchased". I imagine that in your database products they will have an unique id which does not change every time a user makes a purchase, no?. In a good design item_number will have values corresponding to your products ids. In my opinion, any other use of item_number variable is acceptable, but not technically correct. – jlgsoftware Sep 30 '16 at 12:25
  • 1
    @T.J.Crowder I currently have to pass 8 variables to PayPal during the checkout. I'm considering condensing them all beforehand into an array. Your thoughts? – Ethan May 31 '17 at 03:43
  • I understand that you can set up PayPal standard button to include a custom variable and have it sent as a post variable to a URL set up in IPN settings. Will this be where the user gets directed after purchase, i.e. will this override the standard return URL? I need that custom variable to post to the page that the user is directed back to (which I usually set up in the PayPal button as a return_url) – gregpress Dec 29 '17 at 05:05
  • passing `item_number` in the form wasn't returned to my handler via IPN, the only way worked with me is passing `custom` variable. and I see others have the same comment, please either consider an edit or post a working example code. – Mostafa Aug 07 '20 at 21:08
  • @Mostafa - It was for me when I wrote the answer 10 years ago. I haven't used this stuff basically since then and can't speak to why your results would be different; clearly there's some difference between what you're doing and what I was doing. – T.J. Crowder Aug 08 '20 at 08:02
21

Just to add to this old question...

There are option parameters that are commonly used for custom data sending through paypal.

These option tags are on0, on1, or on2 for the custom field names and os0, os1, and os2 for the custom field values.

I would send on0 with a value of "UserID" and os0 the actual ID.

These values will be represented in the IPN as follows:

os0 is represented as option_selection1

os1 is represented as option_selection2

os2 is represented as option_selection3

on0 is represented as option_name1

on1 is represented as option_name2

on2 is represented as option_name3

Here's the info on PayPal's HTML parameters

jumpdart
  • 1,702
  • 16
  • 35
8

According to HTML Variables for PayPal Payments Standard you can send all the "Pass-through" variables:

item_number Pass-through variable for you to track product or service purchased or the contribution made. The value you specify is passed back to you upon payment completion. This variable is required if you want PayPal to track inventory or track profit and loss for the item the button sells.

custom Pass-through variable for your own tracking purposes, which buyers do not see. Default – No variable is passed back to you.

and

invoice Pass-through variable you can use to identify your invoice number for this purchase. Default – No variable is passed back to you.

All these pass-through variables are sent back by the IPN in the payment response info.

You just have to render your HTML template server-side and write the fields back in the HTML code like

<input type="hidden" name="item_number" value="{{ productID }}">
<input type="hidden" name="invoice_id" value="{{ invoiceID }}">
<input type="hidden" name="custom" value="{{ jsonInfo }}">

Technically the field "custom" can be a JSON encoded string if you want to handle more data like

myItemObject = {
   "customerEmail" : "john@doe.com
   "customerID: "AAFF324"
}
jsonInfo = json.dumps( myItemObject )
return render_template(tmpl_name, jsonInfo=jsonInfo, productID=productID, invoiceID=invoiceID)
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
7

I finally get this answer, I want to share with all of you look:

on your HTML form put this code (this is Paypal sandbox):

form action="https://www.sandbox.paypal.com/cgi-bin/webscr?custom=YOUR_VAR" method="post"

On your PHP after the Paypal redirect to your page success: use the cm GET variable:

$example = $_GET["cm"];
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 1
    I tested and confirmed this works. Other answers prior to 2016 say the "custom" value comes back as "custom" but sometime after, the system changed and now it comes back as "cm". – Colin Oct 25 '18 at 04:25
4

I hope this URL solves your issue. As it solved mine as well. Add a custom variable to your form and then retrieve it on your success payment page. Example : <input type='hidden' name='custom' value='<?php echo $email; ?>'/>

and then retrieve it as :

 $_POST['custom']
Nehal
  • 1,542
  • 4
  • 17
  • 30
2
<input type="hidden" name="on0" value="Ajay Gadhavana">
<input type="hidden" name="on1" value="my_phone_number">
<input type="hidden" name="on2" value="my_third_extra_field">

Response from paypal would be

[option_name1] => Ajay Gadhavana [option_name1] => my_phone_number [option_name1] => my_third_extra_field

Ajay Gadhavana
  • 415
  • 5
  • 5
2

What worked for me in 2021 is passing "custom_id" (inside the "purchase_units" array) to PayPal in my client app and checking "custom" on my backend.

Yes, it looks like PayPal renames the parameter for some reason.

Dmitry Kh
  • 126
  • 2
  • 10