简体   繁体   English

将Authorize.net SDK与Laravel结合使用

[英]Using Authorize.net SDK with Laravel

I cannot get the Authorize.net SDK working with Laravel 5.2 whatsoever and I've spent days running into issues. 无论如何,我无法使Authorize.net SDK与Laravel 5.2一起使用,并且花了好几天时间解决问题。

When I var_dump() the response ...all I get is 当我var_dump()时,响应...我得到的只是

object(net\authorize\api\contract\v1\ARBCreateSubscriptionResponse)#413 (4) `{ ["subscriptionId":"net\authorize\api\contract\v1\ARBCreateSubscriptionResponse":private]=> NULL ["refId":"net\authorize\api\contract\v1\ANetApiResponseType":private]=> NULL ["messages":"net\authorize\api\contract\v1\ANetApiResponseType":private]=> NULL ["sessionToken":"net\authorize\api\contract\v1\ANetApiResponseType":private]=> NULL }`

Has anyone been able to get authorize.net working with Laravel and could potentially show me how they did it? 有没有人能够与Laravel一起使用authorize.net,并有可能向我展示他们是如何做到的?

my attempt at simple charging 我尝试简单充电

$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
        $merchantAuthentication->setName("snip");
        $merchantAuthentication->setTransactionKey("snip");
        $refId = 'ref' . time();

        $creditCard = new AnetAPI\CreditCardType();
        $creditCard->setCardNumber("6011000000000012");
        $creditCard->setExpirationDate("2028-12");
        $paymentOne = new AnetAPI\PaymentType();
        $paymentOne->setCreditCard($creditCard);

        // Create a transaction
        $transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType( "authCaptureTransaction"); 
        $transactionRequestType->setAmount(151.51);
        $transactionRequestType->setPayment($paymentOne);

        $request = new AnetAPI\CreateTransactionRequest();
        $request->setMerchantAuthentication($merchantAuthentication);
        $request->setTransactionRequest( $transactionRequestType);
        $controller = new AnetController\CreateTransactionController($request);
        $response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
        var_dump($response);
        echo "<br><br>";
        if ($response != null)
        {
            $tresponse = $response->getTransactionResponse();

if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )   
            {
                echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
                echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "\n";
            }
            else
            {
                echo  "Charge Credit Card ERROR :  Invalid response\n";
            }
        }
        else
        {
            echo  "Charge Credit card Null response returned";
        }

my composer.json 我的composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*",
        "guzzlehttp/guzzle": "^6.0",
        "ext-curl": "^0.0.0",
        "authorizenet/authorizenet": "^1.8"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "symfony/css-selector": "2.8.*|3.0.*",
        "symfony/dom-crawler": "2.8.*|3.0.*",
        "goetas/xsd2php": "2.0.x-dev#fdc2ab0bb3f2b3ab796ca567cf8c0f3446a7ea3a"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": ["app/Http/helpers.php"]
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "repositories": [{
       "type": "vcs",
       "url": "https://github.com/goetas/serializer.git"
  }]
}

We do use the Authorize.net SDK in our Laravel 5.2 project. 我们确实在Laravel 5.2项目中使用Authorize.net SDK Our composer package is the exact same as yours. 我们的作曲家软件包与您的完全相同。 However, we simply interact directly with the AuthorizeNetAIM . 但是,我们只是直接与AuthorizeNetAIM交互。 Our payment processing occurs inside of a Job as well. 我们的付款处理也发生在Job内部。 Here's a quick idea of what ours looks like: 以下是我们的外观的简要说明:

public function handle()
{
    $this->aim = new AuthorizeNetAIM();

    //functionality to add line items:
    $this->aim->addLineItem($identifier, $name, null, $quantity, $price_per_item);

    $this->aim->amount = $this->order->total;
    $this->aim->card_num = $this->card_ref->card_num
    $this->aim->exp_date = $this->card_ref->exp_date
    $this->aim->first_name = $this->card_ref->first_name;
    $this->aim->last_name = $this->card_ref->last_name;
    $this->aim->address = $this->order->billing_address_1 . ' ' . $this->order->billing_address_2;
    $this->aim->city = $this->order->billing_city;
    $this->aim->state = $this->order->billing_state;
    $this->aim->zip = $this->order->billing_zip_code;
    $this->aim->country = $this->order->billing_country;
    $this->aim->email = $this->order->contact->email;
    $this->aim->phone = $this->order->contact->phone;
    $this->aim->ship_to_address = $this->order->shipping_address_1 . ' ' . $this->order->shipping_address_2;
    $this->aim->ship_to_city = $this->order->shipping_city;
    $this->aim->ship_to_country = $this->order->shipping_country;
    $this->aim->ship_to_first_name = $this->order->first_name;
    $this->aim->ship_to_last_name = $this->order->last_name;
    $this->aim->ship_to_state = $this->order->shipping_state;
    $this->aim->ship_to_zip = $this->order->shipping_zip_code;

    //You may also need to assign additional  fields required by your Gateway configuration, do so here:
    $this->aim->setField('card_code', $this->card_ref->cvc);
    $this->aim->setField('invoice_num', $this->order->invoice_num);
    $this->aim->setField('po_num', $this->order->id);

    //now we assign our "ref" as value so we can rerun the transaction in the event of failure.
    $this->aim->setCustomField('ref', 'ref' . time());

    //Finally we auth and capture
    $response = $this->aim->authorizeAndCapture();

    //now you can dump the response
    dd($response);

}

Hopefully this is helpful to you. 希望这对您有帮助。

I am writing an answer in case anyone in the future stumbles upon this issue. 我正在写一个答案,以防将来有人偶然发现此问题。

The code was not working because my authorize.net account was set to LIVE instead of TEST. 该代码不起作用,因为我的authorize.net帐户设置为LIVE而不是TEST。 You cannot test your code through 您无法通过以下方式测试代码

$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);

if the account is set to LIVE. 如果帐户设置为LIVE。

I created testing credentials with this link 我通过此链接创建了测试凭证

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

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