Authorize.net Payment Gateway Integration With Laravel 5.8

I can teach you how you can integrate Authorize.net with Laravel 5.8In this tutorial i will use Authorize sandbox account.

So lets begin with a new installation of Laravel 5.8:

composer create-project --prefer-dist laravel/laravel authorize_payment_gateway "5.8.*"

Open your composer.json file and add the following codes in require:

{
  "require": {
  "php": ">=5.6",
  "authorizenet/authorizenet": "2.0.0"
  }
}

Run the following command in your console terminal:

composer update

GET API LOGIN ID and TRANSACTION KEY FROM AUTHORIZE.NET:

Now, you have to get your API Login ID and transaction key from authorize.net . For this purpose, you have to first sign up at  Create  Sandbox Account Authorize site and then sign in. Then goto Account ->Security Settings -> API Credentials & Keys. You can see API Login ID there and for Transaction Key. Create New Key(s) then they will send pin code on your email, copy pin from email and paste pin there then you will get your transaction key.

Now back to your laravel project.

Now create a new controller with command:

php artisan make:controller AuthorizeCheckoutController

Your Authorize Checkout Controller will be as below:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
class AuthorizeCheckoutController extends Controller
{
    public function index()
    {
        return view('authorize');
    }
    public function chargeCreditCard(Request $request)
    {

        // Common setup for API credentials
 $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName('YOUR_AUTHORIZE_PAYMENT_API_LOGIN_ID');
$merchantAuthentication->setTransactionKey('YOUR_AUTHORIZE_PAYMENT_TRANSACTION_KEY');

        $refId = 'ref'.time();
// Create the payment data for a credit card
        $creditCard = new AnetAPI\CreditCardType();
        $creditCard->setCardNumber('4111111111111111');
        $creditCard->setCardCode($request->code);
//         $creditCard->setExpirationDate( "2038-12");
 $creditCard->setExpirationDate($request->card_expiry_month_year);
        $paymentOne = new AnetAPI\PaymentType();
        $paymentOne->setCreditCard($creditCard);
// Create a transaction
        $transactionRequestType = new AnetAPI\TransactionRequestType();
        $transactionRequestType->setTransactionType("authCaptureTransaction");
//        $transactionRequestType->setAmount('60');
        $transactionRequestType->setAmount($request->amount);
        $transactionRequestType->setPayment($paymentOne);
        $request = new AnetAPI\CreateTransactionRequest();
        $request->setMerchantAuthentication($merchantAuthentication);
        $request->setRefId( $refId);
        $request->setTransactionRequest($transactionRequestType);
        $controller = new AnetController\CreateTransactionController($request);
        $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);

        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";
              //here save data in your database

            }
            else
            {

                echo "Charge Credit Card ERROR :  Invalid response\n";
            }
        }
        else
        {
            echo  "Charge Credit Card Null response returned";
        }

        return redirect('/');
    }
}

Now, open your routes/web.php file:

Route::get('/checkout', 'AuthorizeCheckoutController@index'); 
Route::post('/checkout', 'AuthorizeCheckoutController@chargeCreditCard'); 

Create a new view blade file in resources/views/authorize.blade.php. Your authorize.blade.php file will be like below:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="content">
            <h1 >Laravel Authorize Payment Integration</h1><br>
            <form class="" action="{{ url('/checkout') }}" method="post">

                <h3>Credit Card Information</h3>
                <div class="form-group">
                    <label for="cnumber">Card Number</label>
                    <input type="number" class="form-control" id="cnumber" name="cnumber" placeholder="Enter Card Number eg.4111111111111111">
                </div>
                <div class="form-group">
                    <label for="card-expiry-month-year">Expiration Month and Year</label>
                    <input type="month" name="card_expiry_month_year" class="form-control" required>
                </div>
                <div class="form-group">
                    <label for="ccode">Card Code</label>
                    <input type="number" class="form-control" name="code" placeholder="Enter Card Code">
                </div>
                <div class="form-group">
                    <label for="amount">Amount</label>
                    <input type="number" class="form-control"  name="amount" placeholder="Enter Amount" >
                </div>
                {{ csrf_field() }}
                <button type="submit" class="btn btn-success">Confirm</button>
            </form>
        </div>

    </div>
</div>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *