Link to home
Start Free TrialLog in
Avatar of GVNPublic123
GVNPublic123

asked on

PayPal IPN Buttons HELP

I have been using PayPal IPN for ages, but with "BuyNow" buttons. I have recently created new buttons that are "Shopping Cart" buttons, and now my IPN doesn't work anymore.

Here is my current code:
if (empty($_POST) || @count($_POST) < 1)
		die("No params");
 
	$query = array('cmd=_notify-validate');
	foreach ($_POST as $key => $val) {
		if (!empty($val))	{
			$query[] = $key .'=' .urlencode($val);
			$$key    = trim(strip_tags($val));
		}
	}
	$query = implode('&', $query);
 
	$has_curl = false;
	if (function_exists('curl_init') && $ch = curl_init()) {
		curl_setopt($ch, CURLOPT_URL, 'http://www.paypal.com/cgi-bin/webscr');
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Codehead + Curl');
		$result = curl_exec($ch);
		curl_close($ch);
		$has_curl = true;
	}
	if (!$has_curl) {
		$header  = "POST /cgi-bin/webscr HTTP/1.0\r\n";
		$header .= "Host: www.paypal.com\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: " . strlen($query) . "\r\n\r\n";
		if ($fp = fsockopen(www.paypal.com, 80, $errno, $errstr, 30)) {
			socket_set_timeout($fp, 15);
			fwrite($fp, $header . $query);
			while (!feof($fp)) {
				$result = fgets($fp, 1024);
				if (strcmp($result, 'VERIFIED') == 0)
					break;
			}
			fclose($fp);
		}
	}
	
		$paypal = array();
		$paypal['user_id'] = $user_id;
		$paypal['payment_date'] = time();
		$paypal['completed_date'] = ($payment_status == 'Completed' ? time() : '');
		$paypal['item_name'] = $item_name;
		$paypal['item_number1'] = $item_number; //&item_number=TP_S1
		$paypal['payment_type'] = $payment_type;
		$paypal['payment_status'] = $payment_status;
		$paypal['pending_reason'] = $pending_reason;
		$paypal['payment_amount'] = $mc_gross;
		$paypal['paypal_fee'] = (isset($mc_fee) ? $mc_fee : 0);
		$paypal['payment_currency'] = $mc_currency;
		$paypal['txn_id'] = $txn_id; 
		$paypal['receiver_email'] = $receiver_email;
		$paypal['payer_name'] = $first_name .' ' .$last_name;
		$paypal['payer_email'] = $payer_email;		
		$paypal['raw_payment_data'] = serialize($_POST);
		$paypal['custom'] = $custom;

Open in new window


The item_number used to be just item_number with buynow buttons, but with shopping cart it seems to have numbers (like item_number1).
However I have script email me parameters received, and item_number1 shows as blank, even if its listen in IPN message in PayPal history.

Bellow are samples of IPN messages from PayPal history. First its regular buynow:
mc_gross=18.00&protection_eligibility=Ineligible&address_status=confirmed&payer_id=BH6KD672XT7QN&tax=0.00&address_street=484 w 43rd st. 14m&payment_date=22:40:24 Oct 03, 2011 PDT&payment_status=Completed&charset=windows-1252&address_zip=10036&first_name=Jesse&mc_fee=0.96&address_country_code=US&address_name=Jesse McBride&notify_version=3.4&custom=CUSTOM&payer_status=verified&business=&address_country=United States&address_city=&verify_sign=A8AwgDwQw-W3A-XKrA3Y-EaqmjTpO2P3K09PZnl9DCymW&payer_email=&txn_id=9U496046E6211010U&payment_type=instant&btn_id=24843740&last_name=&address_state=NY&receiver_email=fdhfdfsdf&payment_fee=&shipping_discount=0.00&insurance_amount=0.00&receiver_id=dsfsdfsdfds&txn_type=web_accept&item_name=dfdasfd&discount=0.00&mc_currency=EUR&item_number=ITEM&residence_country=US&shipping_method=Default&handling_amount=0.00&transaction_subject=CUSTOM&payment_gross=&shipping=0.00&ipn_track_id=RNjnly6gChPh-8fJdt7ZdA

Open in new window


Now second is Cart Buynow IPN:
mc_gross=18.00&protection_eligibility=Ineligible&address_status=unconfirmed&item_number1=TP_C5000&payer_id=924ZZYRK93BXN&tax=0.00&address_street=Glärnischstrasse 79&payment_date=12:38:05 Oct 04, 2011 PDT&payment_status=Completed&charset=windows-1252&address_zip=8810&mc_shipping=0.00&mc_handling=0.00&first_name=Pius&mc_fee=1.14&address_country_code=CH&address_name=&notify_version=3.4&custom=rasorfrog&payer_status=verified&business=mm&address_country=Switzerland&num_cart_items=1&mc_handling1=0.00&address_city=n&verify_sign=AeC0pIlHDmjsk5A-GAqvtzxGq8K--9UJCyoRzE1FjQrjW&payer_email=&mc_shipping1=0.00&btn_id1=36427581&txn_id=4MF91487RR2074513&payment_type=instant&last_name=&address_state=&item_name1=ITEM Credits&receiver_email=om&payment_fee=&quantity1=1&receiver_id=6696NT7T9DDHW&txn_type=cart&mc_gross_1=18.00&mc_currency=EUR&residence_country=CH&transaction_subject=CUSTOM&payment_gross=&ipn_track_id=7rCDhJ8.x4tOthO2eXKTpg

Open in new window


So what I need is a way to EXTRACT item_numberX and its quantity and than run foreach loop (loop must run for each quantity & for each item_number).

Please help me out, its really important. Thanks a lot.
Avatar of GVNPublic123
GVNPublic123

ASKER

Ok, stupid me. Its late and Im wasted, its actually variables are created from POST so $item_number is the holder of value and not $paypal['item_number'].

The problem however still exists, how do I get all item numbers and than like array run foreach. Like detect all item_numberx's and quantities of them ordered and than run a loop like foreach($item_number_array as $item_number){for($i=0,$i<$quantity,$i++){/*APPLY PRODUCT*/}}
Maybe I can do something like this, like construct 100 variables for items and quanitities.

for($i=0; $i<100; $i++){
$item_number = ${'item_number'.$i};
for($j=0; $j<100; $j++){
$quantity = ${'quantity'.$j};
//NOW USING $item_number and $quantity perform check of their value and send products if they match
}}

My stoned and tired brain cant think of anything else, so if this post is completely retarded please dont laugh your asses of.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray_Paseur thats is a BEAUTIFUL code. Thanks Ill use it tomorrow... now Im off to bed.
Thanks for the points and for your kind words.  I hope it still looks good to you in the morning ;-)
Oh yes its good. But I have a problem with it, for some reason it doesn't want to work with item_numbers.
I placed test order having 2 different item's so there are item_number1 and item_number2 in IPN message. But for some reason, cart only processes item_number1. The while loop there doesnt work for me.