Saturday, November 29, 2008

Paypal Order Management Integration

//transaction.php

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';


$tx_token = $_GET['tx'];
$auth_token = "nrg5OqSxKRR_x-Z4HW3t1CpA3yegGQYQ2xUu-R6m7TMesBkybp20PbIeU6u";
$req .= "&tx=$tx_token&at=$auth_token";

// 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 ('www.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}

// parse the data
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}

$address_city=$keyarray['address_city'];
$address_street=$keyarray['address_street'];
$address_country= $keyarray['address_street'];
$address_country_code= $keyarray['address_country_code'];
$address_name= $keyarray['address_name'];
$address_state= $keyarray['address_state'];
$address_status= $keyarray['address_status'];
$address_zip=$keyarray['address_zip'];

$address=$address_street." ".$address_city;
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$name=$firstname." ".$lastname;
$payment_status=$keyarray['payment_status'];
$payer_email=$keyarray['payer_email'];
$payer_id=$keyarray['payer_id'];
$payer_status=$keyarray['payer_status'];
$contact_phone=$keyarray['contact_phone'];
$residence_country=$keyarray['residence_country'];
$business=$keyarray['business'];
$receiver_email=$keyarray['receiver_email'];
$receiver_id=$keyarray['receiver_id'];
$memo=$keyarray['payer_email'];
$num_cart_items=$keyarray['num_cart_items'];
$payment_date=$keyarray['payment_date'];
$mc_gross=$keyarray['mc_gross'];
$amount = $keyarray['payment_gross'];
$mc_currency=$keyarray['mc_currency'];
$txn_id=$keyarray['txn_id'];
if ($payment_status=='Completed'){ $stat=0;}else {$stat=1;}

$SQLOrder="INSERT INTO t_order(order_id,order_date,order_status,customer_id,ship_to_name,ship_to_add) VALUES ('{$txn_id}',NOW(),'{$stat}','{$payer_id}','{$name}','{$address}')";
$resultOrder=mysql_query($SQLOrder);
echo mysql_error();


echo "

ITEMS SOLD:


";
for ( $x=1; $x<=$num_cart_items;$x++){
$variable="item_name".$x;
$variable_num="item_number".$x;
$variable_amt="amt".$x;
$variable_qty="quantity".$x;
echo ("Item Number $x: $keyarray[$variable_num]
\n");
echo ("Item Name $x: $keyarray[$variable]
\n");
echo ("Item Quantity $x: $keyarray[$variable_qty]
\n");
echo ("Item Amount $x: $keyarray[$variable_amt]
\n");
$SQLOrder_Content="INSERT INTO t_order_content (order_id,order_quantity,order_price,product_id) VALUES ('{$txn_id}','{$keyarray[$variable_qty]}','{$keyarray[$variable_amt]}','{$keyarray[$variable_num]}')";
$resultOrder_Content=mysql_query($SQLOrder_Content);
echo mysql_error();
}


$item_name_1=$keyarray['item_name1'];
$item_number_1=$keyarray['item_number1'];
$amount1=$keyarray['amount1'];
$quantity1=$keyarray['quantity1'];


echo ("

Thank you for your purchase!

");
echo ("Payment Details
\n");
echo ("
  • Name: $firstname $lastname
  • \n");
    echo ("
  • Amount: $amount
  • \n");
    echo ("
  • Status: $payment_status
  • \n");
    echo ("
  • Payer's Email: $payer_email
  • \n");
    echo ("
  • Currency: $mc_currency
  • \n");
    echo ("
  • Payer's ID: $payer_id
  • \n");
    echo ("
  • Payer's Status: $payer_status
  • \n");
    echo ("
  • Contact Phone: $contact_phone
  • \n");
    echo ("
  • Country: $residence
  • \n");
    echo ("
  • Business: $business
  • \n");
    echo ("
  • Receiver's Email: $receiver_email
  • \n");
    echo ("
  • Receiver's ID: $receiver_id
  • \n");
    echo ("
  • Memo: $memo
  • \n");
    echo ("
  • Number Cart Items: $num_cart_items
  • \n");
    echo ("
  • Payment Date: $payment_date
  • \n");
    echo ("
  • MC Gross: $mc_gross
  • \n");


    }
    else if (strcmp ($lines[0], "FAIL") == 0) {
    // log for manual investigation
    }

    }

    fclose ($fp);

    ?>

    No comments: