使用Kohana 3.0授权.NET

时间:2021-03-11 19:18:34

I am trying to use Authorize.Net in a kohana 3.0 project. I have set up the module in the bootstrap.php file.
I want to best understand the process before I focus on the code. In their sample code, it is written.

我想在kohana 3.0项目中使用Authorize.Net。我在bootstrap.php文件中设置了模块。在关注代码之前,我想最好地理解这个过程。在他们的示例代码中,它是写的。

<?php
require_once 'anet_php_sdk/AuthorizeNet.php'; // Include the SDK you downloaded in Step 2
$api_login_id = 'I_put_my_login_id_here';
$transaction_key = 'and_my_transaction_key_here;
$amount = "5.99";
$fp_timestamp = time();
$fp_sequence = "123" . time(); // Enter an invoice or other unique number.
$fingerprint = AuthorizeNetSIM_Form::getFingerprint($api_login_id,
  $transaction_key, $amount, $fp_sequence, $fp_timestamp)
?>

<form method='post' action="https://test.authorize.net/gateway/transact.dll">
<input type='hidden' name="x_login" value="<?php echo $api_login_id?>" />
<input type='hidden' name="x_fp_hash" value="<?php echo $fingerprint?>" />
<input type='hidden' name="x_amount" value="<?php echo $amount?>" />
<input type='hidden' name="x_fp_timestamp" value="<?php echo $fp_timestamp?>" />
<input type='hidden' name="x_fp_sequence" value="<?php echo $fp_sequence?>" />
<input type='hidden' name="x_version" value="3.1">
<input type='hidden' name="x_show_form" value="payment_form">
<input type='hidden' name="x_test_request" value="false" />
<input type='hidden' name="x_method" value="cc">
<input type='submit' value="Click here for the secure payment form">
</form>

So, I suppose a user accesses my website and tries to buy a product. I handle the process into the controller. I will use the php_curl.

所以,我想用户访问我的网站并尝试购买产品。我将进程处理到控制器中。我将使用php_curl。

public function action_authorize(){
$url = 'https://test.authorize.net/gateway/transact.dll';

                    $post_string = '';
                    $request = curl_init($url);

                    curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
                    curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
                    curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
                    curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.

                    $post_response = curl_exec($request); // execute curl post and store results in $post_response

                    curl_close($request);

                    $response_array = explode($post_values["x_delim_char"],$post_response);
}

Here my problem is how can I know the server response to my query result in *$response_array*. How to identify error codes ? Another thing, I enable the Authorize module. How can I access the *'api_login'* and the *'transaction_key'* from my controller ? When I do this in the controller, I get an error *

这里我的问题是如何在* $ response_array *中知道服务器对我的查询结果的响应。如何识别错误代码?另一件事,我启用了授权模块。如何从我的控制器访问*'api_login'*和*'transaction_key'*?当我在控制器中执行此操作时,出现错误*

$authorize = new Authorize;
$authorize->api_login;
  • gives an error. In fact I want to be able to implement Authorize.NET module in Kohana3.0
  • 给出错误。实际上我希望能够在Kohana3.0中实现Authorize.NET模块

1 个解决方案

#1


1  

Store your Authorize.Net SDK classes in application/vendor/anet_php_sdk/

将Authorize.Net SDK类存储在application / vendor / anet_php_sdk /中

Demo Controller Method:

演示控制器方法:

class Controller_Payment extends Controller_Template
{
    // Set properties at the top of the class for easier changes later
    $this->authorize_url = 'https://test.authorize.net/gateway/transact.dll';
    $this->api_login_id = 'I_put_my_login_id_here';

