简体   繁体   English

带有IPN的Paypal第三方购物车

[英]Paypal Third Party Shopping Cart with IPN

Im have project about an e-commerce site where in carshop and car owners can register and create buy-sell transactions. 我有一个有关电子商务网站的项目,在这里汽车商店和车主可以注册并创建买卖交易。 I have already figured out how to process the payment (buyer=car owner, and seller=carshop). 我已经弄清楚了如何处理付款(买方=车主,卖方=汽车店)。 But I have no idea how to get the transaction informations(from paypal to my server ) so i can save it to my data base.(eg. transaction id). 但是我不知道如何获取交易信息(从Paypal到我的服务器),因此我可以将其保存到我的数据库中(例如交易ID)。

here's my code on the shopping cart of the car owner: 这是我在车主购物车上的代码:

<div id="confrm-body">
<label> Summary of Service Requests:</label><br/>
<label>Services in your cart has been categorized by   the shops the offer them. Please pay each service request respectively.</label>
<br/><label>Check out:0/3</label>   <br/>
<?php get_sum_cart($_SESSION['cid']);?>
 </div>

and here are the functions: 这里是功能:

  <?php
function get_sum_cart($cid){
        $q_get_sum_cart="select distinct(tbl_cart.shop_id),shop_name from tbl_services,tbl_carshop,tbl_cart where tbl_cart.svc_id=tbl_services.svc_id
        and tbl_cart.shop_id=tbl_carshop.shop_id and tbl_cart.client_id='". $cid ."'";
        $get_sum_result=mysql_query($q_get_sum_cart);

        while($sum_row=mysql_fetch_array($get_sum_result)){
            $shop_id=$sum_row['shop_id'];
            $shop_name=$sum_row['shop_name'];   
            ?>
            <form action=<?php echo cart_url;?> method="post" > 
            <table class="tbl_summary">

                <tr>
                    <th colspan="6"><label class="h-lbl"><?php echo $shop_name?></label> <a id="<?php echo $shop_id?>" class="client-btn" href="#">Show/Hide</a></th>
                </tr>
                <div id="slide">
                <tbody class="tbl_summary_cont <?php echo $shop_id?>">
                <tr>
                    <th scope="col" class="sub-th" style="width:100px;"><label>Preview</label></th>
                    <th scope="col" class="sub-th" style="width:210px;"><label>Pacakage/Kit Name</label></th>
                    <th scope="col" class="sub-th" style="width:100px;"><label>Initial Charge</label></th>
                    <th scope="col" class="sub-th" style="width:100px;"><label>Tax</label></th>
                    <th scope="col" class="sub-th" style="width:110px;"><label>Payable</label></th>
                    <th scope="col" class="sub-th" style="width:150px;"><label>Date Added</label></th>

                <input type="hidden" name="business" value="<?php echo get_carshop_paypal($shop_id);?>">
                <input type="hidden" name="cmd" value="_cart">
                <input type="hidden" name="upload" value="1">
                <input type="hidden" name="rm" value="2">
                <input type="hidden" name="notify_url" value="https://localhost/test-ipn/">
                <input type="hidden" name="currency_code" value="<?php echo cart_currency;?>">
                <input type="hidden" name="lc" value="<?php echo cart_country;?>">
                <input type="hidden" name="return" value="<?php echo return_url;?>">

                </tr>

                <?php make_sum_tbl($shop_id,$cid);?>



                <tfoot>
                    <td colspan="6"><input type="submit" name="submit" class="submit" value=""> 

                    <div class="lbl_bill">

                        <?php $sub_total = sum_per_tbl($shop_id,$cid);?>
                        <label>Sub-total:<span>Php <?php echo number_format($sub_total,2);?></span></label>
                        <label>Discount:<span><?php echo $discount;?></span></label>
                        <label>Total:<span>Php <?php echo get_discount($sub_total);?></span></label>

                        </div>
                    </td>

                </tfoot>
                </tbody>
            </table>
            </form> 
            <?php   
            $o_t=$o_t + $sub_total;


        }
?>
                <label>Over-all Total of Payables:<span>Php <?php echo number_format($o_t,2);?></span></label>
<?php       

}

function make_sum_tbl($shop_id,$cid){
    $q_get_tbl="select cart_id,tbl_cart.svc_id,kit_name,material_fee,svc_fee,t_amnt,tax,shop_name,tbl_cart.date from tbl_services,tbl_carshop,tbl_cart where tbl_cart.svc_id=tbl_services.svc_id and tbl_cart.shop_id=tbl_carshop.shop_id
    and tbl_cart.client_id='".$cid."' and tbl_carshop.shop_id='".$shop_id."'";
    $get_tbl_result=mysql_query($q_get_tbl);
    $n=1;
    while($tbl_row=mysql_fetch_array($get_tbl_result)){
        ?>
            <tr class="<?php echo $class;?>">
            <td><label><img class="min-prev" src="<?php echo get_service_pic($tbl_row['svc_id']);?>"></label></td>
            <input type="hidden" name="item_number_<?php echo $n;?>" value="<?php echo $tbl_row['svc_id'];?>">
            <td><label><?php echo $tbl_row['kit_name'];?></label></td>
            <input type="hidden" name="item_name_<?php echo $n;?>" value="<?php echo $tbl_row['kit_name'];?>">
            <td><label><?php echo $tbl_row['material_fee']+$tbl_row['svc_fee'];?></label></td>
            <td><label><?php echo $tbl_row['tax'];?>%</label></td>  
            <td><label>Php<?php echo number_format($tbl_row['t_amnt'],2);?></label></td>
            <input type="hidden" name="amount_<?php echo $n;?>" value="<?php echo $tbl_row['t_amnt'];?>">
            <td><label><?php echo $tbl_row['date'];?></label></td>
            <td><label><?php echo $row['total'];?></label></td>
            </tr>
        <?php
        $n++;
    }
}
function sum_per_tbl($shop_id,$cid){
    $sum_q="select sum(t_amnt) as total from tbl_services,tbl_carshop,tbl_cart where tbl_cart.svc_id=tbl_services.svc_id and tbl_cart.shop_id=tbl_carshop.shop_id and tbl_cart.client_id='".$cid."' and tbl_carshop.shop_id='".$shop_id."'";
    $sum_result=mysql_query($sum_q);
    while($sum=mysql_fetch_array($sum_result)){
        $t=$sum['total'];
        }
        return $t;
    }


function get_discount($sub_total){
$disc=0;

$total=number_format($sub_total - ($sub_total * $disc),2);
return $total;
}   
?>

HERE IS THE CONFIG FILE: 这里是配置文件:

function get_carshop_paypal($shop_id){
$get_p_add= "select pay_pal from tbl_carshop_paypal where shop_id='".$shop_id."'";
$result=mysql_query($get_p_add);
while($row=mysql_fetch_array($result)){
    $shop_paypal=$row['pay_pal'];

}
return $shop_paypal;
}
define('base_url','http://'.$_SERVER['HTTP_HOST'].'/');
define('cart_currency','PHP');
define('cart_country','PH');
define('cart_url','https://www.sandbox.paypal.com/us/cgi-bin/webscr');
define('return_url',base_url.'/occs/accounts/client/index.php?p=con-transact&make='.$make.'&year='.$year.'&model='.$model);

AND here is what im trying to combine(IPN_handler): 这是我试图结合的东西(IPN_handler):

class Paypal_IPN
{

    private $_url;


    public function __construct($mode = 'live')
    {
        if ($mode == 'live')
        $this->_url = 'https://www.paypal.com/cgi-bin/webscr';

        else
        $this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    }

    public function run()
    {
        $postFields = 'cmd=_notify-validate';



        foreach($_POST as $key => $value)
        {   
            $postFields .= "&$key=".urlencode($value);

        }

        $ch = curl_init();

            curl_setopt_array($ch, array(
            CURLOPT_URL => $this->_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $postFields
        ));

        $result = curl_exec($ch);
        curl_close($ch);

        $fh = fopen('result.txt', 'w');
        fwrite($fh, $result . ' -- ' . $postFields);
        fclose($fh);

        echo $postFields;
    }
}?>

can you guys give me idea about how to do this? 你们能给我关于如何做到这一点的想法吗?

All you would need to do is customize that IPN library to include your own database inserts, and then make sure IPN was enabled in your PayPal account profile. 您需要做的就是自定义IPN库以包括您自己的数据库插入,然后确保在您的PayPal帐户配置文件中启用了IPN。 You can also use notify_url in your button code to override the profile setting, but I like to setup my profile as a default IPN listener. 您还可以在按钮代码中使用notify_url覆盖配置文件设置,但我想将配置文件设置为默认IPN侦听器。

You might be interested in my PHP template for PayPal IPN . 您可能对我的PayPal IPN的PHP模板感兴趣。 It's pretty popular, and while it could be re-developed as an object-oriented class (which I plan to do) it works great as-is and provides you with a working IPN solution that logs all IPN in your database. 它非常流行,尽管可以重新开发为面向对象的类(我计划这样做),但它可以按原样运行,并为您提供了一个有效的IPN解决方案,可将所有IPN记录在数据库中。

I use it for nearly all of my projects. 我几乎将其用于所有项目。 Just install the IPN template (instructions included in the download) and then you can relate the IPN data back to your own data using record ID's. 只需安装IPN模板(下载中包含的说明),然后您就可以使用记录ID将IPN数据关联回自己的数据。 For example, I always include the invoice parameter with my PayPal requests so that my local record ID is passed to PayPal. 例如,我总是在我的PayPal请求中包含invoice参数,以便将本地记录ID传递到PayPal。 This way it'll come back in IPN as an invoice parameter as well. 这样,它也将作为发票参数返回IPN中。 I just use this as the relationship with the existing tables for whatever project I'm working, and all of the IPN data is available to that record without any further customization. 我只是将它用作与我正在处理的任何项目的现有表的关系,并且所有IPN数据都可用于该记录,而无需任何进一步的自定义。

You could have it up-and-running in about 10 min. 您可以在大约10分钟内启动并运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM