View source
<?php
function basic_cart_payment_form_order_node_form_alter(&$form, &$form_state, $form_id) {
form_load_include($form_state, 'inc', 'node', 'node.pages');
$pmids = payment_method_options();
if (isset($form_state['values']['basic_cart_payment_methods'])) {
$pmid = $form_state['values']['basic_cart_payment_methods'];
}
else {
foreach ($pmids as $key => $value) {
$pmid = $key;
break;
}
}
$form['basic_cart_payment_methods'] = array(
'#type' => 'radios',
'#title' => t('Available payment method'),
'#default_value' => $pmid,
'#options' => $pmids,
'#weight' => 50,
'#ajax' => array(
'callback' => 'basic_cart_payment_payment_form_generate',
'wrapper' => 'basic-cart-payment-options',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['payment'] = array(
'#title' => t("Payment options"),
'#prefix' => '<div id="basic-cart-payment-options">',
'#suffix' => '</div>',
'#type' => 'fieldset',
'#weight' => 51,
);
$payment_method = entity_load_single('payment_method', $pmid);
$currency = variable_get('basic_cart_currency');
$payment = new Payment(array(
'context' => 'basic_cart_payment',
'context_data' => array(
'destination' => $_GET['q'],
),
'currency_code' => $currency,
'description' => $payment_method->title_specific,
'finish_callback' => 'basic_cart_payment_finish',
'method' => $payment_method,
));
$cart = basic_cart_get_cart();
if (!empty($cart) && is_array($cart)) {
foreach ($cart as $i => $item) {
$payment
->setLineItem(new PaymentLineItem(array(
'amount' => $item->basic_cart_unit_price,
'description' => $item->title,
'name' => $item->title,
'quantity' => $item->basic_cart_quantity,
)));
}
}
$form_info = payment_form_embedded($form_state, $payment, array(
$pmid,
));
unset($form_info['elements']['payment_status']);
$form['payment']['pay'] = $form_info['elements'];
return $form;
}
function basic_cart_payment_node_submit($node, $form, &$form_state) {
if ($node->type == 'order') {
$payment = $form_state['payment'];
entity_save('payment', $payment);
basic_cart_payment_pid($payment->pid);
if ($payment->method->name != 'basic_payment_method') {
node_save($node);
}
$payment
->execute();
}
}
function basic_cart_payment_pid($pid = NULL) {
static $payment_id;
if (!empty($pid) && !isset($payment_id)) {
$payment_id = $pid;
}
return $payment_id;
}
function basic_cart_payment_payment_form_generate($form, $form_state) {
return $form['payment'];
}
function basic_cart_payment_finish(Payment $payment) {
if (payment_access('view', $payment)) {
$view = ' ' . l(t('View payment'), 'payment/' . $payment->pid) . '.';
}
if (payment_status_is_or_has_ancestor($payment
->getStatus()->status, PAYMENT_STATUS_PENDING)) {
drupal_set_message(t('Your payment is still being processed.') . $view);
}
elseif (payment_status_is_or_has_ancestor($payment
->getStatus()->status, PAYMENT_STATUS_SUCCESS)) {
drupal_set_message(t('Your payment was successfully completed.') . $view);
}
elseif (payment_status_is_or_has_ancestor($payment
->getStatus()->status, PAYMENT_STATUS_FAILED)) {
drupal_set_message(t('Your payment failed.') . $view);
}
}