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

Payment update #31

Open
wants to merge 6 commits into
base: development
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/backend/all.js'
//= require spree/backend/subscribable
//= require spree/frontend/ajax_handler
//= require spree/frontend/payment
39 changes: 39 additions & 0 deletions app/assets/javascripts/spree/frontend/payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Spree.ready(function($) {
Spree.updatePaymentMethod = function() {
if (($('.edit_subscription')).is('*')) {
if (($('#existing_cards')).is('*')) {
($('.clearfix')).hide();
($('#use_existing_card_yes')).click(function() {
($('.clearfix')).hide();
return ($('.existing-cc-radio')).prop("disabled", false);
});
($('#use_existing_card_no')).click(function() {
($('.clearfix')).show();
return ($('.existing-cc-radio')).prop("disabled", true);
});
}
($('#payment')).hide();
$("#use_another_card").change(function() {
if($(this).is(':checked')) {
($('#payment')).show();
} else {
($('#payment')).hide();
}
});
$(".cardNumber").payment('formatCardNumber');
$(".cardExpiry").payment('formatCardExpiry');
$(".cardCode").payment('formatCardCVC');
$(".cardNumber").change(function() {
return $(this).parent().siblings(".ccType").val($.payment.cardType(this.value));
});
($(document)).on('click', '#cvv_link', function(event) {
var windowName, windowOptions;
windowName = 'cvv_info';
windowOptions = 'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,scrollbars=1';
window.open(($(this)).attr('href'), windowName, windowOptions);
return event.preventDefault();
});
}
};
return Spree.updatePaymentMethod();
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
//= require spree/frontend/cart_radio_button.js
//= require spree/frontend/datepicker
//= require spree/frontend/ajax_handler.js
//= require spree/frontend/payment
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/frontend/all.js'
34 changes: 34 additions & 0 deletions app/controllers/spree/admin/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ module Admin
class SubscriptionsController < Spree::Admin::ResourceController

before_action :ensure_not_cancelled, only: [:update, :cancel, :cancellation, :pause, :unpause]
before_action :update_params, only: :update

def cancellation
end

def edit
load_variables
end

def cancel
if @subscription.cancel_with_reason(cancel_subscription_attributes)
redirect_to collection_url, success: t('.success')
Expand Down Expand Up @@ -48,6 +53,35 @@ def unpause

private

def update_params
if params[:use_another_card]
if params[:use_existing_card] == 'no'
params[:subscription].merge!(source_id: create_credit_card.try(:id))
elsif params[:use_existing_card] == 'yes'
params[:subscription].merge!(source_id: params[:order][:existing_card])
end
end
end

def create_credit_card
subscription_user.credit_cards.create(card_params) || nil
end

def load_variables
@subscription.reload
@credit_cards = subscription_user.credit_cards.select(&:persisted?) - [@subscription.source]
@order = @subscription.parent_order
end

def card_params
params[:payment_source]['1'].merge!(payment_method_id: subscription_user.credit_cards.first.payment_method_id)
params[:payment_source].require('1').permit(:name, :number, :expiry, :verification_value, :cc_type, :payment_method_id)
end

def subscription_user
@subscription.parent_order.user
end

def cancel_subscription_attributes
params.require(:subscription).permit(:cancellation_reasons)
end
Expand Down
34 changes: 33 additions & 1 deletion app/controllers/spree/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ class SubscriptionsController < Spree::BaseController

before_action :ensure_subscription
before_action :ensure_not_cancelled, only: [:update, :cancel, :pause, :unpause]
before_action :update_params, only: :update

def edit
load_variables
end

def update
Expand All @@ -14,6 +16,7 @@ def update
format.json { render json: { subscription: { price: @subscription.price, id: @subscription.id } }, status: 200 }
end
else
load_variables
respond_to do |format|
format.html { render :edit }
format.json { render json: { errors: @subscription.errors.full_messages.to_sentence }, status: 422 }
Expand Down Expand Up @@ -76,7 +79,7 @@ def unpause

def subscription_attributes
params.require(:subscription).permit(:quantity, :next_occurrence_at, :delivery_number,
:subscription_frequency_id, :variant_id, :prior_notification_days_gap,
:subscription_frequency_id, :variant_id, :prior_notification_days_gap, :source_id,
ship_address_attributes: [:firstname, :lastname, :address1, :address2, :city, :zipcode, :country_id, :state_id, :phone],
bill_address_attributes: [:firstname, :lastname, :address1, :address2, :city, :zipcode, :country_id, :state_id, :phone])
end
Expand All @@ -91,6 +94,35 @@ def ensure_subscription
end
end

def update_params
if params[:use_another_card]
if params[:use_existing_card] == 'no'
params[:subscription].merge!(source_id: create_credit_card.try(:id))
elsif params[:use_existing_card] == 'yes'
params[:subscription].merge!(source_id: params[:order][:existing_card])
end
end
end

def create_credit_card
subscription_user.credit_cards.create(card_params) || nil
end

def load_variables
@subscription.reload
@credit_cards = subscription_user.credit_cards.select(&:persisted?) - [@subscription.source]
@order = @subscription.parent_order
end

def card_params
params[:payment_source]['1'].merge!(payment_method_id: subscription_user.credit_cards.first.payment_method_id)
params[:payment_source].require('1').permit(:name, :number, :expiry, :verification_value, :cc_type, :payment_method_id)
end

def subscription_user
try_spree_current_user
end

def ensure_not_cancelled
if @subscription.not_changeable?
respond_to do |format|
Expand Down
8 changes: 7 additions & 1 deletion app/views/spree/admin/subscriptions/_payment_info.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<fieldset data-hook="credit_card">
<legend><%= Spree.t(:credit_card) %></legend>
<legend><%= Spree.t(:current_credit_card) %></legend>
<table class="table table-condensed table-bordered">
<tr>
<th width="20%"><%= Spree.t(:name_on_card) %>:</th>
Expand All @@ -18,4 +18,10 @@
<td><%= @subscription.source.month %>/<%= @subscription.source.year %></td>
</tr>
</table>
<div>
<%= check_box_tag 'use_another_card' %>
<label for="use_another_card">
<%= Spree.t(:use_another_card) %>
</label>
</div><br><br>
</fieldset>
44 changes: 44 additions & 0 deletions app/views/spree/admin/subscriptions/_payments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<div class="panel panel-default" id="payment" data-hook>
<div class="panel-heading">
<h3 class="panel-title"><%= Spree.t(:payment_information) %></h3>
</div>
<div class="panel-body" data-hook="checkout_payment_step">
<% if @credit_cards.present? %>
<div class="card_options">
<%= radio_button_tag 'use_existing_card', 'yes', true %>
<label for="use_existing_card_yes">
<%= Spree.t(:use_existing_cc) %>
</label>
<br/>
<%= radio_button_tag 'use_existing_card', 'no' %>
<label for="use_existing_card_no">
<%= Spree.t(:use_new_credit_card) %>
</label>
</div>

<div id="existing_cards">
<p class="form-group" data-hook="existing_cards">
<table class="existing-credit-card-list">
<tbody>
<% @credit_cards.each do |card| %>
<tr id="<%= dom_id(card,'spree')%>" class="<%= cycle('even', 'odd') %>">
<td><%= card.name %></td>
<td><%= card.display_number %></td>
<td><%= card.month %> / <%= card.year %></td>
<td>
<%= radio_button_tag "order[existing_card]", card.id, (card == @credit_cards.first), { class: "existing-cc-radio" } %>
</td>
</tr>
<% end %>
</tbody>
</table>
</p>
</div>
<% end %>

<% unless @subscription.not_changeable? %>
<%= render :partial => "spree/checkout/payment/gateway", :locals => { :payment_method => @subscription.source.payment_method } %>
<% end %>

</div>
</div>
1 change: 1 addition & 0 deletions app/views/spree/admin/subscriptions/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<%= render partial: 'summary', locals: { f: f } %>
<%= render partial: 'item', locals: { f: f } %>
<%= render partial: 'payment_info', locals: { f: f } %>
<%= render partial: 'payments', locals: { f: f } %>
<%= render partial: 'addresses', locals: { f: f } %>
<%= render partial: 'orders' %>
<% unless @subscription.not_changeable? %>
Expand Down
8 changes: 7 additions & 1 deletion app/views/spree/subscriptions/_payment_info.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<fieldset data-hook="credit_card">
<legend><%= Spree.t(:credit_card) %></legend>
<legend><%= Spree.t(:current_credit_card) %></legend>
<table class="table table-condensed table-bordered">
<tr>
<th width="20%"><%= Spree.t(:name_on_card) %>:</th>
Expand All @@ -18,4 +18,10 @@
<td><%= @subscription.source.month %>/<%= @subscription.source.year %></td>
</tr>
</table>
<div>
<%= check_box_tag 'use_another_card' %>
<label for="use_another_card">
<%= Spree.t(:use_another_card) %>
</label>
</div><br><br>
</fieldset>
44 changes: 44 additions & 0 deletions app/views/spree/subscriptions/_payments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<div class="panel panel-default" id="payment" data-hook>
<div class="panel-heading">
<h3 class="panel-title"><%= Spree.t(:payment_information) %></h3>
</div>
<div class="panel-body" data-hook="checkout_payment_step">
<% if @credit_cards.present? %>
<div class="card_options">
<%= radio_button_tag 'use_existing_card', 'yes', true %>
<label for="use_existing_card_yes">
<%= Spree.t(:use_existing_cc) %>
</label>
<br/>
<%= radio_button_tag 'use_existing_card', 'no' %>
<label for="use_existing_card_no">
<%= Spree.t(:use_new_credit_card) %>
</label>
</div>

<div id="existing_cards">
<p class="form-group" data-hook="existing_cards">
<table class="existing-credit-card-list">
<tbody>
<% @credit_cards.each do |card| %>
<tr id="<%= dom_id(card,'spree')%>" class="<%= cycle('even', 'odd') %>">
<td><%= card.name %></td>
<td><%= card.display_number %></td>
<td><%= card.month %> / <%= card.year %></td>
<td>
<%= radio_button_tag "order[existing_card]", card.id, (card == @credit_cards.first), { class: "existing-cc-radio" } %>
</td>
</tr>
<% end %>
</tbody>
</table>
</p>
</div>
<% end %>

<% unless @subscription.not_changeable? %>
<%= render :partial => "spree/checkout/payment/gateway", :locals => { :payment_method => @subscription.source.payment_method } %>
<% end %>

</div>
</div>
1 change: 1 addition & 0 deletions app/views/spree/subscriptions/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<%= render partial: 'summary', locals: { f: f } %>
<%= render partial: 'item', locals: { f: f } %>
<%= render partial: 'payment_info', locals: { f: f } %>
<%= render partial: 'payments', locals: { f: f } %>
<%= render partial: 'addresses', locals: { f: f } %>
<%= render partial: 'orders' %>
<% unless @subscription.not_changeable? %>
Expand Down
Loading