3

I'm using PHP 7 with Phalcon 3. I'd like to implement Stripe response in one function. I can handle errors like that.

Here is the code:

try {
  // Use Stripe's library to make requests...
} 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\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} 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
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

Each time I need to call a Stripe method I need to implement all this try catch. How can I do to create one function with all Stripe exceptions ? I got the idea to send stripe function in parameter and use the function in the try but it doesn't work because the function is executed before to be inside the function.

  function stripeResponse($function) {
    try {
      // Use Stripe's library to make requests...
      \Stripe\Stripe::setApiKey("my_key");
      $function();
    } catch(\Stripe\Error\Card $e) {
      // Since it's a decline, \Stripe\Error\Card will be caught
    } catch (\Stripe\Error\RateLimit $e) {
      // Too many requests made to the API too quickly
    } catch (\Stripe\Error\InvalidRequest $e) {
      // Invalid parameters were supplied to Stripe's API
    } catch (\Stripe\Error\Authentication $e) {
      // Authentication with Stripe's API failed
      // (maybe you changed API keys recently)
    } 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
      // yourself an email
    } catch (Exception $e) {
      // Something else happened, completely unrelated to Stripe
    }
  }

    return $this->stripeResponse(\Stripe\Charge::create([
        "amount" => 100,
        "currency" => "eur",
        "source" => "token",
        "description" => "Description"
    ]));

Do you have an idea to do what I want ?

1 Answer 1

5

The way you are calling $this->stripeResponse is incorrect. You are passing it the response of \Stripe\Charge::create instead of a callable.

You can change it to:

return $this->stripeResponse(function() {
    \Stripe\Charge::create([
        "amount" => 100,
        "currency" => "eur",
        "source" => "token",
        "description" => "Description"
    ]);
});
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.