# Payment API

## Example implementation:

```php
    namespace BillinkGateway;

    // To add a manual payment to an invoice in Billink, create a list of
    // Billink\Payment instances and put them in an Payment\PaymentList collection.
    $paymentList = Payment\PaymentList::collect([
        new Payment\Payment(123, 10.00, 'Optional description'),
        new Payment\Payment(456, 10.00),
    ]);
    
    $paymentEndpoint = new Endpoint\Payment($paymentList);
    $paymentEndpoint->setAuthenticationHeader(App::get('auth.header'));

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

        $paymentResponse = 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 ($paymentResponse->isInvalid()) {
        var_dump(
            $paymentResponse->isValid(),
            $paymentResponse->getCode(),
            $paymentResponse->getError(),
            $paymentResponse->getErrorLabel(),
            $paymentResponse
        );

        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 = $paymentResponse->getInvoices();
    foreach ($invoices as $invoice) {
        var_dump(
            $invoice->exists(),
            $invoice->notExists(),
            $invoice->success(),
            $invoice->notAllowed(),
            $invoice->alreadyPaid(),
            $invoice->descriptionTooLong(),
            $invoice->invoiceNumber,
            $invoice->message,
            $invoice->code
        );
    }
```
