PHP code example of stymiee / authnetjson
1. Go to this page and download the library: Download stymiee/authnetjson 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/ */
stymiee / authnetjson example snippets
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->getTransactionDetailsRequest([
'transId' => '2162566217'
]);
if ($response->isSuccessful()) {
echo $response->transaction->transactionStatus;
}
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->createTransactionRequest([
'refId' => rand(1000000, 100000000),
'transactionRequest' => [
'transactionType' => 'authCaptureTransaction',
'amount' => 5,
'payment' => [
'creditCard' => [
'cardNumber' => '4111111111111111',
'expirationDate' => '122026',
'cardCode' => '999',
]
]
]
]);
if ($response->isSuccessful()) {
echo $response->transactionResponse->authCode;
}
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->createTransactionRequest([
'refId' => rand(1000000, 100000000),
'transactionRequest' => [
'transactionType' => 'authCaptureTransaction',
'amount' => 5,
'payment' => [
'creditCard' => [
'cardNumber' => '4111111111111111',
'expirationDate' => '122026',
'cardCode' => '999',
],
],
'order' => [
'invoiceNumber' => '1324567890',
'description' => 'this is a test transaction',
],
'lineItems' => [
'lineItem' => [
0 => [
'itemId' => '1',
'name' => 'vase',
'description' => 'Cannes logo',
'quantity' => '18',
'unitPrice' => '45.00'
],
1 => [
'itemId' => '2',
'name' => 'desk',
'description' => 'Big Desk',
'quantity' => '10',
'unitPrice' => '85.00'
]
]
],
'tax' => [
'amount' => '4.26',
'name' => 'level2 tax name',
'description' => 'level2 tax',
],
'duty' => [
'amount' => '8.55',
'name' => 'duty name',
'description' => 'duty description',
],
'shipping' => [
'amount' => '4.26',
'name' => 'level2 tax name',
'description' => 'level2 tax',
],
'poNumber' => '456654',
'customer' => [
'id' => '18',
'email' => '[email protected] ',
],
'billTo' => [
'firstName' => 'Ellen',
'lastName' => 'Johnson',
'company' => 'Souveniropolis',
'address' => '14 Main Street',
'city' => 'Pecan Springs',
'state' => 'TX',
'zip' => '44628',
'country' => 'USA',
],
'shipTo' => [
'firstName' => 'China',
'lastName' => 'Bayles',
'company' => 'Thyme for Tea',
'address' => '12 Main Street',
'city' => 'Pecan Springs',
'state' => 'TX',
'zip' => '44628',
'country' => 'USA',
],
'customerIP' => '192.168.1.1',
'transactionSettings' => [
'setting' => [
0 => [
'settingName' =>'allowPartialAuth',
'settingValue' => 'false'
],
1 => [
'settingName' => 'duplicateWindow',
'settingValue' => '0'
],
2 => [
'settingName' => 'emailCustomer',
'settingValue' => 'false'
],
3 => [
'settingName' => 'recurringBilling',
'settingValue' => 'false'
],
4 => [
'settingName' => 'testRequest',
'settingValue' => 'false'
]
]
],
'userFields' => [
'userField' => [
0 => [
'name' => 'MerchantDefinedFieldName1',
'value' => 'MerchantDefinedFieldValue1',
],
1 => [
'name' => 'favorite_color',
'value' => 'blue',
],
],
],
],
]);
if ($response->isSuccessful()) {
echo $response->transactionResponse->authCode;
}
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->createCustomerProfileRequest([
'profile' => [
'merchantCustomerId' => '12345',
'email' => '[email protected] ',
'paymentProfiles' => [
'billTo' => [
'firstName' => 'John',
'lastName' => 'Smith',
'address' => '123 Main Street',
'city' => 'Townsville',
'state' => 'NJ',
'zip' => '12345',
'phoneNumber' => '800-555-1234'
],
'payment' => [
'creditCard' => [
'cardNumber' => '4111111111111111',
'expirationDate' => '2026-08',
],
],
],
'shipToList' => [
'firstName' => 'John',
'lastName' => 'Smith',
'address' => '123 Main Street',
'city' => 'Townsville',
'state' => 'NJ',
'zip' => '12345',
'phoneNumber' => '800-555-1234'
],
],
'validationMode' => 'liveMode'
]);
if ($response->isSuccessful()) {
echo $response->customerProfileId;
}
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->ARBCreateSubscriptionRequest([
'refId' => 'Sample',
'subscription' => [
'name' => 'Sample subscription',
'paymentSchedule' => [
'interval' => [
'length' => '1',
'unit' => 'months'
],
'startDate' => '2020-04-18',
'totalOccurrences' => '12',
'trialOccurrences' => '1'
],
'amount' => '10.29',
'trialAmount' => '0.00',
'payment' => [
'creditCard' => [
'cardNumber' => '4111111111111111',
'expirationDate' => '2016-08'
]
],
'billTo' => [
'firstName' => 'John',
'lastName' => 'Smith'
]
]
]);
if ($response->isSuccessful()) {
echo $response->subscriptionId;
}
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->getSettledBatchListRequest([
')) {
foreach ($response->batchList as $batch) {
echo $batch->batchId;
}
}
$response->getTransactionResponseField('TransactionID');
$response->getTransactionResponseField(7);
$response = $request->createWebhooks([
"net.authorize.customer.subscription.expiring",
"net.authorize.customer.subscription.suspended",
"net.authorize.payment.authcapture.created",
"net.authorize.payment.authorization.created",
"net.authorize.payment.capture.created",
"net.authorize.payment.fraud.approved",
"net.authorize.payment.fraud.declined",
"net.authorize.payment.fraud.held",
"net.authorize.payment.priorAuthCapture.created",
"net.authorize.payment.refund.created",
"net.authorize.payment.void.created"
], 'http://www.example.com:55950/api/webhooks', 'active');
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload);
if ($webhook->isValid()) {
// Access notifcation values
// echo $webhook->eventType;
}
$headers = yourGetHeadersFunction();
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload, $headers);
if ($webhook->isValid()) {
// Access notifcation values
// echo $webhook->eventType;
}
$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY);
$response = $request->getUnsettledTransactionListRequest();
echo $request, $response;