# Credit API

## Example implementation:

```php
    namespace BillinkGateway;

    // To (partially) credit an invoice in Billink, create a list of
    // Billink\Credit instances and put them in an Credit\CreditList collection.
    $creditList = Credit\CreditList::collect([
        new Credit\Credit(123, 10.00, 'Optional description'),
        new Credit\Credit(456, 10.00),
    ]);
    
    $creditEndpoint = new Endpoint\Credit($creditList);
    $creditEndpoint->setAuthenticationHeader(App::get('auth.header'))
        // If the order(s) should be processed as an refund, call the asRefund() method.
        ->asRefund();

    // Build the HTTP request through our HTTP client and serialize the 
    // Endpoint/Credit instance into XML by calling the serialize() method.
    try {
        $response = App::get('http.client')
            ->post($creditEndpoint->getUrlEndpoint(), $creditEndpoint->serialize())
            ->send();

        $creditResponse = App::get('serializer')
            ->unserialize($response->getBody(), Endpoint\StartWorkflow::class, 'xml');
    } catch (\Tussendoor\Http\RequestException $e) {
        // When the HTTP request fails, a RequestException is thrown.
        var_dump($e->getErrors());
        exit();
    } catch (\Exception $e) {
        var_dump($e);
        exit();
    }

    // The response will be invalid if a validation error occured over at Billink.
    if ($creditResponse->isInvalid()) {
        var_dump(
            $creditResponse->isValid(),
            $creditResponse->getCode(),
            $creditResponse->getError(),
            $creditResponse->getErrorLabel(),
            $creditResponse
        );

        exit('Order is invalid');
    }

    // Get a list of Credit\Invoice instances, which have methods to figure
    // out the status of an Invoice. The list itself is a Collection.
    $invoices = $creditResponse->getInvoices();
    foreach ($invoices as $invoice) {
        var_dump(
            $invoice->exists(),
            $invoice->notExists(),
            $invoice->success(),
            $invoice->notAllowed(),
            $invoice->notPossible(),
            $invoice->unkownBankAccount(),
            $invoice->invoiceNumber,
            $invoice->message,
            $invoice->code
        );
    }
```
