Skip to content

Commit

Permalink
6 - Reusable Stripe Module
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Oct 11, 2017
1 parent 0ff716c commit 291073b
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 135 deletions.
136 changes: 1 addition & 135 deletions src/billing/templates/billing/payment-method.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,142 +5,8 @@

<div class='col-10 col-md-6 mx-auto'>
<h1>Add Payment Method</h1>

<div class='stripe-payment-form' data-token='{{ publish_key }}' data-next-url='{% if next_url %}{{ next_url }}{% endif %}'></div>


<form class='payment-form' action="/charge" method="post" id="payment-form" data-token='{{ publish_key }}' data-next-url='{% if next_url %}{{ next_url }}{% endif %}'>
<div class="form-row">
<!-- <label for="card-element">
Credit or debit card
</label> -->
<div id="card-element" class='form-control'>
<!-- a Stripe Element will be inserted here. -->
</div>

<!-- Used to display form errors -->
<div id="card-errors" role="alert"></div>
</div>

<button class='btn btn-primary my-3'>Add Payment Method</button>
</form>
<div class='stripe-payment-form' data-token='{{ publish_key }}' data-next-url='{% if next_url %}{{ next_url }}{% endif %}' data-btn-title='Add New Card'></div>
</div>


{% endblock %}


{% block javascript %}
<script>

// https secure site when live

var paymentForm = $(".payment-form")
if (paymentForm.length > 1){
alert("Only one payment form is allowed per page")
paymentForm.css('display', 'none')
}
else if (paymentForm.length == 1) {

var pubKey = paymentForm.attr('data-token')
var nextUrl = paymentForm.attr('data-next-url')
// Create a Stripe client
var stripe = Stripe(pubKey);

// Create an instance of Elements
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
base: {
color: '#32325d',
lineHeight: '24px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};

// Create an instance of the card Element
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});

// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();

stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server
stripeTokenHandler(nextUrl, result.token);
}
});
});


function redirectToNext(nextPath, timeoffset) {
// body...
if (nextPath){
setTimeout(function(){
window.location.href = nextPath
}, timeoffset)
}
}

function stripeTokenHandler(nextUrl, token){
// console.log(token.id)
var paymentMethodEndpoint = '/billing/payment-method/create/'
var data = {
'token': token.id
}
$.ajax({
data: data,
url: paymentMethodEndpoint,
method: "POST",
success: function(data){
var succesMsg = data.message || "Success! Your card was added."
card.clear()
if (nextUrl){
succesMsg = succesMsg + "<br/><br/><i class='fa fa-spin fa-spinner'></i> Redirecting..."
}
if ($.alert){
$.alert(succesMsg)
} else {
alert(succesMsg)
}
redirectToNext(nextUrl, 1500)

},
error: function(error){
console.log(error)
}
})
}
}
</script>
{% endblock %}
131 changes: 131 additions & 0 deletions src/static_my_proj/js/ecommerce.main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
$(document).ready(function(){


var stripeFormModule = $(".stripe-payment-form")
var stripeModuleToken = stripeFormModule.attr("data-token")
var stripeModuleNextUrl = stripeFormModule.attr("data-next-url")
var stripeModuleBtnTitle = stripeFormModule.attr("data-btn-title") || "Add card"

var stripeTemplate = $.templates("#stripeTemplate")
var stripeTemplateDataContext = {
publishKey: stripeModuleToken,
nextUrl: stripeModuleNextUrl,
btnTitle: stripeModuleBtnTitle
}
var stripeTemplateHtml = stripeTemplate.render(stripeTemplateDataContext)
stripeFormModule.html(stripeTemplateHtml)




// https secure site when live

var paymentForm = $(".payment-form")
if (paymentForm.length > 1){
alert("Only one payment form is allowed per page")
paymentForm.css('display', 'none')
}
else if (paymentForm.length == 1) {

var pubKey = paymentForm.attr('data-token')
var nextUrl = paymentForm.attr('data-next-url')
// Create a Stripe client
var stripe = Stripe(pubKey);

// Create an instance of Elements
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
base: {
color: '#32325d',
lineHeight: '24px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};

// Create an instance of the card Element
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});

// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();

stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server
stripeTokenHandler(nextUrl, result.token);
}
});
});


function redirectToNext(nextPath, timeoffset) {
// body...
if (nextPath){
setTimeout(function(){
window.location.href = nextPath
}, timeoffset)
}
}

function stripeTokenHandler(nextUrl, token){
// console.log(token.id)
var paymentMethodEndpoint = '/billing/payment-method/create/'
var data = {
'token': token.id
}
$.ajax({
data: data,
url: paymentMethodEndpoint,
method: "POST",
success: function(data){
var succesMsg = data.message || "Success! Your card was added."
card.clear()
if (nextUrl){
succesMsg = succesMsg + "<br/><br/><i class='fa fa-spin fa-spinner'></i> Redirecting..."
}
if ($.alert){
$.alert(succesMsg)
} else {
alert(succesMsg)
}
redirectToNext(nextUrl, 1500)

},
error: function(error){
console.log(error)
}
})
}
}

})
10 changes: 10 additions & 0 deletions src/templates/base/js.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@
<!-- jquery-confirm JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script>

<!-- js Render -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.88/jsrender.min.js'></script>
{% include 'base/js_templates.html' %}

<!-- Django Secure AJAX JS -->
<script src='{% static "js/csrf.ajax.js" %}'></script>

<!-- eCommerce Custom JS -->
<script src='{% static "js/ecommerce.js" %}'></script>

<!-- eCommerce Main JS -->
<script src='{% static "js/ecommerce.main.js" %}'></script>





22 changes: 22 additions & 0 deletions src/templates/base/js_templates.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% verbatim %}
<!-- stripe template form -->
<script id="stripeTemplate" type="text/x-jsrender">
<form class='payment-form' action="" method="post" id="payment-form" data-token='{{:publishKey}}' data-next-url='{{:nextUrl}}'>
<div class="form-row">
<!-- <label for="card-element">
Credit or debit card
</label> -->
<div id="card-element" class='form-control'>
<!-- a Stripe Element will be inserted here. -->
</div>

<!-- Used to display form errors -->
<div id="card-errors" role="alert"></div>
</div>

<button class='btn btn-primary my-3'>{{:btnTitle }}</button>

</form>

</script>
{% endverbatim %}
25 changes: 25 additions & 0 deletions static_cdn/static_root/css/stripe-custom-style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* The CSS shown here will not be introduced in the Quickstart guide, but shows
* how you can use CSS to style your Element's container.
*/
.StripeElement {
background-color: white;
padding: 8px 12px;
border-radius: 4px;
border: 1px solid transparent;
box-shadow: 0 1px 3px 0 #e6ebf1;
-webkit-transition: box-shadow 150ms ease;
transition: box-shadow 150ms ease;
}

.StripeElement--focus {
box-shadow: 0 1px 3px 0 #cfd7df;
}

.StripeElement--invalid {
border-color: #fa755a;
}

.StripeElement--webkit-autofill {
background-color: #fefde5 !important;
}
Loading

0 comments on commit 291073b

Please sign in to comment.