Last active
October 17, 2017 12:58
-
-
Save mathieutu/2a2a10204396485c90e5d72e64c96500 to your computer and use it in GitHub Desktop.
Stripe Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Allow us to use Stripe's static methods through container, and so to mock them.
Example for Laravel:
Instead of