Paypal第三方购物车与IPN

时间:2013-01-10 09:44:10

标签: php paypal e-commerce

我有关于电子商务网站的项目,车库和车主可以注册并创建买卖交易。我已经知道如何处理付款(买方=车主,卖方=车间)。但我不知道如何获取交易信息(从paypal到我的服务器),所以我可以将它保存到我的数据库。(例如,交易ID)。

这是我在车主购物车上的代码:

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

以下是功能:

  <?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;
}   
?>

这是配置文件:

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

这是我想要结合的东西(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;
    }
}?>

你们能告诉我如何做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您需要做的就是自定义该IPN库以包含您自己的数据库插入,然后确保在您的PayPal帐户配置文件中启用了IPN。您还可以在按钮代码中使用notify_url来覆盖配置文件设置,但我喜欢将我的配置文件设置为默认的IPN侦听器。

您可能对我的PHP template for PayPal IPN感兴趣。它非常受欢迎,虽然它可以重新开发为面向对象的类(我打算这样做),它可以很好地工作,并为您提供一个可以在您的数据库中记录所有IPN的工作IPN解决方案。

我几乎用于所有项目。只需安装IPN模板(下载中包含的说明),然后您就可以使用记录ID将IPN数据与您自己的数据相关联。例如,我总是在我的PayPal请求中包含invoice参数,以便将我的本地记录ID传递给PayPal。这样它也会在IPN中作为发票参数返回。我只是将它用作现有表的关系,用于我正在工作的任何项目,并且所有IPN数据都可用于该记录而无需进一步定制。

你可以在大约10分钟内完成它。