function commerce_sagepay_order_form in Drupal Commerce SagePay Integration 7
Sets up a new form for the submit to Sage Pay button (off site redirect).
Parameters
array $form: The form array.
array $form_state: Currency values and form state.
commerce_order $order: The Commerce Ordering being processed.
array $settings: The Sage Pay settings.
Return value
mixed The form array.
1 call to commerce_sagepay_order_form()
- commerce_sagepay_form_redirect_form in includes/
commerce_sagepay_form.inc - Implements hook_form_redirect_form().
File
- includes/
commerce_sagepay_common.inc, line 22 - Common utility functions shared by all SagePay modules.
Code
function commerce_sagepay_order_form($form, &$form_state, $order, $settings) {
// Wrap the order for easier access to data.
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$total = commerce_line_items_total($wrapper->commerce_line_items);
// Add tax if we have sales tax in the order.
$total['amount'] = $wrapper->commerce_order_total->amount
->value();
// Load customer profile.
$profile = commerce_customer_profile_load($order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id']);
// Get user billing address.
$billing_address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
// Get user delivery address.
$delivery_address = NULL;
if (isset($order->commerce_customer_shipping)) {
$delivery_profile = commerce_customer_profile_load($order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id']);
$delivery_address = $delivery_profile->commerce_customer_address[LANGUAGE_NONE][0];
}
// Encrypt the order details (address and amount) ready to send to SagePay.
$encrypted_order = _commerce_sagepay_encrypted_order($settings, $order, $total, $billing_address, $delivery_address);
// Determine the correct transaction type based on the gateway settings.
switch (variable_get(SAGEPAY_SETTING_TRANSACTION_TYPE)) {
case COMMERCE_CREDIT_AUTH_CAPTURE:
$tx_type = 'PAYMENT';
break;
case COMMERCE_CREDIT_AUTH_ONLY:
$tx_type = 'DEFERRED';
break;
default:
// Set to deferred by default if there is no setting for the gateway.
$tx_type = 'DEFERRED';
}
// Build the data array that will be translated into hidden form values.
$data = array(
'VPSProtocol' => SAGEPAY_PROTOCOL,
'TxType' => $tx_type,
'Vendor' => variable_get(SAGEPAY_SETTING_VENDOR_NAME),
'Crypt' => $encrypted_order,
);
// Determine the correct url based on the transaction mode.
switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
case SAGEPAY_TXN_MODE_LIVE:
$server_url = SAGEPAY_FORM_SERVER_LIVE;
break;
case SAGEPAY_TXN_MODE_TEST:
$server_url = SAGEPAY_FORM_SERVER_TEST;
break;
case SAGEPAY_TXN_MODE_SIMULATION:
$server_url = SAGEPAY_FORM_SERVER_SIMULATION;
break;
}
$form['#action'] = $server_url;
foreach ($data as $name => $value) {
if (!empty($value)) {
$form[$name] = array(
'#type' => 'hidden',
'#value' => $value,
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Proceed to SagePay'),
);
return $form;
}