Skip to content

Rebilly/rebilly-php

main
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 

Rebilly SDK for PHP

Latest Version on Packagist Software License GitHub Actions status Total Downloads Coverage Status

The Rebilly SDK for PHP makes it easy for developers to access Rebilly REST APIs in their PHP code. You can get started in minutes by installing the SDK through Composer or by downloading a single zip file from our latest release.

Installation

Using Composer is the recommended way to install the Rebilly SDK for PHP. To get started, you need run the Composer commands (assume you're in the project's root directory).

  • Install the latest stable version of the SDK:
composer require rebilly/client-php

You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at getcomposer.org.

Requirements

  • PHP 7.1+.
  • CURL (verify peer requires a root certificate authority -- if you have not configured php curl to use one, and your system libs aren't linked to one, you may need to do a manual configuration to use the appropriate certificate authority)
  • PHPUnit (tests only)

Quick Examples

Create a Rebilly Client

use Rebilly\Client;

// Instantiate Rebilly client.
$client = new Client([
  'apiKey' => ApiKeyProvider::env(),
  'baseUrl' => Client::SANDBOX_HOST,
]);

Create a Customer

$form = new Customer();
$form->setFirstName('Sarah');
$form->setLastName('Connor');

try {
  $customer = $client->customers()->create($form);
} catch (DataValidationException $e) {
  var_dump($e->getErrors());
}

Mock Rebilly Client for Test purposes

If you use Guzzle jump to the 2nd example, for regular usage the example with custom handler is below.

Custom handler

use Rebilly\Http\HttpHandler;

MockHandler implements HttpHandler {

    private $responses;

    public function append($response)
    {
        $this->responses[] = $response;
    }

    public function __invoke(Request $request)
    {
        return array_shift($this->responses);
    }
};

$httpHandler = new MockHandler();

new RebillyClient(['httpHandler' => $httpHandler]);

// Here is mocked part
$httpHandler->append($response);

Guzzle handler

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Rebilly\Client as RebillyClient;
use Rebilly\Http\GuzzleAdapter;
 
$mockHandler = new MockHandler();
$guzzleAdapter = new GuzzleAdapter(
    new Client(
        [
            'handler' => $mockHandler,
        ]
    )
);

new RebillyClient([
    'httpHandler' => $guzzleAdapter
]);

// Here is mocked part
$response = new Response(204);
$mockHandler->append($response);

If RebillyClient does several sequenced requests in the code you need to cover, responses have to be appended in the same order as RebillyClient is called:

// Send several sequenced requests
...
$transaction = $rebillyClient->transactions()->load($id);
$refundedTransaction = $rebillyClient->transactions()->refund($id, $amount);
$transactionWithCustomer = $rebillyClient->transactions()->load($id, ['expand' => 'customer']);
...

Tests become:

$response = new Response(200, [], json_encode($transaction));
$responseWithCustomer = new Response(200, [], json_encode($transactionWithCustomer));

/** 
 * hint: if you receive an error message 'Cannot create resource by URI "..."' during the tests
 * most probably the created Response misses appropriate "Location" header.
 *
 * Below is an example of mocked POST response `/transactions/{transactionId}/refund` which is not listed in `Rebilly\Entities\Schema`
 * because it can be simply mapped by "Location" header from the response.
 * The "Location" header shows Client which Schema to use to create an object out of the response content.
 */
  
$responseRefund = new GuzzleResponse(201, ['Location' => 'transactions/{transactionId}'], json_encode($transaction));

// Now append them in the same order as it is called in the code
$mockHandler->append($response);
$mockHandler->append($responseRefund);
$mockHandler->append($responseWithCustomer);

Documentation

Read Rebilly REST APIs documentation for more details.

Tests

# install required files
$ composer self-update
$ composer install

# run the test
phpunit