millercj Posted April 29, 2009 Share Posted April 29, 2009 I really need some help with my Paypal IPN. I know some php but I would never consider myself a php developer. I got the script off of a generator and thus far I've successfully adapted it to me needs. My problem now is I have no clue how to get unit pricing to come through. Below is my current IPN script. I need to get the unit price for each individual item and save it to the "Price" column specified in the sql statement (i've marked it with a comment "SQL WHERE UNIT PRICE IS NEEDED"). I'm really clueless and Paypal documentation doesn't have anything that makes sense and works for me. Hopefully someone out there has done this before...Thanks! <?php // read the post from PayPal system and add 'cmd' $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; } // post back to PayPal system to validate $header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // assign posted variables to local variables $item_name = $_POST['item_name']; $business = $_POST['business']; $item_number = $_POST['item_number']; $payment_status = $_POST['payment_status']; $mc_gross = $_POST['mc_gross']; $payment_currency = $_POST['mc_currency']; $txn_id = $_POST['txn_id']; $receiver_email = $_POST['receiver_email']; $receiver_id = $_POST['receiver_id']; $quantity = $_POST['quantity']; $num_cart_items = $_POST['num_cart_items']; $payment_date = $_POST['payment_date']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $payment_type = $_POST['payment_type']; $payment_status = $_POST['payment_status']; $payment_gross = $_POST['payment_gross']; $payment_fee = $_POST['payment_fee']; $settle_amount = $_POST['settle_amount']; $memo = $_POST['memo']; $payer_email = $_POST['payer_email']; $txn_type = $_POST['txn_type']; $payer_status = $_POST['payer_status']; $address_street = $_POST['address_street']; $address_city = $_POST['address_city']; $address_state = $_POST['address_state']; $address_zip = $_POST['address_zip']; $address_country = $_POST['address_country']; $address_status = $_POST['address_status']; $item_number = $_POST['item_number']; $tax = $_POST['tax']; $option_name1 = $_POST['option_name1']; $option_selection1 = $_POST['option_selection1']; $option_name2 = $_POST['option_name2']; $option_selection2 = $_POST['option_selection2']; $for_auction = $_POST['for_auction']; $invoice = $_POST['invoice']; $custom = $_POST['custom']; $notify_version = $_POST['notify_version']; $verify_sign = $_POST['verify_sign']; $payer_business_name = $_POST['payer_business_name']; $payer_id =$_POST['payer_id']; $mc_currency = $_POST['mc_currency']; $mc_fee = $_POST['mc_fee']; $exchange_rate = $_POST['exchange_rate']; $settle_currency = $_POST['settle_currency']; $parent_txn_id = $_POST['parent_txn_id']; $pending_reason = $_POST['pending_reason']; $reason_code = $_POST['reason_code']; //DB connect creds and email $notify_email = "EMAIL";//email address to which debug emails are sent to $DB_Server = "localhost"; //your MySQL Server $DB_Username = "USERNAME"; //your MySQL User Name $DB_Password = "PASWORD"; //your MySQL Password $DB_DBName = "DATABASE"; //your MySQL Database Name if (!$fp) {/* HTTP ERROR*/} else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") == 0) { //create MySQL connection $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); //select database $Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); $fecha = date("m")."/".date("d")."/".date("Y"); $fecha = date("Y").date("m").date("d"); //check if transaction ID has been processed before $checkquery = "select txnid from paypal_payment_info where txnid='".$txn_id."'"; $sihay = mysql_query($checkquery) or die("Duplicate txn id check query failed:<br>" . mysql_error() . "<br>" . mysql_errno()); $nm = mysql_num_rows($sihay); if ($nm == 0) { if ($txn_type == "cart") { $strQuery = "insert into BuyerData(tid,lastName,firstName,email,address,city,state,zip,invoice,pmt,paypalFee,salesTax,status,shipping,shipped,date) values ('".$txn_id."','".$last_name."','".$first_name."','".$payer_email."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','','".$mc_gross."','".$mc_fee."','".$tax."','".$payment_status."','".$custom."','0','".$fecha."')"; $result = mysql_query($strQuery) or die("Cart - paypal_payment_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno()); for ($i = 1; $i <= $num_cart_items; $i++) { $itemname = "item_name".$i; $itemnumber = "item_number".$i; $on0 = "option_name1_".$i; $os0 = "option_selection1_".$i; $on1 = "option_name2_".$i; $os1 = "option_selection2_".$i; $quantity = "quantity".$i; //SQL WHERE UNIT PRICE IS NEEDED $struery = "insert into OrderData(tid,itemname,itemnumber,qty,price) values ('".$txn_id."','".$_POST[$itemname]."','".$_POST[$itemnumber]."','".$_POST[$quantity]."','UNIT PRICE GOES HERE')"; $result = mysql_query($struery) or die("Cart - paypal_cart_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno()); } } else{ $strQuery = "insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,itemnumber,itemname,os0,on0,os1,on1,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$item_number."','".$item_name."','".$option_name1."','".$option_selection1."','".$option_name2."','".$option_selection2."','".$quantity."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')"; $result = mysql_query("insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,itemnumber,itemname,os0,on0,os1,on1,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$item_number."','".$item_name."','".$option_name1."','".$option_selection1."','".$option_name2."','".$option_selection2."','".$quantity."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')") or die("Default - paypal_payment_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno()); } // send an email in any case echo "Verified"; mail($notify_email, "VERIFIED IPN", "$res\n $req\n $strQuery\n $struery\n $strQuery2"); } else { // send an email mail($notify_email, "VERIFIED DUPLICATED TRANSACTION", "$res\n $req \n $strQuery\n $struery\n $strQuery2"); } //subscription handling branch if ( $txn_type == "subscr_signup" || $txn_type == "subscr_payment" ) { // insert subscriber payment info into paypal_payment_info table $strQuery = "insert into paypal_payment_info(paymentstatus,buyer_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')"; $result = mysql_query($strQuery) or die("Subscription - paypal_payment_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno()); // insert subscriber info into paypal_subscription_info table $strQuery2 = "insert into paypal_subscription_info(subscr_id , sub_event, subscr_date ,subscr_effective,period1,period2, period3, amount1 ,amount2 ,amount3, mc_amount1, mc_amount2, mc_amount3, recurring, reattempt,retry_at, recur_times, username ,password, payment_txn_id, subscriber_emailaddress, datecreation) values ('".$subscr_id."', '".$txn_type."','".$subscr_date."','".$subscr_effective."','".$period1."','".$period2."','".$period3."','".$amount1."','".$amount2."','".$amount3."','".$mc_amount1."','".$mc_amount2."','".$mc_amount3."','".$recurring."','".$reattempt."','".$retry_at."','".$recur_times."','".$username."','".$password."', '".$txn_id."','".$payer_email."','".$fecha."')"; $result = mysql_query($strQuery2) or die("Subscription - paypal_subscription_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno()); mail($notify_email, "VERIFIED IPN", "$res\n $req\n $strQuery\n $struery\n $strQuery2"); } } // if the IPN POST was 'INVALID'...do this else if (strcmp ($res, "INVALID") == 0) { // log for manual investigation mail($notify_email, "INVALID IPN", "$res\n $req"); } } fclose ($fp); } ?> Quote Link to comment Share on other sites More sharing options...
seany123 Posted April 29, 2009 Share Posted April 29, 2009 Have you used this? https://www.paypaltech.com/SG2/ Quote Link to comment Share on other sites More sharing options...
mattal999 Posted April 29, 2009 Share Posted April 29, 2009 Isn't $payment_fee = $_POST['payment_fee']; the price? Just guessing here, so don't call me stupid if i'm wrong... :-\ Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Paypaltech is what I used but it doesn't provide a unit price and i'm not sure how $payment_fee = $_POST['payment_fee']; would work because how would it reference each item if say there were 10 items in a cart? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 what field is the 'price' being stored in your form? these are things you should know, not us. i mean, ultimately, you could pass the item price in most any field .. all you gotta do is grab it when it comes out the other end. so, check out your form, or even post it here, and you/we can determine which field is passing the item price through. Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Ok here is a sample form that gets sent to paypal... <form action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return checkShipping();" method="post" id="finalCheckoutButton"> <input name="business" value="orders@nutsaboutgranola.com" type="hidden"> <input name="cmd" value="_cart" type="hidden"> <input name="upload" value="1" type="hidden"> <input name="currency_code" value="USD" type="hidden"> <input name="item_name_1" value="College Staple - 13oz Bag" type="hidden"> <input name="item_number_1" value="101" type="hidden"> <input name="amount_1" value="5.00" type="hidden"> <input name="quantity_1" value="1" type="hidden"> <input name="item_name_2" value="Lover's Combo - 13oz Bag" type="hidden"> <input name="item_number_2" value="102" type="hidden"> <input name="amount_2" value="5.00" type="hidden"> <input name="quantity_2" value="2" type="hidden"> <input name="shipping_1" value="5.78" type="hidden"> <input name="custom" value="Priority Mail (5.78)" type="hidden"> <input name="return" value="http://beta.nutsaboutgranola.com/marketplace/complete.php" type="hidden"> <input name="shopping_url" value="http://beta.nutsaboutgranola.com/marketplace" type="hidden"> <input name="cancel_return" value="http://beta.nutsaboutgranola.com/marketplace/myBag.php" type="hidden"> <input value="" id="PPcheckout" type="submit"> </form> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 do you know what the price is? 'cause i don't. i'm thinking it's 'amount_1'? throw me a bone to work with here, man. it's up to you as to which input field to use to pass the item price .. usually 'amount' is used .. you can also use 'custom'. you need a field like <input type="hidden" name="amount_1" value="<?php echo $price; ?>" /> or something like that. $price is whatever var you're using to hold the item's price. then it just becomes basic form submission .. do you know how to do that at least? Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Sorry, Yes it is Amount_1, Amount_2 and so forth...do those exact variable names come back via IPN? Where in my code would I implement that? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 the thing with Paypal's IPN is that it only posts back certain fields, not all of them. so just because you passed a value via the form, do not expect to receive it back via the IPN. fish around in the developer section of paypal to see what fields do what, 'cause there's different fields for different types of forms, ie. shopping carts, simple 'Buy Now' button, etc. Paypal Developer - List of HTML Variables you'll need a Sandbox login .. so just register (if you haven't already), and go through those doc's. personally, i'd just use the 'custom' field to pass several values .. makes life much easier. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 and going back to your original post now, you're not assigning 'amount_1' or 'amount_2' in the post back, ie. $amount_1 = $_POST['amount_1']; //you need (something like) this to be able to insert the price into the database; Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Ok, I went back to my code and I ammending the for loop prior to the query as such: <?PHP for ($i = 1; $i <= $num_cart_items; $i++) { $itemname = "item_name".$i; $itemnumber = "item_number".$i; $amount = "amount_".$i; $on0 = "option_name1_".$i; $os0 = "option_selection1_".$i; $on1 = "option_name2_".$i; $os1 = "option_selection2_".$i; $quantity = "quantity".$i; $struery = "insert into OrderData(tid,itemname,itemnumber,qty,price) values ('".$txn_id."','".$_POST[$itemname]."','".$_POST[$itemnumber]."','".$_POST[$quantity]."','".$_POST[$amount]."')"; } ?> I put two products to my cart to checkout. After completing the payment it only posted one item via IPN and it is without the price so it's hanging somewhere...see anything? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 lose the $_POST[] .. you've already defined the vars ($itemname, $amount, etc.), so just slap them into the query minus the $_POST[] wrapping. are you trying to append $i onto a pre-existing $_POST value? just stick with the other code you posted earlier and edit the 'custom' input field in your form : <input name="custom" value="*" type="hidden" replace * in the input value to whatever variable you're using in your checkout to post the price to Paypal, ie. $price if you need the 'Priority Mail (5.78)' that's already in the 'custom' field, do this : <input name="custom" value="Priority Mail (5.78)-<?php echo $price; ?>" type="hidden" /> where - is a delimiter for later usage, and $price is whatever you've been using to store the item's price. now, add this to your post-back vars : $custom = explode("-", $_POST['custom']); and then you can use $custom[0] for your Priority Mail (5.78), and $custom[1] holds your price. Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 You just confused the heck out of me... Itemname, Itemnumber, all that stuff that has $_POST[]'s works fine. It's just Amount. At the very begining of my code there's $item_name = $_POST['item_name']; $amount = $_POST['amount']; $business = $_POST['business']; $item_number = $_POST['item_number']; $payment_status = $_POST['payment_status']; .... then the for loop where i need to add the number of item that it is...I'm not sure why you're saying to remove the posts If amount_x should work why should I use custom? Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Ok, what about this...I set "on0_"to be the the price as well. It properly saves to the database as long as there is only one product kind in the cart If there are more, it fails to write: for ($i = 1; $i <= $num_cart_items; $i++) { $itemname = "item_name".$i; $itemnumber = "item_number".$i; $on0 = "option_name1_".$i; $os0 = "option_selection1_".$i; $on1 = "option_name2_".$i; $os1 = "option_selection2_".$i; $quantity = "quantity".$i; $struery = "insert into OrderData(tid,itemname,itemnumber,qty,price) values ('".$txn_id."','".$_POST[$itemname]."','".$_POST[$itemnumber]."','".$_POST[$quantity]."','".$_POST[$on0]."')"; this is the form for one product: <input name="item_name_1" value="College Staple - 13oz Bag" type="hidden"> <input name="item_number_1" value="101" type="hidden"> <input name="amount_1" value="5.00" type="hidden"> <input name="on0_1" value="5.00" type="hidden"> <input name="quantity_1" value="1" type="hidden"> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 i don't know that 'amount_x' can be posted back .. i remember having issues with that a long while back, which is why i switched to the 'custom' field. try the custom field, see if that works. and sorry, didn't realize you are using a cart .. had a little bit of a brain fart. Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Ok, I will but right now custom is only passed once (for the whole cart). Can it be passed within each item? ie: custom_1 custom_2 etc. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 actually, try 'item_number_ x' .. that will post-back .. just to rule out if 'amount_x' can be used or not. you're right about custom .. it can't be 'custom_x' to my knowledge. Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 ok...it did and didn't work. It posted everything correctly for the first item but must have crashed after that. I altered the code that generates my form do the itemnumber would read 100-1:3.50 where 100-1 is the item number and 3.50 is the price. <input type="hidden" name="item_number_'.$i.'" value="'.$nextresult['PID'].':'.$nextresult['price'].'"> my ipn now looks like this: for ($i = 1; $i <= $num_cart_items; $i++) { $itemname = "item_name".$i; $itemnumber = "item_number".$i; $itemnumber = explode(":", $_POST[$itemnumber]); $on0 = "option_name1_".$i; $os0 = "option_selection1_".$i; $on1 = "option_name2_".$i; $os1 = "option_selection2_".$i; $quantity = "quantity".$i; $struery = "insert into OrderData(tid,itemname,itemnumber,qty,price) values ('".$txn_id."','".$_POST[$itemname]."','".$itemnumber[0]."','".$_POST[$quantity]."','".$itemnumber[1]."')"; } Not sure where the issue is Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 are you sure $num_cart_items is being set correctly? check the logs on your server .. might have some very valuable input there. check the log where the IPN file resides. Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 I'm going to assume it's correct it's getting sent back from paypal... There are no error logs in the folder :-\ Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 I'm going to assume it's correct it's getting sent back from paypal... There are no error logs in the folder :-\ you know what they say when you assume try and find out what $num_cart_items is. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 i don't see 'num_cart_items' in the Paypal documentation as a post-back feature .. that come with the script? Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 Yes sir...But if i remove all this crap about unit price the code runs flawlessly. Ok I took that variable $num_cart_items and tried to put it into the price column of my table. That whole query put nothing into my table. Not sure what that means? $struery = "insert into OrderData(tid,itemname,itemnumber,qty,price) values ('".$txn_id."','".$_POST[$itemname]."','".$itemnumber[0]."','".$_POST[$quantity]."','".$num_cart_items."')"; Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 29, 2009 Share Posted April 29, 2009 what was being inputted into price when it was only running once through? `price` is a field in the db, right? Quote Link to comment Share on other sites More sharing options...
millercj Posted April 29, 2009 Author Share Posted April 29, 2009 As far as i'm concerned, the correct number Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.