rathesDot / chargebee-php

ChargeBee API client implementation for PHP
https://apidocs.chargebee.com/docs/api?lang=php
MIT License
0 stars 0 forks source link

The SDK #6

Closed rathesDot closed 5 years ago

rathesDot commented 5 years ago

In addition to the basic client, this repository also provides a version with a set of all models that are accessible via the Chargebee API. So instead of just calling the API endpoints and working with arrays, you can also choose to work with a set of helper methods to build you requests.

For example, if you want to call the /subscriptions endpoint you can either do it via the client, or you can use the Request Builder that we call the SDK.

<?php

$client = new \Chargebee\Chargebee($site, $apiKey);

$sdk = new \Chargebee\SDK($client);

You can also use the factory method to let the SDK create the client.

<?php

$sdk = \Chargebee\SDK::create($site, $apiKey);

Using that SDK you now have access to a subscription property that you can use to call endpoints of the entity, in our example listing all subscriptions:

<?php

$sdk->subscriptions
    ->list();

You can even add constraints to the request by adding them to the chain

<?php

$response = $sdk->subscriptions
    ->limit(5)
    ->where('plan_id', 'is_not', 'basic')
    ->where('status', 'is', 'active')
    ->list();

The response will now be an array, the same as you would call the get() method on the client.

But you can also make use of our predefined models. Therefore, enable the flag of the sdk to hydrate models.

<?php

$sdk = \Chargebee\SDK::create($site, $apiKey);
$sdk->hydrateModels(true);

By enabling this flag, your response will no longer be a plain array, but an array of \Chargebee\Models\Subscription objects.


Internals

Internally, the \Chargebee\SDK will do nothing more than implement the magic getter (__get) by doing a lookup in the requests/ directory, checking if a model with that key exists.

It will then create instantiate an object and pass the client to it.

So basically, you could also skip the SDK and instantiate the Request Models them manually to run the queries for that entity.

The Request model also all have a hydrate() method implemented and a method that returns the response model so that you can return hydrated responses.

rathesDot commented 5 years ago

Hydration is handled in #11, the rest is implemented in #7