    public function action_authorize()
    {
        require Kohana::find_file('vendor', 'anet_php_sdk/AuthorizeNet');

        if($_POST)
        {
            $request = curl_init($this->authorize_url);

            // Build your post string
            $post_string = http_build_query($_POST);

            // The rest of the Authorize.net code
            curl_setopt($request, CURLOPT_HEADER, 0);
            curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($request, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
            $post_response = curl_exec($request);
            curl_close($request);

            $response_array = explode($post_values["x_delim_char"],$post_response);

            // Look in your SDK docs to determine the actual values you need
            // to check to verify that the transaction was successful:
            if($response_array['code'] == 200)
            {
                Request::current()->redirect('success/page');
            }
            else
            {
                // Error handling
            }
        }

        $transaction_key = 'transaction_key_here;
        $amount = "5.99";
        $fp_timestamp = time();
        $fp_sequence = "123" . time(); // Enter an invoice or other unique number.
        $fingerprint = AuthorizeNetSIM_Form::getFingerprint($this->api_login_id,
            $transaction_key, $amount, $fp_sequence, $fp_timestamp)

        $this->template->content = View::factory('payment/page')
            ->set('api_login_id', $this->api_login_id)
            ->set('amount = $amount)
            ->set('fp_timestamp = $fp_timestamp)
            ->set('fp_sequence = $fp_sequence)
            ->set('fingerprint', $fingerprint);
    }
}

Since you asked, accessing the POST variables from the controller:

既然你问过,从控制器访问POST变量:

$transaction_key = $_POST['transaction_key'];

View file (views/payment/page.php):

查看文件(views / payment / page.php):

<form method='post' action="payment/authorize">
<input type='hidden' name="x_login" value="<?php echo $api_login_id?>" />
<input type='hidden' name="x_fp_hash" value="<?php echo $fingerprint?>" />
<input type='hidden' name="x_amount" value="<?php echo $amount?>" />
<input type='hidden' name="x_fp_timestamp" value="<?php echo $fp_timestamp?>" />
<input type='hidden' name="x_fp_sequence" value="<?php echo $fp_sequence?>" />
<input type='hidden' name="x_version" value="3.1">
<input type='hidden' name="x_show_form" value="payment_form">
<input type='hidden' name="x_test_request" value="false" />
<input type='hidden' name="x_method" value="cc">
<input type='submit' value="Click here for the secure payment form">
</form>

#1


1  

Store your Authorize.Net SDK classes in application/vendor/anet_php_sdk/

将Authorize.Net SDK类存储在application / vendor / anet_php_sdk /中

Demo Controller Method:

演示控制器方法:

class Controller_Payment extends Controller_Template
{
    // Set properties at the top of the class for easier changes later
    $this->authorize_url = 'https://test.authorize.net/gateway/transact.dll';
    $this->api_login_id = 'I_put_my_login_id_here';

    public function action_authorize()
    {
        require Kohana::find_file('vendor', 'anet_php_sdk/AuthorizeNet');

        if($_POST)
        {
            $request = curl_init($this->authorize_url);

            // Build your post string
            $post_string = http_build_query($_POST);

            // The rest of the Authorize.net code
            curl_setopt($request, CURLOPT_HEADER, 0);
            curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($request, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
            $post_response = curl_exec($request);
            curl_close($request);

            $response_array = explode($post_values["x_delim_char"],$post_response);

            // Look in your SDK docs to determine the actual values you need
            // to check to verify that the transaction was successful:
            if($response_array['code'] == 200)
            {
                Request::current()->redirect('success/page');
            }
            else
            {
                // Error handling
            }
        }

        $transaction_key = 'transaction_key_here;
        $amount = "5.99";
        $fp_timestamp = time();
        $fp_sequence = "123" . time(); // Enter an invoice or other unique number.
        $fingerprint = AuthorizeNetSIM_Form::getFingerprint($this->api_login_id,
            $transaction_key, $amount, $fp_sequence, $fp_timestamp)

        $this->template->content = View::factory('payment/page')
            ->set('api_login_id', $this->api_login_id)
            ->set('amount = $amount)
            ->set('fp_timestamp = $fp_timestamp)
            ->set('fp_sequence = $fp_sequence)
            ->set('fingerprint', $fingerprint);
    }
}

Since you asked, accessing the POST variables from the controller:

既然你问过,从控制器访问POST变量:

$transaction_key = $_POST['transaction_key'];

View file (views/payment/page.php):

查看文件(views / payment / page.php):

<form method='post' action="payment/authorize">
<input type='hidden' name="x_login" value="<?php echo $api_login_id?>" />
<input type='hidden' name="x_fp_hash" value="<?php echo $fingerprint?>" />
<input type='hidden' name="x_amount" value="<?php echo $amount?>" />
<input type='hidden' name="x_fp_timestamp" value="<?php echo $fp_timestamp?>" />
<input type='hidden' name="x_fp_sequence" value="<?php echo $fp_sequence?>" />
<input type='hidden' name="x_version" value="3.1">
<input type='hidden' name="x_show_form" value="payment_form">
<input type='hidden' name="x_test_request" value="false" />
<input type='hidden' name="x_method" value="cc">
<input type='submit' value="Click here for the secure payment form">
</form>