globalis/chargebee-php-sdk

Chargebee API PHP Client (for API version 2 and Product Catalog version 2.0)

1.0.2 2022-02-18 10:05 UTC

This package is auto-updated.

Last update: 2024-12-18 16:37:35 UTC


README

Packagist Latest Stable Version License

Overview

This package provides an API client for Chargebee subscription management services.

It connects to Chargebee REST APIs for the following versions:

If your Chargebee site is using Product Catalog version 1.0, you can use our product_catalog_v1 branch. It works mostly the same, but be aware that it is neither maintained nor documented.

Installation

composer require globalis/chargebee-php-sdk php-http/guzzle7-adapter

Basic usage

<?php

use Globalis\Chargebee\Client as ChargebeeClient;
use Http\Client\Exception\HttpException;

$chargebee = new ChargebeeClient('{site}', '{site_api_key}');

try {
    // List last created subscription:
    $response = $chargebee->subscription()->list([
        "limit" => 1,
        "sort_by[desc]" => "created_at",
        "status[is]" => "active",
    ]);
} catch (HttpException $e) {
    // Get API error details:
    $response = json_decode($e->getResponse()->getBody(), true);
    echo sprintf("Error: (%s) %s", $response["api_error_code"], $response["message"]);
}

Events

The API client produces events on API responses. You can listen to those events and connect any callable on them.

The events implement Psr\EventDispatcher\StoppableEventInterface from league/event PSR-14 package.

<?php

use Globalis\Chargebee\Client as Chargebee;
use Globalis\Chargebee\Events\EventChargebeeApiResponseSuccess as EventResponseSuccess;
use Globalis\Chargebee\Events\EventChargebeeApiResponseError as EventResponseError;

Chargebee::onApiResponseSuccess(function (EventResponseSuccess $event) {
    // $event contains data about the API request and response
    // do something
});

Chargebee::onApiResponseError(function (EventResponseError $event) {
    // $event contains data about the API request and response
    // do something
});

Integrations

History

This project is a fork of nathandunn/chargebee-php-sdk package. We forked it so we could implement Product Catalog 2.0 changes. We also added PSR-14 events, and fixed a few bugs.

Documentation

We don't have a full documentation yet.

You will find more details on the usage of HTTPlug and the HttpClient\Builder on the original README.md and wiki.