Skip to content

Instantly share code, notes, and snippets.

@dameety
Last active July 17, 2019 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dameety/ac1ba0c60d437279f8deb2189d023660 to your computer and use it in GitHub Desktop.
Save dameety/ac1ba0c60d437279f8deb2189d023660 to your computer and use it in GitHub Desktop.
<?php
class Handler
{
protected $exception;
public function __construct($exception)
{
$this->exception = $exception;
}
protected $expectedExceptions = [
\Stripe\Error\Card::class,
\Stripe\Error\RateLimit::class,
\Stripe\Error\InvalidRequest::class,
\Stripe\Error\Authentication::class,
\Stripe\Error\ApiConnection::class,
\Stripe\Error\Base::class
];
public function unexpected()
{
$exceptionClass = get_class($this->exception);
if ( ! in_array($exceptionClass, $this->expectedExceptions) ) {
//something unrelated to stripe happened.
//handle it here
}
}
public function respond()
{
$this->unexpected();
if($this->exception instanceof \Stripe\Error\Card ) {
//respond accordingly
} elseif( $this->exception instanceof \Stripe\Error\RateLimit ) {
//respond accordingly
} elseif( $this->exception instanceof \Stripe\Error\InvalidRequestt ) {
//respond accordingly
dd('getting this now');
} elseif( $this->exception instanceof \Stripe\Error\Authentication ) {
//respond accordingly
dd('getting auth key issue now');
} elseif( $this->exception instanceof \Stripe\Error\ApiConnection ) {
//respond accordingly
} elseif( $this->exception instanceof \Stripe\Error\Base ) {
//respond accordingly
}
}
}
<?php
class Handler2
{
protected $exception;
protected $exceptionClass;
public function __construct($exception)
{
$this->exception = $exception;
}
protected $expectedExceptions = [
\Stripe\Error\Card::class,
\Stripe\Error\RateLimit::class,
\Stripe\Error\InvalidRequest::class,
\Stripe\Error\Authentication::class,
\Stripe\Error\ApiConnection::class,
\Stripe\Error\Base::class
];
public function respond()
{
$this->unexpected();
$expectedExceptionMethodName = lcfirst (
preg_replace("/[^a-zA-Z]/", "", $this->exceptionClass)
);
$this->$expectedExceptionMethodName();
}
public function unexpected()
{
$this->exceptionClass = get_class($this->exception);
if ( ! in_array($this->exceptionClass, $this->expectedExceptions) ) {
//something unrelated to stripe happened
//handle it here
}
}
public function stripeErrorCard()
{
//handle it
}
public function stripeErrorInvalidRequest()
{
//handle it
}
public function stripeErrorAuthentication()
{
//handle it
}
public function stripeErrorRateLimit()
{
//handle it
}
public function stripeErrorApiConnection()
{
//handle it
}
public function stripeErrorBase()
{
//handle it
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment