Skip to content

Instantly share code, notes, and snippets.

@edwardsmarkf
Created July 20, 2016 18:45
Show Gist options
  • Save edwardsmarkf/11ae3abfac8f658a7829f05f5e132880 to your computer and use it in GitHub Desktop.
Save edwardsmarkf/11ae3abfac8f658a7829f05f5e132880 to your computer and use it in GitHub Desktop.
<?php
require_once('vendor/autoload.php');
$stripe = array(
"secret_key" => "sk_test_jgji8VZWf90mcw4BBaO1YiMp",
"publishable_key" => "pk_test_Dmx9iBVuUizlpXIS1DsUtYNn"
);
define('STRIPE_KEY_PUBLISHABLE', $stripe['publishable_key'] ); //'pk_test_XXX');
define('STRIPE_KEY_SECRET', $stripe['secret_key']); //'sk_test_XXX');
\Stripe\Stripe::setApiKey(STRIPE_KEY_SECRET);
if($_POST)
{
echo "Oh you posted, here is what I have in my POST: <br><pre>";
echo '<pre>';
print_r($_POST);
echo "</pre><br>";
try {
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token
));
echo '<pre>';
var_dump($customer['sources']['data'][0]['name']);
var_dump($customer['sources']['data'][0]['address_line1']);
var_dump($customer['sources']['data'][0]['address_city']);
var_dump($customer['sources']['data'][0]['address_state']);
var_dump($customer['sources']['data'][0]['address_zip']);
var_dump($customer['sources']['data'][0]['id']);
var_dump($customer['sources']['data'][0]['brand']);
var_dump($customer['sources']['data'][0]['last4']);
echo '</pre>';
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $_POST['original-amount'] * 100,
'currency' => 'usd'
));
} catch (\Stripe\Error\Card $e) {
//declined
print_r($e,0);
}
echo "I just created the customer with id '" . $customer->id ;
} 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 src="https://checkout.stripe.com/checkout.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var handler = StripeCheckout.configure({
key: '<?= STRIPE_KEY_PUBLISHABLE; ?>',
token: function(token) {
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#amount").val(token.amount);
$("#payment-form").submit();
}
});
$('#customButton').on('click', function(e) {
debugger;
var tmpAmount = document.getElementById('original-amount').value * 100 ;
// Open Checkout with further options
handler.open({
name: 'Stripe.com',
description: 'Make a practice program purchase now.',
amount: tmpAmount,
zipCode: true,
billingAddress: true
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
});
</script>
<style type="text/css">
//Let's pretend I know CSS
#stripeResponse {
word-wrap: break-word;
}
</style>
</head>
<body>
<form action="" method="POST" id="payment-form" name='payment-form' >
<input type="hidden" id="stripeToken" name="stripeToken" value="tok_XXX" />
<input type="hidden" id="stripeEmail" name="stripeEmail" />
<input type="email" id="client-email" name="client-email" placeholder="[email protected]" />
<input type="input" id="original-amount" name="original-amount" placeholder = '$$ amt with decimal' value='' />
</form>
<input id="customButton" type="button" value="Make a practice program purchase for us." />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment