PHP code example of leopaulo88 / asaas-sdk-laravel

1. Go to this page and download the library: Download leopaulo88/asaas-sdk-laravel 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/ */

    

leopaulo88 / asaas-sdk-laravel example snippets


use Leopaulo88\Asaas\Facades\Asaas;

// Create a customer
$customer = Asaas::customers()->create([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'cpfCnpj' => '12345678901'
]);

// Create a PIX payment
$payment = Asaas::payments()->create([
    'customer' => $customer->id,
    'billingType' => 'PIX',
    'value' => 100.00,
    'dueDate' => '2025-02-15',
    'description' => 'Product purchase'
]);

// Get PIX QR Code
$pixQrCode = Asaas::payments()->pixQrCode($payment->id);
echo "QR Code: {$pixQrCode->encodedImage}";

// Create a webhook to receive payment notifications
$webhook = Asaas::webhooks()->create([
    'name' => 'Payment Notifications',
    'url' => 'https://myapp.com/webhooks/asaas',
    'email' => '[email protected]',
    'enabled' => true,
    'sendType' => 'SEQUENTIALLY',
    'events' => [
        'PAYMENT_CREATED',
        'PAYMENT_CONFIRMED',
        'PAYMENT_RECEIVED'
    ]
]);

// Create a monthly subscription
$subscription = Asaas::subscriptions()->create([
    'customer' => $customer->id,
    'billingType' => 'CREDIT_CARD',
    'value' => 29.90,
    'nextDueDate' => '2025-02-01',
    'cycle' => 'MONTHLY',
    'description' => 'Premium Plan'
]);

// Create customers
$customer = Asaas::customers()->create($data);

// List customers with filters
$customers = Asaas::customers()->list(['name' => 'John']);

// Update customer
$customer = Asaas::customers()->update($customerId, $data);

// Find specific customer
$customer = Asaas::customers()->find($customerId);

// Create payments (PIX, Boleto, Credit Card)
$payment = Asaas::payments()->create($data);

// Get payment information
$payment = Asaas::payments()->find($paymentId);

// Process refunds
$refund = Asaas::payments()->refund($paymentId, $amount);

// Capture authorized payments
$payment = Asaas::payments()->capture($paymentId);

// PIX transfer
$transfer = Asaas::transfers()->create([
    'value' => 500.00,
    'pixAddressKey' => '11999999999',
    'pixAddressKeyType' => 'PHONE',
    'description' => 'PIX transfer'
]);

// Bank transfer (TED)
$transfer = Asaas::transfers()->create([
    'value' => 1000.00,
    'bankAccount' => [
        'bank' => ['code' => '033'],
        'accountName' => 'John Doe',
        'ownerName' => 'John Doe',
        'cpfCnpj' => '12345678901',
        'agency' => '1234',
        'account' => '56789-0'
    ],
    'operationType' => 'TED'
]);

// Create webhook
$webhook = Asaas::webhooks()->create($data);

// List webhooks
$webhooks = Asaas::webhooks()->list();

// Update webhook
$webhook = Asaas::webhooks()->update($webhookId, $data);

// Remove webhook
Asaas::webhooks()->remove($webhookId);

// Get account information
$account = Asaas::accounts()->info();

// Create sub-account with webhooks
$account = Asaas::accounts()->create([
    'name' => 'Sub Account',
    'email' => '[email protected]',
    'cpfCnpj' => '12345678901',
    'webhooks' => [$webhookConfig]
]);

// Tokenize credit card for secure storage
$token = Asaas::creditCards()->tokenize([
    'customer' => $customerId,
    'creditCard' => [
        'holderName' => 'John Doe',
        'number' => '4111111111111111',
        'expiryMonth' => '12',
        'expiryYear' => '2028',
        'ccv' => '123'
    ]
]);

// Use token in payments
$payment = Asaas::payments()->create([
    'customer' => $customerId,
    'billingType' => 'CREDIT_CARD',
    'value' => 150.00,
    'creditCardToken' => $token->creditCardToken
]);

// Get account balance
$balance = Asaas::finance()->balance();

// Get payment statistics
$stats = Asaas::finance()->statistics();

// Get split statistics
$splitStats = Asaas::finance()->splitStatistics();

use Leopaulo88\Asaas\Entities\Payment\PaymentCreate;use Leopaulo88\Asaas\Entities\Webhook\WebhookCreate;

// Using entities with fluent interface
$payment = PaymentCreate::make()
    ->customer('cus_123456')
    ->billingType('PIX')
    ->value(100.00)
    ->dueDate('2025-02-15')
    ->description('Product purchase');

$result = Asaas::payments()->create($payment);

// WebhookCreate entity
$webhook = (new WebhookCreate)
    ->name('Payment WebhookCreate')
    ->url('https://myapp.com/webhook')
    ->enabled(true)
    ->sendType('SEQUENTIALLY')
    ->events(['PAYMENT_CONFIRMED', 'PAYMENT_RECEIVED']);

// Use specific environment
$payment = Asaas::withApiKey($apiKey, 'production')
    ->payments()
    ->create($data);

// Multiple tenants/accounts
$payment = Asaas::withApiKey($tenant->api_key)
    ->payments()
    ->create($data);

// In your webhook controller
public function handle(Request $request)
{
    $payload = $request->json()->all();
    $event = $payload['event'];
    
    switch ($event) {
        case 'PAYMENT_CONFIRMED':
            $this->handlePaymentConfirmed($payload['payment']);
            break;
        case 'SUBSCRIPTION_CREATED':
            $this->handleSubscriptionCreated($payload['subscription']);
            break;
    }
    
    return response('OK', 200);
}

use Leopaulo88\Asaas\Exceptions\{
    BadRequestException,
    UnauthorizedException,
    NotFoundException
};

try {
    $payment = Asaas::payments()->create($data);
} catch (BadRequestException $e) {
    // Validation errors
    $errors = $e->getErrors();
    foreach ($errors as $field => $messages) {
        echo "Field {$field}: " . implode(', ', $messages);
    }
} catch (UnauthorizedException $e) {
    // Invalid API key
    echo "Authentication failed: " . $e->getMessage();
} catch (NotFoundException $e) {
    // Resource not found
    echo "Resource not found: " . $e->getMessage();
}
bash
php artisan vendor:publish --tag="asaas-config"