You are here

function commerce_paypal_wps_order_form in Commerce PayPal 7

Same name and namespace in other branches
  1. 7.2 modules/wps/commerce_paypal_wps.module \commerce_paypal_wps_order_form()

Builds a Website Payments Standard form from an order object.

Parameters

$order: The fully loaded order being paid for.

$settings: An array of settings used to build out the form, including:

  • server: which server to use, either sandbox or live
  • business: the PayPal e-mail address the payment submits to
  • cancel_return: the URL PayPal should send the user to on cancellation
  • return: the URL PayPal should send the user to on successful payment
  • currency_code: the PayPal currency code to use for this payment if the total for the order is in a non-PayPal supported currency
  • language: the PayPal language code to use on the payment form
  • payment_action: the PayPal payment action to use: sale, authorization, or order
  • payment_method: optionally the name of the Commerce payment method to include in the IPN notify_url

Return value

A renderable form array.

1 call to commerce_paypal_wps_order_form()
commerce_paypal_wps_redirect_form in modules/wps/commerce_paypal_wps.module
Payment method callback: redirect form, a wrapper around the module's general use function for building a WPS form.

File

modules/wps/commerce_paypal_wps.module, line 366
Implements PayPal Website Payments Standard in Drupal Commerce checkout.

Code

function commerce_paypal_wps_order_form($form, &$form_state, $order, $settings) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);

  // Determine the currency code to use to actually process the transaction,
  // which will either be the default currency code or the currency code of the
  // order if it's supported by PayPal if that option is enabled.
  $currency_code = $settings['currency_code'];
  $order_currency_code = $wrapper->commerce_order_total->currency_code
    ->value();
  if (!empty($settings['allow_supported_currencies']) && in_array($order_currency_code, array_keys(commerce_paypal_wps_currencies()))) {
    $currency_code = $order_currency_code;
  }
  $amount = $wrapper->commerce_order_total->amount
    ->value();

  // Ensure a default value for the payment_method setting.
  $settings += array(
    'payment_method' => '',
  );

  // Build the data array that will be translated into hidden form values.
  $data = array(
    // Specify the checkout experience to present to the user.
    'cmd' => '_cart',
    // Signify we're passing in a shopping cart from our system.
    'upload' => 1,
    // The store's PayPal e-mail address
    'business' => $settings['business'],
    // The path PayPal should send the IPN to
    'notify_url' => commerce_paypal_ipn_url($settings['payment_method']),
    // Set the correct character set
    'charset' => 'utf-8',
    // Do not display a comments prompt at PayPal
    'no_note' => 1,
    // Do not display a shipping address prompt at PayPal
    'no_shipping' => 1,
    // Return to the review page when payment is canceled
    'cancel_return' => $settings['cancel_return'],
    // Return to the payment redirect page for processing successful payments
    'return' => $settings['return'],
    // Return to this site with payment data in the POST
    'rm' => 2,
    // The type of payment action PayPal should take with this order
    'paymentaction' => $settings['payment_action'],
    // Set the currency and language codes
    'currency_code' => $currency_code,
    'lc' => $settings['language'],
    // Use the timestamp to generate a unique invoice number
    'invoice' => commerce_paypal_ipn_invoice($order),
    // Define a single item in the cart representing the whole order
    'amount_1' => commerce_currency_amount_to_decimal(commerce_currency_convert($amount, $order_currency_code, $currency_code), $currency_code),
    'item_name_1' => t('Order @order_number at @store', array(
      '@order_number' => $order->order_number,
      '@store' => variable_get('site_name', url('<front>', array(
        'absolute' => TRUE,
      ))),
    )),
    'on0_1' => t('Product count'),
    'os0_1' => commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()),
  );

  // Allow modules to alter parameters of the API request.
  drupal_alter('commerce_paypal_wps_order_form_data', $data, $order);
  $form['#action'] = commerce_paypal_wps_server_url($settings['server']);
  foreach ($data as $name => $value) {
    if (!empty($value)) {
      $form[$name] = array(
        '#type' => 'hidden',
        '#value' => $value,
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Proceed to PayPal'),
  );
  return $form;
}