user3494788
user3494788

Reputation: 13

Pass customer info via Paypal IPN

I have 2 forms (Age,Interest) for customer. After customer filled in 2 forms and click Paypal Buy Now button, I want to pass that info through Paypal IPN and get them. This is my code

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
  <fieldset>
    Age
    <input type="text" name="custom" value="" />
    <br />
    Interest
    <input type="text" name="custom" value="" />
    <br />
    <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="LLM3N8YXZNXJU" />
    <input type="hidden" name="item_name" value="abc" />
    <input type="hidden" name="item_number" value="P1" />
    <input type="hidden" name="notify_url" value="http://24151198.ngrok.com/paypal_ipn.php" />
    <input type="hidden" name="button_subtype" value="services" />
    <input type="hidden" name="rm" value="1" />
    <input type="hidden" name="bn" value="abcd-BuyNowBF:btn_paynowCC_LG.gif:NonHostedGuest" />
    <input style="position:relative; left:-10px; background:#ffffff; border:0;" type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" name="submit" alt="PayPal . The safer, easier way to pay online." />
    <img alt="" style="border:0;" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" />
  </fieldset>
</form>

I use log to record IPN message. If I use 1 "custom" form, I can get that custom info. But if I use 2 "custom" forms, I only get the last custom info. How can I get all custom forms info? thanks

Upvotes: 1

Views: 117

Answers (2)

Whome
Whome

Reputation: 10400

You can change form fields before submit is actually performed, study this proof of concept. Concatenate two or more separate fields into a hidden custom field value. Second example modifies a visible custom field. Whichever suits better for Paypal form handlers.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script>
function beforeSubmit1(sender) {
    sender.custom.value = sender.field1.value+"|"+sender.field2.value;
}
function beforeSubmit2(sender) {
    var c1 = document.getElementById("custom1");
    var c2 = document.getElementById("custom2");
    c1.value = c1.value+"|"+c2.value;
    c2.value = "";
}
</script>
</head>
<body>

<FORM id="frm1" action="http://server.com/doit.php" method="post" onsubmit="beforeSubmit1(this)">
    <input id="custom" name="custom" type="hidden" value=""/>
    <input id="field1" name="field1" type="text" value="Value 1"/>
    <input id="field2" name="field2" type="text" value="Value 2"/>  
    <input type="submit" name="submit" />
</FORM>

<FORM id="frm2" action=http://server.com/doit.php" method="post" onsubmit="beforeSubmit2(this)">
    <input id="custom1" name="custom" type="text" value="Value 1"/>
    <input id="custom2" name="custom" type="text" value="Value 2"/> 
    <input type="submit" name="submit" />
</FORM>

</body>
</html>

Upvotes: 1

Jeremy Carlson
Jeremy Carlson

Reputation: 1273

Actually, you have two fields in one form. :)

Form inputs use the name to pass information on, so when you include a second <input name="custom"...> then you are telling it, in effect, "Custom = this value. No wait, sorry, Custom = that value. Ignore what I told you first."

Since PayPal only accepts one custom variable, you'll need to either:

  • combine the two fields into one before posting to PayPal (which probably means posting locally first—you could concatenate them into a comma-separated field for example), or
  • store the information in a database, to be fetched based upon a key you pass to PayPal. This has the added advantage that you are not limited to the 256-char limit imposed by PayPal.

You MIGHT, however, decide that you want to rename your age field as item_number—this seems a bit shifty to me, but it might let you squeak by in your situation.

More information on IPN can be found in PayPal's Development Docs.

Upvotes: 0

Related Questions