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();
}
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
}
Related
i'm sending emails via phpmailer and i want to override the exceptions as when you send an email and that fail you can have the error message via :
echo json_encode"{$mail->ErrorInfo}";
but when the error is
"Empty body"
i would like to display something else.
Any solution ? Thanks
$new_msg = json_encode"{$mail->ErrorInfo}";
if ($new_msg == 'Empty body') {
throw new CustomException("You custom message ");
}
Wherever you are calling this function, catch the expectation there and show it to the user.
try {
if ($new_msg == 'Empty body') {
throw new CustomException("You custom message ");
}
} catch (CustomException $ex) {
//This is where you can have your own handling, exceptions that you want to handle separately
} catch (Exception $ex) {
// this part will handle general exceptions
// and show user some general error message
}
You can put your echo in a variable and then do an if check to display something else.
$new_msg = json_encode"{$mail->ErrorInfo}";
if ($new_msg == 'Empty body') {
echo 'You put here whatever you want';
} else {
echo json_encode"{$mail->ErrorInfo}";
}
Just catch the Exception and do something completely different!
try {
$something->thatWillThrowAnException();
} catch (Exception $e) {
// Do anything you want here!
}
My application sends an sms message when a user signs up. I'm trying to handle the exception if the number is invalid, and Twilio fails to send it. But when the message fails, it never seems to reach the catch in my try/catch clause.
try {
$message = $twilio->messages
->create(
"$user->phone", // to
array("from" => "$siteNumber", "body" => "$message_text")
);
} catch (TwilioException $e) {
return $e->getMessage();
}
I've also tried the following catch conditions.
} catch (\Services_Twilio_RestException $e) {
return $e->getMessage();
}
catch (RestException $e) {
return redirect()->back()->withFlashSuccess("Failed To Send Text");
}
Please try to change the catch exception
catch (\Exception $e) {
return $e->getMessage();
}
It's working for me
Im using Laravel Cashier and also the authentication registration in one form and to process it all im using a try catch block.
try {
// Process the credit card through Stripe
// Register the user in the system
} catch (\Exception $e) {
// Throw exception with error message
}
This works fine, but im wondering what would happen if the processing of the card worked, but the registering failed.
They would get charged and have no account.
How do you go about reversing actions taken if something fails?
You should use defferent try/catch for each process:
try {
// Process the credit card through Stripe
} catch (\Exception $e) {
// Throw exception with error message
}
And the second for register:
try {
// Register the user in the system
} catch (\Exception $e) {
// Throw exception with error message
}
This way you know exactly what failed.
You should try first register process because it's in our hand we can revert this if any thing goes wrong
try {
// Register the user in the system
// Process the credit card through Stripe
// If process fails throw custom exception
}catch( CustomException $e) {
//revert the Register process
//Throw exception
} catch (\Exception $e) {
// Throw exception with error message
}
I'm trying to customize error messages.
For handling errors i used "try/catch" block according to this Recurly documentation, like this for example:
try {
$account = Recurly_Account::get('my_account_id');
$subscription = new Recurly_Subscription();
$subscription->account = $account;
$subscription->plan_code = 'my_plan_code';
$subscription->coupon_code = 'my_coupon_code';
/* .. etc .. */
$subscription->create();
}
catch (Exception $e) {
$errorMsg = $e->getMessage();
print $errorMsg;
}
I wanted use code in catch block like this:
catch (Exception $e) {
$errorCode = $e->getCode();
print $myErrorMsg[$errorCode]; // array of my custom messages.
}
But getCode() method always returns zero for all possible errors.
My question for Recurly Team (or who there in this theme):
How i get error code for errors? Or please explain me how i can resolve this topic. Thanks!
If you look at the PHP Client on Github and you search for "throw new" which is what is done when an exception is thrown you'll see that they don't set the exception error code the second parameter of the exception constructor method.
Recurly PHP Client on Github: https://github.com/recurly/recurly-client-php/search?utf8=%E2%9C%93&q=throw+new
PHP Exception documentation: http://php.net/manual/en/language.exceptions.extending.php
Therefore, you'll either need to catch more exceptions based on their name
i.e.
catch (Recurly_NotFoundError $e) {
print 'Record could not be found';
}
OR
look at the exception message and compare it
catch (Exception $e) {
$errorMessage = $e->getMessage();
if($errorMessage=='Coupon is not redeemable.')
{
$myerrorCode=1;
}
//Add more else if, or case switch statement to handle the various errors you want to handle
print $myErrorMsg[$myerrorCode]; // array of my custom messages.
}
I am trying to create a subscribe method for my laravel app that uses the mailchimp api to subscribe a user to a given list. The method works fine when the email address is not already on the lsit. when it is already subscribed the mailchimp api throws the following error
Mailchimp_List_AlreadySubscribed blah#blah.co is already subscribed to
list Tc App Test List. Click here to update your profile.
with the following code being shown
public function castError($result) {
if($result['status'] !== 'error' || !$result['name']) throw new Mailchimp_Error('We received an unexpected error: ' . json_encode($result));
$class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mailchimp_Error';
return new $class($result['error'], $result['code']);
}
I have attempted a try catch block to catch the error but it is still being returned to the browser, here is what I tried and were it says MailChimp_Error I tried with Exception as well.
public function subscribe($id, $email, $merge_vars)
{
try {
$this->mailchimp->lists->subscribe($id, $email, $merge_vars);
} catch (MailChimp_Error $e) {
$response = 'an error has occured';
}
return $response;
}
Ultimately I want to be able to run the method and then either return either a success message or a message describing the issue to the user. the 3 possible mailchimp method errors are Email_notexists, list_alreadysubscribed and list does not exist although tihs last one should not occur as I am providing the list in the source code.
edit 1; after being in touch with mailchimp api support they suggested this code but the error still gets returned to the browser in its entirety
try {
$results = $this->mailchimp->lists->subscribe($id, $email, $merge_vars);
} catch (Mailchimp_Error $e) {
if ($e->getMessage()) {
$error = 'Code:'.$e->getCode().': '.$e->getMessage();
}
}
echo $error;
You can do
try
{
$response = $this->mailchimp->lists->addListMember($list_id, [
"email_address" => $email,
"status" => "subscribed",
]);
}
catch (\EXCEPTION $e) {
return $e->getMessage();
}
The \EXCEPTION handles a sort of error for stripe
Subscribe is in a namespace Acme\Emails\Subscribe so catch(Mailchimp_Error $e) looks for Mailchimp_Error in this namespace.
Changing it to catch(\Mailchimp_Error $e) makes it look in the root namespace and then it works as intended