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

Patient checkout stripejs #7

Merged
merged 3 commits into from
Jan 25, 2016
Merged
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
81 changes: 80 additions & 1 deletion app/views/spree/admin/payments/source_forms/_stripe.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
<%= render "spree/admin/payments/source_forms/gateway", payment_method: payment_method, previous_cards: payment_method.reusable_sources(@order) %>
<%= render "spree/admin/payments/source_forms/gateway", payment_method: payment_method, previous_cards: payment_method.reusable_sources(@order) %>

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("<%= ENV['STRIPE_PUB_KEY'] %>");
</script>

<script>
Spree.stripePaymentMethod = $('#payment_method_' + <%= payment_method.id %>);
var mapCC, stripeResponseHandler;

mapCC = function(ccType) {
if (ccType === 'MasterCard') {
return 'mastercard';
} else if (ccType === 'Visa') {
return 'visa';
} else if (ccType === 'American Express') {
return 'amex';
} else if (ccType === 'Discover') {
return 'discover';
} else if (ccType === 'Diners Club') {
return 'dinersclub';
} else if (ccType === 'JCB') {
return 'jcb';
}
};

stripeResponseHandler = function(status, response) {
var paymentMethodId, token;
if (response.error) {
$('#stripeError').html(response.error.message);
return $('#stripeError').show();
} else {
paymentMethodId = Spree.stripePaymentMethod.prop('id').split("_")[2];
Spree.stripePaymentMethod.find('#card_number' + paymentMethodId + ', #card_expiry' + paymentMethodId + ', #card_code' + paymentMethodId ).prop("disabled", true);
Spree.stripePaymentMethod.find(".ccType").prop("disabled", false);
Spree.stripePaymentMethod.find(".ccType").val(mapCC(response.card.brand));
token = response['id'];
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][gateway_payment_profile_id]' value='" + token + "'/>");
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][last_digits]' value='" + response.card.last4 + "'/>");
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][month]' value='" + response.card.exp_month + "'/>");
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][year]' value='" + response.card.exp_year + "'/>");
return Spree.stripePaymentMethod.parents("form").get(0).submit();
}
};

$(document).ready(function() {
alert("This is new");
Spree.stripePaymentMethod.prepend("<div id='stripeError' class='errorExplanation' style='display:none'></div>");
return $('#new_payment [data-hook=buttons]').click(function() {
var expiration, params;
$('#stripeError').hide();
if (Spree.stripePaymentMethod.is(':visible')) {
expiration = $('.cardExpiry:visible').payment('cardExpiryVal');
params = $.extend({
number: $('.cardNumber:visible').val(),
cvc: $('.cardCode:visible').val(),
exp_month: expiration.month || 0,
exp_year: expiration.year || 0
}, Spree.stripeAdditionalInfo);
Stripe.card.createToken(params, stripeResponseHandler);
return false;
}
});
});
</script>

<%- if @order.has_checkout_step?('address') -%>
<script>
Spree.stripeAdditionalInfo = {
name: "<%= @order.bill_address.full_name %>",
address_line1: "<%= @order.bill_address.address1 %>",
address_line2: "<%= @order.bill_address.address2 %>",
address_city: "<%= @order.bill_address.city %>",
address_state: "<%= @order.bill_address.state_text %>",
address_zip: "<%= @order.bill_address.zipcode %>",
address_country: "<%= @order.bill_address.country %>"
}
</script>
<%- end -%>
21 changes: 0 additions & 21 deletions app/views/spree/checkout/payment/_stripe.html.erb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/spree_gateway/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,36 @@ class Engine < Rails::Engine
app.config.spree.payment_methods << Spree::Gateway::Migs
app.config.spree.payment_methods << Spree::Gateway::SpreedlyCoreGateway
end

