<?php

namespace Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

class Payment extends Model
{

    public static function create_link($id,$userid)
    {
        $time = time();
        $data = [
                'pin'    => '887D34F8DB1C1C3DC294',
                'amount'    => DB::table('shop')->where('id', $id)->first()->price,
                'callback' => 'https://jh.ashkancontrol.ir/payment/verify.php?id='.$id."&userid=".$userid,
                'invoice_id' => $userid."_".$id."_".$time,
                'description' => 'خرید از فروشگاه'
                ];

        $data = json_encode($data);
        $ch = curl_init('https://panel.aqayepardakht.ir/api/v2/create');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data))
        );
        
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result);
        if ($result->status == "success") {
            return 'https://panel.aqayepardakht.ir/startpay/' . $result->transid;
        } else {
            return false;
        }
        
    }
    
        public static function verify_payment($id,$trans_id)
    {
        $data = [
                'pin'    => '887D34F8DB1C1C3DC294',
                'amount'    => DB::table('shop')->where('id', $id)->first()->price,
                'transid' => $trans_id
                ];

        $data = json_encode($data);
        $ch = curl_init('https://panel.aqayepardakht.ir/api/v2/verify');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data))
        );
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result);
        if ($result->code == "1") {
        return $result;
        } else {
        return false;
        }
    }

    
}
