Make sure that you have included Stripejs like described here
Here is the test file created. You can modify it according to your need. I used ajax for process the payment.
index.php
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Stripe Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<style>
* {
font-family: "Helvetica Neue", Helvetica;
font-size: 15px;
font-variant: normal;
padding: 0;
margin: 0;
}
html {
height: 100%;
}
body {
background: #E6EBF1;
align-items: center;
min-height: 100%;
display: flex;
width: 100%;
}
form {
width: 480px;
margin: 20px auto;
}
.group {
background: white;
box-shadow: 0 7px 14px 0 rgba(49,49,93,0.10),
0 3px 6px 0 rgba(0,0,0,0.08);
border-radius: 4px;
margin-bottom: 20px;
}
label {
position: relative;
color: #8898AA;
font-weight: 300;
height: 40px;
line-height: 40px;
margin-left: 20px;
display: block;
}
.group label:not(:last-child) {
border-bottom: 1px solid #F0F5FA;
}
label > span {
width: 20%;
text-align: right;
float: left;
}
.field {
background: transparent;
font-weight: 300;
border: 0;
color: #31325F;
outline: none;
padding-right: 10px;
padding-left: 10px;
cursor: text;
width: 70%;
height: 40px;
float: right;
}
.field::-webkit-input-placeholder { color: #CFD7E0; }
.field::-moz-placeholder { color: #CFD7E0; }
.field:-ms-input-placeholder { color: #CFD7E0; }
button {
float: left;
display: block;
background: #666EE8;
color: white;
box-shadow: 0 7px 14px 0 rgba(49,49,93,0.10),
0 3px 6px 0 rgba(0,0,0,0.08);
border-radius: 4px;
border: 0;
margin-top: 20px;
font-size: 15px;
font-weight: 400;
width: 100%;
height: 40px;
line-height: 38px;
outline: none;
}
button:focus {
background: #555ABF;
}
button:active {
background: #43458B;
}
.outcome {
float: left;
width: 100%;
padding-top: 8px;
min-height: 24px;
text-align: center;
}
.success, .error {
display: none;
font-size: 13px;
}
.success.visible, .error.visible {
display: inline;
}
.error {
color: #E4584C;
}
.success {
color: #666EE8;
}
.success .token {
font-weight: 500;
font-size: 13px;
}
</style>
<script src="https://js.stripe.com/v3/"></script>
<form>
<div class="group">
<label>
<span>Name</span>
<input id="name" name="name" class="field" placeholder="" />
</label>
<label>
<span>Email</span>
<input name="email" id="email" class="field" placeholder="" />
</label>
<label>
<span>Phone</span>
<input class="field" placeholder="(123) 456-7890" id="phone" type="phone" />
</label>
</div>
<div class="group">
<label>
<span>Card</span>
<div id="card-element" class="field"></div>
</label>
</div>
<button type="submit">Pay $25</button>
<div class="outcome">
<div class="error" role="alert"></div>
<div class="success">
Success! Your Stripe token is <span class="token"></span>
</div>
</div>
</form>
<script type="text/javascript">
var stripe = Stripe('YOUR-STRIPE-TOKEN');
var elements = stripe.elements();
var card = elements.create('card', {
hidePostalCode:true,
style: {
base: {
iconColor: '#666EE8',
color: '#31325F',
lineHeight: '40px',
fontWeight: 300,
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '15px',
'::placeholder': {
color: '#CFD7E0',
},
},
}
});
card.mount('#card-element');
function setOutcome(result) {
var successElement = document.querySelector('.success');
var errorElement = document.querySelector('.error');
successElement.classList.remove('visible');
errorElement.classList.remove('visible');
if (result.token) {
// Use the token to create a charge or a customer
// https://stripe.com/docs/charges
var stripe_token = result.token.id;
successElement.querySelector('.token').textContent = stripe_token;
successElement.classList.add('visible');
// AJAX
$.post( "do_payment.php", { stripeToken: stripe_token, name: $('#name').val(), "email":$('#email').val(), "phone": $('#phone').val() })
.done(function( data ) {
console.log(data);
});
} else if (result.error) {
errorElement.textContent = result.error.message;
errorElement.classList.add('visible');
}
}
card.on('change', function(event) {
setOutcome(event);
});
document.querySelector('form').addEventListener('submit', function(e) {
e.preventDefault();
var form = document.querySelector('form');
var extraDetails = {
name: form.querySelector('input[name=name]').value,
};
stripe.createToken(card, extraDetails).then(setOutcome);
});
</script>
</body>
</html>
For ajax process create a php file do_payment.php
<?php
require_once('vendor/init.php');
$stripe = array(
"secret_key" => "YOUR STRIPE SECRET KEY",
"publishable_key" => "YOUR STRIPE PUBLISHABLE KEY"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => '[email protected]',
'source' => $token
));
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 2500, //Charge $25 See https://stripe.com/docs/currencies#zero-decimal
'currency' => 'usd'
));
} catch (RateLimit $e) {
$body = $e->getJsonBody();
return ['status' => 'failed', 'res' => $body['error']];
} catch (InvalidRequest $e) {
$body = $e->getJsonBody();
return ['status' => 'failed', 'res' => $body['error']];
} catch (Authentication $e) {
$body = $e->getJsonBody();
return ['status' => 'failed', 'res' => $body['error']];
} catch (ApiConnection $e) {
$body = $e->getJsonBody();
return ['status' => 'failed', 'res' => $body['error']];
} catch (Base $e) {
$body = $e->getJsonBody();
return ['status' => 'failed', 'res' => $body['error']];
} catch (Exception $e) {
return ['status' => 'failed', 'res' => json_encode($e->getMessage())];
}
echo '<h1>Successfully charged $50.00!</h1>';
?>
For getting test credentials see this document
id="stripeToken"elements. Your code looks identical to mine, which works fine. - Barmar