Start Again
'; // get down to business switch ($action) { case 'SetExpressCheckout': $amount = $_POST['price1']*$_POST['qty1']+$_POST['price2']*$_POST['qty2']+$_POST['price3']*$_POST['qty3']; $cancel_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; $return_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?x=GetExpressCheckoutDetails&order_num='.$order_num.'&amount='.$amount; $data = $paypal.'&TENDER=P&TRXTYPE=S&ACTION=S'; // C - Direct Payment using credit card, P - Express Checkout using PayPal account $data .= '&AMT='.$amount.'&CANCELURL['.strlen($cancel_url).']='.$cancel_url.'&RETURNURL['.strlen($return_url).']='.$return_url.'&INVNUM='.$order_num.'&ORDERDESC['.strlen($desc).']='.$desc; $unique_id = $order_num; // call function to return name-value pair $nvp = fetch_data($unique_id, $submiturl, $data); if($nvp['RESPMSG']=='Approved') { $payPalURL = $PayPalURL.urldecode($nvp["TOKEN"]); echo ' Click here if you are not redirected to PayPal within 5 seconds. '; } else { error_handle($nvp); exit; } break; case 'GetExpressCheckoutDetails': $data = $paypal.'&TENDER=P&TRXTYPE=S&ACTION=G&TOKEN='.$_REQUEST['token']; $unique_id = date('ymd-H').rand(1000,9999); // prepare unique id for Action=G // call function to return name-value pair $nvp = fetch_data($unique_id, $submiturl, $data); if($nvp['RESPMSG']=='Approved') { echo '
Token:'.$_REQUEST['token'].'
Order Total:£'.$amount.'
Order Number:'.$nvp['INVNUM'].'
Buyer Details:
First Name:'.$nvp['FIRSTNAME'].'
Last Name:'.$nvp['LASTNAME'].'
Email Address:'.$nvp['EMAIL'].'
Payer Status:'.$nvp['PAYERSTATUS'].'
Shipping Address:
Address Line 1:'.$nvp['SHIPTOSTREET'].'
Address Line 2:'.$nvp['SHIPTOSTREET2'].'
City:'.$nvp['SHIPTOCITY'].'
State:'.$nvp['SHIPTOSTATE'].'
Postal code:'.$nvp['SHIPTOZIP'].'
Country:'.$nvp['SHIPTOCOUNTRY'].'
'; } else { error_handle($nvp); exit; } break; case 'DoExpressCheckout': $token = urlencode($_REQUEST['token']); $payer_id = urlencode($_REQUEST['Payerid']); $serverName = urlencode($_SERVER['SERVER_NAME']); $data = $paypal.'&TENDER=P&TRXTYPE=S&ACTION=D'; $data .= '&TOKEN='.$token.'&PAYERID='.$payer_id.'&AMT='.$amount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName.'&INVNUM='.$order_num.'&ORDERDESC='.$desc; $unique_id = $_REQUEST['unique']; // get it from GetExpressCheckoutDetails form so that no duplication // call function to return name-value pair $nvp = fetch_data($unique_id, $submiturl, $data); if($nvp['RESPMSG']=='Approved') { if($nvp['DUPLICATE']=='1') { echo '

Error!

This is a duplicatation of your previous order.

'; echo '

Error Mesaage: Transaction duplicated.

'; } else { echo '

Transaction Completed!

Thank you for your order.

'; if($nvp['PENDINGREASON']=='completed') { echo '

This is a test transaction.
Your credit card will not be charged.

'; } elseif($nvp['PENDINGREASON']=='echeck') { echo '

The payment is pending because it was made by an eCheck that has not yet cleared.

'; } else { // see Integration guide for more responses echo '

The payment is pending. Please login to your PayPal account for more details.

'; } while (list($key, $val) = each($nvp)) { echo "\n" . $key . ": " . $val . "\n
"; } } } else { error_handle($nvp); exit; } break; case 'DoDirectPayment': // Payment details $card_num = str_replace(' ','',$_POST['card_num']); $card = $_POST['card']; $cvv2 = $_POST['cvv2']; // 123 $expiry = $_POST['mm'].$_POST['yy']; $amount = number_format($_POST['amount'],2); $currency = $_POST['currency']; // Billing Details $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $addr1 = $_POST['address']; $addr2 = $_POST['city']; $addr3 = $_POST['state']; $addr4 = $_POST['zip']; //$country = $_POST['country']; // 2-digits ISO code // Other information $ipaddr = $_SERVER['REMOTE_ADDR']; $custom = 'Testing Only'; $data = $paypal.'&TENDER=C&TRXTYPE=S'; // C - Direct Payment using credit card, P - Express Checkout using PayPal account $data .= '&ACCT='.$card_num.'&CVV2='.$cvv2.'&EXPDATE='.$expiry.'&ACCTTYPE='.$card.'&AMT='.$amount.'&CURRENCY='.$currency; $data .= '&FIRSTNAME='.$fname.'&LASTNAME='.$lname.'&ADDRESS='.$addr1.'&CITY='.$addr2.'&STATE='.$addr3.'&ZIP='.$addr1;//.'&COUNTRY='.$country; $data .= '&EMAIL='.$email.'&CLIENTIP='.$ipaddr.'&COMMENT1='.$custom.'&INVNUM='.$order_num.'&ORDERDESC='.$desc; $unique_id = $order_num; // call function to return name-value pair $nvp = fetch_data($unique_id, $submiturl, $data); if($nvp['RESPMSG']=='Approved') { if($nvp['DUPLICATE']=='1') { echo '

Error!

This is a duplicatation of your previous order.

'; echo '

Error Mesaage: Transaction duplicated.

'; } else { echo '

Transaction Completed!

Thank you for your order.

'; echo '

This is a test transaction.
Your credit card will not be charged.

'; while (list($key, $val) = each($nvp)) { echo "\n" . $key . ": " . $val . "\n
"; } } } else { error_handle($nvp); exit; } break; case 'GetDirectPaymentDetails': $amount = $_POST['price1']*$_POST['qty1']+$_POST['price2']*$_POST['qty2']+$_POST['price3']*$_POST['qty3']; // checokout - enter card details and shipping echo '
Total:'.$currency.'
Order Number:
First Name:
Last Name:
Credit Card:
Card Number:
Expiry Date:
Card Verification Number:
Address:
City/Town:
State/County:
Postcode:
Country:
Notification E-mail:
'; break; default: session_unset(); // shopping list $order_num = date('ymd-H').rand(1000,9999); echo '
Product Description Unit Price Quantity
Tooth BrushGBP 0.02
Tooth PasteGBP 0.03
Dental FlossGBP 0.04
Pay by:
Credit Card (Direct Payment)
PayPal Express Checkout
'; break; } // footer echo ' '; // API functions and error handling function fetch_data($unique_id, $submiturl, $data) { // get data ready for API //$tempstr = $_POST['AMT'].date('YmdGis'); $request_id = md5($unique_id); //echo "Request ID:".$request_id.'
'; $user_agent = $_SERVER['HTTP_USER_AGENT']; // Here's your custom headers; adjust appropriately for your setup: $headers[] = "Content-Type: text/namevalue"; //or maybe text/xml $headers[] = "X-VPS-Timeout: 15"; $headers[] = "X-VPS-VIT-OS-Name: Linux"; // Name of your OS $headers[] = "X-VPS-VIT-OS-Version: RHEL 4"; // OS Version $headers[] = "X-VPS-VIT-Client-Type: PHP/cURL"; // What you are using $headers[] = "X-VPS-VIT-Client-Version: 0.01"; // For your info $headers[] = "X-VPS-VIT-Client-Architecture: x86"; // For your info $headers[] = "X-VPS-VIT-Client-Certification-Id: 13abb2433ff2923d7b191d2d011b7fde"; $headers[] = "X-VPS-VIT-Integration-Product: PHPv4::cURL"; // For your info, would populate with application name $headers[] = "X-VPS-VIT-Integration-Version: 0.01"; // Application version $headers[] = "X-VPS-Request-ID: " . $request_id; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $submiturl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt($ch, CURLOPT_HEADER, 1); // tells curl to include headers in response curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 90); // times out after 90 secs curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // this line makes it work under https curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //adding POST data curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //verifies ssl certificate curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE); //forces closure of connection when done curl_setopt($ch, CURLOPT_POST, 1); //data sent as POST $result = curl_exec($ch); $headers = curl_getinfo($ch); curl_close($ch); $result = strstr($result, "RESULT"); // prepare responses into array $proArray = array(); while(strlen($result)){ // name $keypos= strpos($result,'='); $keyval = substr($result,0,$keypos); // value $valuepos = strpos($result,'&') ? strpos($result,'&'): strlen($result); $valval = substr($result,$keypos+1,$valuepos-$keypos-1); // decoding the respose $proArray[$keyval] = $valval; $result = substr($result,$valuepos+1,strlen($result)); } return $proArray; } function error_handle($nvp) { echo '

Error!

We were unable to process your order.

'; echo '

Error '.$nvp['RESULT'].': '.$nvp['RESPMSG'].'.

'; while (list($key, $val) = each($nvp)) { echo "\n" . $key . ": " . $val . "\n
"; } } ?>