PHP Classes

PHP Authorize.Net Tutorial on Making Easier Payments through its API - PHP Authorize.net Integration with JSON API package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Authorize.net Integration with JSON API PHP Authorize.net Integration with JSON API   Blog PHP Authorize.net Integration with JSON API package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Authorize.Net Tut...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2020-06-15

Posted on:

Package: PHP Authorize.net Integration with JSON API

Authorize.Net is a popular payment gateway that is used by many e-commerce Web sites and applications to process payments of products and services that those sites sell.

It supports many useful features like managing subscriptions, payment profiles, fraud management and sales reporting.

Read this tutorial to learn how to use the Authorize.net API to integrate payment processing to sell more products and services in your Web site or application using the PHP Authorize.net Integration with JSON API package.




In this article you will learn:

What is the PHP Authorize.net Integration with JSON API package?

How to Use the PHP Authorize.net Integration with JSON API package?

How to Create a Payment Request Transaction?

How to Get the Payment Transaction Request Response?

How to Create Recurring Payment Requests?

How to Get Payment Transaction Responses using Web hooks?

How to Debug the Payment Requests and Responses?

Downloading All Package Files

Installing the Package with Composer

Authorize.net Logo from https://upload.wikimedia.org/wikipedia/en/1/17/Authorizenet_logo.png

What is the PHP Authorize.net Integration with JSON API package?

The Authorize.Net service provides an API that allows e-commerce sites to process payments for sales that happen in those sites.

The PHP Authorize.Net Integration with JSON API package offers access to the entire JSON API offered by Authorize.Net that include:

  • Use the Accept.js JavaScript library to send payments directly to Authorize.net
  • Handle regular sales payments
  • Handle subscription sales payments
  • Create payment profiles
  • Handle fraud attempts
  • Provide transaction reporting
  • Use Web hooks to capture payment transaction reports.

How to Use the PHP Authorize.net Integration with JSON API package?

To work with this library you do not need an actual production account with Authorize.Net or a merchant account. Authorize.Net offers a sandbox account for developers that allows for full testing of an integration with their APIs without making actual payments.

With this package calls are only two steps:

  1. Create an instance of the AuthnetJsonRequest class
  2. Make your API call and receive your response

To create an instance of the AuthnetJsonRequest class with which you can make your API call, you will call AuthnetApiFactory::getJsonApiHandler() with your API login and transaction key. These will be provided to you in your Authorize.Net control panel.

The third parameter indicates that you are testing with your sandbox account and to use the URL for it. If you omit this parameter you will hit the production API endpoint.

$request = AuthnetApiFactory::getJsonApiHandler(
    [API LOGIN], 
    [TRANSACTION KEY],
    AuthnetApiFactory::USE_DEVELOPMENT_SERVER
);

Once you have instantiated the object it is just a matter of passing the requisite parameters for the API you wish to make.

The method you will use is always the name of the API function you wish to call.

How to Create a Payment Request Transaction?

In the case of making a payment you will use the createTransactionRequest() method.

$response = $request->createTransactionRequest([
    'transactionRequest' => [
        'transactionType' => 'authCaptureTransaction',
        'amount' => 10,
        'payment' => [
            'creditCard' => [
                'cardNumber' => '4427802641004797',
                'expirationDate' => '122028',
                'cardCode' => '123',
            ],
        ],
    ],
]);

The structure of the array used to pass in the API call parameters follows the structure set forth in the JSON structure outlined in the Authorize.Net API reference. Working code examples for all of these APIs can be found in the examples directory in the library.

How to Get the Payment Transaction Request Response?

One you have your response you can access the values returned by Authorize.Net in your $response object. Just like the API calls, the structure of that object follows the structure set forth in the JSON structure outlined in the Authorize.Net API reference.

For your convenience, some helper methods have been added to test for successful, declined, and error states.

Below is a full example that show creating an API call and handling the response:

