<script>
let stripe = Stripe('stripe_pk_key');
let elements = stripe.elements();
let style = {
base: {
color: '#32325d',
lineHeight: '18px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
let card = elements.create('card', {style: style});
card.mount('#card-element');
card.addEventListener('change', function (event) {
let displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
//initialize firebase
let config = {
"apiKey": "firebase_api_key",
"authDomain": "your-app.firebaseapp.com",
"databaseURL": "https://your-app.firebaseio.com",
"projectId": "your-app",
"storageBucket": "your-app.appspot.com",
"messagingSenderId": "your-app-messaging-id"
};
firebase.initializeApp(config);
$('form').on('submit', function (e) {
e.preventDefault();
$('#loading').addClass('loading');
let password = document.getElementById('password').value;
let email = document.getElementById('email').value;
let plan = document.getElementById('plan').value;
//process Stripe
stripe.createToken(card).then(function (result) {
if (result.error) {
alert(result.error.message);
$('#loading').removeClass('loading');
} else {
let stripeToken = result.token.id;
let stripeToken = result.token.id;
$('<input>').attr({
type: 'hidden',
id: 'stripeToken',
name: 'stripeToken',
value:stripeToken
}).appendTo('form');
//create firebase account
firebase.auth().createUserWithEmailAndPassword(email, password).then(function (user) {
//subscribe
$.ajax({
url: 'register.php',
data: $('form').serialize(),
type: 'POST',
success: function (response) {
let res = JSON.parse(response);
if (res.type === 'error') {
user.delete();
alert(res.msg)
} else {
$('form').find('input').val('');
alert(res.msg);
}
},
error: function (error) {
user.delete();
alert('Unable to process your request. Refresh and try again');
}
});
$('#loading').removeClass('loading');
}
).catch(function (error) {
let errorCode = error.code;
let errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode === 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
$('#loading').removeClass('loading');
});
}
});
})
</script>
Integrating Stripe subscription with firebase authentication
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.