solidusio / solidus_paypal_commerce_platform

💳 Integrate Solidus with Paypal Commerce Platform
https://developer.paypal.com/docs/platforms/
BSD 3-Clause "New" or "Revised" License
17 stars 24 forks source link

Error in Order Calculations #191

Open jakemumu opened 1 year ago

jakemumu commented 1 year ago

The PayPal Order Breakdown is Incorrect:

    def breakdown
      {
        item_total: price(@order.item_total),
        shipping: price(@order.shipment_total),
        tax_total: price(@order.additional_tax_total),
        discount: price(@order.all_adjustments.promotion.sum(&:amount).abs)
      }
    end

Solidus Version: 3.1.8

To Reproduce

Step 1 - create an order. Step 2 - add promotions on the order, ensure that one of the promotions is: eligible: false -- ours looks like:

#<Spree::Adjustment:0x00007fa4d8190f58
 id: 213210,
 source_type: "Spree::PromotionAction",
 source_id: 115,
 adjustable_type: "Spree::LineItem",
 adjustable_id: 242932,
 amount: -0.5337e3,
 label: "Promotion (Bundle Upgrade)",
 eligible: false,
 created_at: Wed, 01 Mar 2023 09:38:51.503385000 CST -06:00,
 updated_at: Wed, 01 Mar 2023 18:09:07.143385000 CST -06:00,
 order_id: 208491,
 included: false,
 promotion_code_id: nil,
 adjustment_reason_id: nil,
 finalized: false>

Current behavior The order totals will be incorrect and PayPal will reject and not process the order. This is because all non eligible promotions will also be included inside of: discount: price(@order.all_adjustments.promotion.sum(&:amount).abs)

Expected behavior The order totals should always be perfect

Screenshots

Screenshot 2023-03-01 at 4 30 24 PM

Desktop (please complete the following information): NA

Smartphone (please complete the following information): NA

Additional context NA

jakemumu commented 1 year ago

This patch has fixed it:

 # frozen_string_literal: true

 module Decorators
   module SolidusPaypalCommercePlatform
     module PaypalOrder
       def breakdown
         breakdown = super
         breakdown[:discount] = price(@order.promo_total.abs)
         breakdown
       end

       ::SolidusPaypalCommercePlatform::PaypalOrder.prepend(self)
     end
   end
 end
kennyadsl commented 1 year ago

Looks legit. Can you please open a PR?

jakemumu commented 1 year ago

Sure, should I do it against master?