A
A
Albert Tobacco2015-06-19 14:32:18
Yii
Albert Tobacco, 2015-06-19 14:32:18

A method that intercepts method calls, calls them and returns the result?

Good afternoon, I'm working with Stripe API, I wrote a small wrapper for myself.
And at the moment I have to implement each class method somehow like this:

public function someOne(){
try {
 Мой код  
}catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
  // Display a very generic error to the user, and maybe send

} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}
}

Of course, I understand that this is a disgrace.
I understand that it is necessary to make a call centrally through one method, but without crutches, I don’t see a solution yet.
I'd be grateful for a hint on a solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-06-19
@Denormalization

You can see how the wrapper is made here https://github.com/laravel/cashier
Specifically here:
https://github.com/laravel/cashier/blob/5.0/src/La...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question