带有 CodeIgniter 的 PayPal IPN

发布于 2024-09-28 10:46:23 字数 418 浏览 0 评论 0原文

我正在尝试在 CodeIgniter 构建的网站上实现会员订阅服务。我希望使用 PayPal 来管理付款,但实现这一点非常困难。

我想要实现的是:

  1. 用户填写会员表格 个人详细信息
  2. 用户选择 订阅选项(8 种选择中的 1 种 - 每种价格不同)并提交表格
  3. 用户被发送到 PayPal 进行付款
  4. 用户在成功付款后返回到网站,个人详细信息存储在创建用户帐户(会员资格)的数据库中。

还有附加的表单验证,我在 CodeIgniter 中使用 form_validation 帮助程序,但这需要在 PayPal 付款开始之前完成。

我尝试实现 Ran Aroussi 的 PayPal_Lib,但我觉得它没有足够清晰的文档或指导。任何已实施的示例或建议将不胜感激。

卢卡斯

I am trying to implement a membership subscription service on a website built in CodeIgniter. I wish to use PayPal to manage payments, and am having a very hard time implementing this.

What I am trying to achieve is:

  1. User fills in a membership form with
    personal details
  2. User selects a
    subscription option (1 of 8 choices - each different price) and submits form
  3. User is sent to PayPal to pay
  4. User is returned to site upon successful payment and personal details are stored in database which creates user account (membership).

There is also the addition of form validation, I use the form_validation helper in CodeIgniter, but this needs to be done before PayPal payment can commence.

I have attempted to implement the PayPal_Lib from Ran Aroussi, but I feel it has not enough clear documentation or guidance on it. Any implemented examples or advice would be much appreciated.

Lucas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↙厌世 2024-10-05 10:46:23

我发现 Ran 的库也有点难以使用,所以我编写了一个替代品 - 它还具有对交易执行更多检查的好处,并在数据库中记录 IPN 调用和订单详细信息。这是 GitHub 上的库,希望您觉得它有用:

https://github.com/orderly/ codeigniter-paypal-ipn

I found Ran's library a little hard to use too so I've written a replacement - which also has the benefit of performing more checks on the transaction, and logging the IPN call and the order details in your database. Here's the library on GitHub, I hope you find it useful:

https://github.com/orderly/codeigniter-paypal-ipn

孤单情人 2024-10-05 10:46:23

下面是我在 Ran 的库中使用的未修改的代码。
希望有帮助。

<?php
/**
 * PayPal_Lib Controller Class (Paypal IPN Class)
 *
 * Paypal controller that provides functionality to the creation for PayPal forms, 
 * submissions, success and cancel requests, as well as IPN responses.
 *
 * The class requires the use of the PayPal_Lib library and config files.
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Commerce
 * @author      Ran Aroussi <[email protected]>
 * @copyright   Copyright (c) 2006, http://aroussi.com/ci/
 *
 */

class Paypal extends Controller 
{

    function Paypal()
    {
        parent::Controller();
        $this->load->library('Paypal_Lib');
    }

    function index()
    {
        $this->form();
    }

    function form()
    {

        $this->paypal_lib->add_field('business', '[email protected]');
        $this->paypal_lib->add_field('return',          site_url('paypal/success') );
        $this->paypal_lib->add_field('cancel_return',   site_url('paypal/cancel') );
        $this->paypal_lib->add_field('notify_url',      site_url('paypal/ipn') ); // <-- IPN url
        $this->paypal_lib->add_field('custom', '470874552'); // <-- Verify return

        $this->paypal_lib->add_field('item_name', 'Paypal Test Transaction');
        $this->paypal_lib->add_field('item_number', '5');
        $this->paypal_lib->add_field('amount', '9.95');

        // if you want an image button use this:
        $this->paypal_lib->image('button_03.gif');

        // otherwise, don't write anything or (if you want to 
        // change the default button text), write this:
        // $this->paypal_lib->button('Click to Pay!');

        $data['paypal_form'] = $this->paypal_lib->paypal_form();

        $this->load->view('paypal/form', $data);

    }

    function cancel()
    {
        $this->load->view('paypal/cancel');
    }

    function success()
    {
        //$data['pp_info'] = $this->input->post();
        $data['pp_info'] = $_POST; //FIX?
        $this->load->view('paypal/success', $data);
    }

    function ipn()
    {

        $ipn_valid = $this->paypal_lib->validate_ipn();
        if ( $ipn_valid == TRUE )
        {


            /**
                Log IPN
                TODO: bunu daha guzel gozukecek sekilde yapayim ilerde.
            **/
            date_default_timezone_set('Europe/Istanbul');
            $this->load->helper('date');

            $raw = '';
            foreach ($this->paypal_lib->ipn_data as $key=>$value)
            {
                $raw .= "\n$key: $value";
            }

            $this->load->model('model_paypal');

            $data_ipn['user_id']            = $this->paypal_lib->ipn_data['custom']; /* get USER_ID from custom field. */
            $data_ipn['txn_id']             = $this->paypal_lib->ipn_data['txn_id'];
            $data_ipn['payment_status']     = $this->paypal_lib->ipn_data['payment_status'];
            $data_ipn['mc_gross']           = $this->paypal_lib->ipn_data['mc_gross'];
            $data_ipn['mc_fee']             = $this->paypal_lib->ipn_data['mc_fee'];
            $data_ipn['mc_currency']        = $this->paypal_lib->ipn_data['mc_currency'];
            $data_ipn['item_number']        = $this->paypal_lib->ipn_data['item_number'];
            $data_ipn['datetime']           = mdate( "%Y-%m-%d %H:%i:%s" );
            $data_ipn['status']             = IPN_ENTRY_AWAITING;
            $data_ipn['raw']                = $raw;

            $this->model_paypal->ipn_entry_add ( $data_ipn );



            $ipn_payment_status = $this->paypal_lib->ipn_data['payment_status'];
            if ( strtolower($ipn_payment_status) == 'pending' )
            {


                log_message('debug', 'payment status TAMAM');
                $this->load->model('model_user_premium');


                $ipn_item_number    = $this->paypal_lib->ipn_data['item_number'];
                $item_info          = $this->model_user_premium->Premium_item_info ( $ipn_item_number );

                $ipn_mc_gross       = $this->paypal_lib->ipn_data['mc_gross'];

                log_message('debug', 'Item fee: '. $item_info['item_fee'] );                

                if ( $item_info['item_fee'] == $ipn_mc_gross ) 
                {

                    log_message('debug', 'fee ile gross TAMAM');



                    $data_account['user_id']        = $data_ipn['user_id'];
                    $data_account['type']           = $item_info['item_type'];
                    $data_account['date_expire']    = date("Y-m-d", mktime(0, 0, 0, date("m") + $item_info['date_extender'], date("d"), date("y") ) ); 


                    log_message('debug', 'UserID: '.    $data_account['user_id']        );
                    log_message('debug', 'Type:'.       $data_account['type']           );
                    log_message('debug', 'Expire:'.     $data_account['date_expire']    );


                    $this->model_user_premium->Premium_membership_change( $data_ipn['user_id'], $data_account );

                }
                else
                {

                    //TODO: report eksik transaction.

                }

            }



        }
        elseif ( $ipn_valid == FALSE )
        {

            $this->load->library('email');
            $this->email->to( '[email protected]' );
            $this->email->subject('IPN - FAILED');
            $this->email->from( '[email protected]', 'PAYPAL' );
            $this->email->message('or 4 life'); 

            $this->email->send();

        }


    }

