0

I'm looking for a way to default the Billing Information on the PayPal site when using PayPal WPS to process my payments in Drupal Commerce (v7). Currently, I have the Billing Information defaulted correctly on my Drupal site from the user profile so the customers do not have to enter it again during checkout (already entered when they registered), but do not want them to have to fill their Billing Information on the PayPal site once redirected during payment processing.

I have done this before when creating my own "home-grown" solution using the simple Pay Now button at PayPal by setting input form fields on the page that submits the form to PayPal.

<input name="first_name" type="hidden" value="frank" />

It would be a far better experience for users that do not have PayPal accounts to have this information defaulted on the "Pay with a debit or credit card" section of the PayPal page.

Thanks for any help! Frank

2
  • Have you checked the project's issue queue to see if there's an open feature request or pending patch? Dec 12, 2013 at 20:24
  • Hi Ryan. Yes, I checked the issue queue and did not see anything unless I missed something. I have however found a hook where I should be able to override the data sent to PalPal WPS. Once I have tested this I will respond back with my findings and hopefully my answer :)
    – FNik
    Dec 12, 2013 at 22:10

3 Answers 3

2

Thanks for your helping clues. I'm using Drupal Commerce V7.x-1.8 with drupal 7.26 and commerce_paypal WPS 7.x-2.3.

I made a custom module named my_module. Code Below :

function my_module_commerce_paypal_wps_order_form_data_alter(&$data, $order) {
  $user = user_load($order->uid);
  $profile_id = $order->commerce_customer_billing['und'][0]['profile_id'];
  $profile = commerce_customer_profile_load($profile_id);

  $data['first_name'] = $profile->commerce_customer_address['und'][0]['first_name'];
  $data['last_name'] = $profile->commerce_customer_address['und'][0]['last_name'];
  $data['address1'] = $profile->commerce_customer_address['und'][0]['thoroughfare'];
  $data['address2'] = $profile->commerce_customer_address['und'][0]['premise'];
  $data['city'] = $profile->commerce_customer_address['und'][0]['locality'];
  $data['zip'] = $profile->commerce_customer_address['und'][0]['postal_code'];
  $data['night_phone_b'] = $profile->commerce_customer_address['und'][0]['phone_number'];
  $data['email'] = $user->mail;

}

Now the customer billing informations are automatically filled up in the Paypal Form. Notice for phone field : I used "night_phone_b" field for European phone format. I also hope this helps someone else.

1

After a little more thorough review of the commerce_paypal_wps module I was able to successfully pass default billing information (name, address, email, etc) from my site to the PayPal site. This is helpful if some users may not already have a PayPal account nor want to create one. In this case when they select the "Pay with a debit or credit card" option on the PayPal payment page all their billing information is defaulted from my site as seen on the checkout/review pages so they do not have to enter it again.

The *commerce_paypal_wps_order_form* function in the commerce_paypal_wps.module creates a hook that you can use:

// Allow modules to alter parameters of the API request.
drupal_alter('commerce_paypal_wps_order_form_data', $data, $order);

In my *hook_commerce_paypal_wps_order_form_data_alter* function (in my custom module) I set the additional billing address fields as described by PayPal at https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/formbasics/#id08A6F0SJ04Y (in the "Sample HTML Code for Filling Out FORMs Automatically for Buyers" section).

function hook_commerce_paypal_wps_order_form_data_alter(&$data, $order) {
  $profile = user_load($order->uid);
  $data['first_name'] = $profile->field_first_name['und'][0]['value'];
  $data['last_name'] = $profile->field_last_name['und'][0]['value'];
  $data['address1'] = $profile->field_address_1['und'][0]['value'];
  ...
}

Thanks for an extensible commerce_paypal module and I hope this helps someone else.

1

I answered a variation of this on:

Send Billing Information from Address Field to PayPal site using WPS

However am cross-posting it here since I believe it is a better approach:

The user profile may not have the data saved to it yet, if it is a new user or if you allow anonymous checkout somehow, so it is better to retrieve this data directly from the order. To do that, we have to do some fancy footwork using the entity API - this also avoids any snafus related to database changes or translation arrays. I created a simple function that uses this API to extract an array of address fields:

/**
 * Helper to retrieve the profile from an order
 *
 * @param order
 *   The order object
 * @param type
 *   Either shipping or billing
 * @return
 *   The requested data
 */
function _my_module_address_from_order($order, $type="billing") {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $type = "commerce_customer_".$type;
  return $wrapper->$type->commerce_customer_address->value();
}

Then I call it thusly in the actual hook:

/**
 * Send default billing address to paypal
 */
function my_module_commerce_paypal_wps_order_form_data_alter(&$data, $order) {
  $address = _my_module_address_from_order($order);
  $data['address1'] = $address['thoroughfare'];
  $data['address2'] = $address['premise'];
  $data['first_name'] = $address['first_name'];
  $data['last_name'] = $address['last_name'];
  $data['city'] = $address['locality'];
  $data['state'] = $address['administrative_area'];
  $data['zip'] = $address['postal_code'];
  $data['email'] = $order->mail;
}

Note that paypal will not display a billing address unless it gets all of the values address1, city, state and zip as well as first_name and last_name!

Much of this solution was arrived at from consulting http://www.drupalcommerce.org/discussions/756/load-address-order

2
  • Patch submitted here: drupal.org/node/2239805
    – sfyn
    Apr 13, 2014 at 10:16
  • Thanks for the cross-post. In my case the user profile always existed, but this approach is appreciated.
    – FNik
    Apr 14, 2014 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.