PHP如何在PayPal购物车中使用IPN?

时间:2015-02-23 14:29:22

标签: php paypal web system paypal-ipn

好吧所以我知道如何使用PayPal的IPN系统进行立即购买和基本付款,虽然我现在已经创建了一个基本的商店,其中包含大约12种不同的商品,可以使用购物车系统多次订购而且它有点复杂

我的问题是,在这类代码中选择POST的项目是:

mc_gross=0.02


item_number1=0003
item_number2=0012


payment_date=06:07:02 Feb 23, 2015 PST
payment_status=Completed
first_name=Name
business=email@hotmail.co.uk
payer_email=email@hotmail.co.uk
last_name=lastname
receiver_email=email@hotmail.co.uk


custom=


num_cart_items=2
item_name1=Professional Website Design
item_name2=Additional Website Page

quantity1=1
quantity2=1


txn_type=cart
mc_currency=GBP

mc_gross_1=0.01
mc_gross_2=0.01

我如何能够进行MySQL更新以添加每个不同的项目,或多个项目&他们的价格?

例如,这是我发布到我的数据库的单个项目的内容......

// assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    if ($_POST['mc_gross'] != NULL)
        $payment_amount = $_POST['mc_gross'];
    else
        $payment_amount = $_POST['mc_gross1'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
    $custom = $_POST['custom'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];

    if($payment_status == 'Completed'){
        $txn_id_check = mysql_query("SELECT `txn_id` FROM `payment_logs` WHERE `txn_id`='".$txn_id."'");
        if(mysql_num_rows($txn_id_check)!= 1){

            if($receiver_email == 'email@hotmail.co.uk'){

                if($item_name == 'Personal Website Design'){

                    // Add TXN ID to LOG
                    mysql_query("INSERT INTO `payment_logs` VALUES ('','".$first_name."','".$last_name."','".$payer_email."','".$txn_id."','".$item_name."','£".$payment_amount."',NOW(),'0','','£0.00')");

如果用户选择了多个项目,我将如何更改代码&我的数据库包含买家可以使用的各种不同选择?

1 个答案:

答案 0 :(得分:0)

以下是我用来解析IPN购物车商品的代码段。

$i = 1;
$cart_items = array();   
while(isset($_POST['item_number' . $i]))   
{   
    $item_number = isset($_POST['item_number' . $i]) ? $_POST['item_number' . $i] : '';   
    $item_name = isset($_POST['item_name' . $i]) ? $_POST['item_name' . $i] : '';   
    $quantity = isset($_POST['quantity' . $i]) ? $_POST['quantity' . $i] : '';  
    $mc_gross = isset($_POST['mc_gross_' . $i]) ? $_POST['mc_gross_' . $i] : 0;
    $mc_handling = isset($_POST['mc_handling' . $i]) ? $_POST['mc_handling' . $i] : 0;
    $mc_shipping = isset($_POST['mc_shipping' . $i]) ? $_POST['mc_shipping' . $i] : 0;
    $custom = isset($_POST['custom' . $i]) ? $_POST['custom' . $i] : '';   
    $option_name1 = isset($_POST['option_name1_' . $i]) ? $_POST['option_name1_' . $i] : '';   
    $option_selection1 = isset($_POST['option_selection1_' . $i]) ? $_POST['option_selection1_' . $i] : '';   
    $option_name2 = isset($_POST['option_name2_' . $i]) ? $_POST['option_name2_' . $i] : '';   
    $option_selection2 = isset($_POST['option_selection2_' . $i]) ? $_POST['option_selection2_' . $i] : '';
    $option_name3 = isset($_POST['option_name3_' . $i]) ? $_POST['option_name3_' . $i] : '';   
    $option_selection3 = isset($_POST['option_selection3_' . $i]) ? $_POST['option_selection3_' . $i] : '';
    $option_name4 = isset($_POST['option_name4_' . $i]) ? $_POST['option_name4_' . $i] : '';   
    $option_selection4 = isset($_POST['option_selection4_' . $i]) ? $_POST['option_selection4_' . $i] : '';
    $option_name5 = isset($_POST['option_name5_' . $i]) ? $_POST['option_name5_' . $i] : '';   
    $option_selection5 = isset($_POST['option_selection5_' . $i]) ? $_POST['option_selection5_' . $i] : '';
    $option_name6 = isset($_POST['option_name6_' . $i]) ? $_POST['option_name6_' . $i] : '';   
    $option_selection6 = isset($_POST['option_selection6_' . $i]) ? $_POST['option_selection6_' . $i] : '';
    $option_name7 = isset($_POST['option_name7_' . $i]) ? $_POST['option_name7_' . $i] : '';   
    $option_selection7 = isset($_POST['option_selection7_' . $i]) ? $_POST['option_selection7_' . $i] : '';
    $option_name8 = isset($_POST['option_name8_' . $i]) ? $_POST['option_name8_' . $i] : '';   
    $option_selection8 = isset($_POST['option_selection8_' . $i]) ? $_POST['option_selection8_' . $i] : '';
    $option_name9 = isset($_POST['option_name9_' . $i]) ? $_POST['option_name9_' . $i] : '';   
    $option_selection9 = isset($_POST['option_selection9_' . $i]) ? $_POST['option_selection9_' . $i] : '';

    $btn_id = isset($_POST['btn_id' . $i]) ? $_POST['btn_id' . $i] : '';

    $current_item = array(   
                           'item_number' => $item_number,   
                           'item_name' => $item_name,   
                           'quantity' => $quantity, 
                           'mc_gross' => $mc_gross, 
                           'mc_handling' => $mc_handling, 
                           'mc_shipping' => $mc_shipping, 
                           'custom' => $custom,   
                           'option_name1' => $option_name1,   
                           'option_selection1' => $option_selection1,   
                           'option_name2' => $option_name2,   
                           'option_selection2' => $option_selection2, 
                           'option_name3' => $option_name3, 
                           'option_selection3' => $option_selection3, 
                           'option_name4' => $option_name4, 
                           'option_selection4' => $option_selection4, 
                           'option_name5' => $option_name5, 
                           'option_selection5' => $option_selection5, 
                           'option_name6' => $option_name6, 
                           'option_selection6' => $option_selection6, 
                           'option_name7' => $option_name7, 
                           'option_selection7' => $option_selection7, 
                           'option_name8' => $option_name8, 
                           'option_selection8' => $option_selection8, 
                           'option_name9' => $option_name9, 
                           'option_selection9' => $option_selection9, 
                           'btn_id' => $btn_id
                          );   

    array_push($cart_items, $current_item);   
    $i++;   
}

然后,您可以为数据库更新添加循环...

foreach($cart_items as $cart_item)
{
    /**
     * Update database with values like...
     * $cart_item['item_name']  
     * $cart_item['item_number']
     * $cart_item['quantity']
     * $cart_item['mc_gross']
     */
}