2

I am using Stripe for a german website, I could translate the JS (stripeResponseHandler) error messages for the form following this post but not the exception of validation ($e->getMessage()). I get the exception error in english, how can I translate it?

My PHP :

try {
    require_once('Stripe/init.php');
    \Stripe\Stripe::setApiKey("myKey"); //Secret Key
    $token = $_POST['stripeToken'];
    $coupon = \Stripe\Coupon::retrieve($_POST['couponId']);
    $charge =  \Stripe\Charge::create(array(...)
} catch (Exception $e) {
    echo $e->getMessage();
}
Community
  • 1
  • 1

1 Answers1

0

The error messages returned by the API are in English -- at this time, Stripe does not have support for localized API error messages.

You can do what's described in the SO answer you linked to provide your own translations via error handling. E.g. you could do something like:

try {
    $charge = \Stripe\Charge::create(...);
} catch(\Stripe\Error\Card $e) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    switch ($err['code']) {
        case 'card_declined':
            // use your own "card declined" error message
            break;
        case 'invalid_number':
            // use your own "invalid number" error message
            break;
        // etc.
    }
} catch (\Stripe\Error\Base $e) {
    // another Stripe error happened, use a generic "error with our payment processor" message
} catch (Exception $e) {
    // something else happened, unrelated to Stripe
}
Community
  • 1
  • 1
Ywain
  • 16,854
  • 4
  • 51
  • 67
  • Thank you, these errors I already translated them in then JS phase, but there are other error coming from the API exceptionafter the submit. –  Sep 26 '16 at 11:23
  • Yes, it's possible for a card number to be successfully tokenized in the frontend code, and then for the charge creation to fail (e.g. because the card has insufficient funds). So you'll need to catch the errors in your server-side code as well. – Ywain Sep 26 '16 at 16:02