You are here

function commerce_robokassa_result in Commerce robokassa 7

Same name and namespace in other branches
  1. 7.2 commerce_robokassa.module \commerce_robokassa_result()

Page callback to receive payment notification from robokassa.

1 string reference to 'commerce_robokassa_result'
commerce_robokassa_menu in ./commerce_robokassa.module
Implements hook_menu().

File

./commerce_robokassa.module, line 208

Code

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);
      }
      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,
        ), 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);
    }
  }
}