Skip to content

Instantly share code, notes, and snippets.

@mathieutu
Last active October 17, 2017 12:58
Show Gist options
  • Save mathieutu/2a2a10204396485c90e5d72e64c96500 to your computer and use it in GitHub Desktop.
Save mathieutu/2a2a10204396485c90e5d72e64c96500 to your computer and use it in GitHub Desktop.
Stripe Wrapper
<?php
namespace App\Services\Stripe;
class HighOrderProxy
{
private $class;
public function __construct($stripeObject)
{
$this->class = get_class($stripeObject);
}
public function __call($method, $parameters)
{
return $this->class::{$method}(...$parameters);
}
}
<?php
// Laravel Service Provider
namespace App\Providers;
use App\Services\Stripe\Wrapper;
use Illuminate\Support\ServiceProvider;
class StripeServiceProvider extends ServiceProvider
{
protected $defer = true;
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('stripe', function () {
return new Wrapper($this->app['config']['services.stripe.secret']);
});
}
public function provides()
{
return ['stripe'];
}
}
<?php
namespace App\Services\Stripe;
use Stripe\Stripe;
use Stripe\Util\Util;
class Wrapper
{
public function __construct($secret)
{
Stripe::setApiKey($secret);
}
public function __get($object)
{
$stripeObject = Util::convertToStripeObject([
'object' => snake_case($object)
], []);
return new HighOrderProxy($stripeObject);
}
}
@mathieutu
Copy link
Author

mathieutu commented Oct 17, 2017

Allow us to use Stripe's static methods through container, and so to mock them.

Example for Laravel:

app('stripe')->customer->all();

Instead of

\Stripe\Customer::all();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment