Created
April 20, 2015 13:39
-
-
Save redhedded1/5884425fa4efbc6f9113 to your computer and use it in GitHub Desktop.
respond to Stripe response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function donate_store(CreateDonationRequest $request){ | |
\Stripe\Stripe::setApiKey(getenv("STRIPE_PRIVATE_KEY")); | |
try { | |
if($request->input('donation_amt') == 'other'){ | |
$amount = $request->input('donation_specified_amt'); | |
}else{ | |
$amount = $request->input('donation_amt'); | |
} | |
$amount = round(100 * (float)$amount); | |
\Stripe\Charge::create(array( | |
"amount" => $amount, | |
"currency" => "usd", | |
"source" => $request->input('stripeToken'), | |
"description" => 'Donation from: ' . $request->input('email'))); | |
} catch (\Stripe\Error\Card $e) { | |
$body = $e->getJsonBody(); | |
$err = $body['error']; | |
$status = $e->getHttpStatus(); | |
$code = isset($err['code']) ? $err['code'] : ' '; | |
$param = isset($err['param']) ? $err['param']: ' '; | |
switch($err['type']){ | |
case 'invalid_request_error': | |
$errorType = 'Invalid parameters in request'; | |
break; | |
case 'api_error': | |
$errorType = 'Temporary problem with card processing server'; | |
break; | |
case 'card_error': | |
$errorType = 'Card Processing Error'; | |
break; | |
} | |
if($err['type'] == 'card_error'){ | |
switch($code) { | |
case 'incorrect_number': | |
$cardError = "Card number is incorrect."; | |
break; | |
case 'invalid_number': | |
$cardError = "Card number is not a valid credit card number."; | |
break; | |
case 'incorrect_expiry_month': | |
$cardError = "The card's expiration month is invalid."; | |
break; | |
case 'incorrect_expiry_year': | |
$cardError = "The card's expiration year is invalid."; | |
break; | |
case 'invalid_cvc': | |
$cardError = "The card's security code is invalid."; | |
break; | |
case 'expired_card': | |
$cardError = "The card has expired."; | |
break; | |
case 'incorrect_cvc': | |
$cardError = "The card's security code is incorrect."; | |
break; | |
case 'incorrect_zip': | |
$cardError = "The card's billing zip code failed validation."; | |
break; | |
case 'card_declined': | |
$cardError = "The transaction was declined."; | |
break; | |
case 'missing': | |
$cardError = "There is no card for a customer that is being charged."; | |
break; | |
case 'processing_error': | |
$cardError = "An error occurred while processing the card."; | |
break; | |
case 'rate_limit': | |
$cardError = "An error occurred due to excessive frequency of API requests."; | |
break; | |
} | |
} | |
return \Redirect::to('/donate') | |
->withInput() | |
->with('message', $cardError); | |
} catch (\Stripe\Error\InvalidRequest $e) { | |
// Invalid parameters were supplied to Stripe's API | |
$body = $e->getJsonBody(); | |
$err = $body['error']; | |
$status = $e->getHttpStatus(); | |
$code = isset($err['code']) ? $err['code'] : ' '; | |
$param = isset($err['param']) ? $err['param']: ' '; | |
switch($err['type']){ | |
case 'invalid_request_error': | |
$errorType = 'Invalid parameters in request'; | |
break; | |
case 'api_error': | |
$errorType = 'Temporary problem with card processing server'; | |
break; | |
case 'card_error': | |
$errorType = 'Card Processing Error'; | |
break; | |
} | |
return \Redirect::to('/donate') | |
->withInput() | |
->with('message', $body['error']['message']); | |
} catch (\Stripe\Error\ApiConnection $e) { | |
// Network communication with Stripe failed | |
$body = $e->getJsonBody(); | |
$err = $body['error']; | |
$status = $e->getHttpStatus(); | |
$code = isset($err['code']) ? $err['code'] : ' '; | |
$param = isset($err['param']) ? $err['param']: ' '; | |
switch($err['type']){ | |
case 'invalid_request_error': | |
$errorType = 'Invalid parameters in request'; | |
break; | |
case 'api_error': | |
$errorType = 'Temporary problem with card processing server'; | |
break; | |
case 'card_error': | |
$errorType = 'Card Processing Error'; | |
break; | |
} | |
return \Redirect::to('/donate') | |
->withInput() | |
->with('message', $errorType); | |
} catch (\Stripe\Error\Base $e) { | |
// Display a very generic error to the user, and maybe send | |
// yourself an email | |
return \Redirect::to('/donate') | |
->withInput() | |
->with('message', 'An error occurred, please try again'); | |
} catch (Exception $e) { | |
// Something else happened, completely unrelated to Stripe | |
return \Redirect::to('/donate') | |
->withInput() | |
->with('message', 'An error occurred, please try again'); | |
} | |
if($request->input('donation_amt') == 'other'){ | |
$amount = $request->input('donation_specified_amt'); | |
}else{ | |
$amount = $request->input('donation_amt'); | |
} | |
$amount = 100 * (float)$amount; | |
if($request->input('participant_id')){ | |
$participant_id = $request->input('participant_id'); | |
}else{ | |
$participant_id = ''; | |
} | |
if($request->input('team_id')){ | |
$team_id = $request->input('team_id'); | |
}else{ | |
$team_id = ''; | |
} | |
$donation = Donation::create(array( | |
'participant_id' => $participant_id, | |
'team_id' => $team_id, | |
'cardholdername' => $request->input('cardholdername'), | |
'address1' => $request->input('address_line1'), | |
'address2' => $request->input('address_line2'), | |
'city' => $request->input('city'), | |
'state' => $request->input('state'), | |
'postalcode' => $request->input('postal_code'), | |
'country' => $request->input('country'), | |
'email' => $request->input('email'), | |
'amount' => $amount, | |
'stripeToken' => $request->input('stripeToken') | |
)); | |
$success = 'Thank you for your donation!'; | |
return \Redirect::route('donate') | |
->with('message', $success); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment