function commerce_braintree_dropin_submit_form_elements in Commerce Braintree 7.3
Same name and namespace in other branches
- 7.2 modules/commerce_braintree_dropin/commerce_braintree_dropin.module \commerce_braintree_dropin_submit_form_elements()
Returns the common FAPI form elements for all Drop-in UI implementations.
Parameters
$payment_method: The payment method being used.
array $arguments: An array of arguments to be passed Braintree token method.
mixed $order: An optional order object.
Return value
array An array of form elements for the drop-in ui.
4 calls to commerce_braintree_dropin_submit_form_elements()
- commerce_braintree_dropin_cardonfile_form in modules/
commerce_braintree_dropin/ commerce_braintree_dropin.module - Form callback for commerce_cardonfile entities.
- commerce_braintree_dropin_form_commerce_payment_order_transaction_add_form_alter in modules/
commerce_braintree_dropin/ commerce_braintree_dropin.module - Implements hook_form_FORM_ID_alter().
- commerce_braintree_dropin_submit_form in modules/
commerce_braintree_dropin/ commerce_braintree_dropin.module - Form callback for Braintree Drop-in payment method.
- commerce_braintree_js_cardonfile_form in ./
commerce_braintree.module - Form callback for commerce_cardonfile entities.
File
- modules/
commerce_braintree_dropin/ commerce_braintree_dropin.module, line 215 - Provides integration with Braintree Drop-in UI.
Code
function commerce_braintree_dropin_submit_form_elements($payment_method, $arguments = array(), $order = FALSE) {
global $user;
$form = array();
// Initialize Braintree and create a token.
commerce_braintree_initialize($payment_method);
$js_settings = array(
'environment' => $payment_method['settings']['environment'],
'options' => array(
'authorization' => Braintree_ClientToken::generate($arguments),
'container' => '#commerce-braintree-dropin-container',
),
'submitSelector' => '.checkout-continue',
'nonceInput' => 'commerce_payment[payment_details][nonce]',
);
// Add PayPal payment option if enabled.
if (!empty($payment_method['settings']['paypal_flow'])) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$amount = $order_wrapper->commerce_order_total
->value();
$js_settings['options']['paypal']['flow'] = $payment_method['settings']['paypal_flow'];
$js_settings['options']['paypal']['amount'] = commerce_currency_amount_to_decimal($amount['amount'], $amount['currency_code']);
$js_settings['options']['paypal']['currency'] = $amount['currency_code'];
}
// Allow other modules to alter the JS settings.
drupal_alter('commerce_braintree_dropin_js', $js_settings, $payment_method);
// The custom token is required to generate the Drop-in payment form.
$form['#attached']['js'][] = array(
'data' => array(
'commerceBraintreeDropin' => $js_settings,
),
'type' => 'setting',
);
$form['#attached']['library'][] = array(
'commerce_braintree_dropin',
'braintree.dropin',
);
// Include a container div for the Drop-in form to attach to.
$form['braintree_dropin'] = array(
'#markup' => '<div id="commerce-braintree-dropin-container"></div>',
);
// Add a field to store the nonce.
$form['nonce'] = array(
'#type' => 'hidden',
);
// Add option to save card on file for authenticated users.
if (!empty($payment_method['settings']['cardonfile']) && !empty($user->uid)) {
$storage = variable_get('commerce_cardonfile_storage', 'opt-in');
if ($storage !== 'required') {
$form['cardonfile'] = array(
'#type' => 'checkbox',
'#title' => t('Securely save this payment method for next time.'),
'#default_value' => $storage == 'opt-in' ? FALSE : TRUE,
);
}
else {
$form['cardonfile'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
}
return $form;
}