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

Fix payments settling #124

Open
wants to merge 3 commits into
base: 2-4-stable
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
15 changes: 8 additions & 7 deletions app/models/spree/braintree_checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class BraintreeCheckout < ActiveRecord::Base
scope :in_state, ->(state) { where(state: state) }
scope :not_in_state, ->(state) { where.not(state: state) }

after_save :update_payment_and_order
after_commit :update_payment_and_order

FINAL_STATES = %w(authorization_expired processor_declined gateway_rejected failed voided settled settlement_declined refunded released)

Expand Down Expand Up @@ -66,12 +66,13 @@ def can_credit?(_payment)
private

def update_payment_and_order
if state_changed? && payment
utils = Gateway::BraintreeVzeroBase::Utils.new(Gateway::BraintreeVzeroBase.first, order)
payment_state = utils.map_payment_status(state)
payment.send(payment_action(payment_state))
order.update!
end
return unless (changes = previous_changes[:state])
return unless changes[0] != changes[1]
return unless payment

utils = Gateway::BraintreeVzeroBase::Utils.new(Gateway::BraintreeVzeroBase.first, order)
payment_state = utils.map_payment_status(state)
payment.send(payment_action(payment_state))
end

def self.braintree_card_type_to_spree(type)
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/gateway/braintree_vzero_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def authorize(money_in_cents, source, gateway_options)

def settle(amount, checkout, _gateway_options)
result = Transaction.new(provider, checkout.transaction_id).submit_for_settlement(amount / 100.0)
checkout.update_attribute(:state, result.transaction.status)
checkout.update(state: result.transaction.status)
result
end

Expand Down
9 changes: 9 additions & 0 deletions app/models/spree/payment_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Spree::Payment.class_eval do
alias_method :original_update_order, :update_order

def update_order
# without reload order was updated with inaccurate data
order.reload
original_update_order
end
end
4 changes: 2 additions & 2 deletions app/views/spree/shared/braintree_vzero/_dropin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ container: container,
paypal: {
singleUse: <%= payment_method.preferred_store_payments_in_vault.eql?('do_not_store') %>,
amount: <%= @order.total %>,
currency: "<%= Spree::Config[:currency] %>",
currency: "<%= @order.currency %>",
enableShippingAddress: true,
shippingAddressOverride: {
recipientName: '<%= "#{shipping_address.firstname} #{shipping_address.lastname}" %>',
Expand Down Expand Up @@ -34,7 +34,7 @@ onPaymentMethodReceived: function (result) {
$(formId).append("<input type='hidden' name='payment_method_nonce' value=" + data.nonce + ">");
else
$(formId).append("<input type='hidden' name='order[payments_attributes][][braintree_nonce]' value=" + data.nonce + ">");
$(formId).submit();
$(formId)[0].submit();
}

if (SpreeBraintreeVzero.threeDSecure && result.type == "CreditCard") {
Expand Down