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

Consider payments when calculating total #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions app/controllers/spree/paypal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ def express

def confirm
order = current_order || raise(ActiveRecord::RecordNotFound)
order_total = order.total
if order.payments.present?
order_total = order_total - order.payments.map(&:amount).sum
end
order.payments.create!({
source: Spree::PaypalExpressCheckout.create({
token: params[:token],
payer_id: params[:PayerID]
}),
amount: order.total,
amount: order_total,
payment_method: payment_method
})
order.next
Expand Down Expand Up @@ -116,6 +120,22 @@ def payment_details items
# functionality doesn't currently exist in Spree core
item_sum = current_order.total - shipment_sum - current_order.additional_tax_total

order_total = current_order.total
if current_order.payments.present?
payments = current_order.payments.map(&:amount).sum
payment_numbers = current_order.payments.map(&:number).join(", ")
item_sum = item_sum - payments
order_total = order_total - payments
items << {
Name: "Payment #{payment_numbers}",
Quantity: 1,
Amount: {
currencyID: current_order.currency,
value: -(payments)
}
}
end

if item_sum.zero?
# Paypal does not support no items or a zero dollar ItemTotal
# This results in the order summary being simply "Current purchase"
Expand All @@ -129,7 +149,7 @@ def payment_details items
{
OrderTotal: {
currencyID: current_order.currency,
value: current_order.total
value: order_total
},
ItemTotal: {
currencyID: current_order.currency,
Expand Down