Automattic / woocommerce-payments

Accept payments via credit card. Manage transactions within WordPress.
https://wordpress.org/plugins/woocommerce-payments/
Other
176 stars 69 forks source link

Fix WCPay Subscription coupons #3180

Closed james-allan closed 3 years ago

james-allan commented 3 years ago

Describe the bug

Since we have moved to use price data for line items, taxes, fees, and shipping we no longer need to send coupons or discount data.

The reason for this is because we use the $item->get_total() to determine the price data amount, and the total is after discounts have been applied.

We could use $item->get_subtotal() instead, however, that then throws out taxes because tax line items don't store subtotals (prediscount figures), it only stores the effective tax amount (the total).

To Reproduce

  1. Add a subscription to your cart.
  2. Apply the coupon.
  3. Purchase the subscription using WCPay.
  4. Compare the WC and WCPay subscription totals.

Screenshots

Screen Shot 2021-10-21 at 6 29 45 pm Screen Shot 2021-10-21 at 6 29 53 pm

Expected behavior

The totals should match.

Desktop (please complete the following information):

Smartphone (please complete the following information):

james-allan commented 3 years ago

I managed to find a fix for this. We just need to pass the subtotal (pre discount amount) when generating the product line item data.

eg

@@ -670,7 +670,7 @@ class WC_Payments_Subscription_Service {
                                'price_data' => $this->format_item_price_data(
                                        $subscription->get_currency(),
                                        $this->product_service->get_wcpay_product_id( $product ),
-                                       $item->get_total() / $item->get_quantity(),
+                                       $item->get_subtotal() / $item->get_quantity(),
                                        $subscription->get_billing_period(),
                                        $subscription->get_billing_interval()
                                ),