Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use stripejs to tokenize cards #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
<script data-main="js/main.js" src="js/vendor/requirejs/require.js"></script>

<script src="//localhost:35724/livereload.js"></script>

<!-- Stripe JS v2 using Celery publishable key -->
<script src="https://js.stripe.com/v2/"></script>
<script>
Stripe.setPublishableKey('pk_mbqX6nrqdVLalm1tUwOxX3XGuMlz6');
</script>
</body>
</html>
117 changes: 73 additions & 44 deletions src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ define(function(require) {
},

createOrder: function() {
var order = this._generateOrder();

celeryClient.createOrder(order, this.handleOrder);
this._generateOrder(function(order) {
celeryClient.createOrder(order, this.handleOrder);
}.bind(this));
},

handleOrder: function(err, res) {
Expand Down Expand Up @@ -275,63 +275,92 @@ define(function(require) {
this.$form.find('.Celery-OrderSummary-price--total').text(total);
},

_generateOrder: function() {
var $form = this.$form;
var order = {
buyer: {},
shipping_address: {},
line_items: [],
payment_source: {
card: {
number: '',
exp_month: '',
exp_year: '',
cvc: ''
_generateOrder: function(callback) {
this._createStripeToken(this._getCard(), function(err, token) {
if (err) {
return this.handleError(err);
}

var order = {
buyer: {},
shipping_address: {},
line_items: [],
payment_source: {
card: {
stripe_token: '',
}
}
};

// Set stripe token
if (token) {
order.payment_source.card.stripe_token = token.id;
}
};

order.user_id = shop.data.user_id;
order.buyer.email = this._getFieldValue('email');
order.user_id = shop.data.user_id;
order.buyer.email = this._getFieldValue('email');

// Set shipping
order.shipping_address.country = this._getCountry();

// Card
var card = order.payment_source.card;
// Set taxes
if (config.features.taxes) {
order.shipping_address.zip = this._getZip();
}

card.number = this._getFieldValue('card_number');
card.cvc = this._getFieldValue('cvc');
// Set coupon
if (config.features.coupons) {
var couponCode = this._getCouponCode();

// Card Expiry
var expiry = this._getFieldValue('expiry');
var expiryParts = expiry.split('/');
if (couponCode) {
order.discount_codes = [couponCode];
}
}

card.exp_month = expiryParts[0].trim();
card.exp_year = expiryParts[1].trim();
// Set line item
var lineItem = {
product_id: shop.data.product._id,
quantity: this._getQuantity()
};

// Shipping
order.shipping_address.country = this._getCountry();
order.line_items.push(lineItem);

if (config.features.taxes) {
order.shipping_address.zip = this._getZip();
return callback(order)
}.bind(this));
},

_getCard: function() {
if (this._getTotal() === 0) {
return null;
}

// Coupon
if (config.features.coupons) {
var couponCode = this._getCouponCode();
// Card Expiry
var expiry = this._getFieldValue('expiry');
var expiryParts = expiry.split('/');

if (couponCode) {
order.discount_codes = [couponCode];
}
return {
number: this._getFieldValue('card_number'),
cvc: this._getFieldValue('cvc'),
exp_month: expiryParts[0].trim(),
exp_year: expiryParts[1].trim(),
address_zip: this._getFieldValue('billing_zip'),
}
},

// Line Item
var lineItem = {
product_id: shop.data.product._id,
quantity: this._getQuantity()
};
_createStripeToken: function(card, callback) {
var Stripe = window.Stripe || {};

order.line_items.push(lineItem);
if (!card) {
return callback(null, null);
}

return order;
Stripe.card.createToken(card, function(status, response) {
if (response.error) {
return callback(response.error);
}

return callback(null, response);
});
},

_getDiscount: function() {
Expand Down