Created
April 23, 2015 19:36
-
-
Save anonymous/7141d71eb32ce5403ef0 to your computer and use it in GitHub Desktop.
Example for sajid
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_once('vendor/autoload.php'); | |
define('STRIPE_KEY_PUBLISHABLE', 'pk_test_XXX'); | |
define('STRIPE_KEY_SECRET', 'sk_test_XXX'); | |
\Stripe\Stripe::setApiKey(STRIPE_KEY_SECRET); | |
if($_POST) | |
{ | |
try { | |
echo "Oh you posted, here is what I have in my POST: <br><pre>"; | |
var_dump($_POST); | |
echo "</pre><br>"; | |
// Get the credit card details submitted by the form | |
$token = $_POST['stripeToken']; | |
$email = $_POST['stripeEmail']; | |
$amount = $_POST['myAmount']; | |
$customer = \Stripe\Customer::create(array( | |
"card" => $token, | |
"email" => $email) | |
); | |
$charge = \Stripe\Charge::create(array( | |
"amount" => round($amount*100), | |
"currency" => "usd", | |
"customer" => $customer->id | |
)); | |
echo "I just created a charge with id " . $charge->id . " for customer: " . $customer->id; | |
echo "<br><br><br><br><br> Try a new charge?"; | |
} catch(\Stripe\Error\Card $e) { | |
echo "Card error exception: $e"; | |
} catch (\Stripe\Error\InvalidRequest $e) { | |
echo "Invalid request exception: $e"; | |
} catch (\Stripe\Error\Authentication $e) { | |
echo "Authentication request exception: $e"; | |
} catch (\Stripe\Error\ApiConnection $e) { | |
echo "Invalid request exception: $e"; | |
} catch (\Stripe\Error\Base $e) { | |
echo "Generic Stripe exception: $e"; | |
} catch (Exception $e) { | |
echo "Exception raised but not from Stripe: $e"; | |
} | |
} | |
?> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.js"></script> | |
<script type="text/javascript" src="https://js.stripe.com/v2/"></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
Stripe.setPublishableKey('<?= STRIPE_KEY_PUBLISHABLE; ?>'); | |
$("#customButton").on("click", function() { | |
var $form = $(this); | |
// Disable the submit button to prevent repeated clicks | |
$form.find('button').prop('disabled', true); | |
Stripe.card.createToken({ | |
number: $('#cardNumber').val(), | |
cvc: $('#CVC').val(), | |
exp_month: $('#expMonth').val(), | |
exp_year: $('#expYear').val() | |
}, stripeResponseHandler); | |
// Prevent the form from submitting with the default action | |
return false; | |
}); | |
function stripeResponseHandler(status, response) { | |
if (response.error) { | |
// Show the errors on the form | |
$('.payment-errors').text(response.error.message); | |
return; | |
} | |
$("#token").val(response.id); | |
$("#payment-form").submit(); | |
} | |
}); | |
</script> | |
<style type="text/css"> | |
//Let's pretend I know CSS | |
#stripeResponse { | |
word-wrap: break-word; | |
} | |
</style> | |
</head> | |
<body> | |
<span class="payment-errors"></span> | |
<form action="test_sajid.php" method="POST" id="payment-form"> | |
<table> | |
<tr> | |
<td>Amount to pay</td> | |
<td><input id="amount" name="myAmount" type="text" size="20" value="10"/></td> | |
</tr> | |
<tr> | |
<td>Your email</td> | |
<td><input id="email" name="stripeEmail" type="text" size="20" value="[email protected]"/></td> | |
</tr> | |
<tr> | |
<td>Card Number</td> | |
<td><input id="cardNumber" type="text" size="20" data-stripe="number" value="4242424242424242"/></td> | |
</tr> | |
<tr> | |
<td>CVC</td> | |
<td><input id="CVC" type="text" size="4" data-stripe="cvc" value="555"/></td> | |
</tr> | |
<tr> | |
<td>Expiry Month</td> | |
<td><input id="expMonth" type="text" size="2" data-stripe="exp-month" value="09"/></td> | |
</tr> | |
<tr> | |
<td>Expiry Year</td> | |
<td><input id="expYear" type="text" size="4" data-stripe="exp-year" value="2016"/></td> | |
</tr> | |
</table> | |
<input id="customButton" type="button" value="Pay like it's 1999"/> | |
<input type="hidden" id="token" name="stripeToken" value="tok_XXX" /> | |
<input type="hidden" id="email" name="stripeEmail" value="tok_XXX" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment