<?php
$token = $subm_mbrs[paypal_pdt_token];    $tx = $_REQUEST[tx];
$payment_data = process_pdt($tx, $token);

if(!empty($payment_data)) {
  $key        = $payment_data[item_number];
  $level      = $payment_data[custom];
  $cemail     = $payment_data[payer_email];
  $cbreceipt  = $payment_data[txn_id];
  $fname      = $payment_data[first_name];
  $lname      = $payment_data[last_name];
  $cbvldEflag  = 1;
}

function process_pdt($tx, $token) {
//$paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
$paypal_url = "https://www.paypal.com/cgi-bin/webscr";

        $request = curl_init();
        // Set request options
        curl_setopt_array($request, array
        (
                CURLOPT_URL => $paypal_url,
                CURLOPT_POST => TRUE,
                CURLOPT_POSTFIELDS => http_build_query(array
                (
                        'cmd' => '_notify-synch',
                        'tx' => $tx,
                        'at' => $token,
                )),
                CURLOPT_RETURNTRANSFER => TRUE,
                CURLOPT_HEADER => FALSE,
                CURLOPT_SSL_VERIFYPEER => TRUE,
                CURLOPT_CAINFO => 'cacert.pem',
        ));
        // Execute request and get response and status code
        $response = curl_exec($request);
        $status   = curl_getinfo($request, CURLINFO_HTTP_CODE);       
        curl_close($request); // Close connection

        // Validate response
        if($status == 200 AND strpos($response, 'SUCCESS') === 0)
        {
                // Remove SUCCESS part (7 characters long)
                $response = substr($response, 7);
                
                // Urldecode it
                $response = urldecode($response);

                // Turn it into associative array
                preg_match_all('/^([^=\r\n]++)=(.*+)/m', $response, $m, PREG_PATTERN_ORDER);
                $response = array_combine($m[1], $m[2]);

                // Fix character encoding if needed
                if(isset($response['charset']) AND strtoupper($response['charset']) !== 'UTF-8')
                {
                        foreach($response as $key => &$value) {
                                $value = mb_convert_encoding($value, 'UTF-8', $response['charset']);
                        }
                        $response['charset_original'] = $response['charset'];
                        $response['charset'] = 'UTF-8';
                }
                // Sort on keys
                ksort($response);
                // Done!
                return $response;
        }
        return FALSE;
}
?>