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();
}
$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 = '',
);