diff --git a/commerce_robokassa.module b/commerce_robokassa.module index a658f30..11d7932 100644 --- a/commerce_robokassa.module +++ b/commerce_robokassa.module @@ -40,8 +40,7 @@ function commerce_robokassa_commerce_payment_method_info() { 'description' => t('Integrates Robokassa Merchant'), 'terminal' => FALSE, 'offsite' => TRUE, - // @todo Maybe better make it auto? - //'offsite_autoredirect' => TRUE, + 'offsite_autoredirect' => commerce_robokassa_get_settings('offsite_autoredirect'), 'active' => TRUE, ); return $payment_methods; @@ -102,10 +101,12 @@ function commerce_robokassa_build_redirect_form($form, &$form_state, $order, $se $amount = $wrapper->commerce_order_total->amount->value(); // Get real price. $amount = commerce_currency_amount_to_decimal($amount, $currency_code); - + + $transaction = commerce_robokassa_create_transaction($order->order_id, 'pending', 'Payment has not received'); + $form["MrchLogin"] = array('#type' => 'hidden', '#value' => commerce_robokassa_get_settings('login')); $form["OutSum"] = array('#type' => 'hidden', '#value' => $amount); - $form["InvId"] = array('#type' => 'hidden', '#value' => $order->order_id); + $form["InvId"] = array('#type' => 'hidden', '#value' => $transaction->transaction_id); // Calculate signature. $form["SignatureValue"] = array( @@ -113,7 +114,7 @@ function commerce_robokassa_build_redirect_form($form, &$form_state, $order, $se '#value' => md5( commerce_robokassa_get_settings('login') . ':' . $amount . ':' - . $order->order_id . ':' + . $transaction->transaction_id . ':' . commerce_robokassa_get_settings('pass1') )); @@ -208,51 +209,69 @@ function commerce_robokassa_fail() { function commerce_robokassa_result() { if (!empty($_POST)) { // @todo check data first. - //Are we really send OK if no processing happen? - echo 'OK' . $_POST['InvId']; - - // Get robokassa variables. - $order_in = $_POST['InvId']; - $amount_in = $_POST['OutSum']; - $signature_in = $_POST['SignatureValue']; - - // Calculate the hash. - $pass = commerce_robokassa_get_settings('pass2'); - $md5string = $amount_in . ':'. $order_in . ':'. $pass; - $md5 = strtoupper(md5($md5string)); - - if ($md5 == $signature_in) { - $order = commerce_order_load($order_in); - $wrapper = entity_metadata_wrapper('commerce_order', $order); - - $currency_code = $wrapper->commerce_order_total->currency_code->value(); - $amount = $wrapper->commerce_order_total->amount->value(); - // Get real price. - $amount = commerce_currency_amount_to_decimal($amount, $currency_code); - - $amount_received = abs($amount_in); - $amount_stored = abs($amount); - - if ($amount_received == $amount_stored) { - commerce_robokassa_create_transaction($order_in, commerce_robokassa_get_settings('status')); - watchdog('commerce_robokassa', 'Order #@order paid successfully.', array('@order' => $order_in), WATCHDOG_INFO); + + // Get robokassa InvId. + $transaction_id = $_POST['InvId']; + + $transaction = commerce_payment_transaction_load($transaction_id); + + if ($transaction && $transaction->status != 'success') { + dsm($transaction->status); + // Get robokassa other variables. + $amount_in = $_POST['OutSum']; + $signature_in = $_POST['SignatureValue']; + + // Calculate the hash. + $pass = commerce_robokassa_get_settings('pass2'); + $md5string = $amount_in . ':'. $transaction_id . ':'. $pass; + $md5 = strtoupper(md5($md5string)); + + if ($md5 == $signature_in) { + $order = commerce_order_load($transaction->order_id); + $wrapper = entity_metadata_wrapper('commerce_order', $order); + + $currency_code = $wrapper->commerce_order_total->currency_code->value(); + $amount = $wrapper->commerce_order_total->amount->value(); + // Get real price. + $amount = commerce_currency_amount_to_decimal($amount, $currency_code); + + $amount_received = abs($amount_in); + $amount_stored = abs($amount); + + if ($amount_received == $amount_stored) { + //Success + //Are we really send OK if no processing happen? + echo 'OK' . $_POST['InvId']; + + $transaction->status = 'success'; + $transaction->message = 'Payment has been received'; + commerce_payment_transaction_save($transaction); + watchdog('commerce_robokassa', 'Order #@order paid successfully.', array('@order' => $transaction->order_id), WATCHDOG_INFO); + } + else { + echo 'bad'.$amount_in; + watchdog('commerce_robokassa', 'Order #@order was not paid: recieved (@am_rec) and real(@am_int) order info do not match.', array( + '@order' => $transaction->order_id, + '@am_rec' => $amount_in, + '@am_int' => $amount, + ), WATCHDOG_ERROR); + } } else { - watchdog('commerce_robokassa', 'Order #@order was not paid: recieved (@am_rec) and real(@am_int) order info do not match.', array( - '@order' => $order_in, - '@am_rec' => $amount_in, - '@am_int' => $amount, + echo 'bad'.$signature_in; + watchdog('commerce_robokassa', 'Wrong signature received. %sig_int != %sig_in (request data @data)', array( + '%sig_int' => $md5, + '%sig_in' => $signature_in, + '@data' => $md5string, ), WATCHDOG_ERROR); } - } + } else { - watchdog('commerce_robokassa', 'Wrong signature received. %sig_int != %sig_in (request data @data)', array( - '%sig_int' => $md5, - '%sig_in' => $signature_in, - '@data' => $md5string, - ), WATCHDOG_ERROR); + echo 'bad'.$transaction->transaction_id; + //Transaction does not exist + watchdog('commerce_robokassa', 'There is no transaction or she was paid.', array(), WATCHDOG_ERROR); } - } + } } /** @@ -267,7 +286,7 @@ function commerce_robokassa_result() { * @param $name * The name entered on the submission form. */ -function commerce_robokassa_create_transaction($order_id, $status) { +function commerce_robokassa_create_transaction($order_id, $status, $message) { $order = commerce_order_load($order_id); $payment_method = commerce_payment_method_instance_load($order->data['payment_method']); @@ -282,14 +301,18 @@ function commerce_robokassa_create_transaction($order_id, $status) { //$transaction->amount = $order->commerce_order_total[LANGUAGE_NONE][0]['amount']; //$transaction->currency_code = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code']; + $transaction->amount = $amount; + // @doto Sheck statuses deeper - $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS; - $transaction->message = ''; + $transaction->status = $status; + $transaction->message = $message; $transaction->message_variables = array(); commerce_payment_transaction_save($transaction); // @doto is this actually need? rules_invoke_all('commerce_checkout_complete', $order); + + return $transaction; } /** diff --git a/includes/commerce_robokassa.admin.inc b/includes/commerce_robokassa.admin.inc index 6f684da..5e46707 100644 --- a/includes/commerce_robokassa.admin.inc +++ b/includes/commerce_robokassa.admin.inc @@ -13,6 +13,7 @@ function commerce_robokassa_settings_form($settings = NULL) { 'status' => array('pending'), 'success_message' => '', 'fail_message' => '', + 'offsite_autoredirect' => 'true', ); $form['login'] = array( @@ -45,6 +46,16 @@ function commerce_robokassa_settings_form($settings = NULL) { ), '#default_value' => $settings['server'], ); + + $form['offsite_autoredirect'] = array( + '#type' => 'radios', + '#title' => t('Offsite autoredirect'), + '#options' => array( + 'true' => ('True'), + 'false' => ('False'), + ), + '#default_value' => $settings['offsite_autoredirect'], + ); $form['status'] = array( '#type' => 'select',