forked from codingforentrepreneurs/eCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ff716c
commit 291073b
Showing
6 changed files
with
320 additions
and
135 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
}) |
This file contains 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
This file contains 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
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 %} |
This file contains 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
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; | ||
} |
Oops, something went wrong.