<?php

function razorpayForm($order_res) {
  $CI = & get_instance();

  $productinfo = 'Accessories';
  $txnid = $order_res['order_id'];
  $key_id = RAZOR_KEY_ID;
  $currency_code = 'INR';
  $total = (($order_res['total_amount']  - $order_res['coupon_discount_amount'])*100);
  $amount = (($order_res['total_amount']  - $order_res['coupon_discount_amount'])*100);
  $merchant_order_id = $order_res['order_id'];
  $card_holder_name = $order_res['billing_name'];
  $email = $order_res['email'];
  $phone = $order_res['billing_phone'];
  $name = $order_res['first_name'];
  $return_url = site_url() . 'payment/callback/'.$merchant_order_id;


  //$amount = $order_res['total_amount'] + $order_res['shipping_amount'];
  //$orderid = $order_res['order_id'];
  //$order_no = $order_res['invoice_number'];
  ?>
  <body style="margin:0px;">    
    <div style="text-align:left; font-size: 22px; font-weight:bold;  background-color:#f9f9f9; border:#efefef; padding:20px;"><?php echo $CI->config->item('site_name'); ?></div>
    <table style="width: 100%;">
      <tr>
        <td width="100%" align="center">
          <div style="font-family: Arial; font-size: 16px; text-align: center; margin-top: 170px; background-color:#f9f9f9; border:#efefef; padding:20px; font-weight:bold; width:500px;"> 
            <?php echo "We are just transfering you to the RazorPay in few seconds"; ?><br />  <br />
            <div style="width: 200px; margin-left:180px; text-align: left;  font-family: Arial; font-size: 22px; color:#090;">Please wait <span id="loading_please_wait"></span></div> 
          </div> 
        </td>
      </tr>
      <tr>     
        <td width="100%" align="center">
        </td>
      </tr>
    </table>
    <div class="row">
      <div class="col-lg-12">
        <?php if (!empty($CI->session->flashdata('msg'))) { ?>
          <div class="alert alert-success">
            <?php echo $CI->session->flashdata('msg'); ?>
          </div>        
        <?php } ?>
        <?php if (validation_errors()) { ?>
          <div class="alert alert-danger">
            <?php echo validation_errors(); ?>
          </div>
        <?php } ?>
      </div>
    </div>
    <form name="razorpay-form" id="razorpay-form" action="<?php echo $return_url; ?>" method="POST">
      <input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id" />
      <input type="hidden" name="merchant_order_id" id="merchant_order_id" value="<?php echo $merchant_order_id; ?>"/>
      <input type="hidden" name="merchant_trans_id" id="merchant_trans_id" value="<?php echo $txnid; ?>"/>
      <input type="hidden" name="merchant_product_info_id" id="merchant_product_info_id" value="<?php echo $productinfo; ?>"/>

      <input type="hidden" name="merchant_surl_id" id="merchant_surl_id" value="<?php echo base_url(); ?>payment/success/<?php echo $merchant_order_id; ?>"/>

      <input type="hidden" name="merchant_furl_id" id="merchant_furl_id" value="<?php echo base_url(); ?>payment/failed/<?php echo $merchant_order_id; ?>"/>

      <input type="hidden" name="card_holder_name_id" id="card_holder_name_id" value="<?php echo $card_holder_name; ?>"/>
      <input type="hidden" name="merchant_total" id="merchant_total" value="<?php echo $total; ?>"/>
      <input type="hidden" name="merchant_amount" id="merchant_amount" value="<?php echo $amount; ?>"/>
      <input type="hidden" name="payment_capture" id="payment_capture" value="1"/>
    </form>

    <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
    <script>
      var razorpay_options = {
        key: "<?php echo $key_id; ?>",
        amount: "<?php echo $total; ?>",
        name: "<?php echo $name; ?>",
        description: "Order # <?php echo $merchant_order_id; ?>",
        netbanking: true,
        currency: "<?php echo $currency_code; ?>",
        payment_capture: 1,
        callback_url:'<?php echo $return_url; ?>',
        redirect:true,
        prefill: {
          name: "<?php echo $card_holder_name; ?>",
          email: "<?php echo $email; ?>",
          contact: "<?php echo $phone; ?>"
        }, 
        notes: {
          soolegal_order_id: "<?php echo $merchant_order_id; ?>",
        },
        handler: function (transaction) {
          document.getElementById('razorpay_payment_id').value = transaction.razorpay_payment_id;
          document.getElementById('razorpay-form').submit();
        },
        "modal": {
          "ondismiss": function () {
            // location.reload()
            window.location.href="<?php echo base_url().'payment/thanks/'.md5($txnid); ?>";
          }
        }
      };
      var razorpay_submit_btn, razorpay_instance;

      function razorpaySubmit(el) {
        if (typeof Razorpay == 'undefined') {
          setTimeout(razorpaySubmit, 200);
          if (!razorpay_submit_btn && el) {
            razorpay_submit_btn = el;
            el.disabled = true;
            el.value = 'Please wait...';
          }
        } else {
          if (!razorpay_instance) {
            razorpay_instance = new Razorpay(razorpay_options);
            if (razorpay_submit_btn) {
              razorpay_submit_btn.disabled = false;
              razorpay_submit_btn.value = "Pay Now";
            }
          }
          razorpay_instance.open();
        }
      }
      
      razorpaySubmit();
    </script>
    <?php
    die();
  }
  ?>