def self.activate
if SpreeGateway::Engine.frontend_available?
Rails.application.config.assets.precompile += [
'lib/assets/javascripts/spree/frontend/spree_gateway.js',
'lib/assets/javascripts/spree/frontend/spree_gateway.css',
]
Dir.glob(File.join(File.dirname(__FILE__), "../../controllers/frontend/*/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
end

def self.backend_available?
@@backend_available ||= ::Rails::Engine.subclasses.map(&:instance).map{ |e| e.class.to_s }.include?('Spree::Backend::Engine')
end

def self.frontend_available?
@@frontend_available ||= ::Rails::Engine.subclasses.map(&:instance).map{ |e| e.class.to_s }.include?('Spree::Frontend::Engine')
end

if self.backend_available?
paths["app/views"] << "lib/views/backend"
end

if self.frontend_available?
paths["app/controllers"] << "lib/controllers/frontend"
paths["app/views"] << "lib/views/frontend"
end

config.to_prepare &method(:activate).to_proc
end
end
81 changes: 81 additions & 0 deletions lib/views/frontend/spree/checkout/payment/_stripe.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<%= render "spree/checkout/payment/gateway", payment_method: payment_method %>

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("<%= ENV['STRIPE_PUB_KEY'] %>");
</script>

<script>
Spree.stripePaymentMethod = $('#payment_method_' + <%= payment_method.id %>);
var mapCC, stripeResponseHandler;

mapCC = function(ccType) {
if (ccType === 'MasterCard') {
return 'mastercard';
} else if (ccType === 'Visa') {
return 'visa';
} else if (ccType === 'American Express') {
return 'amex';
} else if (ccType === 'Discover') {
return 'discover';
} else if (ccType === 'Diners Club') {
return 'dinersclub';
} else if (ccType === 'JCB') {
return 'jcb';
}
};

stripeResponseHandler = function(status, response) {
var paymentMethodId, token;
if (response.error) {
$('#stripeError').html(response.error.message);
return $('#stripeError').show();
} else {
Spree.stripePaymentMethod.find('#card_number, #card_expiry, #card_code').prop("disabled", true);
Spree.stripePaymentMethod.find(".ccType").prop("disabled", false);
Spree.stripePaymentMethod.find(".ccType").val(mapCC(response.card.brand));
token = response['id'];
paymentMethodId = Spree.stripePaymentMethod.prop('id').split("_")[2];
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][gateway_payment_profile_id]' value='" + token + "'/>");
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][last_digits]' value='" + response.card.last4 + "'/>");
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][month]' value='" + response.card.exp_month + "'/>");
Spree.stripePaymentMethod.append("<input type='hidden' class='stripeToken' name='payment_source[" + paymentMethodId + "][year]' value='" + response.card.exp_year + "'/>");
return Spree.stripePaymentMethod.parents("form").get(0).submit();
}
};

$(document).ready(function() {
Spree.stripePaymentMethod.prepend("<div id='stripeError' class='errorExplanation' style='display:none'></div>");
return $('#checkout_form_payment [data-hook=buttons]').click(function() {
var expiration_month, expiration_year, params;
$('#stripeError').hide();
if (Spree.stripePaymentMethod.is(':visible')) {
paymentMethodId = Spree.stripePaymentMethod.prop('id').split("_")[2];
expiration_month = $('#payment_source_'+ paymentMethodId +'_month:visible').val();
expiration_year = $('#payment_source_'+ paymentMethodId +'_year:visible').val();
params = $.extend({
number: $('#card_number:visible').val(),
cvc: $('#card_code:visible').val(),
exp_month: expiration_month || 0,
exp_year: expiration_year || 0
}, Spree.stripeAdditionalInfo);
Stripe.card.createToken(params, stripeResponseHandler);
return false;
}
});
});
</script>

<%- if @order.has_checkout_step?('address') -%>
<script>
Spree.stripeAdditionalInfo = {
name: "<%= @order.bill_address.full_name %>",
address_line1: "<%= @order.bill_address.address1 %>",
address_line2: "<%= @order.bill_address.address2 %>",
address_city: "<%= @order.bill_address.city %>",
address_state: "<%= @order.bill_address.state_text %>",
address_zip: "<%= @order.bill_address.zipcode %>",
address_country: "<%= @order.bill_address.country %>"
}
</script>
<%- end -%>