require "../vendor/autoload.php"; $client = new \ClearBooks\Client('demo'); // the paypal bank account $paypalBankAccount = 7502004; // the paypal entity id $paypalEntityId = 12; // the amount received by paypal $received = 1150; // the sales invoice id to apply the payment to $salesInvoiceId = 189; // fetch the original invoice $invoiceQuery = new \ClearBooks\InvoiceQuery(); $invoiceQuery->id[] = $salesInvoiceId; $invoiceQuery->ledger = 'sales'; $invoiceQuery->status = 'approved'; $invoices = $client->listInvoices($invoiceQuery); if (!$invoices) { throw new \Exception('Invoice not found'); } $invoice = $invoices[0]; // calculate the invoice total $total = 0; array_walk($invoice->items, function ($item) use(&$total) { /** @var $item \Clearbooks_Soap_1_0_item */ $total += $item->unitPrice + $item->vat; }); // the paypal fee is the amount received minus the invoice total (1150 - 1200 = -50) $fee = $received - $total; // create an invoice for the paypal fee $invoice = new \ClearBooks\Invoice(); $invoice->creditTerms = 0;