### Client Initialization Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Demonstrates the updated method for initializing the Chargebee client, including namespace changes and configuration parameters. ```php use Chargebee\ChargeBee\Models\Environment; Environment::configure("{site}", "{site_api_key}"); ``` ```php use Chargebee\ChargebeeClient; $chargebee = new ChargebeeClient([ "site" => "{site}", "apiKey" => "{site_api_key}", ]); ``` -------------------------------- ### Install Chargebee PHP Client Source: https://github.com/chargebee/chargebee-php/blob/master/README.md Installs the Chargebee PHP client library using Composer and includes the autoloader for use in your project. ```php composer require chargebee/chargebee-php require_once('vendor/autoload.php'); ``` -------------------------------- ### Accessing Response Properties Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Demonstrates the change in accessing properties within response objects, moving from method calls to direct property access. ```php $result = Customer::retrieve("{customer-id}"); $customer = $result->customer(); $firstName = $customer->firstName(); ``` ```php $result = $chargebee->customer()->retrieve("{customer-id}"); $customer = $result->customer; $firstName = $customer->first_name; ``` -------------------------------- ### Input Parameter Naming Convention Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Shows the updated input parameter naming convention, which has shifted from `camelCase` to `snake_case` for method arguments. ```php $result = Subscription::createWithItems("{customer-id}", array( "subscriptionItems" => array( array( "itemPriceId" => "day-pass-USD", "unitPrice" => 100 ), array( "itemPriceId" => "basic-USD", "billingCycles" => 2, "quantity" => 1 ) ) )); ``` ```php $result = $chargebee->subscription()->createWithItems("{customer-id}", [ "subscription_items" => [ [ "item_price_id" => "day-pass-USD", "unit_price" => 100 ], [ "item_price_id" => "basic-USD", "billing_cycles" => 2, "quantity" => 1 ] ] ]); ``` -------------------------------- ### Method Binding and Operations Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Illustrates the new approach to calling Chargebee API operations directly through the client instance, eliminating the need for importing individual model classes. ```php use ChargeBee\ChargeBee\Environment; use ChargeBee\ChargeBee\Models\Customer; Environment::configure("{site}", "{site_api_key}"); $result = Customer::retrieve("{customer-id}"); ``` ```php use Chargebee\ChargebeeClient; $chargebee = new ChargebeeClient([ "site" => "{site}", "apiKey" => "{site_api_key}", ]); $response = $chargebee->customer()->retrieve("{customer-id}"); ``` -------------------------------- ### Response Object Structure Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Compares the response object structure before and after the update, highlighting the change from a generic `Result` class to specific response classes for each action. ```php $customerResponse = Customer::retrieve("{customer-id}"); $subscriptionResponse = Subscription::retrieve("{customer-id}"); echo get_class($customerResponse) . "\n"; echo get_class($subscriptionResponse) . "\n"; ``` ```console ChargeBee\ChargeBee\Result ChargeBee\ChargeBee\Result ``` ```php $customerResponse = $chargebee->customer()->retrieve("{customer-id}"); $subscriptionResponse = $chargebee->subscription()->retrieve("{subs-id}"); echo get_class($customerResponse) . "\n"; echo get_class($subscriptionResponse) . "\n"; ``` ```console Chargebee\Responses\CustomerResponse\RetrieveCustomerResponse Chargebee\Responses\SubscriptionResponse\RetrieveSubscriptionResponse ``` -------------------------------- ### List API Filters - ID Filter Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Demonstrates the updated syntax for passing filters in list APIs, specifically for the 'id' parameter with an 'is' condition. ```php $result = Subscription::all([ "id[is]" => "{subscription-id}" ]); ``` ```php $result = $chargebee->subscription()->all([ "id" => ["is" => "{subscription-id}"] ]); ``` -------------------------------- ### List API Filters - Status Filter Source: https://github.com/chargebee/chargebee-php/wiki/Migration-guide-for-v4 Illustrates the updated syntax for passing filters in list APIs, specifically for the 'status' parameter with an 'in' condition. ```php $result = Subscription::all([ "status[in]" => ["cancelled"], "limit" => 2, ]); ``` ```php $result = $chargebeeClient->subscription()->all([ "status" => ["in" => ["cancelled"]], "limit" => 2, ]); ``` -------------------------------- ### Create a New Subscription Source: https://github.com/chargebee/chargebee-php/blob/master/README.md Demonstrates how to create a new subscription with specified items using the Chargebee PHP client. It initializes the client with site and API key, then calls the create method on the subscription object. ```php use Chargebee\ChargebeeClient; $chargebee = new ChargebeeClient(options: [ "site" => "{your_site}", "apiKey" => "{your_apiKey}", ]); $result = $chargebee->subscription()->createWithItems("customer_id", [ "subscription_items" => [ [ "item_price_id" => "Montly-Item", "quantity" => "3", ] ] ]); $subscription = $result->subscription; $customer = $result->customer; ``` -------------------------------- ### Create an Idempotent Customer Request Source: https://github.com/chargebee/chargebee-php/blob/master/README.md Shows how to create a customer with an idempotency key to ensure safe retries of POST requests. It includes initializing the client, making the customer creation call, and retrieving response headers and idempotency status. ```php use Chargebee\ChargebeeClient; $chargebee = new ChargebeeClient(options: [ "site" => "{your_site}", "apiKey" => "{your_apiKey}", ]); $responseCustomer = $chargebee->customer()->create([ "first_name" => "John", "last_name" => "Doe", "email" => "john@test.com", "card" => [ "first_name" => "Joe", "last_name" => "Doe", "number" => "4012888888881881", "expiry_month" => "10", "expiry_year" => "29", "cvv" => "231" ] ], [ "chargebee-idempotency-key" => "<>" // Replace <> with a unique string ] ); $responseHeaders = $responseCustomer->getResponseHeaders(); // Retrieves response headers print_r($responseHeaders); $idempotencyReplayedValue = $responseCustomer->isIdempotencyReplayed(); // Retrieves Idempotency replayed header value print_r($idempotencyReplayedValue); ``` -------------------------------- ### Configure Rate Limit Retry Logic Source: https://github.com/chargebee/chargebee-php/blob/master/README.md Demonstrates how to initialize the Chargebee client with custom retry configurations for rate-limited requests. This includes enabling the retry mechanism, setting the maximum number of retries, the delay between retries, and specifying which HTTP status codes should trigger a retry. ```php use Chargebee\ChargebeeClient; // Retry Configurations $retryConfig = new RetryConfig(); $retryConfig->setEnabled(true); // Enable retry mechanism $retryConfig->setMaxRetries(5); // Maximum number of reties $retryConfig->setDelayMs(1000); // Delay in milliseconds before retrying $retryConfig->setRetryOn([429]); // Retry on 429 HTTP status codes $chargebee = new ChargebeeClient(options: [ "site" => "{your_site}", "apiKey" => "{your_apiKey}", "retryConfig" => $retryConfig, ]); // ... your Chargebee API operations below ... ``` -------------------------------- ### Configure Chargebee SDK Retry Logic Source: https://github.com/chargebee/chargebee-php/blob/master/README.md Demonstrates how to enable and customize the built-in retry logic for the Chargebee PHP client. This includes setting the maximum number of retries, delay between retries, and specific HTTP status codes to retry on. ```php use Chargebee\ChargebeeClient; use Chargebee\Models\RetryConfig; // Retry Configurations $retryConfig = new RetryConfig(); $retryConfig->setEnabled(true); // Enable retry mechanism $retryConfig->setMaxRetries(5); // Maximum number of retries $retryConfig->setDelayMs(1000); // Delay in milliseconds before retrying $retryConfig->setRetryOn([500, 502, 503, 504]); // Retry on these HTTP status codes $chargebee = new ChargebeeClient(options: [ "site" => "{your_site}", "apiKey" => "{your_apiKey}", "retryConfig" => $retryConfig, ]); // ... your Chargebee API operations below ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.