Markus Proctor
Markus Proctor

Reputation: 435

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.

When I var_dump() the response ...all I get is

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?

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

{
    "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"
  }]
}

Upvotes: 0

Views: 1742

Answers (2)

Markus Proctor
Markus Proctor

Reputation: 435

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. You cannot test your code through

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

if the account is set to LIVE.

I created testing credentials with this link

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50787

We do use the Authorize.net SDK in our Laravel 5.2 project. Our composer package is the exact same as yours. However, we simply interact directly with the AuthorizeNetAIM. Our payment processing occurs inside of a Job as well. 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.

Upvotes: 1

Related Questions