function commerce_mollie_redirect_form in Commerce Mollie 7
Implements hook_commerce_redirect_form().
Returns the form that is submitted to Mollie.
File
- ./
commerce_mollie.module, line 171
Code
function commerce_mollie_redirect_form($form, &$form_state, $order, $payment_method) {
// Get amount (in cents) and the currency code.
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$amount = $wrapper->commerce_order_total->amount
->value();
$currency_code = $wrapper->commerce_order_total->currency_code
->value();
// Order description which will be submitted to Mollie.
$payment_description = t("Order @order_id - @site_name", array(
'@order_id' => $order->order_number,
'@site_name' => variable_get('site_name', ''),
));
// Create a new payment transaction for this order and set the status to pending.
$transaction = commerce_payment_transaction_new('commerce_mollie', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $amount;
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
$transaction->currency_code = $currency_code;
// Save payment transaction (needed because we send the transaction ID to Mollie as metadata.
commerce_payment_transaction_save($transaction);
// The url of the page the customer should be returned back to by Mollie.
$redirect_url = url('checkout/' . $order->order_id . '/payment/response/' . $order->data['payment_redirect_key'], array(
'absolute' => TRUE,
));
// The webhook url Mollie should callback to update the transaction status.
$webhook_url = url('commerce_mollie/webhook', array(
'absolute' => TRUE,
));
try {
$mollie = new Mollie_API_Client();
$mollie
->setApiKey($payment_method['settings']['commerce_mollie_test_mode'] ? $payment_method['settings']['commerce_mollie_api_key_test'] : $payment_method['settings']['commerce_mollie_api_key_live']);
$payment = $mollie->payments
->create(array(
"amount" => commerce_currency_amount_to_decimal($amount, $currency_code),
"description" => $payment_description,
"redirectUrl" => $redirect_url,
"webhookUrl" => $webhook_url,
"metadata" => array(
"order_id" => $order->order_id,
"transaction_id" => $transaction->transaction_id,
"currency_code" => $currency_code,
),
));
// Store the Mollie payment ID.
$transaction->remote_id = $payment->id;
// Save payment transaction.
commerce_payment_transaction_save($transaction);
// Set redirect url.
$form['#action'] = $payment
->getPaymentUrl();
$form['mollie_information'] = array(
'#markup' => '<p class="commerce-mollie-info">' . t($payment_method['settings']['commerce_mollie_redirect_page_text']) . '</p>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t($payment_method['settings']['commerce_mollie_redirect_page_button_text']),
);
} catch (Mollie_API_Exception $e) {
watchdog('commerce_mollie', 'Could not create a Mollie payment. Error: %error', array(
'%error' => htmlspecialchars($e
->getMessage()),
), WATCHDOG_ERROR);
drupal_set_message(t('An error occured while preparing your Mollie payment. Please try again or contact the webmaster.'), 'error');
drupal_goto('cart/checkout');
}
return $form;
}