PHP code example of billie / api-php-sdk

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

    

billie / api-php-sdk example snippets


$isSandbox = true;
$billieClient = \Billie\Sdk\Util\BillieClientFactory::getBillieClientInstance('YOUR-CLIENT-ID', 'YOUR-CLIENT-SECRET', $isSandbox);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient **/

$requestService = new \Billie\Sdk\Service\Request\Order\CreateOrderRequest($billieClient);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient **/

$requestService = new \Billie\Sdk\Service\Request\Order\CreateOrderRequest();
// [...]
$requestService->setClient($billieClient);
// [...]
$requestService->execute(...);

/** @var \Billie\Sdk\Model\Request\AbstractRequestModel $requestModel */

$requestModel->setValidateOnSet(false);

$isSandbox = true;
$tokenRequestService = new \Billie\Sdk\Service\Request\Auth\GetTokenRequest($isSandbox);

$requestModel = new \Billie\Sdk\Model\Request\Auth\GetTokenRequestModel();
$requestModel
    ->setClientId('YOUR-CLIENT-ID')
    ->setClientSecret('YOUR-SECRET-ID');
    
/** @var \Billie\Sdk\Model\Response\Auth\GetTokenResponseModel */
$responseModel = $tokenRequestService->execute($requestModel);
$accessToken = $responseModel->getAccessToken(); // use this token for further requests.

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$validateTokenRequest = new \Billie\Sdk\Service\Request\Auth\ValidateTokenRequest($billieClient);

/** @var \Billie\Sdk\Model\Response\Auth\ValidateTokenResponse $responseModel */
$responseModel = $validateTokenRequest->execute(new \Billie\Sdk\Model\Request\Auth\ValidateTokenRequestModel());

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$validateTokenRequest = new \Billie\Sdk\Service\Request\Auth\RevokeTokenRequest($billieClient);

/** @var bool $responseModel */
$responseModel = $validateTokenRequest->execute(new \Billie\Sdk\Model\Request\Auth\RevokeTokenRequestModel());

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\CheckoutSession\CreateSessionRequest($billieClient);

$requestModel = new \Billie\Sdk\Model\Request\CheckoutSession\CreateSessionRequestModel();
$requestModel->setMerchantCustomerId('THE-NUMBER-OR-ID-OF-THE-CUSTOMER');

/** @var \Billie\Sdk\Model\Response\CreateSessionResponseModel $responseModel */
$responseModel = $requestService->execute($requestModel);

$checkoutSessionId = $responseModel->getCheckoutSessionId(); // use this session ID and submit it to the widget.


/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\CheckoutSession\CheckoutSessionConfirmRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\CheckoutSession\CheckoutSessionConfirmRequestModel();
$requestModel
  ->setSessionUuid('CHECKOUT-SESSION-ID')
  ->setCompany(new \Billie\Sdk\Model\Request\CheckoutSession\Confirm\Debtor())
  ->setAmount(new \Billie\Sdk\Model\Amount())
  ->setDuration(14)
  ->setDeliveryAddress(new \Billie\Sdk\Model\Address());
  
/** @var \Billie\Sdk\Model\Order $responseModel */
$responseModel = $requestService->execute($requestModel); // this is the finally created order


/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\CheckoutSession\GetCheckoutAuthorizationRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\CheckoutSession\GetCheckoutAuthorizationRequestModel();
$requestModel
  ->setSessionUuid('CHECKOUT-SESSION-ID');
  
/** @var \Billie\Sdk\Model\Response\CheckoutSession\GetCheckoutAuthorizationResponseModel $responseModel */
$responseModel = $requestService->execute($requestModel);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Order\CreateOrderRequest($billieClient);

$requestModel = new \Billie\Sdk\Model\Request\Order\CreateOrderRequestModel();
$requestModel
    ->setAmount(new \Billie\Sdk\Model\Amount())
    ->setDuration(12)
    ->setDebtor(new \Billie\Sdk\Model\Request\Order\CreateOrder\Debtor())
    ->setPerson(new \Billie\Sdk\Model\Person())
    ->setComment('order comment')
    ->setExternalCode('merchant-order-number')
    ->setDeliveryAddress(new \Billie\Sdk\Model\Address())
    ->setLineItems([
        new \Billie\Sdk\Model\LineItem(),
        new \Billie\Sdk\Model\LineItem(),
    ])
    
/** @var \Billie\Sdk\Model\Order $responseModel */
$responseModel = $requestService->execute($requestModel); // this is the finally created order

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Order\UpdateOrderRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\Order\UpdateOrderRequestModel('REFERENCE-ID');
$requestModel
  ->setExternalCode('SHOP-ORDER-NUMBER')
  ->setAmount(new \Billie\Sdk\Model\Amount());
  
/** @var true $success */
$success = $requestService->execute($requestModel); // true if successful

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Order\GetOrderRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\OrderRequestModel('REFERENCE-ID');

/** @var \Billie\Sdk\Model\Order $responseModel */
$responseModel = $requestService->execute($requestModel);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Invoice\CreateInvoiceRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\Invoice\CreateInvoiceRequestModel();
$requestModel
    ->setOrders(['REFERENCE-ID']) // use this method to set Billie reference-id or merchants order-number
    ->setOrderUuId('REFERENCE-UD') // use this method to set Billie reference-id
    ->setOrderExternalCode('MERCHANT_ORDER_ID') // use this method to set merchants order-number
    // to keep your code clean, only use one of the above methods
    ->setInvoiceNumber('merchant-invoice-number')
    ->setInvoiceUrl('https://public-url.com/to/to/merchant-invoice.pdf')
    ->setAmount(
        (new Amount())
            ->setGross(100)
            ->setTaxRate(19.00)
    )
    // optional parameters:
    ->setLineItems([
        new \Billie\Sdk\Model\Request\Invoice\LineItem('merchant-product-id-2', 1),
        new \Billie\Sdk\Model\Request\Invoice\LineItem('merchant-product-id-1', 2)  
    ])
    ->setShippingInformation(new \Billie\Sdk\Model\ShippingInformation())

/** @var \Billie\Sdk\Model\Response\CreateInvoiceResponseModel $responseModel */
$responseModel = $requestService->execute($requestModel);
$uuid = $responseModel->getUuid(); // uuid of the invoice

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Invoice\GetInvoiceRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\InvoiceRequestModel('INVOICE-UUID');

/** @var \Billie\Sdk\Model\Invoice $responseModel */
$responseModel = $requestService->execute($requestModel);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Invoice\UpdateInvoiceRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\Invoice\UpdateInvoiceRequestModel('INVOICE-REFERENCE-UUID');
$requestModel
    ->setInvoiceNumber('merchant-invoice-number')
    ->setInvoiceUrl('https://public-url.com/to/to/merchant-invoice.pdf');

if ($requestService->execute($requestModel)) {
    // invoice has been updated
}

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Invoice\CancelInvoiceRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\InvoiceRequestModel('INVOICE-REFERENCE-UUID');

if ($requestService->execute($requestModel)) {
    // invoice has been deleted/canceled
}

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Invoice\CreateCreditNoteRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\Invoice\CreateCreditNoteRequestModel('INVOICE-UUID', 'MERCHANT/EXTERNAL_CREDIT_NOTE_NUMBER');
$requestModel
    ->setAmount(
        (new Amount())
            ->setGross(100)
            ->setTaxRate(19.00)
    )
    // optional parameters:
    ->setLineItems([
        new \Billie\Sdk\Model\Request\Invoice\LineItem('merchant-product-id-2', 1),
        new \Billie\Sdk\Model\Request\Invoice\LineItem('merchant-product-id-1', 2)  
    ])
    ->setComment('custom comment for refund')

