I propose that we pass the order object to hook_commerce_paypal_wpp_request_alter(). This will make it easier to update the paypal request name-value pairs based on the state of the current order. While it is currently possible to pull the order number from the $nvp['CUSTOM'] string and load the order, it would be more reliable to have the order object passed in the drupal_render() call.

I'll use my current situation as an example. I'm using commerce_multicurrency to allow users to select from multiple currencies. In order to send the order totals to paypal in the user's selected currency, I've implemented hook_commerce_paypal_wpp_request_alter() in a small custom module to make this happen.

function commerce_multicurrency_paypal_wpp_commerce_paypal_wpp_request_alter(&$nvp, $order) {
  
  // The following 2 lines can be removed if the order object is passed as an argument
  //$order_number = intval(substr($nvp['CUSTOM'], 6));
  //$order = commerce_order_load_by_number($order_number);

  // get currency code from order
  $order_currency_code = $order->commerce_order_total['und'][0]['currency_code'];

  // update $nvp if
  //  order's currency code is different from PayPal currency code
  //  AND order's currency code is an available PayPal currency code
  if ($order_currency_code != $nvp['CURRENCYCODE'] && in_array($order_currency_code, array_keys(commerce_paypal_wpp_currencies()))) {
    $nvp['CURRENCYCODE'] = $order_currency_code;
    $nvp['AMT'] = round(commerce_currency_amount_to_decimal($order->commerce_order_total['und'][0]['amount']), 2);
  }
}

Comments

mikehues’s picture

Here is a patch file that implements this change. This patch works for me.

guy_schneerson’s picture

subscribing

rszrama’s picture

Status: Needs review » Fixed

Perfect use case and solution. Committed. : )

guy_schneerson’s picture

Status: Fixed » Needs review

great guys i got a client who needed this for a multi-currency site.

@rszrama: I think it would be good to add a configuration option for “use order currency if supported” This will allow the current currency of the order to be used if supported by PayPal and the one selected in the configuration to act as a fallback if not.
This will allow the PayPal module to support multi-currency sites out of he box without having to write code. If you think its a good idea ill be happy to write a patch

rszrama’s picture

Status: Needs review » Fixed

Yep, sounds like a great idea! Let's definitely make it an additional option in the payment method configuration, as I'd hate for someone to suddenly start getting payments in unexpected currencies. : P

I'd go ahead and post the patch in a separate issue for review if you don't mind.

guy_schneerson’s picture

great, sounds like a plan :)

mikehues’s picture

Please post a link to the new issue here so I can follow the progress. thanks!

guy_schneerson’s picture

Status: Fixed » Closed (fixed)

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