<?php

namespace App\Http\Controllers;

use App\Models\Deposit;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    public function checkcbi()
    {
        $post = array(
            'trader' =>
            (object) array(
                'label' => 'finanza',
                'amount' => '10',
                'depoid' => 45,
                'custom2' => 'payment',
                'callback_url' => 'https://crm.finanza-invest.com/dopayment',
            ),
            'transaction' =>
            array(
                'id' => 'GP103069',
                'currency' => 'EUR',
                'currency_amount' => 10.0,
                'crypto' => 'BTC',
                'crypto_amount' => '0.00107424',
                'completed_at' => '2020-07-27 08:13:36',
            ),
            'status' => 'Declined',
            'user' =>
            array(
                'first_name' => 'Bill',
                'last_name' => 'Zolti',
                'email' => 'support@allfinancialmarket.com',
            ),
            'error_message' => 'Issuer country in blacklist',
            'card_last_four' => '1111',
            'buy_percentage_fee' => '6',
        );
        dd($post);
    }

    public function dopayment(Request $request)
    {
        //$this->makeconnection();

        if ($request['status'] == 'Completed' || $request['status'] == 'completed' || $request['status'] == 'Success') {

            $checkdepo = Deposit::where('id', $request['trader']['depoid'])->first();
            $comnt = 'deposit-allglobal';

            //   $url = $this->apicall.'MAKE_DEPOIST_BALANCE?MT4Account='.$checkdepo->mt4account.'&Amount='.$request->amount.'&Comment='.$comnt.'&ManagerIndex='.$this->manager;
            $postfields = array(
                'method' => 'Deposit',
                'key' => '0001',
                'array' =>
                array(
                    0 =>
                    array(
                        'index' => 0,
                        'accountID' => $checkdepo->mt4account,
                        'comment' => $comnt,
                        'amount' => $request['trader']['depoid'],
                    ),
                ),
            );
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "http://jenya.securedns.us:3001/capitalmanageinv",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($postfields),
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: text/plain"
                ),
            ));
            $response = curl_exec($curl);
            curl_close($curl);
            $getresult = json_decode($response);

            if ($getresult->applicationStatus == 'OK') {
                Deposit::where('id', $request['trader']['depoid'])->update(array(
                    'displaystatus'     => '1',
                ));
            }
        }
    }
}
