<?php
namespace front\controls;
class ShopOrder
{
  public static function payment_confirmation()
  {
    global $settings;

    $order = \front\factory\ShopOrder::order_details( null, \Shared\Helpers\Helpers::get( 'order_hash' ) );

    return \Shared\Tpl\Tpl::view( 'shop-order/payment-confirmation', [
              'order' => $order,
              'settings' => $settings
            ] );
  }

  public static function payment_status_tpay()
  {
    global $mdb;

    file_put_contents( 'tpay.txt', print_r( $_POST, true ) . print_r( $_GET, true ), FILE_APPEND );

    if ( \Shared\Helpers\Helpers::get( 'tr_status' ) == 'TRUE' and \Shared\Helpers\Helpers::get( 'tr_crc' ) )
    {
      $order = new \shop\Order( 0, \Shared\Helpers\Helpers::get( 'tr_crc' ) );

      if ( $order -> id )
      {
        $order -> set_as_paid( true );
        $order -> update_status( 4, true );
        echo 'TRUE';
        exit;
      }
    }

    echo 'FALSE';
    exit;
  }

  public static function payment_status_przelewy24pl()
  {
    global $mdb, $settings;

    $post = [
      'p24_merchant_id' => \Shared\Helpers\Helpers::get( 'p24_merchant_id' ),
      'p24_pos_id' => \Shared\Helpers\Helpers::get( 'p24_pos_id' ),
      'p24_session_id' => \Shared\Helpers\Helpers::get( 'p24_session_id' ),
      'p24_amount' => \Shared\Helpers\Helpers::get( 'p24_amount' ),
      'p24_currency' => \Shared\Helpers\Helpers::get( 'p24_currency' ),
      'p24_order_id' => \Shared\Helpers\Helpers::get( 'p24_order_id' ),
      'p24_sign' => md5( \Shared\Helpers\Helpers::get( 'p24_session_id' ) . '|' . \Shared\Helpers\Helpers::get( 'p24_order_id' ) . '|' . \Shared\Helpers\Helpers::get( 'p24_amount' ) . '|' . \Shared\Helpers\Helpers::get( 'p24_currency' ) . '|' . $settings['przelewy24_crc_key'] )
    ];

    $ch = curl_init();
    if ( $settings['przelewy24_sandbox'] )
      curl_setopt( $ch, CURLOPT_URL, 'https://sandbox.przelewy24.pl/trnVerify' );
    if ( !$settings['przelewy24_sandbox'] )
      curl_setopt( $ch, CURLOPT_URL, 'https://secure.przelewy24.pl/trnVerify' );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $post ) );
    $response = curl_exec( $ch );

    $order = new \shop\Order( 0, '', \Shared\Helpers\Helpers::get( 'p24_session_id' ) );

    if ( $order['status'] == 0 and $order['summary'] * 100 == \Shared\Helpers\Helpers::get( 'p24_amount' ) )
    {
      if ( $order['id'] )
      {
        $mdb -> update( 'pp_shop_orders', [ 'status' => 1, 'paid' => 1 ], [ 'id' => $order['id'] ] );
        $mdb -> insert( 'pp_shop_order_statuses', [ 'order_id' => $order['id'], 'status_id' => 1, 'mail' => 1 ] );

        $order -> status = 4;
        $order -> send_status_change_email();
      }
    }

    exit;
  }

  public static function payment_status_hotpay()
  {
    global $mdb, $lang;

    if ( !empty( $_POST["KWOTA"] ) && !empty( $_POST["ID_PLATNOSCI"] ) && !empty( $_POST["ID_ZAMOWIENIA"] ) && !empty( $_POST["STATUS"] ) && !empty( $_POST["SEKRET"] ) && !empty( $_POST["HASH"] ) )
    {
      $order = new \shop\Order( $_POST['ID_ZAMOWIENIA'] );

      if ( $order['id'] )
      {
        if ( is_array( $order['products'] ) and count( $order['products'] ) ):
          foreach ( $order['products'] as $product ):
            $product_tmp = \front\factory\ShopProduct::product_details( $product['product_id'], $lang['id'] );
            $summary_tmp += \Shared\Helpers\Helpers::normalize_decimal( $product['price_netto'] + $product['price_netto'] * $product['vat'] / 100 ) * $product['quantity'];
          endforeach;
          $summary_tmp += $order['transport_cost'];
        endif;

        if ( hash( "sha256", "ProjectPro1916;" . round( $summary_tmp, 2 ) . ";" . $_POST["ID_PLATNOSCI"] . ";" . $_POST["ID_ZAMOWIENIA"] . ";" . $_POST["STATUS"] . ";" . $_POST["SEKRET"] ) == $_POST["HASH"] )
        {
          if ( $_POST["STATUS"] == "SUCCESS" )
          {
            $mdb -> update( 'pp_shop_orders', [ 'status' => 1, 'paid' => 1 ], [ 'id' => $order['id'] ] );
            $mdb -> insert( 'pp_shop_order_statuses', [ 'order_id' => $order['id'], 'status_id' => 1, 'mail' => 1 ] );

            $order -> status = 4;
            $order -> send_status_change_email();

            echo \Shared\Helpers\Helpers::lang( 'zamowienie-zostalo-oplacone' );
          }
          else if ( $_POST["STATUS"] == "FAILURE" )
          {
            $mdb -> update( 'pp_shop_orders', [ 'status' => 2 ], [ 'id' => $order['id'] ] );
            $mdb -> insert( 'pp_shop_order_statuses', [ 'order_id' => $order['id'], 'status_id' => 2, 'mail' => 1 ] );

            $order -> status = 2;
            $order -> send_status_change_email();

            echo \Shared\Helpers\Helpers::lang( 'platnosc-zostala-odrzucona' );
          }
        }
        else
        {
          $mdb -> update( 'pp_shop_orders', [ 'status' => 3 ], [ 'id' => $order['id'] ] );
          $mdb -> insert( 'pp_shop_order_statuses', [ 'order_id' => $order['id'], 'status_id' => 3, 'mail' => 1 ] );

          $order -> status = 3;
          $order -> send_status_change_email();

          echo \Shared\Helpers\Helpers::lang( 'zamowienie-zostalo-oplacone-reczne' );
        }
      }
    }
    exit;
  }

  public static function order_details()
  {
    global $page, $settings;

    $page['language']['meta_title'] = \Shared\Helpers\Helpers::lang( 'meta-title-szczegoly-zamowienia' ) . ' | ' . $settings['firm_name'];
    $order = \front\factory\ShopOrder::order_details(
      \front\factory\ShopOrder::order_id( \Shared\Helpers\Helpers::get( 'order_hash' ) )
    );
    $coupon = (int)$order['coupon_id'] ? new \shop\Coupon( (int)$order['coupon_id'] ) : null;

    return \Shared\Tpl\Tpl::view( 'shop-order/order-details', [
      'order' => $order,
      'coupon' => $coupon,
      'client' => \Shared\Helpers\Helpers::get_session( 'client' ),
      'settings' => $settings
    ] );
  }
}
