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);

    ?>

    Monday, November 10, 2008

    PHP MySQL Shopping Cart Tutorial

    Yes, this is a another shopping cart tutorial. I am planning to make this tutorial to cover a more sophisticated shopping cart solution but for now it only explains a basic shopping cart. I will improve it in time so stay tuned.
    Here is what we have in this site :
    1. Introduction
    This page explains the big picture. What kind of shopping cart software we will create. The shopping cart flow, file organizations, requirements and configuration.

    2. Database Design
    Here you will see what tables do we need and the relation between tables. An Entity Relationship (ER) diagram is also included so you can see the database relation better.

    3. Admin, Control Panel
    Where all shopping cart administration work takes place. This page explains the basic structure of the admin pages and brief explanation on each admin submodules.

    4. Admin, Login
    We have to create this one first of course. Making an administrator page without one is simply a crazy move :)

    5. Admin, View Category
    List all available categories and it's child categories.

    6. Admin, Add Category
    You can't build the online store if you don't have product categories, right ?

    7. Admin, Edit Category
    Modify existing categories, to change the name, description or maybe the image

    8. Admin, Delete Category
    Unused categories can be deleted here.

    9. Admin, View Products
    List all products in our online shop, search and view by category is also available

    10. Admin, Add Product
    After finish working with the category pages. We start writing the product scripts.

    11. Admin, Edit Product
    Modify every aspect of the product

    12. Admin, Delete Product
    Remove product from the database.

    13. Admin, Order Management
    Let's see how many orders we have, how many completed orders and abandoned ones.

    14. Admin, Shop Configuration
    Here we can set several aspects in our shop. For now we can only set the shop information ( name, address, etc ), the currency and shipping cost.

    15. Admin, User Management
    This page covers adding a user, modify password and delete user

    16. Shop, Main page
    This is where the customers will go. They will browse around and view the products and hopefully if they're interested enough they'll put some products in the cart.

    17. Shop, Browse Categories
    Just to add some navigation so the customer can browse around

    18. Shop, View Product List
    Display all product in a category

    19. Shop, View Product Detail
    Display detailed information about a product.

    20. Shop, Add to Cart
    All interesting goodies goes here

    21. View Shopping Cart
    Customer can view what's in her shoping cart. Probably modifying the product quantity or dump some products ( or even abandon the whole cart ).

    22. Shop, Checkout
    Our real target, getting the sale.

    23. Resources
    Some useful online resources like a study on shopping cart usability, shopping cart security issues, alternative shopping cart solutions, etc




    PHP Mysql Shopping cart Database table

    CREATE TABLE tbl_order (

    id int(10) unsigned NOT NULL auto_increment,
    date datetime default NULL,
    last_update datetime NOT NULL default '0000-00-00 00:00:00',
    status enum('New', 'Paid', 'Shipped','Completed','Cancelled') NOT NULL default 'New',
    memo varchar(255) NOT NULL default '',
    shipping_first_name varchar(50) NOT NULL default '',
    shipping_last_name varchar(50) NOT NULL default '',
    shipping_address1 varchar(100) NOT NULL default '',
    shipping_address2 varchar(100) NOT NULL default '',
    shipping_phone varchar(32) NOT NULL default '',
    shipping_city varchar(100) NOT NULL default '',
    shipping_state varchar(32) NOT NULL default '',
    shipping_postal_code varchar(10) NOT NULL default '',
    shipping_cost decimal(5,2) default '0.00',
    payment_first_name varchar(50) NOT NULL default '',
    payment_last_name varchar(50) NOT NULL default '',
    payment_address1 varchar(100) NOT NULL default '',
    payment_address2 varchar(100) NOT NULL default '',
    payment_phone varchar(32) NOT NULL default '',
    payment_city varchar(100) NOT NULL default '',
    payment_state varchar(32) NOT NULL default '',
    payment_postal_code varchar(10) NOT NULL default '',
    PRIMARY KEY ( id)
    ) TYPE=MyISAM AUTO_INCREMENT=1001 ;