### Install Pancake PHP SDK Source: https://github.com/pancakeapp/php-sdk/blob/master/README.md Use Composer to install the Pancake PHP SDK. This is the primary method for adding the SDK to your PHP project. ```bash composer require pancake/sdk ``` -------------------------------- ### Initialize Pancake Server Connection Source: https://context7.com/pancakeapp/php-sdk/llms.txt Instantiate the Server class with your Pancake installation URL and API key. The constructor automatically formats the URL for the API endpoint. This object is required for all subsequent SDK operations. ```php get() and server->post() are available for raw API calls. // All higher-level classes accept $server as their first argument. ``` -------------------------------- ### Server - Initialize Connection Source: https://context7.com/pancakeapp/php-sdk/llms.txt The Server class is the entry point for SDK operations. It initializes a GuzzleHttp client configured for the Pancake API base URL and automatically injects the API key into every request. ```APIDOC ## Server — Initialize Connection `Server` is the entry point for every SDK operation. It wraps a GuzzleHttp client configured for the Pancake API base URL and injects the API key into every request automatically. ```php get() and server->post() are available for raw API calls. // All higher-level classes accept $server as their first argument. ``` ``` -------------------------------- ### Create New Invoice with Line Items and Files Source: https://context7.com/pancakeapp/php-sdk/llms.txt Instantiate an Invoice, configure its properties and line items, then call save() to POST it to the API. On success, the object is reloaded with the server-assigned unique_id and all computed fields. Supports adding standard, flat-rate, and discount line items, as well as attaching files. ```php client_id = 2; $invoice->description = "Web Development Services — Q2 2024"; $invoice->notes = "Net 30 payment terms apply."; $invoice->type = "DETAILED"; // Default; also supports "LOGGED_TIME" // Line items — taxes can be a name, a percentage string, or a tax ID. $invoice->addStandardLineItem( "Website Redesign", // name 1, // qty 3500.00, // rate ["VAT", "10%"], // taxes (name or percentage; created if missing) "Full redesign", // description 0 // discount ); $invoice->addFlatRateLineItem("Hosting Setup", 150.00, [], "Annual hosting fee", 0); $invoice->addFixedDiscountLineItem("Loyalty Discount", 100); // $100 off $invoice->addPercentageDiscountLineItem("Referral Discount", 5); // 5% off // Attach files (URL or local path) $invoice->addFile("https://example.com/assets/contract.pdf"); $invoice->addFile("/tmp/scope-of-work.pdf"); $invoice->addFileFromContents("Payment due within 30 days.", "terms.txt"); $result = $invoice->save(); // $result['unique_id'] is now populated echo "Created invoice: {$invoice->unique_id}\n"; echo "Total: {$invoice->currency_symbol}{$invoice->total}\n"; } catch (ApiException $e) { echo "API Error: " . $e->getMessage(); var_dump($e->getResponse()); } ``` -------------------------------- ### Set Up Partial Payment Schedule for Invoice Source: https://context7.com/pancakeapp/php-sdk/llms.txt Use addPaymentPart() to define a payment schedule before saving a new invoice. This method allows specifying whether the amount is a percentage or a fixed value, the amount, the due date, and notes for each payment part. ```php client_id = 3; $invoice->addStandardLineItem("Consulting", 10, 200.00, [], "10 hours @ $200", 0); // 50% due immediately $invoice->addPaymentPart( true, // is_percentage 50, // amount (50%) Carbon::now(), // due date (Carbon instance) "Deposit due now" // notes ); // Remaining 50% due in 30 days $invoice->addPaymentPart( true, 50, Carbon::now()->addDays(30), "Balance due on project completion" ); $result = $invoice->save(); echo "Invoice {$result['unique_id']} created with 2-part payment schedule.\n"; } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` -------------------------------- ### Fetch Reusable Invoice Items Source: https://context7.com/pancakeapp/php-sdk/llms.txt Retrieves all pre-defined reusable line items from Pancake. Useful for populating item pickers or auto-filling invoices. Also shows how to fetch a specific item by its ID. ```php getMessage(); } ``` -------------------------------- ### Fetch All Invoices Source: https://context7.com/pancakeapp/php-sdk/llms.txt Use Invoices::get() to retrieve all invoices from the server. This method returns an array of hydrated Invoice objects. Ensure proper error handling with a try-catch block for ApiException. ```php invoice_number, $invoice->client_id, $invoice->status, $invoice->currency_symbol, $invoice->total, $invoice->is_paid ? 'Yes' : 'No' ); } // Output: // Invoice INV-001 | Client 4 | Status: sent | Total: $250.00 | Paid: No // Invoice INV-002 | Client 7 | Status: paid | Total: $1200.00 | Paid: Yes } catch (ApiException $e) { $response = $e->getResponse(); var_dump($response); // Array with Pancake error details or raw HTML on server error } ``` -------------------------------- ### Record Payment Against an Invoice Source: https://context7.com/pancakeapp/php-sdk/llms.txt Use addPayment() to post a payment record against an existing invoice. This automatically reloads the invoice to reflect the updated paid_amount and status. It supports specifying amount, gateway, datetime, status, transaction ID, fee, and whether to send a notification email. ```php addPayment( 500.00, // amount "Stripe", // gateway name time(), // payment datetime (Unix timestamp; null = now) "Completed", // payment status "ch_3OxABC123", // transaction ID 14.50, // transaction fee true // send notification email to client ); if ($success) { echo "Payment recorded.\n"; echo "Paid so far: {$invoice->currency_symbol}{$invoice->paid_amount}\n"; echo "Still owed: {$invoice->currency_symbol}{$invoice->unpaid_amount}\n"; echo "Status: {$invoice->status}\n"; } } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` -------------------------------- ### ApiException Source: https://context7.com/pancakeapp/php-sdk/llms.txt Handles errors thrown by SDK operations on API or network failures. ```APIDOC ## ApiException — Error Handling `ApiException` is thrown by all SDK operations on any API or network failure. `getResponse()` returns either a structured array (Pancake API error) or a raw HTML string (Pancake server-side exception), enabling differentiated error handling. ### Method ```php public function getResponse() ``` ### Parameters No parameters required. ### Response #### Success Response - Returns the API response, which can be an array (for structured errors) or a string (for raw HTML errors). ### Request Example ```php getResponse(); if (is_array($response)) { // Pancake returned a structured JSON error echo "API error: " . $e->getMessage() . "\n"; // $response may contain keys: 'status', 'error', 'error_message', 'message' echo "Full response: \n"; print_r($response); } else { // Pancake returned an HTML error page (e.g., PHP fatal error) // The message will be pre-parsed from the HTML if possible echo "Server-side error: " . $e->getMessage() . "\n"; // $response contains the raw HTML for detailed debugging file_put_contents('/tmp/pancake-error.html', $response); echo "Full error saved to /tmp/pancake-error.html\n"; } } ?> ``` ``` -------------------------------- ### Invoice::save — Create a New Invoice Source: https://context7.com/pancakeapp/php-sdk/llms.txt Instantiate an Invoice object, configure its properties and line items, then call save() to POST it to the API. On success, the object is reloaded with the server-assigned unique_id and all computed fields. ```APIDOC ## Invoice::save — Create a New Invoice Instantiate an `Invoice`, configure its properties and line items, then call `save()` to POST it to the API. On success, the object is reloaded with the server-assigned `unique_id` and all computed fields. ```php client_id = 2; $invoice->description = "Web Development Services — Q2 2024"; $invoice->notes = "Net 30 payment terms apply."; $invoice->type = "DETAILED"; // Default; also supports "LOGGED_TIME" // Line items — taxes can be a name, a percentage string, or a tax ID. $invoice->addStandardLineItem( "Website Redesign", // name 1, // qty 3500.00, // rate ["VAT", "10%"], // taxes (name or percentage; created if missing) "Full redesign", // description 0 // discount ); $invoice->addFlatRateLineItem("Hosting Setup", 150.00, [], "Annual hosting fee", 0); $invoice->addFixedDiscountLineItem("Loyalty Discount", 100); // $100 off $invoice->addPercentageDiscountLineItem("Referral Discount", 5); // 5% off // Attach files (URL or local path) $invoice->addFile("https://example.com/assets/contract.pdf"); $invoice->addFile("/tmp/scope-of-work.pdf"); $invoice->addFileFromContents("Payment due within 30 days.", "terms.txt"); $result = $invoice->save(); // $result['unique_id'] is now populated echo "Created invoice: {$invoice->unique_id}\n"; echo "Total: {$invoice->currency_symbol}{$invoice->total}\n"; } catch (ApiException $e) { echo "API Error: " . $e->getMessage(); var_dump($e->getResponse()); } ``` ``` -------------------------------- ### ReusableInvoiceItems::get Source: https://context7.com/pancakeapp/php-sdk/llms.txt Fetches all pre-defined reusable line items configured in Pancake. ```APIDOC ## ReusableInvoiceItems::get — Fetch Saved Line Item Templates `ReusableInvoiceItems::get()` returns all pre-defined reusable line items configured in Pancake, useful for populating item pickers or auto-filling invoices. ### Method ```php public static function get(Server $server) public static function getById(Server $server, int $id) ``` ### Parameters #### Path Parameters - **server** (Server) - Required - The Pancake server instance. - **id** (int) - Required - The ID of the reusable item to fetch. ### Response #### Success Response - Returns an array of reusable invoice items or a single reusable invoice item by ID. ### Request Example ```php getMessage(); } ?> ``` ``` -------------------------------- ### Invoice::addPaymentPart — Split Invoice into Partial Payments Source: https://context7.com/pancakeapp/php-sdk/llms.txt `addPaymentPart()` (and its convenience wrappers `addPercentagePaymentPart()` / `addFixedPaymentPart()`) define a payment schedule before saving a new invoice. ```APIDOC ## Invoice::addPaymentPart — Split Invoice into Partial Payments `addPaymentPart()` (and its convenience wrappers `addPercentagePaymentPart()` / `addFixedPaymentPart()`) define a payment schedule before saving a new invoice. ```php client_id = 3; $invoice->addStandardLineItem("Consulting", 10, 200.00, [], "10 hours @ $200", 0); // 50% due immediately $invoice->addPaymentPart( true, // is_percentage 50, // amount (50%) Carbon::now(), // due date (Carbon instance) "Deposit due now" // notes ); // Remaining 50% due in 30 days $invoice->addPaymentPart( true, 50, Carbon::now()->addDays(30), "Balance due on project completion" ); $result = $invoice->save(); echo "Invoice {$result['unique_id']} created with 2-part payment schedule.\n"; } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` ``` -------------------------------- ### Handle Pancake API Errors Source: https://context7.com/pancakeapp/php-sdk/llms.txt Demonstrates how to catch and differentiate between structured API errors and raw server-side exceptions thrown by the SDK. The response can be an array or an HTML string. ```php getResponse(); if (is_array($response)) { // Pancake returned a structured JSON error echo "API error: " . $e->getMessage() . "\n"; // $response may contain keys: 'status', 'error', 'error_message', 'message' echo "Full response: \n"; print_r($response); } else { // Pancake returned an HTML error page (e.g., PHP fatal error) // The message will be pre-parsed from the HTML if possible echo "Server-side error: " . $e->getMessage() . "\n"; // $response contains the raw HTML for detailed debugging file_put_contents('/tmp/pancake-error.html', $response); echo "Full error saved to /tmp/pancake-error.html\n"; } } ``` -------------------------------- ### Invoices::get - Fetch All Invoices Source: https://context7.com/pancakeapp/php-sdk/llms.txt Retrieves all invoices from the server and returns them as an array of hydrated `Invoice` objects. Each object has properties automatically cast to their declared PHP types. ```APIDOC ## Invoices::get — Fetch All Invoices `Invoices::get()` retrieves every invoice on the server and returns an array of hydrated `Invoice` objects with all fields auto-cast to their declared PHP types. ```php invoice_number, $invoice->client_id, $invoice->status, $invoice->currency_symbol, $invoice->total, $invoice->is_paid ? 'Yes' : 'No' ); } // Output: // Invoice INV-001 | Client 4 | Status: sent | Total: $250.00 | Paid: No // Invoice INV-002 | Client 7 | Status: paid | Total: $1200.00 | Paid: Yes } catch (ApiException $e) { $response = $e->getResponse(); var_dump($response); // Array with Pancake error details or raw HTML on server error } ``` ``` -------------------------------- ### Invoice::addPayment — Record a Payment Against an Invoice Source: https://context7.com/pancakeapp/php-sdk/llms.txt `addPayment()` posts a payment record against an existing invoice and automatically reloads the invoice to reflect the updated `paid_amount` and `status`. ```APIDOC ## Invoice::addPayment — Record a Payment Against an Invoice `addPayment()` posts a payment record against an existing invoice and automatically reloads the invoice to reflect the updated `paid_amount` and `status`. ```php addPayment( 500.00, // amount "Stripe", // gateway name time(), // payment datetime (Unix timestamp; null = now) "Completed", // payment status "ch_3OxABC123", // transaction ID 14.50, // transaction fee true // send notification email to client ); if ($success) { echo "Payment recorded.\n"; echo "Paid so far: {$invoice->currency_symbol}{$invoice->paid_amount}\n"; echo "Still owed: {$invoice->currency_symbol}{$invoice->unpaid_amount}\n"; echo "Status: {$invoice->status}\n"; } } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` ``` -------------------------------- ### Fetch Invoices by Client ID Source: https://context7.com/pancakeapp/php-sdk/llms.txt Filter invoices for a specific client using Invoices::getByClientId(). This method returns an array of Invoice objects. It includes logic to check for overdue invoices. ```php unique_id}: {$invoice->currency_symbol}{$invoice->total} — {$invoice->status}\n"; if ($invoice->days_overdue > 0) { echo " *** OVERDUE by {$invoice->days_overdue} day(s) ***\n"; } } } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` -------------------------------- ### Invoices::getByUniqueId - Fetch a Single Invoice Source: https://context7.com/pancakeapp/php-sdk/llms.txt Retrieves a single invoice by its unique ID, providing detailed information including totals, taxes, discounts, and partial payments. Returns `null` if the invoice is not found. ```APIDOC ## Invoices::getByUniqueId — Fetch a Single Invoice `Invoices::getByUniqueId()` retrieves a fully detailed single invoice including totals, taxes, discounts, and partial payment parts. Returns `null` if not found. ```php invoice_number}\n"; echo "Status: {$invoice->status}\n"; echo "Sub-total: {$invoice->currency_symbol}{$invoice->sub_total}\n"; echo "Tax total: {$invoice->currency_symbol}{$invoice->tax_total}\n"; echo "Total: {$invoice->currency_symbol}{$invoice->total}\n"; echo "Paid: {$invoice->currency_symbol}{$invoice->paid_amount}\n"; echo "Unpaid: {$invoice->currency_symbol}{$invoice->unpaid_amount}\n"; echo "View URL: {$invoice->url}\n"; } } catch (ApiException $e) { $response = $e->getResponse(); if (is_array($response)) { var_dump($response); } else { echo $response; // Raw HTML from Pancake error page } } ``` ``` -------------------------------- ### Fetch Single Invoice by Unique ID Source: https://context7.com/pancakeapp/php-sdk/llms.txt Retrieve a single, detailed invoice using Invoices::getByUniqueId(). Returns null if the invoice is not found. Handles potential API errors and displays detailed invoice information or raw error responses. ```php invoice_number}\n"; echo "Status: {$invoice->status}\n"; echo "Sub-total: {$invoice->currency_symbol}{$invoice->sub_total}\n"; echo "Tax total: {$invoice->currency_symbol}{$invoice->tax_total}\n"; echo "Total: {$invoice->currency_symbol}{$invoice->total}\n"; echo "Paid: {$invoice->currency_symbol}{$invoice->paid_amount}\n"; echo "Unpaid: {$invoice->currency_symbol}{$invoice->unpaid_amount}\n"; echo "View URL: {$invoice->url}\n"; } } catch (ApiException $e) { $response = $e->getResponse(); if (is_array($response)) { var_dump($response); } else { echo $response; // Raw HTML from Pancake error page } } ``` -------------------------------- ### Invoice::setPaymentDetails Source: https://context7.com/pancakeapp/php-sdk/llms.txt Updates the gateway and transaction metadata for a specific payment part on an already-saved invoice. ```APIDOC ## Invoice::setPaymentDetails — Update an Existing Payment Part `setPaymentDetails()` updates the gateway and transaction metadata for a specific payment part (identified by index) on an already-saved invoice. ### Method ```php public function setPaymentDetails(int $payment_i, string $gateway, int $payment_datetime, string $payment_status, string $transaction_id, float $transaction_fee, bool $send_notification_email) ``` ### Parameters #### Path Parameters - **payment_i** (int) - Required - zero-based part index - **gateway** (string) - Required - The payment gateway used - **payment_datetime** (int) - Required - Unix timestamp of the payment - **payment_status** (string) - Required - The status of the payment (e.g., "Completed") - **transaction_id** (string) - Required - The unique ID of the transaction - **transaction_fee** (float) - Required - The fee associated with the transaction - **send_notification_email** (bool) - Required - Whether to send a notification email ### Request Example ```php setPaymentDetails( 0, // payment_i: zero-based part index "PayPal", // gateway strtotime("2024-06-01"), // payment_datetime "Completed", // payment_status "PAYID-ABC987", // transaction_id 2.75, // transaction_fee false // send_notification_email ); echo $success ? "Payment details updated.\n" : "Update failed.\n"; } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ?> ``` ### Response #### Success Response - Returns `true` on success, `false` on failure. ``` -------------------------------- ### Send Invoice via Email Source: https://context7.com/pancakeapp/php-sdk/llms.txt Sends an invoice email to the client. Can be used statically with a unique ID or on an invoice instance to reload its state. Ensure the invoice exists. ```php send(); echo "Last sent timestamp: {$invoice->last_sent}\n"; } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` -------------------------------- ### Update Invoice Payment Details Source: https://context7.com/pancakeapp/php-sdk/llms.txt Updates the gateway and transaction metadata for a specific payment part of an existing invoice. Ensure the invoice and payment part index are valid. ```php setPaymentDetails( 0, // payment_i: zero-based part index "PayPal", // gateway strtotime("2024-06-01"), // payment_datetime "Completed", // payment_status "PAYID-ABC987", // transaction_id 2.75, // transaction_fee false // send_notification_email ); echo $success ? "Payment details updated.\n" : "Update failed.\n"; } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` -------------------------------- ### Invoice::send Source: https://context7.com/pancakeapp/php-sdk/llms.txt Sends the invoice email to the client associated with the invoice and reloads the invoice state. ```APIDOC ## Invoice::send — Email Invoice to Client `Invoices::send()` (static) or `$invoice->send()` (instance) sends the invoice email to the client associated with the invoice and reloads the invoice state. ### Method ```php // Static form public static function send(Server $server, string $unique_id) // Instance form public function send() : bool ``` ### Parameters #### Static Form - **server** (Server) - Required - The Pancake server instance. - **unique_id** (string) - Required - The unique ID of the invoice. #### Instance Form No parameters required. ### Response #### Success Response - Returns `true` if the email was sent successfully, `false` otherwise. ### Request Example ```php send(); echo "Last sent timestamp: {$invoice->last_sent}\n"; } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ?> ``` ``` -------------------------------- ### Invoices::getByClientId - Fetch Invoices for a Client Source: https://context7.com/pancakeapp/php-sdk/llms.txt Filters the invoice list to retrieve only those invoices associated with a specific client ID. Returns an array of `Invoice` objects. ```APIDOC ## Invoices::getByClientId — Fetch Invoices for a Client `Invoices::getByClientId()` filters the invoice list to those belonging to a specific client, returning an array of `Invoice` objects. ```php unique_id}: {$invoice->currency_symbol}{$invoice->total} — {$invoice->status}\n"; if ($invoice->days_overdue > 0) { echo " *** OVERDUE by {$invoice->days_overdue} day(s) ***\n"; } } } catch (ApiException $e) { echo "Error: " . $e->getMessage(); } ``` ``` -------------------------------- ### Delete Invoice by Unique ID Source: https://context7.com/pancakeapp/php-sdk/llms.txt Permanently removes an invoice using its unique identifier. Use with caution as this action is irreversible. ```php getMessage(); } ``` -------------------------------- ### Invoices::delete Source: https://context7.com/pancakeapp/php-sdk/llms.txt Permanently removes an invoice by its unique ID. ```APIDOC ## Invoices::delete — Delete an Invoice `Invoices::delete()` permanently removes an invoice by its unique ID. ### Method ```php public static function delete(Server $server, string $unique_id) ``` ### Parameters #### Path Parameters - **server** (Server) - Required - The Pancake server instance. - **unique_id** (string) - Required - The unique ID of the invoice to delete. ### Response #### Success Response - Returns `true` if the invoice was deleted successfully, `false` otherwise. ### Request Example ```php getMessage(); } ?> ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.