PHP code example of cobrefacil / sdk-php

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

    

cobrefacil / sdk-php example snippets


use CobreFacil\CobreFacil;

$cobrefacil = new CobreFacil($appId, $secret);

$cobrefacil = new CobreFacil($appId, $secret, false);
$cobrefacil->setProduction(false);

$params = [
    'customer_id' => 'Y73MNPGJ18Y18V5KQODX',
    'payable_with' => 'bankslip',
    //...
];
$response = $cobrefacil->invoice->create($params);

[
    'id' => '2KD9LGERW897NZ6JM5V4',
    'customer_id' => 'Y73MNPGJ18Y18V5KQODX',
    'payable_with' => 'bankslip',
    //...
];

// POST /customers
$cobrefacil->customer->create($params);

// PUT /customers/{id}
$cobrefacil->customer->update($id, $params);

// GET /customers
$cobrefacil->customer->search();

// GET /[email protected]
$cobrefacil->customer->search(['email' => '[email protected]']);

// GET /customers/{id}
$cobrefacil->customer->getById($id);

// DELETE /customers/{id}
$cobrefacil->customer->remove($id);

// POST /cards
$cobrefacil->card->create($params);

// POST /cards/{id}/default
$cobrefacil->card->setDefault($id);

// GET /cards/{id}
$cobrefacil->card->getById($id);

// GET /cards
$cobrefacil->card->search();

// GET /cards?customer_id=Z8J53ROD2JK1PQ47WG0E
$cobrefacil->card->search(['customer_id' => 'Z8J53ROD2JK1PQ47WG0E']);

// DELETE /cards/{id}
$cobrefacil->card->remove($id);

// POST /invoices
$cobrefacil->invoice->create($params);

// POST /invoices/{id}/capture
$cobrefacil->invoice->capture($id, $amount);

// GET /invoices
$cobrefacil->invoice->search();

// GET /invoices?status=paid
$cobrefacil->invoice->search(['status' => 'paid']);

// POST /invoices/{id}/refund
$cobrefacil->invoice->refund($id, $amount);

// DELETE /invoices/{id}
$cobrefacil->invoice->cancel($id);

use CobreFacil\Exceptions\InvalidCredentialsException;

try {
    $cobrefacil = new CobreFacil($appId, $secret);
} catch (InvalidCredentialsException $e) {
    // 401 Unauthorized: As credenciais são inválidas
}

use CobreFacil\Exceptions\InvalidParamsException;
use CobreFacil\Exceptions\ResourceNotFoundException;

try {
    $cobrefacil->customer->create($params);
} catch (InvalidParamsException $e) {
    // 400 Bad Request: Algum parâmetro obrigatório não foi enviado ou é inválido
    $e->getErrors();// retorna um array contendo os erros
} catch (ResourceNotFoundException $e) {
    // 404 Not Found: O registro solicitado não existe
}

$receivedEvent = WebhookEvent::createFromJson($json);

$receivedEvent->getEvent();// nome do evento recebido, exemplo: invoice.created
$receivedEvent->getData();// dados da cobrança em forma de array associativo

$receivedEvent->isInvoiceCreated();// invoice.created
$receivedEvent->isInvoiceViewed();// invoice.viewed
$receivedEvent->isInvoiceReversed();// invoice.reversed
$receivedEvent->isInvoiceDeclined();// invoice.declined
$receivedEvent->isInvoicePreAuthorized();// invoice.pre_authorized
$receivedEvent->isInvoicePaid();// invoice.paid
$receivedEvent->isInvoiceRefunded();// invoice.refunded
$receivedEvent->isInvoiceCanceled();// invoice.canceled
$receivedEvent->isInvoiceDispute();// invoice.dispute
$receivedEvent->isInvoiceDisputeSucceeded();// invoice.dispute_succeeded
$receivedEvent->isInvoiceDisputeChargedBack();// invoice.charged_back

composer