    function ipn_list()
    {
        //TODO: admin check
        $this->load->helper('form');
        $this->load->model('model_theme');

        switch ( $_SERVER['REQUEST_METHOD'] ) 
        {

            case 'GET':

                /* Theme System with Reference Variable ( first param ) */
                $this->model_theme->Theme_returnThemeInfo( $data, 'paypal' );
                $this->load->view( $data['theme_folder_vault'] . 'master-ipn_list', $data );


            break;

            case 'POST':

                $this->load->model('model_paypal');
                $user_id    = $this->input->post('user_id');
                $txn_id     = $this->input->post('txn_id');

                $list_ipn = $this->model_paypal->ipn_entry_list ( $user_id, $txn_id );

                echo '<pre>';
                print_r( $list_ipn );
                echo '</pre>';


            break;


            default:
            break;
        }


    }


    function ipn_test()
    {

        $this->load->model('model_user_premium');

        $data_account['user_id']        = 123;
        $data_account['type']           = 4;
        $data_account['date_expire']    = date("Y-m-d", mktime(0, 0, 0, date("m") + 12, date("d"), date("y") ) );

        echo '<pre>';
        print_r( $data_account );
        echo '</pre>';


        $this->model_user_premium->Premium_membership_change( 123, $data_account );

    }

}

?>

Below is the unmodified code that i used with Ran's library.
Hope it helps.

<?php
/**
 * PayPal_Lib Controller Class (Paypal IPN Class)
 *
 * Paypal controller that provides functionality to the creation for PayPal forms, 
 * submissions, success and cancel requests, as well as IPN responses.
 *
 * The class requires the use of the PayPal_Lib library and config files.
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Commerce
 * @author      Ran Aroussi <[email protected]>
 * @copyright   Copyright (c) 2006, http://aroussi.com/ci/
 *
 */

class Paypal extends Controller 
{

    function Paypal()
    {
        parent::Controller();
        $this->load->library('Paypal_Lib');
    }

    function index()
    {
        $this->form();
    }

    function form()
    {

        $this->paypal_lib->add_field('business', '[email protected]');
        $this->paypal_lib->add_field('return',          site_url('paypal/success') );
        $this->paypal_lib->add_field('cancel_return',   site_url('paypal/cancel') );
        $this->paypal_lib->add_field('notify_url',      site_url('paypal/ipn') ); // <-- IPN url
        $this->paypal_lib->add_field('custom', '470874552'); // <-- Verify return

        $this->paypal_lib->add_field('item_name', 'Paypal Test Transaction');
        $this->paypal_lib->add_field('item_number', '5');
        $this->paypal_lib->add_field('amount', '9.95');

        // if you want an image button use this:
        $this->paypal_lib->image('button_03.gif');

        // otherwise, don't write anything or (if you want to 
        // change the default button text), write this:
        // $this->paypal_lib->button('Click to Pay!');

        $data['paypal_form'] = $this->paypal_lib->paypal_form();

        $this->load->view('paypal/form', $data);

    }

    function cancel()
    {
        $this->load->view('paypal/cancel');
    }

    function success()
    {
        //$data['pp_info'] = $this->input->post();
        $data['pp_info'] = $_POST; //FIX?
        $this->load->view('paypal/success', $data);
    }

    function ipn()
    {

        $ipn_valid = $this->paypal_lib->validate_ipn();
        if ( $ipn_valid == TRUE )
        {


            /**
                Log IPN
                TODO: bunu daha guzel gozukecek sekilde yapayim ilerde.
            **/
            date_default_timezone_set('Europe/Istanbul');
            $this->load->helper('date');

            $raw = '';
            foreach ($this->paypal_lib->ipn_data as $key=>$value)
            {
                $raw .= "\n$key: $value";
            }

            $this->load->model('model_paypal');

            $data_ipn['user_id']            = $this->paypal_lib->ipn_data['custom']; /* get USER_ID from custom field. */
            $data_ipn['txn_id']             = $this->paypal_lib->ipn_data['txn_id'];
            $data_ipn['payment_status']     = $this->paypal_lib->ipn_data['payment_status'];
            $data_ipn['mc_gross']           = $this->paypal_lib->ipn_data['mc_gross'];
            $data_ipn['mc_fee']             = $this->paypal_lib->ipn_data['mc_fee'];
            $data_ipn['mc_currency']        = $this->paypal_lib->ipn_data['mc_currency'];
            $data_ipn['item_number']        = $this->paypal_lib->ipn_data['item_number'];
            $data_ipn['datetime']           = mdate( "%Y-%m-%d %H:%i:%s" );
            $data_ipn['status']             = IPN_ENTRY_AWAITING;
            $data_ipn['raw']                = $raw;

            $this->model_paypal->ipn_entry_add ( $data_ipn );



            $ipn_payment_status = $this->paypal_lib->ipn_data['payment_status'];
            if ( strtolower($ipn_payment_status) == 'pending' )
            {


                log_message('debug', 'payment status TAMAM');
                $this->load->model('model_user_premium');


                $ipn_item_number    = $this->paypal_lib->ipn_data['item_number'];
                $item_info          = $this->model_user_premium->Premium_item_info ( $ipn_item_number );

                $ipn_mc_gross       = $this->paypal_lib->ipn_data['mc_gross'];

                log_message('debug', 'Item fee: '. $item_info['item_fee'] );                

                if ( $item_info['item_fee'] == $ipn_mc_gross ) 
                {

                    log_message('debug', 'fee ile gross TAMAM');



                    $data_account['user_id']        = $data_ipn['user_id'];
                    $data_account['type']           = $item_info['item_type'];
                    $data_account['date_expire']    = date("Y-m-d", mktime(0, 0, 0, date("m") + $item_info['date_extender'], date("d"), date("y") ) ); 


                    log_message('debug', 'UserID: '.    $data_account['user_id']        );
                    log_message('debug', 'Type:'.       $data_account['type']           );
                    log_message('debug', 'Expire:'.     $data_account['date_expire']    );


                    $this->model_user_premium->Premium_membership_change( $data_ipn['user_id'], $data_account );

                }
                else
                {

                    //TODO: report eksik transaction.

                }

            }



        }
        elseif ( $ipn_valid == FALSE )
        {

            $this->load->library('email');
            $this->email->to( '[email protected]' );
            $this->email->subject('IPN - FAILED');
            $this->email->from( '[email protected]', 'PAYPAL' );
            $this->email->message('or 4 life'); 

            $this->email->send();

        }


    }

    function ipn_list()
    {
        //TODO: admin check
        $this->load->helper('form');
        $this->load->model('model_theme');

        switch ( $_SERVER['REQUEST_METHOD'] ) 
        {

            case 'GET':

                /* Theme System with Reference Variable ( first param ) */
                $this->model_theme->Theme_returnThemeInfo( $data, 'paypal' );
                $this->load->view( $data['theme_folder_vault'] . 'master-ipn_list', $data );


            break;

            case 'POST':

                $this->load->model('model_paypal');
                $user_id    = $this->input->post('user_id');
                $txn_id     = $this->input->post('txn_id');

                $list_ipn = $this->model_paypal->ipn_entry_list ( $user_id, $txn_id );

                echo '<pre>';
                print_r( $list_ipn );
                echo '</pre>';


            break;


            default:
            break;
        }


    }


    function ipn_test()
    {

        $this->load->model('model_user_premium');

        $data_account['user_id']        = 123;
        $data_account['type']           = 4;
        $data_account['date_expire']    = date("Y-m-d", mktime(0, 0, 0, date("m") + 12, date("d"), date("y") ) );

        echo '<pre>';
        print_r( $data_account );
        echo '</pre>';


        $this->model_user_premium->Premium_membership_change( 123, $data_account );

    }

}

?>
夜光 2024-10-05 10:46:23

这是 Jamie Rumbelow 提供的一个 paypal 库,我一直在使用它并进行一些小的调整:

http://bitbucket。 org/jamierumbelow/codeigniter-paypal/src

Here's a paypal library from Jamie Rumbelow that I've been using with minor tweaks:

http://bitbucket.org/jamierumbelow/codeigniter-paypal/src

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文