Shopify/active_merchant — pull request #771 by jordanwheeler (2013-07-19 16:59:20+00:00)
Title: Correctly calculates PP Express discounts for non-decimal currencies

My solution to fix https://github.com/Shopify/active_merchant/issues/759

I can't see a better way to fix this, and PayPal isn't much help. PayPal Express does have a shipping discount field, but if you want to have a discount on the order itself you just have to add an extra item with a negative amount. Of course that's going to cause problems when you've got non-decimal currencies and percentage-based discount codes, but we just have to deal with it.

This works because the Discount item is always the last item sent with an order, and you can't have a product with a negative price. If there's a negative item amount, we know we're processing a PayPal Express order with a discount code.

This will calculate the difference between the actual order amount and the subtotal after splitting at the decimal. That difference is added to the discount amount to ensure that everything adds up. I've tested locally as well.

Review
@jduff @BlakeMesdag