$request = AuthnetApiFactory::getJsonApiHandler(
    [API LOGIN],
    [TRANSACTION KEY],
    AuthnetApiFactory::USE_DEVELOPMENT_SERVER
);
$response = $request->createTransactionRequest([
    'transactionRequest' => [
        'transactionType' => 'authCaptureTransaction',
        'amount' => 10,
        'payment' => [
            'creditCard' => [
                'cardNumber' => '4427802641004797',
                'expirationDate' => '122022',
                'cardCode' => '123',
            ],
        ],
    ],
]);
if ($response->isSuccessful()) {
printf('Authorization Code: %s%s', $response->transactionResponse->authCode, PHP_EOL);
printf('Transaction Id: %s%s', $response->transactionResponse->transId, PHP_EOL);
} elseif ($response->isDeclined()) {
printf('Transaction is declined%s', PHP_EOL);
} else {
printf('Error Code: %s%s', $response->getErrorCode(), PHP_EOL);
printf('Error Message: %s%s', $response->getErrorText(), PHP_EOL);
}

How to Create Recurring Payment Requests?

Like mentioned above, this structure applies to the entire JSON API. Below is an example of creating a recurring subscription:

$request = AuthnetApiFactory::getJsonApiHandler(
    [API LOGIN],
    [TRANSACTION KEY],
    AuthnetApiFactory::USE_DEVELOPMENT_SERVER
);
$response = $request->ARBCreateSubscriptionRequest([
    'refId' => 'Sample',
    'subscription' => [
        'name' => 'Sample subscription',
        'paymentSchedule' => [
            'interval' => [
                'length' => '1',
                'unit' => 'months'
            ],
            'startDate' => '2020-04-18',
            'totalOccurrences' => AuthnetJson::BOUNDLESS_OCCURRENCES,
            'trialOccurrences' => '1'
        ],
        'amount' => '10.29',
        'trialAmount' => '0.00',
        'payment' => [
            'creditCard' => [
                'cardNumber' => '4427802641004797',
                'expirationDate' => '2026-08'
            ]
        ],
        'billTo' => [
            'firstName' => 'John',
            'lastName' => 'Smith'
        ]
    ]
]);
if ($response->isSuccessful()) {
printf('Subscription Id: %s%s', $response->subscriptionId, PHP_EOL);
} else {
printf('Error Code: %s%s', $response, PHP_EOL);
printf('Error Message: %s%s', $response, PHP_EOL);
}

How to Get Payment Transaction Responses using Web hooks?

Authorize.Net also supports Web hooks for managing transactions after they occur. The AuthnetJson library handles both the creation of and receiving of a Web hook notification. You can even then make another API call to get more information about the event sent in the Web hook notification.

Here is how you would create a Web hook for being notified on payment (you would replace the URL with your webhook notification URL):

$response = $request->createWebhooks([
    "net.authorize.payment.authcapture.created",
], 'http://www.example.com/api/webhooks', 'active');
Here's how you would use the passive listening for a Web hook notification and respond by getting additional transaction information. You can generate your signature in your Authorize.Net control panel:
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook([SIGNATURE], $payload);
if ($webhook->isValid()) {
    // Get the transaction ID
    $transactionId = $webhook->payload->id;
 
    // Here you can get more information about the transaction
    $request  = AuthnetApiFactory::getJsonApiHandler([API LOGIN], [TRANSACTION KEY]);
    $response = $request->getTransactionDetailsRequest([
        'transId' => $transactionId
    ]);
 
    /* You can put these response values in the database or whatever your business logic dictates.
    $response->transaction->transactionType
    $response->transaction->transactionStatus
    $response->transaction->authCode
    $response->transaction->AVSResponse
    */
}

How to Debug the Payment Requests and Responses?

To assist with debugging you can echo the $request and $response variable and see your configuration values as well as the JSON request and responses generated as a result of your API call.

$request = AuthnetApiFactory::getJsonApiHandler(
    [API LOGIN],
    [TRANSACTION KEY],
    AuthnetApiFactory::USE_DEVELOPMENT_SERVER
);
$response = $request->getUnsettledTransactionListRequest();
echo $request, $response;

Downloading All Package Files

If you want to use this package files in your development computer, you can just go here and download all package files in a single compressed archive in ZIP or tar.gz formats.

Installing the Package with Composer

If you want to use install this package files in your development or even in the production environment, you can just go here and find instructions to add this package to your projects' composer.json file.




You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP Authorize.net Integration with JSON API PHP Authorize.net Integration with JSON API   Blog PHP Authorize.net Integration with JSON API package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Authorize.Net Tut...  

For more information send a message to info at phpclasses dot org.