Authorize.Net PHP SDK

repository·master·Indexed 19 days ago

https://github.com/authorizenet/sdk-php

A structured PHP SDK for integrating with Authorize.Net payment processing APIs. It supports sandbox and production environments, TLS 1.2 enforcement, and sensitive data masking in logs. The SDK provides tools for the Advanced Integration Method (AIM), Automated Recurring Billing (ARB) for subscription management, and Customer Information Manager (CIM) for customer profile management.

Tokens
17.4K
Snippets
48
Records
62
Agent score
66%

What's inside authorizenet-sdk-php

  1. Use the Transaction Details API with AuthorizeNetTD

    master

    The AuthorizeNetTD class is used to interact with the Authorize.Net Transaction Details API. It allows you to retrieve specific transaction information, settled batch lists, and transaction lists.

    Responses from the API are returned as objects that can be navigated using PHP's SimpleXML syntax or via xpath queries.

    $request = new AuthorizeNetTD;
    // Use methods like getTransactionDetails, getSettledBatchList, etc.
  2. How the Advanced Integration Method (AIM) works

    master

    The AuthorizeNetAIM class is used to create request objects for submitting transactions to the Authorize.Net AIM API.

    Workflow:

    1. Create an instance of AuthorizeNetAIM.
    2. Set transaction fields (amount, card details, etc.).
    3. Call a transaction method (e.g., authorizeAndCapture()).
    4. Receive an AuthorizeNetAIM_Response object containing the results.

    Note that the SDK removes the x_ prefix from AIM API fields. For example, use $sale->card_num instead of $sale->x_card_num.

    $sale = new AuthorizeNetAIM();
    $sale->amount = "1999.99";
    $sale->card_num = '6011000000000012';
    $sale->exp_date = '04/15';
    $response = $sale->authorizeAndCapture();
  3. Use Validation Mode for Customer Profiles

    master

    Validation mode allows you to verify field validity without performing a real charge.

    • Test Mode: Only performs field validation.
    • Live Mode: Generates a $0.00 or $0.01 transaction that is immediately voided.

    To use it, pass "testMode" as the second argument to createCustomerProfile.

    You can inspect validation results using $response->getValidationResponses().

    $response = $request->createCustomerProfile($customerProfile, "testMode");
    
    $validationResponses = $response->getValidationResponses();
    foreach ($validationResponses as $vr) {
        echo $vr->approved;
    }
  4. Use the Card Present (CP) API

    master

    The AuthorizeNetCP class is used to create request objects for submitting transactions to the AuthorizeNet Card Present (CP) API.

    Key details:

    • AuthorizeNetCP extends the AuthorizeNetAIM class. For general transaction basics (like authentication and response handling), refer to the AIM documentation.
    • Important: If you are using both Card Not Present (CNP) and Card Present (CP) APIs, you must use different merchant credentials for each.
    // AuthorizeNetCP extends AuthorizeNetAIM
    $sale = new AuthorizeNetCP(CP_API_LOGIN_ID, CP_TRANSACTION_KEY);
  5. Use the AuthorizeNetSOAP class for SOAP API access

    master
    The AuthorizeNetSOAP class acts as a lightweight wrapper around PHP's native SoapClient. It simplifies connecting to the Authorize.Net SOAP API by pre-configuring the necessary WSDL, Sandbox, and Live Production server URLs. Use this class when you need to interact with the SOAP interface rather than the REST API.
  6. Install the Authorize.Net PHP SDK via Composer

    master

    The recommended way to install the SDK is using Composer. Add the authorizenet/authorizenet package to your composer.json file and run composer update.

    After installation, ensure you require the Composer autoloader in your script or bootstrap file to access the SDK classes.

    {
      "require": {
        "php": ">=5.6",
        "authorizenet/authorizenet": "2.0.3"
      }
    }
    require 'vendor/autoload.php';
  7. Migrate from legacy Authorize.Net classes to new API models

    master

    The PHP SDK no longer supports several legacy classes (e.g., AuthorizeNetAIM.php, AuthorizeNetSIM.php). To ensure compatibility and access modern features, you must migrate your code to use the new Authorize.Net API classes.

    Use the following mapping to identify the replacement feature for your legacy class:

    Legacy ClassNew Feature / Replacement
    AuthorizeNetAIM.phpPaymentTransactions
    AuthorizeNetARB.phpRecurringBilling
    AuthorizeNetCIM.phpCustomerProfiles
    Hosted CIMAccept Customer
    AuthorizeNetCP.phpPaymentTransactions
    AuthorizeNetDPM.phpAccept.JS
    AuthorizeNetSIM.phpAccept Hosted
    AuthorizeNetSOAP.phpPaymentTransactions
    AuthorizeNetTD.phpTransactionReporting

    For a detailed upgrade guide, visit the official Authorize.Net API Upgrade Guide.

  8. Create or update subscriptions using the ARB API

    master

    To manage subscriptions via the Automated Recurring Billing (ARB) API, you must first instantiate an AuthorizeNet_Subscription object and populate its properties. Once the subscription object is configured, use an AuthorizeNetARB instance to submit the request.

    Creating a Subscription

    1. Create an AuthorizeNet_Subscription object.
    2. Set properties such as name, intervalLength, intervalUnit, startDate, totalOccurrences, amount, and payment details (creditCardCardNumber, creditCardExpirationDate, creditCardCardCode).
    3. Call $request->createSubscription($subscription) on an AuthorizeNetARB instance.

    Updating a Subscription

    To update an existing subscription, call $request->updateSubscription($subscription_id, $subscription) on an AuthorizeNetARB instance, passing the unique subscription ID and the updated AuthorizeNet_Subscription object.

    $subscription = new AuthorizeNet_Subscription;
    $subscription->name = "Short subscription";
    $subscription->intervalLength = "1";
    $subscription->intervalUnit = "months";
    $subscription->startDate = "2011-03-12";
    $subscription->totalOccurrences = "14";
    $subscription->amount = rand(1,100);
    $subscription->creditCardCardNumber = "6011000000000012";
    $subscription->creditCardExpirationDate = "2018-10";
    $subscription->creditCardCardCode = "123";
    $subscription->billToFirstName = "john";
    $subscription->billToLastName = "doe";
    
    $request = new AuthorizeNetARB;
    $response = $request->createSubscription($subscription);
    
    // Or to update:
    // $response = $request->updateSubscription($subscription_id, $subscription);
  9. Create and Void Transactions

    master

    Transactions are created using the createCustomerProfileTransaction method on an AuthorizeNetCIM instance. You must specify the transaction type (e.g., "AuthCapture" or "Void") and an AuthorizeNetTransaction object.

    An AuthorizeNetTransaction can include:

    • amount: The transaction amount.
    • customerProfileId: The ID of the customer.
    • customerPaymentProfileId: The ID of the payment method.
    • customerShippingAddressId: The ID of the shipping address.
    • lineItems: An array of AuthorizeNetLineItem objects.

    To void a transaction, use the type "Void" and provide the transId in the transaction object.

    // Create Auth & Capture Transaction
    $transaction = new AuthorizeNetTransaction;
    $transaction->amount = "9.79";
    $transaction->customerProfileId = $customerProfileId;
    $transaction->customerPaymentProfileId = $paymentProfileId;
    $transaction->customerShippingAddressId = $customerAddressId;
        
    $lineItem = new AuthorizeNetLineItem;
    $lineItem->itemId = "4";
    $lineItem->name = "Cookies";
    $lineItem->quantity = "4";
    $lineItem->unitPrice = "1.00";
    $lineItem->taxable = "true";
    
    $transaction->lineItems[] = $lineItem;
        
    $response = $request->createCustomerProfileTransaction("AuthCapture", $transaction);
    $transactionResponse = $response->getTransactionResponse();
    $transactionId = $transactionResponse->transaction_id;
    
    // Voiding a Transaction
    $voidTransaction = new AuthorizeNetTransaction;
    $voidTransaction->transId = $transactionId;
    $response = $request->createCustomerProfileTransaction("Void", $voidTransaction);
  10. Implement the Server Integration Method (SIM) with AuthorizeNetSIM_Form

    master

    The Server Integration Method (SIM) allows you to offload the payment experience to Authorize.Net. The AuthorizeNetSIM_Form class helps automate the creation of the hidden form fields required to securely initiate a transaction.

    To create a 'Buy Now' button that redirects users to a hosted order page, you must generate a fingerprint using getFingerprint and then instantiate AuthorizeNetSIM_Form with the required transaction parameters. This ensures the request is authenticated and tied to a specific amount and sequence.

    <form method="post" action="https://test.authorize.net/gateway/transact.dll">
    <?php
    $amount = "9.99";
    $fp_sequence = "123";
    $time = time();
    
    $fingerprint = AuthorizeNetSIM_Form::getFingerprint($api_login_id, $transaction_key, $amount, $fp_sequence, $time);
    $sim = new AuthorizeNetSIM_Form(
        array(
        'x_amount'        => $amount,
        'x_fp_sequence'   => $fp_sequence,
        'x_fp_hash'       => $fingerprint,
        'x_fp_timestamp'  => $time,
        'x_relay_response'=> "FALSE",
        'x_login'         => $api_login_id,
        )
    );
    echo $sim->getHiddenFieldString();?>
    <input type="submit" value="Buy Now">
    </form>
  11. Handle Relay Responses with the AuthorizeNetSIM class

    master

    When using SIM, Authorize.Net can post transaction details back to your server via a 'Relay Response'. You can configure this URL in the Merchant Interface or by including the x_relay_url field in your initial SIM form submission.

    To process this response, use the AuthorizeNetSIM class. This class allows you to verify that the incoming POST request actually originated from Authorize.Net using isAuthorizeNet() and provides easy access to transaction properties like approved and cust_id.

    $response = new AuthorizeNetSIM;
    if ($response->isAuthorizeNet())
    {
      if ($response->approved)
      {
        // Activate magazine subscription
        magazine_subscription_activate($response->cust_id);
      }
    }
  12. Install the Authorize.Net PHP SDK using a Custom SPL Autoloader

    master

    If Composer cannot be run on your target system, you can use the provided custom SPL autoloader. You must still have the vendor directory and all dependencies present (e.g., by running Composer on a different machine and copying the directory over).

    Reference the autoload.php file within the SDK directory.

    require 'path/to/anet_php_sdk/autoload.php';