function commerce_paypal_wps_order_form in Commerce PayPal 7.2
Same name and namespace in other branches
- 7 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 a payment method instance ID 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 408 - 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_currencies('paypal_wps')))) {
$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' => '',
);
$line_item_types = array();
if (function_exists('commerce_product_line_item_types')) {
$line_item_types = commerce_product_line_item_types();
}
// 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']),
// The application generating the API request
'bn' => 'CommerceGuys_Cart_PPS',
// 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),
// Provide a default email for non-PayPal users.
'email' => substr($order->mail, 0, 127),
// Define a single item in the cart representing the whole order.
'amount_1' => commerce_paypal_price_amount(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, $line_item_types),
);
// If a billing address is available, pass it to PayPal to populate
// field values for non-PayPal users paying with their card.
if (!empty($wrapper->commerce_customer_billing->commerce_customer_address)) {
// Assign billing address fields to hidden form values.
$billing_address = $wrapper->commerce_customer_billing->commerce_customer_address
->value();
$data['address1'] = substr($billing_address['thoroughfare'], 0, 100);
$data['address2'] = substr($billing_address['premise'], 0, 100);
$data['city'] = substr($billing_address['locality'], 0, 40);
$data['state'] = $billing_address['administrative_area'];
$data['zip'] = substr($billing_address['postal_code'], 0, 32);
$data['country'] = $billing_address['country'];
$data['first_name'] = drupal_substr($billing_address['first_name'], 0, 32);
$data['last_name'] = drupal_substr($billing_address['last_name'], 0, 32);
}
// 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 (isset($value)) {
$form[$name] = array(
'#type' => 'hidden',
'#value' => $value,
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Proceed to PayPal'),
);
return $form;
}