diff --git a/code-samples/business/address.php b/code-samples/business/address.php new file mode 100644 index 0000000..455ed8e --- /dev/null +++ b/code-samples/business/address.php @@ -0,0 +1,76 @@ + '', + 'privateKey' => '', +]); + +echo "FasterPay Address API Examples\n"; +echo "==============================\n\n"; + +// Example 1: Get address fields for United States +echo "1. Getting address fields for United States\n"; +echo "--------------------------------------------\n"; + +try { + $addressFieldsResponse = $businessGateway->addressService()->getAddressFields('US'); + + if ($addressFieldsResponse->isSuccessful()) { + echo "Address fields retrieved successfully\n"; + $data = $addressFieldsResponse->getDecodeResponse(); + $countryCode = isset($data['data']['country_code']) ? $data['data']['country_code'] : 'US'; + $fields = isset($data['data']['fields']) ? $data['data']['fields'] : []; + $subdivisions = isset($data['data']['subdivisions']) ? $data['data']['subdivisions'] : []; + + echo " Country: $countryCode\n"; + echo " Required fields:\n"; + + foreach ($fields as $field) { + $type = isset($field['type']) ? $field['type'] : 'text'; + $label = isset($field['label']) ? $field['label'] : ''; + $name = isset($field['name']) ? $field['name'] : ''; + echo " - $label ($name): $type\n"; + } + + echo " States/subdivisions: " . count($subdivisions) . " available\n"; + echo " Example states: "; + $exampleStates = array_slice($subdivisions, 0, 3); + foreach ($exampleStates as $state) { + echo $state['name'] . ' (' . $state['code'] . ') '; + } + echo "\n"; + } else { + echo "Error: " . $addressFieldsResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Get address fields for Canada +echo "2. Getting address fields for Canada\n"; +echo "------------------------------------\n"; + +try { + $canadaFieldsResponse = $businessGateway->addressService()->getAddressFields('CA'); + + if ($canadaFieldsResponse->isSuccessful()) { + echo "Canadian address fields retrieved\n"; + $data = $canadaFieldsResponse->getDecodeResponse(); + $fields = isset($data['data']['fields']) ? $data['data']['fields'] : []; + + echo " Canada requires " . count($fields) . " fields:\n"; + foreach ($fields as $field) { + echo " - " . (isset($field['label']) ? $field['label'] : 'Unknown') . "\n"; + } + } else { + echo "Error: " . $canadaFieldsResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\nAddress API examples completed!\n"; diff --git a/code-samples/business/contact.php b/code-samples/business/contact.php new file mode 100644 index 0000000..2a967a6 --- /dev/null +++ b/code-samples/business/contact.php @@ -0,0 +1,193 @@ + '', + 'privateKey' => '', +]); + +echo "FasterPay Contact API Examples\n"; +echo "===============================\n\n"; + +// Example 1: Create a new individual contact +echo "1. Creating a new individual contact\n"; +echo "------------------------------------\n"; + +$contactData = [ + 'email' => 'john.smith@example.com', + 'phone' => '2015550124', + 'phone_country_code' => 'US', + 'first_name' => 'John', + 'last_name' => 'Smith', + 'country' => 'US', + 'favorite' => true +]; + +try { + $contactResponse = $businessGateway->contactService()->createContact($contactData); + + if ($contactResponse->isSuccessful()) { + echo "Contact created successfully\n"; + $responseData = $contactResponse->getDecodeResponse(); + $contactId = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'CT-' . time(); + echo " Contact ID: " . $contactId . "\n"; + echo " Name: " . $contactData['first_name'] . " " . $contactData['last_name'] . "\n"; + echo " Email: " . $contactData['email'] . "\n"; + echo " Phone: " . $contactData['phone'] . "\n"; + echo " Country: " . $contactData['country'] . "\n"; + } else { + echo "Error: " . $contactResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Create another contact (for listing) +echo "2. Creating another contact (for listing)\n"; +echo "---------------------------\n"; + +$secondContactData = [ + 'email' => 'jane.doe@example.com', + 'phone' => '2015550987', + 'phone_country_code' => 'US', + 'first_name' => 'Jane', + 'last_name' => 'Doe', + 'country' => 'US', + 'favorite' => false +]; + +try { + $secondContactResponse = $businessGateway->contactService()->createContact($secondContactData); + + if ($secondContactResponse->isSuccessful()) { + echo "Second contact created successfully\n"; + $responseData = $secondContactResponse->getDecodeResponse(); + $secondContactId = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'CT-2-' . time(); + echo " Contact ID: " . $secondContactId . "\n"; + echo " Name: " . $secondContactData['first_name'] . " " . $secondContactData['last_name'] . "\n"; + echo " Email: " . $secondContactData['email'] . "\n"; + echo " Phone: " . $secondContactData['phone'] . "\n"; + } else { + echo "Error: " . $secondContactResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 3: Get contact details +echo "3. Getting contact details\n"; +echo "--------------------------\n"; + +try { + $detailsResponse = $businessGateway->contactService()->getContact($contactId); + + if ($detailsResponse->isSuccessful()) { + echo "Contact details retrieved successfully\n"; + $details = $detailsResponse->getDecodeResponse(); + + if (isset($details['data'])) { + $contact = $details['data']; + echo " ID: " . (isset($contact['id']) ? $contact['id'] : 'N/A') . "\n"; + echo " Name: " . (isset($contact['first_name']) ? $contact['first_name'] : '') . " " . (isset($contact['last_name']) ? $contact['last_name'] : '') . "\n"; + echo " Email: " . (isset($contact['email']) ? $contact['email'] : 'N/A') . "\n"; + echo " Phone: " . (isset($contact['phone_full_number']) ? $contact['phone_full_number'] : 'N/A') . "\n"; + echo " Country: " . (isset($contact['country']) ? $contact['country'] : 'N/A') . "\n"; + } + } else { + echo "Error: " . $detailsResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 4: Update contact +echo "4. Updating contact information\n"; +echo "-------------------------------\n"; + +$updateData = [ + 'phone' => '2015550125', + 'phone_country_code' => 'US', + 'favorite' => false, + 'first_name' => 'Jonathan' +]; + +try { + $updateResponse = $businessGateway->contactService()->updateContact($contactId, $updateData); + + if ($updateResponse->isSuccessful()) { + echo "Contact updated successfully\n"; + echo " Contact ID: " . $contactId . "\n"; + echo " New phone: " . $updateData['phone'] . "\n"; + echo " Updated name: " . $updateData['first_name'] . "\n"; + echo " Favorite: " . ($updateData['favorite'] ? 'Yes' : 'No') . "\n"; + } else { + echo "Error: " . $updateResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 5: List contacts with filters +echo "5. Listing contacts with filters\n"; +echo "--------------------------------\n"; + +$filters = [ + 'per_page' => 20, + 'page' => 1 +]; + +try { + $listResponse = $businessGateway->contactService()->listContacts($filters); + + if ($listResponse->isSuccessful()) { + echo "Contact list retrieved successfully\n"; + + $listData = $listResponse->getDecodeResponse(); + if (isset($listData['data']['data']) && is_array($listData['data']['data'])) { + $contacts = $listData['data']['data']; + echo " Found " . count($contacts) . " contacts\n"; + + foreach ($contacts as $contact) { + $name = (isset($contact['first_name']) ? $contact['first_name'] : '') . ' ' . (isset($contact['last_name']) ? $contact['last_name'] : ''); + $email = isset($contact['email']) ? $contact['email'] : 'No email'; + echo " - " . trim($name) . " (" . $email . ")\n"; + } + } + } else { + echo "Error: " . $listResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 6: Delete a contact +echo "6. Deleting a contact\n"; +echo "---------------------\n"; + +$contactToDelete = isset($secondContactId) ? $secondContactId : 'CT-DELETE-' . time(); + +try { + $deleteResponse = $businessGateway->contactService()->deleteContact($contactToDelete); + + if ($deleteResponse->isSuccessful()) { + echo "Contact deleted successfully\n"; + echo " Deleted Contact ID: " . $contactToDelete . "\n"; + } else { + echo "Error: " . $deleteResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\nContact API examples completed!\n"; \ No newline at end of file diff --git a/code-samples/business/e-invoice/discount.php b/code-samples/business/e-invoice/discount.php new file mode 100644 index 0000000..6572b6b --- /dev/null +++ b/code-samples/business/e-invoice/discount.php @@ -0,0 +1,226 @@ + '', + 'privateKey' => '', +]); + +echo "FasterPay Invoice Discount API Examples\n"; +echo "==========================================\n\n"; + +// Example 1: Create a flat discount +echo "1. Creating a flat discount\n"; +echo "---------------------------\n"; + +$flatDiscountData = [ + 'name' => 'Early Bird Discount', + 'type' => 'flat', + 'value' => 10.00, + 'currency' => 'USD', + 'description' => '$10 discount for early payment' +]; + +try { + $discountResponse = $businessGateway->invoiceDiscountService()->createDiscount($flatDiscountData); + + if ($discountResponse->isSuccessful()) { + echo "Flat discount created successfully\n"; + $responseData = $discountResponse->getDecodeResponse(); + $discountId = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'DC-' . time(); + echo " Discount ID: " . $discountId . "\n"; + echo " Name: " . $flatDiscountData['name'] . "\n"; + echo " Type: " . $flatDiscountData['type'] . "\n"; + echo " Value: $" . number_format($flatDiscountData['value'], 2) . " " . $flatDiscountData['currency'] . "\n"; + echo " Description: " . $flatDiscountData['description'] . "\n"; + } else { + echo "Error: " . $discountResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Create a percentage discount +echo "2. Creating a percentage discount\n"; +echo "---------------------------------\n"; + +$percentageDiscountData = [ + 'name' => 'Volume Discount', + 'type' => 'percentage', + 'value' => 15, + 'description' => '15% discount for bulk orders' +]; + +try { + $volumeResponse = $businessGateway->invoiceDiscountService()->createDiscount($percentageDiscountData); + + if ($volumeResponse->isSuccessful()) { + echo "Percentage discount created successfully\n"; + $responseData = $volumeResponse->getDecodeResponse(); + $volumeDiscountId = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'DC-VOLUME-' . time(); + echo " Discount ID: " . $volumeDiscountId . "\n"; + echo " Name: " . $percentageDiscountData['name'] . "\n"; + echo " Type: " . $percentageDiscountData['type'] . "\n"; + echo " Value: " . $percentageDiscountData['value'] . "%\n"; + echo " Description: " . $percentageDiscountData['description'] . "\n"; + } else { + echo "Error: " . $volumeResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 3: Create loyalty discount +echo "3. Creating loyalty discount\n"; +echo "----------------------------\n"; + +$loyaltyDiscountData = [ + 'name' => 'Loyalty Customer Discount', + 'type' => 'percentage', + 'value' => 25, + 'description' => '25% discount for loyal customers' +]; + +try { + $loyaltyResponse = $businessGateway->invoiceDiscountService()->createDiscount($loyaltyDiscountData); + + if ($loyaltyResponse->isSuccessful()) { + echo "Loyalty discount created successfully\n"; + $responseData = $loyaltyResponse->getDecodeResponse(); + $loyaltyDiscountId = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'DC-LOYALTY-' . time(); + echo " Discount ID: " . $loyaltyDiscountId . "\n"; + echo " Name: " . $loyaltyDiscountData['name'] . "\n"; + echo " Type: " . $loyaltyDiscountData['type'] . "\n"; + echo " Value: " . $loyaltyDiscountData['value'] . "%\n"; + echo " Description: " . $loyaltyDiscountData['description'] . "\n"; + } else { + echo "Error: " . $loyaltyResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 4: Get discount details +echo "4. Retrieving discount details\n"; +echo "------------------------------\n"; + +$testDiscountId = isset($discountId) ? $discountId : 'DC-250527-WZX0'; + +try { + $getDiscountResponse = $businessGateway->invoiceDiscountService()->getDiscount($testDiscountId); + + if ($getDiscountResponse->isSuccessful()) { + echo "Discount details retrieved successfully\n"; + $discountData = $getDiscountResponse->getDecodeResponse(); + if (isset($discountData['data'])) { + $discount = $discountData['data']; + echo " Discount ID: " . (isset($discount['id']) ? $discount['id'] : $testDiscountId) . "\n"; + echo " Name: " . (isset($discount['name']) ? $discount['name'] : 'N/A') . "\n"; + echo " Type: " . (isset($discount['type']) ? $discount['type'] : 'N/A') . "\n"; + echo " Value: " . (isset($discount['value']) ? $discount['value'] : 'N/A') . "\n"; + if (isset($discount['currency'])) { + echo " Currency: " . $discount['currency'] . "\n"; + } + echo " Description: " . (isset($discount['description']) ? $discount['description'] : 'N/A') . "\n"; + } + } else { + echo "Error: " . $getDiscountResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 5: Update discount +echo "5. Updating discount\n"; +echo "--------------------\n"; + +$updateDiscountData = [ + 'name' => 'Updated Early Bird Discount', + 'description' => 'Updated early payment discount with better terms', + 'value' => 15.00 +]; + +try { + $updateResponse = $businessGateway->invoiceDiscountService()->updateDiscount($testDiscountId, $updateDiscountData); + + if ($updateResponse->isSuccessful()) { + echo "Discount updated successfully\n"; + echo " Discount ID: " . $testDiscountId . "\n"; + echo " Updated Name: " . $updateDiscountData['name'] . "\n"; + echo " Updated Value: $" . number_format($updateDiscountData['value'], 2) . "\n"; + echo " Updated Description: " . $updateDiscountData['description'] . "\n"; + } else { + echo "Error: " . $updateResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 6: List discounts +echo "6. Listing discounts\n"; +echo "--------------------\n"; + +$filters = [ + 'per_page' => 20, + 'page' => 1 +]; + +try { + $listResponse = $businessGateway->invoiceDiscountService()->listDiscounts($filters); + + if ($listResponse->isSuccessful()) { + echo "Discount list retrieved successfully\n"; + $listData = $listResponse->getDecodeResponse(); + if (isset($listData['data']['data']) && is_array($listData['data']['data'])) { + echo " Found " . count($listData['data']['data']) . " discounts:\n"; + foreach ($listData['data']['data'] as $discount) { + $id = isset($discount['id']) ? $discount['id'] : 'Unknown'; + $name = isset($discount['name']) ? $discount['name'] : 'Unnamed'; + $type = isset($discount['type']) ? $discount['type'] : 'Unknown'; + $value = isset($discount['value']) ? $discount['value'] : '0'; + $currency = isset($discount['currency']) ? ' ' . $discount['currency'] : ''; + echo " - " . $name . " (" . $id . ") - " . $type . ": " . $value . $currency . "\n"; + } + } + } else { + echo "Error: " . $listResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 7: Delete discount +echo "7. Deleting discount\n"; +echo "--------------------\n"; + +$deleteTestDiscountId = isset($loyaltyDiscountId) ? $loyaltyDiscountId : 'DC-DELETE-' . time(); + +try { + $deleteResponse = $businessGateway->invoiceDiscountService()->deleteDiscount($deleteTestDiscountId); + + if ($deleteResponse->isSuccessful()) { + echo "Discount deleted successfully\n"; + echo " Deleted Discount ID: " . $deleteTestDiscountId . "\n"; + } else { + echo "Error: " . $deleteResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +echo "\nInvoice Discount API examples completed!\n"; \ No newline at end of file diff --git a/code-samples/business/e-invoice/invoice.php b/code-samples/business/e-invoice/invoice.php new file mode 100644 index 0000000..2280523 --- /dev/null +++ b/code-samples/business/e-invoice/invoice.php @@ -0,0 +1,479 @@ + 'your_public_key', + 'privateKey' => 'your_private_key', +]); + +// Store component IDs for later use +$createdTemplateId = null; +$createdTaxId = null; +$createdDiscountId = null; +$createdProductId = null; +$createdContactId = null; + +// =================================================================== +// CREATE CONTACT FIRST +// =================================================================== + +$contactData = [ + 'email' => 'john.smith@example.com', + 'phone' => '2015550124', + 'phone_country_code' => 'US', + 'first_name' => 'John', + 'last_name' => 'Smith', + 'country' => 'US', + 'favorite' => true +]; + +try { + $response = $businessGateway->contactService()->createContact($contactData); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $createdContactId = $responseData['data']['id']; + echo 'Contact created: ' . $createdContactId . "\n"; + } else { + echo 'Error creating contact: ' . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo 'Exception creating contact: ' . $e->getMessage() . "\n"; +} + +// =================================================================== +// CREATE TEMPLATE COMPONENT +// =================================================================== + +$templateData = [ + 'name' => 'Project Template', + 'footer' => 'Thank you for your business!', + 'colors' => [ + 'primary' => '#2563eb', + 'secondary' => '#f8fafc' + ], + 'localized_address' => [ + 'address_line1' => '123 Business Ave', + 'locality' => 'San Francisco', + 'administrative_area' => 'CA', + 'postal_code' => '94105' + ], + 'country_code' => 'US' + // 'logo' => new SplFileInfo('/path/to/logo.jpg') + // 'logo' => '/path/to/logo.jpg' +]; + +try { + $response = $businessGateway->invoiceTemplateService()->createTemplate($templateData); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $createdTemplateId = $responseData['data']['id']; + echo 'Template created: ' . $createdTemplateId . "\n"; + } else { + echo 'Error creating template: ' . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo 'Exception creating template: ' . $e->getMessage() . "\n"; +} + +// =================================================================== +// CREATE TAX COMPONENT +// =================================================================== + +$taxData = [ + 'name' => 'Sales Tax', + 'type' => 'percentage', + 'value' => 8.5, + 'description' => '8.5% sales tax for California' +]; + +try { + $response = $businessGateway->invoiceTaxService()->createTax($taxData); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $createdTaxId = $responseData['data']['id']; + echo 'Tax created: ' . $createdTaxId . "\n"; + } else { + echo 'Error creating tax: ' . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo 'Exception creating tax: ' . $e->getMessage() . "\n"; +} + +// =================================================================== +// CREATE DISCOUNT COMPONENT +// =================================================================== + +$discountData = [ + 'name' => 'Early Payment Discount', + 'type' => 'percentage', + 'value' => 10.0, + 'description' => '10% discount for early payment' +]; + +try { + $response = $businessGateway->invoiceDiscountService()->createDiscount($discountData); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $createdDiscountId = $responseData['data']['id']; + echo 'Discount created: ' . $createdDiscountId . "\n"; + } else { + echo 'Error creating discount: ' . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo 'Exception creating discount: ' . $e->getMessage() . "\n"; +} + +// =================================================================== +// CREATE PRODUCT COMPONENT +// =================================================================== + +$productData = [ + 'name' => 'Website Development Package', + 'sku' => 'WEB-DEV-001', + 'type' => 'digital', + 'description' => 'Complete website development with responsive design.', + 'prices' => [ + [ + 'price' => 2500.00, + 'currency' => 'USD' + ] + ] + // 'image' => new SplFileInfo('/path/to/image.jpg') + // 'image' => '/path/to/image.jpg' +]; + +try { + $response = $businessGateway->invoiceProductService()->createProduct($productData); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $createdProductId = $responseData['data']['id']; + echo 'Product created: ' . $createdProductId . "\n"; + } else { + echo 'Error creating product: ' . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo 'Exception creating product: ' . $e->getMessage() . "\n"; +} + +// =================================================================== +// CREATE INVOICE - Using Created Component IDs +// =================================================================== + +echo "1. Creating invoice (non-embedded)\n"; +echo "-------------------\n"; + +$createdInvoiceId = null; + +if ($createdTemplateId && $createdTaxId && $createdDiscountId && $createdProductId && $createdContactId) { + $invoiceDataWithIds = [ + 'invoice_template_id' => $createdTemplateId, + 'tax_id' => $createdTaxId, + 'discount_id' => $createdDiscountId, + 'currency' => 'USD', + 'summary' => 'Website development project using created components', + 'number' => 'INV-' . date('Y') . '-' . sprintf('%06d', rand(1, 999999)), + 'contact_id' => $createdContactId, + 'due_date' => date('Y-m-d', strtotime('+30 days')), + 'items' => [ + [ + 'price' => 2500.00, + 'product_id' => $createdProductId, + 'tax_id' => $createdTaxId, + 'discount_id' => $createdDiscountId, + 'quantity' => 1 + ] + ] + ]; + + try { + $response = $businessGateway->invoiceService()->createInvoice($invoiceDataWithIds); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $createdInvoiceId = $responseData['data']['id']; + echo 'Invoice created with component IDs: ' . $createdInvoiceId . "\n"; + } else { + echo 'Error creating invoice: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception creating invoice: ' . $e->getMessage() . "\n"; + } +} else { + echo "Cannot create invoice - missing required components\n"; +} + +// =================================================================== +// CREATE INVOICE - With Embedded Components (Alternative approach) +// =================================================================== + +echo "2. Creating invoice (embedded)\n"; +echo "-------------------\n"; + +$embeddedInvoiceId = null; + +if ($createdContactId) { + $invoiceDataWithEmbedded = [ + 'template' => [ + 'name' => 'Embedded Template', + 'footer' => 'Thank you for choosing our services!', + 'colors' => [ + 'primary' => '#059669', + 'secondary' => '#f0fdf4' + ], + 'localized_address' => [ + 'address_line1' => '456 Commerce St', + 'locality' => 'Los Angeles', + 'administrative_area' => 'CA', + 'postal_code' => '90210' + ], + 'country_code' => 'US' + // 'logo' => new SplFileInfo('/path/to/embedded-logo.jpg') + // 'logo' => '/path/to/embedded-logo.jpg' + ], + 'tax' => [ + 'name' => 'State Sales Tax', + 'type' => 'percentage', + 'value' => 7.25, + 'description' => '7.25% California state sales tax' + ], + 'discount' => [ + 'name' => 'First Time Customer Discount', + 'type' => 'flat', + 'value' => 100.00, + 'currency' => 'USD', + 'description' => '$100 discount for first-time customers' + ], + 'currency' => 'USD', + 'summary' => 'Website development with embedded components', + 'number' => 'INV-EMB-' . date('Y') . '-' . sprintf('%06d', rand(1, 999999)), + 'contact_id' => $createdContactId, + 'due_date' => date('Y-m-d', strtotime('+45 days')), + 'items' => [ + [ + 'price' => 3000.00, + 'product' => [ + 'sku' => 'WEB-DEV-PREMIUM', + 'type' => 'digital', + 'name' => 'Premium Website Development', + 'description' => 'Premium website development with advanced features', + // 'image' => new SplFileInfo('/path/to/product-image.jpg') + // 'image' => '/path/to/product-image.jpg' + 'prices' => [ + [ + 'price' => 3000.00, + 'currency' => 'USD' + ] + ] + ], + 'tax' => [ + 'name' => 'Service Tax', + 'type' => 'percentage', + 'value' => 5.0, + 'description' => '5% service tax' + ], + 'discount' => [ + 'name' => 'Bulk Service Discount', + 'type' => 'percentage', + 'value' => 5.0, + 'description' => '5% discount for premium services' + ], + 'quantity' => 1 + ] + ] + ]; + + try { + $response = $businessGateway->invoiceService()->createInvoice($invoiceDataWithEmbedded); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $embeddedInvoiceId = $responseData['data']['id']; + echo 'Invoice created with embedded components: ' . $embeddedInvoiceId . "\n"; + } else { + echo 'Error creating embedded invoice: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception creating embedded invoice: ' . $e->getMessage() . "\n"; + } +} + +// =================================================================== +// UPDATE INVOICE - Use Created Invoice ID +// =================================================================== + +echo "3. Updating invoice\n"; +echo "-------------------\n"; + +if ($createdInvoiceId) { + $updateData = [ + 'summary' => 'Updated website development project', + 'items' => [ + [ + 'price' => 2750.00, // Updated price + 'product_id' => $createdProductId, + 'tax_id' => $createdTaxId, + 'discount_id' => $createdDiscountId, + 'quantity' => 1 + ] + ] + ]; + + try { + $response = $businessGateway->invoiceService()->updateInvoice($createdInvoiceId, $updateData); + + if ($response->isSuccessful()) { + echo 'Invoice updated successfully: ' . $createdInvoiceId . "\n"; + } else { + echo 'Error updating invoice: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception updating invoice: ' . $e->getMessage() . "\n"; + } +} + +// =================================================================== +// LIST INVOICES +// =================================================================== + +echo "4. Listing invoices\n"; +echo "-------------------\n"; + +$filters = [ + 'per_page' => 20, + 'page' => 1 + 'status' => 'draft', + 'include' => 'items,items.product.prices' +]; + +try { + $response = $businessGateway->invoiceService()->listInvoices($filters); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $invoices = $responseData['data']['data']; + echo 'Found ' . count($invoices) . ' draft invoices' . "\n"; + + foreach ($invoices as $invoice) { + echo ' - Invoice ID: ' . $invoice['id'] . ' - Status: ' . $invoice['status'] . "\n"; + } + } else { + echo 'Error listing invoices: ' . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo 'Exception listing invoices: ' . $e->getMessage() . "\n"; +} + +// =================================================================== +// GET INVOICE DETAILS - Use Created Invoice ID +// =================================================================== + +echo "5. Getting invoice details\n"; +echo "-------------------\n"; + +if ($createdInvoiceId) { + try { + $response = $businessGateway->invoiceService()->getInvoice($createdInvoiceId); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $invoice = $responseData['data']; + echo 'Retrieved invoice details:' . "\n"; + echo ' ID: ' . $invoice['id'] . "\n"; + echo ' Status: ' . $invoice['status'] . "\n"; + echo ' Currency: ' . $invoice['currency'] . "\n"; + echo ' Summary: ' . $invoice['summary'] . "\n"; + } else { + echo 'Error getting invoice details: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception getting invoice details: ' . $e->getMessage() . "\n"; + } +} + +// =================================================================== +// SEND INVOICE - Use Embedded Invoice ID +// =================================================================== + +echo "6. Sending invoice\n"; +echo "-------------------\n"; + +if ($embeddedInvoiceId) { + $sendParams = [ + 'email' => 'customer@example.com' + ]; + + try { + $response = $businessGateway->invoiceService()->sendInvoice($embeddedInvoiceId, $sendParams); + + if ($response->isSuccessful()) { + echo 'Invoice sent successfully to: ' . $sendParams['email'] . ' for invoice: ' . $embeddedInvoiceId . "\n"; + } else { + echo 'Error sending invoice: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception sending invoice: ' . $e->getMessage() . "\n"; + } +} + +// =================================================================== +// UPDATE INVOICE STATUS - Use Created Invoice ID +// =================================================================== + +echo "7. Updating invoice status\n"; +echo "-------------------\n"; + +if ($createdInvoiceId) { + $statusData = [ + 'status' => 'void' + ]; + + try { + $response = $businessGateway->invoiceService()->updateInvoiceStatus($createdInvoiceId, $statusData); + + if ($response->isSuccessful()) { + echo 'Invoice status updated to: ' . $statusData['status'] . ' for invoice: ' . $createdInvoiceId . "\n"; + } else { + echo 'Error updating invoice status: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception updating invoice status: ' . $e->getMessage() . "\n"; + } +} + +// =================================================================== +// DOWNLOAD INVOICE PDF - Use Created Invoice ID +// =================================================================== + +echo "8. Downloading invoice PDF\n"; +echo "-------------------\n"; + +if ($createdInvoiceId) { + try { + $response = $businessGateway->invoiceService()->downloadInvoicePdf($createdInvoiceId); + + if ($response->isSuccessful()) { + $pdfContent = $response->getRawResponse(); + echo 'PDF downloaded successfully for: ' . $createdInvoiceId . "\n"; + echo 'PDF size: ' . strlen($pdfContent) . ' bytes' . "\n"; + + // Save to file if needed + // file_put_contents('invoice_' . $createdInvoiceId . '.pdf', $pdfContent); + } else { + echo 'Error downloading PDF: ' . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo 'Exception downloading PDF: ' . $e->getMessage() . "\n"; + } +} \ No newline at end of file diff --git a/code-samples/business/e-invoice/product.php b/code-samples/business/e-invoice/product.php new file mode 100644 index 0000000..f60028e --- /dev/null +++ b/code-samples/business/e-invoice/product.php @@ -0,0 +1,174 @@ + '', + 'privateKey' => '', +]); + +echo "FasterPay Invoice Product API Examples\n"; +echo "=========================================\n\n"; + +// Example 1: Create product +echo "1. Creating product\n"; +echo "-------------------\n"; + +$createData = [ + 'name' => 'Website Development Package', + 'sku' => 'WEB-DEV-001', + 'type' => 'digital', + 'description' => 'Complete website development with modern responsive design.', + 'prices' => [ + [ + 'price' => 2500.00, + 'currency' => 'USD' + ] + ] + // 'image' => '/path/to/product-image.jpg' // Uncomment for image upload (file path) + // 'image' => new SplFileInfo('/path/to/product-image.jpg') // Or use SplFileInfo object +]; + +try { + $response = $businessGateway->invoiceProductService()->createProduct($createData); + + if ($response->isSuccessful()) { + echo "Product created successfully\n"; + $data = $response->getDecodeResponse(); + $productId = $data['data']['id'] ?: 'PD-' . time(); + echo " Product ID: $productId\n"; + echo " Name: {$createData['name']}\n"; + echo " Type: {$createData['type']}\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Get product details +echo "2. Getting product details\n"; +echo "--------------------------\n"; + +try { + $response = $businessGateway->invoiceProductService()->getProduct($productId); + + if ($response->isSuccessful()) { + echo "Product details retrieved\n"; + $data = $response->getDecodeResponse(); + $product = $data['data'] ?: []; + + echo " ID: " . ($product['id'] ?: $productId) . "\n"; + echo " SKU: " . ($product['sku'] ?: 'N/A') . "\n"; + echo " Name: " . ($product['name'] ?: 'N/A') . "\n"; + echo " Type: " . ($product['type'] ?: 'N/A') . "\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 3: Update product +echo "3. Updating product\n"; +echo "-------------------\n"; + +$updateData = [ + 'name' => 'Website Development Package - Premium', + 'description' => 'Premium website development with enhanced features.', + 'prices' => [ + [ + 'price' => 3500.00, + 'currency' => 'USD' + ] + ] + // 'image' => '/path/to/updated-image.jpg' // Uncomment for image upload (file path) + // 'image' => new SplFileInfo('/path/to/updated-image.jpg') // Or use SplFileInfo object +]; + +try { + $response = $businessGateway->invoiceProductService()->updateProduct($productId, $updateData); + + if ($response->isSuccessful()) { + echo "Product updated successfully\n"; + echo " Updated name and pricing\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 4: List products +echo "4. Listing products\n"; +echo "-------------------\n"; + +try { + $response = $businessGateway->invoiceProductService()->listProducts([ + 'per_page' => 20, + 'page' => 1 + ]); + + if ($response->isSuccessful()) { + echo "Products retrieved successfully\n"; + $data = $response->getDecodeResponse(); + $products = $data['data']['data'] ?: []; + echo " Found " . count($products) . " products\n"; + + foreach ($products as $product) { + $id = $product['id'] ?: 'Unknown'; + $name = $product['name'] ?: 'Unnamed'; + echo " - $name ($id)\n"; + } + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 5: Delete product price +echo "5. Deleting product price\n"; +echo "-------------------------\n"; + +try { + $response = $businessGateway->invoiceProductService()->deleteProductPrice($productId, 'USD'); + + if ($response->isSuccessful()) { + echo "Product price deleted successfully\n"; + echo " Deleted USD price for Product ID: $productId\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 6: Delete product +echo "6. Deleting product\n"; +echo "-------------------\n"; + +try { + $response = $businessGateway->invoiceProductService()->deleteProduct($productId); + + if ($response->isSuccessful()) { + echo "Product deleted successfully\n"; + echo " Deleted Product ID: $productId\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\nInvoice Product API examples completed!\n"; \ No newline at end of file diff --git a/code-samples/business/e-invoice/tax.php b/code-samples/business/e-invoice/tax.php new file mode 100644 index 0000000..d2bf246 --- /dev/null +++ b/code-samples/business/e-invoice/tax.php @@ -0,0 +1,196 @@ + '', + 'privateKey' => '', +]); + +echo "FasterPay Invoice Tax API Examples\n"; +echo "===================================\n\n"; + +// Example 1: Create a tax +echo "1. Creating a tax\n"; +echo "----------------------------------\n"; + +$taxData = [ + 'name' => 'Sales Tax', + 'type' => 'percentage', + 'value' => 8.5, + 'description' => '8.5% sales tax for California' +]; + +try { + $taxResponse = $businessGateway->invoiceTaxService()->createTax($taxData); + + if ($taxResponse->isSuccessful()) { + echo "Tax created successfully\n"; + $responseData = $taxResponse->getDecodeResponse(); + $taxId = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'TX-' . time(); + echo " Tax ID: " . $taxId . "\n"; + echo " Name: " . $taxData['name'] . "\n"; + echo " Type: " . $taxData['type'] . "\n"; + echo " Rate: " . $taxData['value'] . "%\n"; + echo " Description: " . $taxData['description'] . "\n"; + } else { + echo "Error: " . $taxResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Get tax details +echo "2. Getting tax details\n"; +echo "----------------------\n"; + +$taxId = isset($taxId) ? $taxId : 'TX-' . time(); + +try { + $detailsResponse = $businessGateway->invoiceTaxService()->getTax($taxId); + + if ($detailsResponse->isSuccessful()) { + echo "Tax details retrieved\n"; + $details = $detailsResponse->getDecodeResponse(); + $tax = isset($details['data']) ? $details['data'] : []; + + echo " ID: " . (isset($tax['id']) ? $tax['id'] : $taxId) . "\n"; + echo " Name: " . (isset($tax['name']) ? $tax['name'] : 'N/A') . "\n"; + echo " Type: " . (isset($tax['type']) ? $tax['type'] : 'N/A') . "\n"; + echo " Value: " . (isset($tax['value']) ? $tax['value'] : '0') . "\n"; + echo " Currency: " . (isset($tax['currency']) ? $tax['currency'] : 'N/A') . "\n"; + echo " Description: " . (isset($tax['description']) ? $tax['description'] : 'N/A') . "\n"; + } else { + echo "Error: " . $detailsResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 3: Update tax +echo "3. Updating tax\n"; +echo "---------------\n"; + +$updateData = [ + 'name' => 'Updated Sales Tax', + 'value' => 9.0, + 'description' => 'Updated 9% sales tax rate' +]; + +try { + $updateResponse = $businessGateway->invoiceTaxService()->updateTax($taxId, $updateData); + + if ($updateResponse->isSuccessful()) { + echo "Tax updated successfully\n"; + echo " Updated rate to: " . $updateData['value'] . "%\n"; + echo " Updated description\n"; + } else { + echo "Error: " . $updateResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 4: List all taxes +echo "4. Listing all taxes\n"; +echo "--------------------\n"; + +$listFilters = [ + 'page' => 1, + 'per_page' => 10 +]; + +try { + $listResponse = $businessGateway->invoiceTaxService()->listTaxes($listFilters); + + if ($listResponse->isSuccessful()) { + echo "Tax list retrieved\n"; + $listData = $listResponse->getDecodeResponse(); + $taxes = isset($listData['data']['data']) ? $listData['data']['data'] : []; + + echo " Found " . count($taxes) . " taxes\n"; + foreach ($taxes as $tax) { + $id = isset($tax['id']) ? $tax['id'] : 'Unknown'; + $name = isset($tax['name']) ? $tax['name'] : 'Unnamed'; + $type = isset($tax['type']) ? $tax['type'] : 'unknown'; + $value = isset($tax['value']) ? $tax['value'] : '0'; + echo " - $name ($type: $value) - ID: $id\n"; + } + } else { + echo "Error: " . $listResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 5: Create multiple regional taxes +echo "5. Creating multiple regional taxes\n"; +echo "-----------------------------------\n"; + +$regionalTaxes = [ + [ + 'name' => 'New York State Tax', + 'type' => 'percentage', + 'value' => 8.25, + 'description' => '8.25% sales tax for New York State' + ], + [ + 'name' => 'Texas State Tax', + 'type' => 'percentage', + 'value' => 6.25, + 'description' => '6.25% sales tax for Texas' + ], + [ + 'name' => 'European VAT', + 'type' => 'percentage', + 'value' => 19.0, + 'description' => '19% VAT for European Union' + ] +]; + +foreach ($regionalTaxes as $taxData) { + try { + $response = $businessGateway->invoiceTaxService()->createTax($taxData); + + if ($response->isSuccessful()) { + $responseData = $response->getDecodeResponse(); + $id = isset($responseData['data']['id']) ? $responseData['data']['id'] : 'TX-REGION-' . time(); + echo " Created: {$taxData['name']} ({$taxData['value']}%) - ID: $id\n"; + } else { + echo " Error creating {$taxData['name']}: " . $response->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo " Exception creating {$taxData['name']}: " . $e->getMessage() . "\n"; + } +} + +echo "\n"; + +// Example 6: Delete tax +echo "6. Deleting tax\n"; +echo "----------------\n"; + +$deleteTaxId = isset($vatTaxId) ? $vatTaxId : 'TX-DELETE-' . time(); + +try { + $deleteResponse = $businessGateway->invoiceTaxService()->deleteTax($deleteTaxId); + + if ($deleteResponse->isSuccessful()) { + echo "Tax deleted successfully\n"; + echo " Deleted Tax ID: " . $deleteTaxId . "\n"; + } else { + echo "Error: " . $deleteResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\nInvoice Tax API examples completed!\n"; \ No newline at end of file diff --git a/code-samples/business/e-invoice/template.php b/code-samples/business/e-invoice/template.php new file mode 100644 index 0000000..a7b88e5 --- /dev/null +++ b/code-samples/business/e-invoice/template.php @@ -0,0 +1,151 @@ + '', + 'privateKey' => '', +]); + +echo "FasterPay Invoice Template API Examples\n"; +echo "========================================\n\n"; + +// Example 1: Create template +echo "1. Creating template\n"; +echo "--------------------\n"; + +$templateData = [ + 'name' => 'Professional Template', + 'footer' => 'Thank you for your business!', + 'colors' => [ + 'primary' => '#2563eb', + 'secondary' => '#f8fafc' + ], + 'localized_address' => [ + 'address_line1' => '123 Business Avenue', + 'locality' => 'San Francisco', + 'administrative_area' => 'CA', + 'postal_code' => '94105' + ], + 'country_code' => 'US', + // 'logo' => '/path/to/logo.png' // Uncomment for logo upload (file path) + // 'logo' => new SplFileInfo('/path/to/logo.png') // Or use SplFileInfo object +]; + +try { + $response = $businessGateway->invoiceTemplateService()->createTemplate($templateData); + + if ($response->isSuccessful()) { + echo "Template created successfully\n"; + $data = $response->getDecodeResponse(); + $templateId = $data['data']['id'] ?: 'IT-' . time(); + echo " Template ID: $templateId\n"; + echo " Name: {$templateData['name']}\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Update template +echo "2. Updating template\n"; +echo "--------------------\n"; + +$updateData = [ + 'footer' => 'Updated footer text', + 'colors' => [ + 'primary' => '#059669', + 'secondary' => '#ecfdf5' + ], + // 'logo' => '/path/to/new-logo.png' // Uncomment for logo upload (file path) + // 'logo' => new SplFileInfo('/path/to/new-logo.png') // Or use SplFileInfo object +]; + +try { + $response = $businessGateway->invoiceTemplateService()->updateTemplate($templateId, $updateData); + + if ($response->isSuccessful()) { + echo "Template updated successfully\n"; + echo " Updated colors and footer\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 3: Get template details +echo "3. Getting template details\n"; +echo "---------------------------\n"; + +try { + $response = $businessGateway->invoiceTemplateService()->getTemplate($templateId); + + if ($response->isSuccessful()) { + echo "Template details retrieved\n"; + $data = $response->getDecodeResponse(); + $template = $data['data'] ?: []; + + echo " ID: " . ($template['id'] ?: $templateId) . "\n"; + echo " Name: " . ($template['name'] ?: 'N/A') . "\n"; + echo " Country: " . ($template['country_code'] ?: 'N/A') . "\n"; + echo " Footer: " . ($template['footer'] ?: 'N/A') . "\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 4: List templates +echo "4. Listing templates\n"; +echo "--------------------\n"; + +try { + $response = $businessGateway->invoiceTemplateService()->listTemplates(['page' => 1, 'per_page' => 10]); + + if ($response->isSuccessful()) { + echo "Templates retrieved successfully\n"; + $data = $response->getDecodeResponse(); + $templates = $data['data']['data'] ?: []; + echo " Found " . count($templates) . " templates\n"; + + foreach ($templates as $template) { + $id = $template['id'] ?: 'Unknown'; + $name = $template['name'] ?: 'Unnamed'; + echo " - $name ($id)\n"; + } + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 5: Delete template +echo "5. Deleting template\n"; +echo "--------------------\n"; + +try { + $response = $businessGateway->invoiceTemplateService()->deleteTemplate($templateId); + + if ($response->isSuccessful()) { + echo "Template deleted successfully\n"; + echo " Deleted Template ID: $templateId\n"; + } else { + echo "Error: " . $response->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\nInvoice Template API examples completed!\n"; \ No newline at end of file diff --git a/code-samples/business/payout.php b/code-samples/business/payout.php new file mode 100644 index 0000000..adc383f --- /dev/null +++ b/code-samples/business/payout.php @@ -0,0 +1,128 @@ + '', + 'privateKey' => '', +]); + +echo "=== FasterPay Mass Payout API Examples ===\n\n"; + +// Example 1: Create mass payout following API documentation +echo "1. Creating mass payout\n"; +echo "-----------------------\n"; + +$massPayoutParams = [ + 'source_currency' => 'USD', + 'template' => 'wallet', + 'payouts' => [ + [ + 'reference_id' => 'payout_001', + 'amount' => '150.00', + 'amount_currency' => 'USD', + 'target_currency' => 'USD', + 'receiver_full_name' => 'John Doe', + 'receiver_email' => 'john@example.com', + 'receiver_type' => 'private' + ], + [ + 'reference_id' => 'payout_002', + 'amount' => '75.50', + 'amount_currency' => 'USD', + 'target_currency' => 'USD', + 'receiver_full_name' => 'Jane Smith Corporation', + 'receiver_email' => 'jane@example.com', + 'receiver_type' => 'business' + ] + ] +]; + +$createdPayoutIds = []; + +try { + $massPayoutResponse = $businessGateway->payoutService()->createPayout($massPayoutParams); + + if ($massPayoutResponse->isSuccessful()) { + echo "Mass payout created successfully\n"; + $responseData = $massPayoutResponse->getDecodeResponse(); + + // Extract payout IDs from response for further operations + if (isset($responseData['data']) && is_array($responseData['data'])) { + foreach ($responseData['data'] as $payoutItem) { + $createdPayoutIds[] = $payoutItem['id']; + echo "Payout ID: " . $payoutItem['id'] . " - Status: " . $payoutItem['status'] . "\n"; + } + } + } else { + echo "Error: " . $massPayoutResponse->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} + +echo "\n"; + +// Example 2: Get payout details using ID from create response +echo "2. Getting payout details\n"; +echo "-------------------------\n"; + +if (!empty($createdPayoutIds)) { + $payoutId = $createdPayoutIds[0]; // Use first created payout ID + echo "Using payout ID from create response: " . $payoutId . "\n"; + + try { + $payoutDetails = $businessGateway->payoutService()->getPayoutDetails($payoutId); + + if ($payoutDetails->isSuccessful()) { + echo "Payout details retrieved successfully\n"; + $responseData = $payoutDetails->getDecodeResponse(); + + if (isset($responseData['data'])) { + $data = $responseData['data']; + echo "Payout ID: " . $data['id'] . "\n"; + echo "Status: " . $data['status'] . "\n"; + echo "Receiver: " . $data['receiver_full_name'] . " (" . $data['receiver_email'] . ")\n"; + echo "Amount: " . $data['amounts']['target_amount'] . " " . $data['amounts']['target_currency'] . "\n"; + } + } else { + echo "Error: " . $payoutDetails->getErrors()->getMessage() . "\n"; + } + } catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; + } +} else { + echo "No payout ID available from previous create operation\n"; +} + +echo "\n"; + +// Example 3: Get payout list with filters +echo "3. Getting payout list with filters\n"; +echo "-----------------------------------\n"; + +$filters = [ + 'per_page' => 20, + 'page' => 1, +]; + +try { + $payoutList = $businessGateway->payoutService()->getPayoutList($filters); + + if ($payoutList->isSuccessful()) { + echo "Payout list retrieved successfully\n"; + $responseData = $payoutList->getDecodeResponse(); + + if (isset($responseData['data']['data']) && is_array($responseData['data']['data'])) { + echo "Found " . count($responseData['data']['data']) . " payouts\n"; + foreach ($responseData['data']['data'] as $payout) { + echo "- " . $payout['id'] . " (" . $payout['status'] . ") - " . + $payout['amounts']['target_amount'] . " " . $payout['amounts']['target_currency'] . "\n"; + } + } + } else { + echo "Error: " . $payoutList->getErrors()->getMessage() . "\n"; + } +} catch (FasterPay\Exception $e) { + echo "Exception: " . $e->getMessage() . "\n"; +} \ No newline at end of file diff --git a/lib/FasterPay/BusinessGateway.php b/lib/FasterPay/BusinessGateway.php new file mode 100644 index 0000000..a43f551 --- /dev/null +++ b/lib/FasterPay/BusinessGateway.php @@ -0,0 +1,156 @@ +config = $config; + $this->config->setBaseUrl(self::BUSINESS_API_BASE_URL); + + $header = [ + 'X-ApiKey: ' . $this->config->getPrivateKey(), + 'Content-Type: application/json' + ]; + + $this->http = new HttpClient($header); + } + + protected function getBaseUrl() + { + if (!$url = $this->config->getApiBaseUrl()) { + $url = $this->baseUrl; + } + + return $url . '/'; + } + + public function getEndPoint($endpoint) + { + return $this->getBaseUrl() . $endpoint; + } + + public function getHttpClient() + { + return $this->http; + } + + public function signature() + { + return new Signature($this); + } + + public function pingback() + { + return new Pingback($this); + } + + /** + * Mass Payout Service + * + * @return Payout + */ + public function payoutService() + { + return new Payout(new HttpService($this)); + } + + /** + * Address Service + * + * @return Address + */ + public function addressService() + { + return new Address(new HttpService($this)); + } + + /** + * Contact Service + * + * @return Contact + */ + public function contactService() + { + return new Contact(new HttpService($this)); + } + + /** + * Invoice Service + * + * @return Invoice + */ + public function invoiceService() + { + return new Invoice(new HttpService($this)); + } + + /** + * Invoice Template Service + * + * @return Template + */ + public function invoiceTemplateService() + { + return new Template(new HttpService($this)); + } + + /** + * Invoice Tax Service + * + * @return Tax + */ + public function invoiceTaxService() + { + return new Tax(new HttpService($this)); + } + + /** + * Invoice Discount Service + * + * @return Discount + */ + public function invoiceDiscountService() + { + return new Discount(new HttpService($this)); + } + + /** + * Invoice Product Service + * + * @return Product + */ + public function invoiceProductService() + { + return new Product(new HttpService($this)); + } + + public function getConfig() + { + return $this->config; + } +} \ No newline at end of file diff --git a/lib/FasterPay/Gateway.php b/lib/FasterPay/Gateway.php index 54419fe..4387369 100644 --- a/lib/FasterPay/Gateway.php +++ b/lib/FasterPay/Gateway.php @@ -10,14 +10,12 @@ use FasterPay\Services\Signature; use FasterPay\Services\Pingback; -class Gateway +class Gateway implements GatewayInterface { protected $config; protected $http; protected $baseUrl = ''; - protected $project; - protected $extraParams = []; public function __construct($config = []) { diff --git a/lib/FasterPay/GatewayInterface.php b/lib/FasterPay/GatewayInterface.php new file mode 100644 index 0000000..e52278e --- /dev/null +++ b/lib/FasterPay/GatewayInterface.php @@ -0,0 +1,14 @@ +extractFiles($testParams); + + if (!empty($files)) { + return $this->postMultipart($endpoint, $params, $headers); + } + return $this->call($endpoint, $params, 'POST', $headers); } public function put($endpoint, array $params = [], array $headers = []) { + $testParams = $params; + $files = $this->extractFiles($testParams); + + if (!empty($files)) { + $params['_method'] = 'PUT'; + return $this->postMultipart($endpoint, $params, $headers); + } + return $this->call($endpoint, $params, 'PUT', $headers); } @@ -48,6 +63,112 @@ public function delete($endpoint, array $params = [], array $headers = []) return $this->call($endpoint, $params, 'DELETE', $headers); } + public function postMultipart($endpoint, array $params = [], array $headers = []) + { + $ch = $this->init(); + + $header = array_merge($this->header, $headers); + + $header = array_filter($header, function($h) { + return stripos($h, 'Content-Type:') !== 0; + }); + + curl_setopt($ch, CURLOPT_HTTPHEADER, $header); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_URL, $endpoint); + + $files = $this->extractFiles($params); + $postData = []; + + $this->buildPostData($params, $postData, ''); + + foreach ($files as $fieldName => $fileValue) { + if (is_object($fileValue)) { + if ($fileValue instanceof \SplFileInfo) { + // Convert SplFileInfo to file path for HTTP compatibility + $filePath = $fileValue->getPathname(); + if (class_exists('CURLFile')) { + $postData[$fieldName] = new \CURLFile($filePath); + } else { + $postData[$fieldName] = '@' . $filePath; + } + } else { + // Handle other file objects (CURLFile, etc.) + $postData[$fieldName] = $fileValue; + } + } elseif (is_string($fileValue) && file_exists($fileValue)) { + if (class_exists('CURLFile')) { + $postData[$fieldName] = new \CURLFile($fileValue); + } else { + $postData[$fieldName] = '@' . $fileValue; + } + } + } + + curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); + + $response = curl_exec($ch); + $info = curl_getinfo($ch); + curl_close($ch); + + return [ + 'response' => $response, + 'httpCode' => $info['http_code'] + ]; + } + + private function buildPostData(array $data, array &$postData, $prefix) + { + foreach ($data as $key => $value) { + $currentKey = $prefix ? $prefix . '[' . $key . ']' : $key; + + if (is_array($value)) { + $this->buildPostData($value, $postData, $currentKey); + } else { + $postData[$currentKey] = $value; + } + } + } + + private function extractFiles(array &$params) + { + $files = []; + $this->extractFilesRecursive($params, $files, ''); + return $files; + } + + private function extractFilesRecursive(array &$params, array &$files, $prefix) + { + foreach ($params as $key => $value) { + $currentKey = $prefix ? $prefix . '[' . $key . ']' : $key; + + if (is_array($value)) { + $this->extractFilesRecursive($params[$key], $files, $currentKey); + } elseif ($this->isFileObject($value)) { + $files[$currentKey] = $value; + unset($params[$key]); + } + } + } + + private function isFileObject($value) + { + if (is_object($value)) { + return $value instanceof \CURLFile || + (class_exists('SplFileInfo') && $value instanceof \SplFileInfo) || + (class_exists('finfo') && method_exists($value, 'getPathname')) || + method_exists($value, '__toString'); + } + + if (is_string($value) && !empty($value)) { + if (filter_var($value, FILTER_VALIDATE_URL)) { + return false; + } + return file_exists($value); + } + + return false; + } private function call($endpoint, array $params = [], $method, array $headers = []) { @@ -58,20 +179,23 @@ private function call($endpoint, array $params = [], $method, array $headers = [ switch (strtoupper($method)) { case 'POST': + curl_setopt($ch, CURLOPT_POST, true); if (!empty($params)) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); } + break; case 'PUT': case 'DELETE': curl_setopt($ch, CURLOPT_HTTPGET, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); + if (!empty($params)) { + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); + } break; case 'GET': if (!empty($params)) { $endpoint .= '?' . http_build_query($params); } - curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_HTTPGET, true); break; @@ -79,17 +203,13 @@ private function call($endpoint, array $params = [], $method, array $headers = [ curl_setopt($ch, CURLOPT_URL, $endpoint); - $response = curl_exec($ch); - - $info = curl_getinfo($ch); curl_close($ch); - return array( + return [ 'response' => $response, 'httpCode' => $info['http_code'] - ); + ]; } - -} +} \ No newline at end of file diff --git a/lib/FasterPay/Response/JsonResponse.php b/lib/FasterPay/Response/JsonResponse.php new file mode 100644 index 0000000..9b9a258 --- /dev/null +++ b/lib/FasterPay/Response/JsonResponse.php @@ -0,0 +1,48 @@ +getDecodeResponse(); + $httpCode = $this->getHttpCode(); + + // First check HTTP status + if ($httpCode != self::SUCCESS_CODE) { + $errorMessage = empty($response['message']) ? self::RESPONSE_ERROR_TEXT : $response['message']; + $this->errors = new ResponseError([ + 'message' => $errorMessage, + 'code' => $httpCode + ]); + return; + } + + // Then check if response was successfully decoded + if ($response === null) { + $this->errors = new ResponseError([ + 'message' => 'Failed to decode JSON response', + 'code' => self::DEFAULT_ERROR_CODE + ]); + return; + } + + // Check for success flag in response body + if (!empty($response['success'])) { + $this->success = true; + } else { + // Response decoded but success is empty/false/missing + $errorMessage = isset($response['message']) ? $response['message'] : 'Request was not successful'; + $this->errors = new ResponseError([ + 'message' => $errorMessage, + 'code' => isset($response['code']) ? $response['code'] : self::DEFAULT_ERROR_CODE + ]); + } + } + + public function isSuccessful() + { + return $this->success === true && empty($this->errors); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Response/PaymentResponse.php b/lib/FasterPay/Response/PaymentResponse.php index 9f97dbd..90d2030 100644 --- a/lib/FasterPay/Response/PaymentResponse.php +++ b/lib/FasterPay/Response/PaymentResponse.php @@ -13,7 +13,10 @@ public function handleResult() if ($httpCode != self::SUCCESS_CODE) { $errorMessage = empty($response['message']) ? self::RESPONSE_ERROR_TEXT : $response['message']; - $this->errors = new ResponseError(array('message' => $errorMessage, 'code' => $httpCode)); + $this->errors = new ResponseError([ + 'message' => $errorMessage, + 'code' => $httpCode, + ]); } else { $this->success = true; } diff --git a/lib/FasterPay/Response/SubscriptionResponse.php b/lib/FasterPay/Response/SubscriptionResponse.php index 6d9064b..c67aa31 100644 --- a/lib/FasterPay/Response/SubscriptionResponse.php +++ b/lib/FasterPay/Response/SubscriptionResponse.php @@ -14,7 +14,10 @@ public function handleResult() } elseif (!empty($response['type']) && $response['type'] == self::RESPONSE_ERROR_TEXT) { $errorMessage = empty($response['message']) ? 'Error' : $response['message']; $code = empty($response['code']) ? self::DEFAULT_ERROR_CODE : $response['code']; - $this->errors = new ResponseError(array('message' => $errorMessage, 'code' => $code)); + $this->errors = new ResponseError([ + 'message' => $errorMessage, + 'code' => $code, + ]); } } diff --git a/lib/FasterPay/Services/Business/Address.php b/lib/FasterPay/Services/Business/Address.php new file mode 100644 index 0000000..138870a --- /dev/null +++ b/lib/FasterPay/Services/Business/Address.php @@ -0,0 +1,36 @@ +httpService->getEndPoint($this->endpoint . '/fields/' . strtoupper($countryCode)); + + $response = $this->httpService->getHttpClient()->get($endpoint); + + return new GeneralResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/Business/Contact.php b/lib/FasterPay/Services/Business/Contact.php new file mode 100644 index 0000000..3365cb7 --- /dev/null +++ b/lib/FasterPay/Services/Business/Contact.php @@ -0,0 +1,56 @@ +httpService->getEndPoint($this->endpoint); + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + return new GeneralResponse($response); + } + + public function getContact($contactId = '') + { + if (empty($contactId)) { + throw new Exception('Contact ID is required'); + } + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $contactId); + $response = $this->httpService->getHttpClient()->get($endpoint); + return new GeneralResponse($response); + } + + public function updateContact($contactId = '', array $params = []) + { + if (empty($contactId)) { + throw new Exception('Contact ID is required'); + } + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $contactId); + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + return new GeneralResponse($response); + } + + public function deleteContact($contactId = '') + { + if (empty($contactId)) { + throw new Exception('Contact ID is required'); + } + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $contactId); + $response = $this->httpService->getHttpClient()->delete($endpoint); + return new GeneralResponse($response); + } + + public function listContacts(array $filters = []) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + return new GeneralResponse($response); + } +} diff --git a/lib/FasterPay/Services/Business/EInvoice/Discount.php b/lib/FasterPay/Services/Business/EInvoice/Discount.php new file mode 100644 index 0000000..b9875d4 --- /dev/null +++ b/lib/FasterPay/Services/Business/EInvoice/Discount.php @@ -0,0 +1,103 @@ +httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + + return new GeneralResponse($response); + } + + /** + * Get discount details + * + * @param string $discountId Discount ID + * @return GeneralResponse + * @throws Exception + */ + public function getDiscount($discountId = '') + { + if (empty($discountId)) { + throw new Exception('Discount ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $discountId); + + $response = $this->httpService->getHttpClient()->get($endpoint); + + return new GeneralResponse($response); + } + + /** + * Update discount + * + * @param string $discountId Discount ID + * @param array $params Updated discount parameters + * @return GeneralResponse + * @throws Exception + */ + public function updateDiscount($discountId = '', array $params = array()) + { + if (empty($discountId)) { + throw new Exception('Discount ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $discountId); + + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + + return new GeneralResponse($response); + } + + /** + * Delete discount + * + * @param string $discountId Discount ID + * @return GeneralResponse + * @throws Exception + */ + public function deleteDiscount($discountId = '') + { + if (empty($discountId)) { + throw new Exception('Discount ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $discountId); + + $response = $this->httpService->getHttpClient()->delete($endpoint); + + return new GeneralResponse($response); + } + + /** + * List discounts + * + * @param array $filters Optional filters + * @return GeneralResponse + */ + public function listDiscounts(array $filters = array()) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + + return new GeneralResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/Business/EInvoice/Invoice.php b/lib/FasterPay/Services/Business/EInvoice/Invoice.php new file mode 100644 index 0000000..50dd3b5 --- /dev/null +++ b/lib/FasterPay/Services/Business/EInvoice/Invoice.php @@ -0,0 +1,175 @@ +httpService->getEndPoint($this->endpoint); + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * Get e-invoice details + * + * @param string $invoiceId Invoice ID + * @param array $params Optional query parameters (include, etc.) + * @return GeneralResponse + * @throws Exception + */ + public function getInvoice($invoiceId = '', array $params = []) + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId); + $response = $this->httpService->getHttpClient()->get($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * List invoices with optional filters + * + * @param array $filters Optional filters + * @return GeneralResponse + */ + public function listInvoices(array $filters = []) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + return new GeneralResponse($response); + } + + /** + * Update e-invoice + * + * @param string $invoiceId Invoice ID + * @param array $params Updated invoice parameters + * @return GeneralResponse + * @throws Exception + */ + public function updateInvoice($invoiceId = '', array $params = []) + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId); + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * Update invoice status + * + * @param string $invoiceId Invoice ID + * @param array $params Status parameters + * @return GeneralResponse + * @throws Exception + */ + public function updateInvoiceStatus($invoiceId = '', array $params = []) + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId . '/status'); + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * Preview invoice (get HTML) + * + * @param string $invoiceId Invoice ID + * @return GeneralResponse + * @throws Exception + */ + public function previewInvoice($invoiceId = '') + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId . '/preview'); + $response = $this->httpService->getHttpClient()->get($endpoint); + return new GeneralResponse($response); + } + + /** + * Download invoice PDF + * + * @param string $invoiceId Invoice ID + * @return GeneralResponse + * @throws Exception + */ + public function downloadInvoicePdf($invoiceId = '') + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId . '/pdf'); + $response = $this->httpService->getHttpClient()->get($endpoint); + return new GeneralResponse($response); + } + + /** + * Send invoice to customer + * + * @param string $invoiceId Invoice ID + * @param array $params Send parameters (test, etc.) + * @return GeneralResponse + * @throws Exception + */ + public function sendInvoice($invoiceId = '', array $params = []) + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId . '/send'); + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * Delete e-invoice + * + * @param string $invoiceId Invoice ID + * @return GeneralResponse + * @throws Exception + */ + public function deleteInvoice($invoiceId = '') + { + if (empty($invoiceId)) { + throw new Exception('Invoice ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $invoiceId); + $response = $this->httpService->getHttpClient()->delete($endpoint); + return new GeneralResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/Business/EInvoice/Product.php b/lib/FasterPay/Services/Business/EInvoice/Product.php new file mode 100644 index 0000000..73192ba --- /dev/null +++ b/lib/FasterPay/Services/Business/EInvoice/Product.php @@ -0,0 +1,130 @@ +httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + + return new GeneralResponse($response); + } + + /** + * Get product details + * + * @param string $productId Product ID + * @return GeneralResponse + * @throws Exception + */ + public function getProduct($productId = '') + { + if (empty($productId)) { + throw new Exception('Product ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $productId); + + $response = $this->httpService->getHttpClient()->get($endpoint); + + return new GeneralResponse($response); + } + + /** + * Update product + * Supports multipart/form-data for image uploads + * + * @param string $productId Product ID + * @param array $params Updated product parameters + * @return GeneralResponse + * @throws Exception + */ + public function updateProduct($productId = '', array $params = []) + { + if (empty($productId)) { + throw new Exception('Product ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $productId); + + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + + return new GeneralResponse($response); + } + + /** + * Delete product + * + * @param string $productId Product ID + * @return GeneralResponse + * @throws Exception + */ + public function deleteProduct($productId = '') + { + if (empty($productId)) { + throw new Exception('Product ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $productId); + + $response = $this->httpService->getHttpClient()->delete($endpoint); + + return new GeneralResponse($response); + } + + /** + * List products with optional filters + * + * @param array $filters Optional filters + * @return GeneralResponse + */ + public function listProducts(array $filters = []) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + + return new GeneralResponse($response); + } + + /** + * Delete product price by currency + * + * @param string $productId Product ID + * @param string $currency Currency code (ISO-4217 format) + * @return GeneralResponse + * @throws Exception + */ + public function deleteProductPrice($productId = '', $currency = '') + { + if (empty($productId)) { + throw new Exception('Product ID is required'); + } + + if (empty($currency)) { + throw new Exception('Currency is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $productId . '/prices/' . $currency); + + $response = $this->httpService->getHttpClient()->delete($endpoint); + + return new GeneralResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/Business/EInvoice/Tax.php b/lib/FasterPay/Services/Business/EInvoice/Tax.php new file mode 100644 index 0000000..5326cb1 --- /dev/null +++ b/lib/FasterPay/Services/Business/EInvoice/Tax.php @@ -0,0 +1,103 @@ +httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + + return new GeneralResponse($response); + } + + /** + * Get tax details + * + * @param string $taxId Tax ID + * @return GeneralResponse + * @throws Exception + */ + public function getTax($taxId = '') + { + if (empty($taxId)) { + throw new Exception('Tax ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $taxId); + + $response = $this->httpService->getHttpClient()->get($endpoint); + + return new GeneralResponse($response); + } + + /** + * Update tax + * + * @param string $taxId Tax ID + * @param array $params Updated tax parameters + * @return GeneralResponse + * @throws Exception + */ + public function updateTax($taxId = '', array $params = []) + { + if (empty($taxId)) { + throw new Exception('Tax ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $taxId); + + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + + return new GeneralResponse($response); + } + + /** + * Delete tax + * + * @param string $taxId Tax ID + * @return GeneralResponse + * @throws Exception + */ + public function deleteTax($taxId = '') + { + if (empty($taxId)) { + throw new Exception('Tax ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $taxId); + + $response = $this->httpService->getHttpClient()->delete($endpoint); + + return new GeneralResponse($response); + } + + /** + * List taxes + * + * @param array $filters Optional filters + * @return GeneralResponse + */ + public function listTaxes(array $filters = []) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + + return new GeneralResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/Business/EInvoice/Template.php b/lib/FasterPay/Services/Business/EInvoice/Template.php new file mode 100644 index 0000000..7c1043a --- /dev/null +++ b/lib/FasterPay/Services/Business/EInvoice/Template.php @@ -0,0 +1,98 @@ +httpService->getEndPoint($this->endpoint); + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * Get template details + * + * @param string $templateId Template ID + * @return GeneralResponse + * @throws Exception + */ + public function getTemplate($templateId = '') + { + if (empty($templateId)) { + throw new Exception('Template ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $templateId); + + $response = $this->httpService->getHttpClient()->get($endpoint); + + return new GeneralResponse($response); + } + + /** + * Update template + * + * @param string $templateId Template ID + * @param array $params Updated template parameters (files auto-detected) + * @return GeneralResponse + * @throws Exception + */ + public function updateTemplate($templateId = '', array $params = []) + { + if (empty($templateId)) { + throw new Exception('Template ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $templateId); + $response = $this->httpService->getHttpClient()->put($endpoint, $params); + return new GeneralResponse($response); + } + + /** + * Delete template + * + * @param string $templateId Template ID + * @return GeneralResponse + * @throws Exception + */ + public function deleteTemplate($templateId = '') + { + if (empty($templateId)) { + throw new Exception('Template ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $templateId); + + $response = $this->httpService->getHttpClient()->delete($endpoint); + + return new GeneralResponse($response); + } + + /** + * List templates + * + * @param array $filters Optional filters + * @return GeneralResponse + */ + public function listTemplates(array $filters = []) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + return new GeneralResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/Business/Payout.php b/lib/FasterPay/Services/Business/Payout.php new file mode 100644 index 0000000..ddd8274 --- /dev/null +++ b/lib/FasterPay/Services/Business/Payout.php @@ -0,0 +1,62 @@ +httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->post($endpoint, $params); + + return new JsonResponse($response); + } + + /** + * Get payout details + * + * @param string $payoutId Payout ID + * @return JsonResponse + * @throws Exception + */ + public function getPayoutDetails($payoutId = '') + { + if (empty($payoutId)) { + throw new Exception('Payout ID is required'); + } + + $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $payoutId); + + $response = $this->httpService->getHttpClient()->get($endpoint); + + return new JsonResponse($response); + } + + /** + * Get payout list + * + * @param array $filters Optional filters + * @return JsonResponse + */ + public function getPayoutList(array $filters = []) + { + $endpoint = $this->httpService->getEndPoint($this->endpoint); + + $response = $this->httpService->getHttpClient()->get($endpoint, $filters); + + return new JsonResponse($response); + } +} \ No newline at end of file diff --git a/lib/FasterPay/Services/HttpService.php b/lib/FasterPay/Services/HttpService.php index 17dcece..65288df 100644 --- a/lib/FasterPay/Services/HttpService.php +++ b/lib/FasterPay/Services/HttpService.php @@ -2,15 +2,16 @@ namespace FasterPay\Services; -use FasterPay\Gateway; +use FasterPay\GatewayInterface; class HttpService implements HttpServiceInterface { + /** + * @var GatewayInterface + */ protected $client; - protected $endpoint; - - public function __construct(Gateway $client) + public function __construct(GatewayInterface $client) { $this->client = $client; } @@ -28,7 +29,7 @@ public function getEndPoint($endpoint = '') public function __call($function, $params) { if (method_exists($this, $function)) { - return call_user_func_array(array($this, $function), $params); + return call_user_func_array([$this, $function], $params); } } } diff --git a/lib/FasterPay/Services/Payment.php b/lib/FasterPay/Services/Payment.php index 0f0ef4a..0f2965b 100644 --- a/lib/FasterPay/Services/Payment.php +++ b/lib/FasterPay/Services/Payment.php @@ -19,7 +19,7 @@ public function refund($orderId = 0, $amount = 0) } $endpoint = $this->httpService->getEndPoint($this->endpoint . '/' . $orderId . '/refund'); - $params = array('amount' => $amount); + $params = ['amount' => $amount]; $response = $this->httpService->getHttpClient()->post($endpoint, $params); diff --git a/lib/FasterPay/Services/Pingback.php b/lib/FasterPay/Services/Pingback.php index 34299f5..266d5da 100644 --- a/lib/FasterPay/Services/Pingback.php +++ b/lib/FasterPay/Services/Pingback.php @@ -1,7 +1,7 @@ gateway = $gateway; } diff --git a/lib/FasterPay/Services/Signature.php b/lib/FasterPay/Services/Signature.php index 92eb11d..92732b8 100644 --- a/lib/FasterPay/Services/Signature.php +++ b/lib/FasterPay/Services/Signature.php @@ -1,7 +1,7 @@ gateway = $gateway; } diff --git a/lib/autoload.php b/lib/autoload.php index b9908a1..d7ea526 100644 --- a/lib/autoload.php +++ b/lib/autoload.php @@ -7,6 +7,23 @@ throw new Exception('JSON PHP extension is required'); } +if (!function_exists('hash_equals')) { + function hash_equals($a, $b) { + echo 'strlen($a): ' . strlen($a); + echo 'strlen($b): ' . strlen($b); + if (strlen($a) !== strlen($b)) { + return false; + } + + $result = 0; + for ($i = 0; $i < strlen($a); $i++) { + $result |= ord($a[$i]) ^ ord($b[$i]); + } + echo $result === 0 ? 'YYEESSS' : 'NONONON'; + return $result === 0; + } +} + function autoload($className) { if (strpos($className, 'FasterPay') !== 0) {