<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Payment_m extends CI_Model {

	public function paytm_init($slug='',$account_slug='',$price,$type='admin'){
        $data = [];   
        $settings = get_setting(); 
        $u_info = get_all_user_info_slug($slug);
        $package = $this->admin_m->get_package_info_by_slug($account_slug);  
        $paytm = json_decode($settings['paytm_config']);
        $data['order_id'] = 'order_12389'.time();


        $environment = $paytm->is_paytm_live;
        if($environment==0){
            $data['url'] = 'https://securegw-stage.paytm.in';
        }else{
            $data['url'] = 'https://securegw.paytm.in';
        }

        if($type=='admin'){
        	$callback_url = base_url('payment/paytm_verify/?slug='.$u_info['username'].'&account_slug='.$package['slug'].'&key='.$paytm->merchant_key);
        }else{
        	$callback_url = "";
        }

        $params = array(
            'order_id' => $data['order_id'],
            'mid' => $paytm->merchant_id,
            'mik' => $paytm->merchant_key,
            'is_paytm_live' => 0,
            'username' => $slug,
            'account_slug' => $account_slug,
            'callback_url' => $callback_url,
            'price' => $price,
        );
        $token = $this->token($params);
        $data['token']=$token['body']['txnToken'];
        return $data;
    }

    public function token($data){

    /*
    * import checksum generation utility
    * You can get this utility from https://developer.paytm.com/docs/checksum/
    */
    require_once("vendor/paytm/paytmchecksum/PaytmChecksum.php");

    $paytmParams = array();

    $paytmParams["body"] = array(
        "requestType"  => "Payment",
        "mid"  => $data['mid'],
        "websiteName"  => "WEBSTAGING",
        "orderId"  => $data['order_id'],
        "callbackUrl"  => $data['callback_url'],
        "txnAmount"   => array(
            "value"   => $data['price'],
            "currency" => "INR",
        ),
        "userInfo"   => array(
            "custId"  => "CUST_001",
        ),
    );


    /*
    * Generate checksum by parameters we have in body
    * Find your Merchant Key in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys
    */
    $checksum = PaytmChecksum::generateSignature(json_encode($paytmParams["body"], JSON_UNESCAPED_SLASHES), $data['mik']);

    $paytmParams["head"] = array(
        "signature" => $checksum
    );

    $post_data = json_encode($paytmParams, JSON_UNESCAPED_SLASHES);
    if($data['is_paytm_live']==0):
        /* for Staging */
        $url = "https://securegw-stage.paytm.in/theia/api/v1/initiateTransaction?mid={$data['mid']}&orderId={$data['order_id']}";
    else:
        /* for Production */
        $url = "https://securegw.paytm.in/theia/api/v1/initiateTransaction?mid={$data['mid']}&orderId={$data['order_id']}";
    endif;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

    $response = curl_exec($ch);
    return json_decode($response,true);



}

public function mercado_init($slug,$account_slug,$price){
	$data = [];
	$u_info = get_all_user_info_slug($slug);
	$package = $this->admin_m->get_package_info_by_slug($account_slug);
	$settings = get_setting();
	$mercado = !empty($settings['mercado_config'])?json_decode($settings['mercado_config']):'';

	MercadoPago\SDK::setAccessToken($mercado->access_token);
	$preference = new MercadoPago\Preference();
    // Create a preference item
	$item = new MercadoPago\Item();
	$item->title = $package['type_name'];
	$item->quantity = 1;
	$item->unit_price = $price;
	$item->currency_id = get_currency('currency_code');
	$preference->items = array($item);
	$preference->back_urls = array(
		"success" => base_url("payment/mercado?slug={$slug}&account_slug={$account_slug}"),
		"failure" => base_url("payment/mercado?slug={$slug}&account_slug={$account_slug}"),
		"pending" => base_url("payment/mercado?slug={$slug}&account_slug={$account_slug}")
	);
	$preference->auto_return = "approved";

	$preference->save();
	$data['f_id'] = $preference->id;
	$data['init'] = $preference->init_point;
	return $data;
}



}

/* End of file Payment_m.php */
/* Location: ./application/models/Payment_m.php */