From #1864842-7: Add support for PayPal Express Checkout:

The API request function commerce_paypal_ec_set_express_checkout() indicates in two places where we need to better itemize the order information we're submitting to PayPal. Full itemization is a requirement of the integration, so these need to be addressed. We should also itemize order data in commerce_paypal_ec_do_payment().

Specifically, if you look in commerce_paypal_ec_set_express_checkout(), you'll see the @todo comments where we need to include tax in our API requests via the PAYMENTREQUEST_n_TAXAMT and L_PAYMENTREQUEST_n_TAXAMTm parameters described at https://www.x.com/developers/paypal/documentation-tools/api/setexpressch.... We don't need to add any additional parameters for now.

Since the code to itemize orders needs to be used in these two different functions, we can also abstract the creation of the itemized array out into a separate function called commerce_paypal_ec_itemize_order(). We should just pass in the $order to it and it should return an array that can be merged into the $nvp array being built to submit the request.

Comments

JulienD’s picture

I've created a function called commerce_paypal_ec_itemize_order() to abstract the creation of the itemized array.

To work, the API was needed a missing parameter PAYMENTREQUEST_0_ITEMAMT and I had to change the amount value per line items sent.

JulienD’s picture

Status: Active » Needs review
JulienD’s picture

Updated the patch, I forgot to remove an unused var.

Apparently, that's not possible to edit a file uploaded in a previous comment :/

rszrama’s picture

Status: Needs review » Needs work

Copying this functionality over to the Payflow Link integration, I noticed a couple of things:

  1. Let's keep PAYMENTREQUEST_0_AMT in the main request, as it won't change whether or not the order is itemized.
  2. There are Tax API functions we can use to determine the amount of tax from an array of price components.
  3. We should have an API function in commerce_paypal.module to format price amounts per PayPal's requirements.

Look at this commit to the Payflow module and you should be able to copy / edit the updates in your Express Checkout module. : )

http://drupalcode.org/project/commerce_paypal.git/commitdiff/fb4469d

(Note: a few of the hunks aren't relevant and just came in incidentally, so ignore those.)

JulienD’s picture

1. I moved this field into the itemize function because both commerce_paypal_ec_set_express_checkout and commerce_paypal_ec_do_express_checkout implement it. Paypal EC is not waiting for a simple amount to bill the customer but they will compare the total amount of the order to the total of each line items.

2. I changed my code with your example.

3. I used the commerce_paypal_price_amount() to be more compliant with Paypal's requirements.

rszrama’s picture

Status: Needs work » Fixed

Excellent reroll. I took it a bit further to ensure we always have matching final amounts - instead of keeping a running total of individual line item amounts, I just subtract the total order tax amount and the shipping amount from the order total. Also, it is possible for shipping line items to have taxes, so I made sure to subtract the tax amount from the running total of shipping line items.

I've tested this with and without shipping, with taxes, and with a discount based on shipping. All looks good!

Commit: http://drupalcode.org/project/commerce_paypal.git/commitdiff/45bd3eb

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Adding the issue number.