<?php

namespace App\Controllers;

class DigiCallBack extends BaseController
{
  public function cardAccount(){
      $raw_json = file_get_contents('php://input');
      $in = json_decode($raw_json, true);
      $out =[];
      //$this-> logCallback($in);

      log_message('critical', "DigiCallBack:: END =============================================================================== " );
      $inEvent  =  $in["event"];
      switch($inEvent){
          case 'subscription.create':
              log_message('critical', "DigiCallBack::EVENT ${inEvent}" );
              $saveArray["event"]   = $inEvent;
              $saveArray["customer_code"]    =   $in["data"]["customer"]["customer_code"];
              $saveArray["customer_email"]    =   $in["data"]["customer"]["email"];
              $saveArray["plan_name"]    =   $in["data"]["plan"]["name"];
              $saveArray["plan_code"]    =   $in["data"]["plan"]["plan_code"];
              $saveArray["subscription_code"]    =   $in["data"]["subscription_code"];
              $saveArray["amount"]    =   $in["data"]["amount"];
              $saveArray["authorization_code"]   =   $in["data"]["authorization"]["authorization_code"];
              $saveArray["data"] =  serialize( $in["data"]) ;
              $addedData = $this->insert_db('subscriptions', $saveArray);
              break;

          case 'charge.success':
              log_message('critical', "DigiCallBack::EVENT ${inEvent}" );
              $saveArray["event"]   = $inEvent;
              $saveArray["customer_code"]   =   $in["data"]["customer"]["customer_code"];
              $saveArray["customer_email"]   =   $in["data"]["customer"]["email"];
              $saveArray["plan_name"]   =   $in["data"]["plan"]["name"];
              $saveArray["plan_code"]   =   $in["data"]["plan"]["plan_code"];
              $saveArray["subscription_code"]   =   $in["data"]["subscription_code"];
              $saveArray["amount"]   =   $in["data"]["amount"];
              $saveArray["authorization_code"]  =   $in["data"]["authorization"]["authorization_code"];
              $saveArray["gateway_response"] =  $in["data"]["gateway_response"];
              $saveArray["gateway_status"] =  $in["data"]["status"];
              $saveArray["reference"] =  $in["data"]["reference"];
              $saveArray["data"] =  serialize( $in["data"]) ;
              $addedData = $this->insert_db('charges', $saveArray);
              break;

      }


//        $arr = array(1, 2, 3, 4);
//foreach ($arr as &$value) {
//    $value = $value * 2;
//}

      return  []; //json_encode( $final_out );
  }

  private function logCallback($in){
      log_message('critical', "DigiCallBack:: START =============================================================================== " );
      $inKeys = array_keys($in);
      foreach ($inKeys as $item){
          log_message('critical', "ITEM *  ${item}  ---------- ". serialize( $in[$item]) );
          if (is_array($in[$item])){
              foreach (array_keys($in[$item]) as $item1){
                  log_message('critical', "  ITEM1 **  ${item1} ------- ". serialize( $in[$item][$item1]) );
                  if (is_array($in[$item][$item1])){
                      $arr2 = $in[$item][$item1];
                      foreach (array_keys($arr2) as $item2){
                          log_message('critical', "     ITEM2 ***  ${item2} ---- ". serialize($arr2[$item2]) );
                      }
                  }
              }
          }
      }
      return 0;
  }
}