PHP code example of squareetlabs / laravel-authorizenet
1. Go to this page and download the library: Download squareetlabs/laravel-authorizenet 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/ */
squareetlabs / laravel-authorizenet example snippets
namespace App\Models;
use Squareetlabs\AuthorizeNet\Traits\ANetPayments;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use ANetPayments;
// ... your model code
}
$user = User::find(1);
// All Authorize.Net operations are available via $user->anet()
$user->anet()->createCustomerProfile();
$response = $user->anet()->createCustomerProfile();
// Access the profile ID
$profileId = $response->getCustomerProfileId();
use Illuminate\Http\Request;
public function storePaymentProfile(Request $request)
{
$user = auth()->user();
$response = $user->anet()->createPaymentProfile(
$request->opaqueData,
$request->source
);
if ($response->getCustomerPaymentProfileId()) {
return response()->json([
'success' => true,
'payment_profile_id' => $response->getCustomerPaymentProfileId()
]);
}
return response()->json(['success' => false], 422);
}
// Get all payment methods
$paymentMethods = $user->anet()->getPaymentMethods();
// Get only card payment profiles
$cards = $user->anet()->getPaymentCardProfiles();
// Get only bank account payment profiles
$banks = $user->anet()->getPaymentBankProfiles();
// Get just the payment profile IDs
$profileIds = $user->anet()->getPaymentProfiles();
use Squareetlabs\AuthorizeNet\Models\UserPaymentProfile;
// Get all payment profiles for a user
$profiles = UserPaymentProfile::where('user_id', $user->id)->get();
// Get only cards using scope
$cards = UserPaymentProfile::where('user_id', $user->id)->cards()->get();
// Get only banks using scope
$banks = UserPaymentProfile::where('user_id', $user->id)->banks()->get();
// Access model properties
foreach ($profiles as $profile) {
echo $profile->payment_profile_id;
echo $profile->last_4;
echo $profile->brand;
echo $profile->type; // 'card' or 'bank'
}
// Charge $19.99 (amount in cents)
$response = $user->anet()->charge(1999, $paymentProfileId);
// Check if transaction was approved
if ($response->getMessages()->getResultCode() === 'Ok') {
$transactionResponse = $response->getTransactionResponse();
if ($transactionResponse->getResponseCode() === '1') {
$transactionId = $transactionResponse->getTransId();
// Transaction successful
}
}
// Refund $10.00 (amount in cents)
$response = $user->anet()->refund(
1000, // Amount in cents
$refTransId, // Reference transaction ID from charge
$paymentProfileId // Payment profile ID
);
// After charging a payment profile
$chargeResponse = $user->anet()->charge(1000, $paymentProfileId);
// Create transaction service instance
$transaction = $user->anet()->transactions($chargeResponse);
// Check if transaction was approved
if ($transaction->isApproved()) {
// Transaction was successful
}
// Check if request was successful (doesn't mean transaction was approved)
if ($transaction->isRequestSuccessful()) {
// Request reached Authorize.Net successfully
}
// Get transaction ID
$transactionId = $transaction->getId();
// Access transaction response methods
$transaction->getRefTransID();
$transaction->getResponseCode();
// ... and other transaction response methods
use Squareetlabs\AuthorizeNet\Models\UserGatewayProfile;
// Get customer profile for user
$profile = UserGatewayProfile::where('user_id', $user->id)->first();
// Access properties
$profile->profile_id; // Authorize.Net customer profile ID
$profile->user_id;
use Squareetlabs\AuthorizeNet\Models\UserPaymentProfile;
// Relationships
$profile->user; // BelongsTo User model
// Scopes
UserPaymentProfile::cards()->get(); // Only card payment profiles
UserPaymentProfile::banks()->get(); // Only bank payment profiles
// Access properties
$profile->payment_profile_id; // Authorize.Net payment profile ID
$profile->last_4; // Last 4 digits of card/account
$profile->brand; // Card brand (VISA, MasterCard, etc.)
$profile->type; // 'card' or 'bank'
use PHPUnit\Framework\TestCase;
use Squareetlabs\AuthorizeNet\Services\CustomerProfileService;
use net\authorize\api\contract\v1\CreateCustomerProfileResponse;
class PaymentTest extends TestCase
{
public function test_customer_profile_creation()
{
// Mock the service
$service = $this->createMock(CustomerProfileService::class);
// Create mock response
$mockResponse = new CreateCustomerProfileResponse();
// ... configure mock response
$service->expects($this->once())
->method('create')
->willReturn($mockResponse);
// Test your code
}
}
if (app()->environment('testing')) {
// Return mock data instead of calling Authorize.Net
return $this->mockAuthorizeNetResponse();
}
// Normal API call
return $user->anet()->charge(1000, $paymentProfileId);