Baremetrics-api is a simple package which helps to integrate baremetrics into your app.
Add package to composer.json
composer require oseintow/baremetrics-api
NB: Create account and Obtain your api_key from baremetrics
Add the service provider to config/app.php in the providers array.
<?php
'providers' => [
...
Oseintow\Baremetrics\BaremetricsServiceProvider::class,
],
Setup alias for the Facade
<?php
'aliases' => [
...
'Baremetrics' => Oseintow\Baremetrics\Facades\Baremetrics::class,
],
This process will enable us to obtain the baremetrics sources
use Oseintow\Baremetrics\Facades\Baremetrics;
Route::get("baremetric_sources",function()
{
// If you have "BAREMETRICS_API_KEY" set in your env file then the will be no need to set `setApiKey`
$apiKey = "xx-xx-x-xx-xx-xx"
// Use isLiveMode to set your mode to production / sandbox
// True for production and false for sandbox
// It is false by default
$response = Baremetrics::setApiKey($apikey)->isLiveMode(true)->get("sources");
$sourceId = "";
foreach($response['sources] as $sourece){
if($source['provider'] == "baremetrics"){
$sourceId = $source['id'];
}
}
});
To access API resource use
Baremetrics::get("resource uri", ["query string params"]);
Baremetrics::post("resource uri", ["post body"]);
Baremetrics::put("resource uri", ["put body"]);
Baremetrics::delete("resource uri");
Let use our api key to get plans from baremetrics.
NB: You can use this to access any resource on baremetrics (be it plans, subscription, customers, etc)
$sourceId = "1233243";
$products = Baremetrics::setApiKey("xx-xxx-xx-xx-xx")->get("{$sourceId}/plans");
To pass query params
// returns Collection
$sourceId = "1233243";
$baremetrics = Baremetrics::setApiKey("xx-xxx-xx-xx-xx");
$plans = $baremetrics->get(""{$sourceId}/plans", ["search"=> "xxx-xxxx-xxx"]);
If you prefer to use dependency injection over facades like me, then you can inject the Class:
use Illuminate\Http\Request;
use Oseintow\Baremetrics\Baremetrics;
class Foo
{
protected $baremetrics;
public function __construct(Baremetrics $baremetrics)
{
$this->baremetrics = $baremetrics;
}
/*
* returns Collection
*/
public function getPlans(Request $request)
{
$sourceId = "xxxx-xx-xx";
$plans = $this->baremetrics->setApiKey("xx-xxx-xx-xx-xx")
->get('{$sourceId}/plans');
}
}
To get Response headers
Baremetrics::getHeaders();
To get specific header
Baremetrics::getHeader("Content-Type");
Check if header exist
if(Baremetrics::hasHeader("Content-Type")){
echo "Yes header exist";
}
To get response status code or status message
Baremetrics::getStatusCode(); // 200
Baremetrics::getReasonPhrase(); // ok
use Oseintow\Baremetrics\Baremetrics;
$baremetrics = new Baremetrics();
$sources = $baremetrics->isLiveMode(true)->setApiKey("xxx-xxxx-xxxx-xxx-xx")->get("sources");
$sourceId= "123456";
$plans = $baremetrics->get("{$sourceid}/plans");