<?php


namespace Mehrab\Payment\Gateways\Nextpay;


use Illuminate\Http\Request;
use Mehrab\Payment\Contracts\GatewayContract;
use Mehrab\Payment\Models\Payment;
use Mehrab\Payment\Repositories\PaymentRepo;

class NextpayAdaptor implements GatewayContract
{
    private $invoiceId;
    private $client;

    public function request($amount,$order_id)
    {
        $this->client = new Nextpay();
        $callback_url = route('payment.callback');
        $api_key = "062a9a46-e0bd-43ad-a040-947202027936";
//        $api_key = "********-****-****-****-************";
        $result = $this->client->request($api_key,$amount,$order_id,$callback_url);
        if ($result['code'] == -1){
            $this->invoiceId = $result['trans_id'];
            return $this->invoiceId;
        }else{
            return [
              'status' => $result['code'],
            ];
        }
    }

    public function verify(Payment $payment)
    {

        $api_key = "062a9a46-e0bd-43ad-a040-947202027936";
        $this->client = new Nextpay();
        $result = $this->client->verify($api_key,$payment->paymentable->price,$payment->invoice_id);

        if ($result['code'] == 0 && isset($result['code'])){
            return $result['Shaparak_Ref_Id'];
        }
        return false;
    }

    public function redirect()
    {
      return $this->client->redirect($this->invoiceId);
    }

    public function getName()
    {
        return 'nextpay';
    }

    public function getInvoiceIdFromRequest(Request $request){
        return $request->trans_id;
    }
}
