Last active
December 11, 2015 18:58
-
-
Save michaelschade/4645080 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
require __DIR__ . '/lib/Stripe.php'; | |
if ($_POST) { | |
Stripe::setApiKey("sk_test_OXXXXXXXXXXXXXXXXXXX9"); | |
try { | |
// Use Stripe's bindings... | |
} catch(Stripe_CardError $e) { | |
// Since it's a decline, Stripe_CardError will be caught | |
$body = $e->getJsonBody(); | |
$err = $body['error']; | |
$msg = NULL; | |
switch($err['code']) { | |
case "incorrect_number": | |
$msg = "Your card number doesn't look valid. Try typing it again"; | |
break; | |
case "expired_card": | |
$msg = "Unfortunately, it looks like your card has expired."; | |
break; | |
case "card_declined": | |
$msg = "Your credit card has declined. Please contact the card issuer, or try a different card."; | |
break; | |
case "processing_error": | |
$msg = "There was an error in processing your transaction. Please contact us for assistance."; | |
break; | |
// More case statements for the error codes mentioned at | |
// https://stripe.com/docs/api?lang=php#catching_errors | |
default: | |
$msg = "Something went wrong. You should try again or contact support."; | |
break; | |
} | |
} catch (Stripe_InvalidRequestError $e) { | |
// You could do the same with other error messages... | |
// Though you would not have access to $err['code'], as that | |
// is only for `Stripe_CardError`s | |
} | |
// get the credit card details submitted by the form | |
$token = $_POST['stripeToken']; | |
$description = $_POST['description']; | |
$email = $_POST['email']; | |
$value = $_POST['value']*100; | |
// create a Customer | |
$customer = Stripe_Customer::create(array( | |
"card" => $token, | |
"description" => "$description", | |
"email" => "$email") | |
); | |
// charge the Customer instead of the card | |
Stripe_Charge::create(array( | |
"amount" => $value, # amount in cents, again | |
"currency" => "usd", | |
"customer" => $customer->id) | |
); | |
// save the customer ID in your database so you can use it later | |
//saveStripeCustomerId($user, $customer->id); | |
// later | |
//$customerId = getStripeCustomerId($user); | |
//Stripe_Charge::create(array( | |
//"amount" => 1500, # $15.00 this time | |
//"currency" => "usd", | |
//"customer" => $customerId) | |
//); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment