PHP code example of yohns / authorize.net-payments

1. Go to this page and download the library: Download yohns/authorize.net-payments library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yohns / authorize.net-payments example snippets




use Yohns\Payments\BillingAddress;
use Yohns\Payments\CreditCard;
use Yohns\Payments\LineItem;
use Yohns\Payments\PaymentGateway;

$gateway = new PaymentGateway(
    apiLoginId:     'YOUR_API_LOGIN_ID',
    transactionKey: 'YOUR_TRANSACTION_KEY',
    sandbox:        true, // set false for production
);

$items = [
    new LineItem('SKU-001', 'Blue Dream 3.5g',  45.00, 2, '3.5g premium flower', taxable: true),
    new LineItem('SKU-002', 'Gelato Cartridge', 55.00, 1, '1g live resin cart',  taxable: true),
    new LineItem('SKU-003', 'Glass One-Hitter', 12.00, 1, 'Borosilicate pipe',   taxable: false),
];

$billing = new BillingAddress(
    firstName: 'Jane',
    lastName:  'Doe',
    address:   '420 High St',
    city:      'Denver',
    state:     'CO',
    zip:       '80202',
);

$card = new CreditCard(
    cardNumber:     '4111111111111111',
    expirationDate: '2027-09', // YYYY-MM
    cvv:            '123',
);

$result = $gateway->charge(
    card:        $card,
    billing:     $billing,
    lineItems:   $items,
    tax:         9.45,
    shipping:    5.00,
    invoiceNum:  'INV-1042',
    description: 'Online order',
);

if ($result->isSuccess()) {
    echo 'Approved! Transaction ID: ' . $result->getTransactionId();
} else {
    echo 'Failed: ' . $result->getErrorText();
}

$gateway = new PaymentGateway(
    apiLoginId:     string,
    transactionKey: string,
    sandbox:        bool = true,
);

$result = $gateway->charge(
    card:        CreditCard,
    billing:     BillingAddress,
    lineItems:   LineItem[],   // 1–30 items ars
    description: string = '',  // max 255 chars
    customerId:  string = '',  // your internal customer ID, max 20 chars
    customerIp:  string = '',  // enables fraud detection filters
): PaymentResult

$result = $gateway->authorizeOnly(
    card:        CreditCard,
    billing:     BillingAddress,
    lineItems:   LineItem[],
    tax:         float = 0.00,
    shipping:    float = 0.00,
    discount:    float = 0.00,
    invoiceNum:  string = '',
    description: string = '',
): PaymentResult

$result = $gateway->captureAuthorized(
    priorTransactionId: string,
    amount:             float,
): PaymentResult

$result = $gateway->void(transactionId: string): PaymentResult

$result = $gateway->refund(
    transactionId:   string, // original transaction ID
    maskedCardNum:   string, // last 4 digits of card, e.g. "1111"
    expirationDate:  string, // YYYY-MM
    amount:          float,  // can be less than original for partial refund
): PaymentResult

$item = new LineItem(
    itemId:      string, // max 31 chars — your SKU or product ID
    name:        string, // max 31 chars — display name
    unitPrice:   float,
    quantity:    int    = 1,
    description: string = '', // max 255 chars
    taxable:     bool   = false,
);

$item->getTotal(); // unitPrice × quantity

$card = new CreditCard(
    cardNumber:     string, // spaces and dashes are stripped automatically
    expirationDate: string, // must be YYYY-MM format
    cvv:            string = '',
);

$billing = new BillingAddress(
    firstName: string,
    lastName:  string,
    address:   string,
    city:      string,
    state:     string,
    zip:       string,
    country:   string = 'US',
    company:   string = '',
    phone:     string = '',
    email:     string = '',
);

$result->isSuccess(): bool
$result->isDeclined(): bool    // responseCode === 2
$result->isHeld(): bool        // responseCode === 4 (held for review)

$result->getTransactionId(): string
$result->getAuthCode(): string
$result->getResponseCode(): int    // 1=Approved, 2=Declined, 3=Error, 4=Held
$result->getResponseText(): string
$result->getAvsResultCode(): string
$result->getCvvResultCode(): string
$result->getAccountNumber(): string  // masked, e.g. XXXX1111
$result->getAccountType(): string    // e.g. Visa, Mastercard
$result->getErrorCode(): ?string
$result->getErrorText(): ?string

$result->toArray(): array

// Sandbox (testing)
$gateway = new PaymentGateway('login', 'key', sandbox: true);

// Production (live charges)
$gateway = new PaymentGateway('login', 'key', sandbox: false);

src/
    BillingAddress.php
    CreditCard.php
    LineItem.php
    PaymentGateway.php
    PaymentResult.php
composer.json
README.md
example.php