0

我正在尝试在 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>

因此,我假设用户访问我的网站并尝试购买产品。我将过程处理到控制器中。我将使用 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);
}

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

$authorize = new Authorize;
$authorize->api_login;
  • 给出一个错误。实际上我希望能够在 Kohana3.0 中实现 Authorize.NET 模块
4

1 回答 1

1

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

演示控制器方法:

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

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

$transaction_key = $_POST['transaction_key'];

查看文件(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>
于 2013-01-15T16:37:42.713 回答