/** @var \Billie\Sdk\Model\Response\CreateInvoiceResponseModel $responseModel */
$responseModel = $requestService->execute($requestModel);
$uuid = $responseModel->getUuid(); // uuid of the invoice

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Invoice\ConfirmPaymentRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\Invoice\ConfirmPaymentRequestModel('INVOICE-UUID');
$requestModel
    ->setPaidAmount(250);

/** @var true $success */
$success = $requestService->execute($requestModel);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\Order\CancelOrderRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\OrderRequestModel('REFERENCE-ID');

/** @var true $success */
$success = $requestService->execute($requestModel);

/** @var \Billie\Sdk\HttpClient\BillieClient $billieClient */

$requestService = new \Billie\Sdk\Service\Request\GetLegalFormsRequest($billieClient);
$requestModel = new \Billie\Sdk\Model\Request\GetLegalFormsRequestModel();

/** @var \Billie\Sdk\Model\Response\GetLegalFormsResponseModel $responseModel */
$responseModel = $requestService->execute($requestModel);


$streetName = \Billie\Util\AddressHelper::getStreetName('Musterstraße 123'); // will return "Musterstraße"
$houseNumber = \Billie\Util\AddressHelper::getHouseNumber('Musterstraße 123'); // will return "123"

$amount = new \Billie\Sdk\Model\Amount();
$amount->setGross(119);
$amount->setNet(100);

$tax = $amount->getTax(); // will return `19`

$amount = new \Billie\Sdk\Model\Amount();
$amount->setGross(119);
$amount->setTaxRate(19);

$tax = $amount->getTax(); // will return `19`

$amount = new \Billie\Sdk\Model\Amount();
$amount->setNet(100);
$amount->setTaxRate(19);

$tax = $amount->getTax(); // will return `19`
$gross = $amount->getGross(); // will return `119`

$amount = new \Billie\Sdk\Model\Amount();
$amount->setGross(119);
$amount->setTaxRate(19);

$tax = $amount->getTax(); // will return `19`
$net = $amount->getNet(); // will return `100`


$amount = new \Billie\Sdk\Model\Amount();
$amount->setGross(119);
$amount->setNet(30);
$amount->setTax(40);

$amount->getGross(); // will return `119`
$amount->getNet(); // will return `30`
$amount->getTax(); // will return `40`


namespace YourNamespace;

class MyClass
{

    /**
     * @var \Billie\Sdk\Service\Request\AbstractRequest
     */
    private $requestService;
    
    public function __construct(\Billie\Sdk\Service\Request\AbstractRequest $requestService) 
    {
        $this->requestService = $requestService;
    }
    
    public function process()
    {
        $response = $this->requestService->execute(...);
    }
}


namespace YourNamespace;

class BillieClientFactory
{
    public function createBillieClient()
    {
        $isSandbox = true;
        return \Billie\Sdk\Util\BillieClientFactory::getBillieClientInstance(
            'YOUR-CLIENT-ID',
            'YOUR-CLIENT-SECRET',
            $isSandbox
        );
    }
}

/* @var \Billie\Sdk\Service\Request\AbstractRequest $requestService */
$isSandbox = true;
$requestService->setClient(new \Billie\Sdk\HttpClient\BillieClient('YOUR-CLIENT-ID', 'YOUR-CLIENT-SECRET', $isSandbox));

$logFile = '/path/to/your/logfile.log';
$logger = new \Monolog\Logger('name-for-the-logger');

// handler for errors
$handlerDebug = new \Monolog\Handler\StreamHandler('/path/to/your/log-file.debug.log', LogLevel::DEBUG);
$logger->pushHandler($handlerDebug);

// handler for debug-information
$handlerError = new \Monolog\Handler\StreamHandler('/path/to/your/log-file.error.log', LogLevel::ERROR);
$logger->pushHandler($handlerError);

\Billie\Sdk\Util\Logging::setPsr3Logger($logger);
// call this if you want to log the request-headers too
\Billie\Sdk\Util\Logging::setLogHeaders(true);
bash
composer