ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+...383-7(-.+



-%%--------------------------------------------------ÿÀ  ·" ÿÄ               ÿÄ 6     !1AQa"q‘±ð¡ÁÑ2Bá#Rb‚ñr ÿÄ              ÿÄ $         !1A2Qaq‘Ñ"ÿÚ   ? óª4¦ù‡;ýÔ­aH6’
¯ëxòPº°pp-LæsÊÿ DÖ7:,ûž«ðwZÃ›-FÈ¨Øðf:‹ß¦šè¸*¹Û”Énó¯¿Âùó†ñ&¶£AiÌ39Ž‡ÙÛ”Ï.«ÜþÆ6³§PT$‚5ê‹žæçKw±Ñµ£¡û÷©a5¡9tDB„ÀAB B„€Lsá<¨ª±*p|à¹Þ8 ÜÅÿ 
C!sÜK"=Jæòï¾—Æ9í‰ŒÄNl×n¹ü[HÒ;÷W¸…Fîá ¼÷ôÕbbñRH6ëÓªÆZÒ¦*¤´óßÞû,ªµãen«š	ÓQ§—%B¸ßšèÌˆèÃˆ(UK«ù‰õ5L™ Æý#ïeàCˆÍ—cßèµx/Ä_,¹ ÎäLÍï~~¥?ê?1¡Ìs€ {·íÔÿ z—šŠ~sgsV8_þZ•n2‡7ôé•àë¾ ž–^³ð§¡@‡Ó€hÜÄÃB¼9ôCC^Òàóvº\"Ý ºßáßâ˜ÐÆºCt%½¬£c~ž«:Íû_å}ó†ŠV¯8øsãºn
ùà´ÿ ÚÇY]ßâ¬&›ƒ‚Öwß¬ë<]BªÀB€„ !	                                                                                                                                                                       
ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+...383-7(-.+



-%%--------------------------------------------------ÿÀ  ·" ÿÄ               ÿÄ 6     !1AQa"q‘±ð¡ÁÑ2Bá#Rb‚ñr ÿÄ              ÿÄ $         !1A2Qaq‘Ñ"ÿÚ   ? óª4¦ù‡;ýÔ­aH6’
¯ëxòPº°pp-LæsÊÿ DÖ7:,ûž«ðwZÃ›-FÈ¨Øðf:‹ß¦šè¸*¹Û”Énó¯¿Âùó†ñ&¶£AiÌ39Ž‡ÙÛ”Ï.«ÜþÆ6³§PT$‚5ê‹žæçKw±Ñµ£¡û÷©a5¡9tDB„ÀAB B„€Lsá<¨ª±*p|à¹Þ8 ÜÅÿ 
C!sÜK"=Jæòï¾—Æ9í‰ŒÄNl×n¹ü[HÒ;÷W¸…Fîá ¼÷ôÕbbñRH6ëÓªÆZÒ¦*¤´óßÞû,ªµãen«š	ÓQ§—%B¸ßšèÌˆèÃˆ(UK«ù‰õ5L™ Æý#ïeàCˆÍ—cßèµx/Ä_,¹ ÎäLÍï~~¥?ê?1¡Ìs€ {·íÔÿ z—šŠ~sgsV8_þZ•n2‡7ôé•àë¾ ž–^³ð§¡@‡Ó€hÜÄÃB¼9ôCC^Òàóvº\"Ý ºßáßâ˜ÐÆºCt%½¬£c~ž«:Íû_å}ó†ŠV¯8øsãºn
ùà´ÿ ÚÇY]ßâ¬&›ƒ‚Öwß¬ë<]BªÀB€„ !	                                                                                                                                                                       
<?php

namespace App\Traits;

use Exception;
use Illuminate\Support\Facades\Http;

trait Bkash
{
    protected function getBaseURL($mode = "test")
    {
        return $mode == 'test' ? "https://tokenized.sandbox.bka.sh/v1.2.0-beta/tokenized/checkout" : "https://tokenized.pay.bka.sh/v1.2.0-beta/tokenized/checkout";
    }

    protected function sentHttpRequestToBkash($token, $appKey, $uri, $data)
    {
        return Http::withHeaders([
            'Content-Type' => 'application/json',
            'Authorization' => $token,
            'X-APP-Key' => $appKey,
        ])
            ->acceptJson()
            ->post($uri, $data);
    }

    protected function getToken($gateway)
    {
        throw_if(empty($gateway->parameters->username ?? "") || empty($gateway->parameters->password ?? "") || empty($gateway->parameters->app_key ?? "") || empty($gateway->parameters->app_secret ?? ""), "Unable to process with bKash.");

        $uri = $this->getBaseURL($gateway->environment) . "/token/grant";
        $data = [
            'app_key' => $gateway->parameters->app_key,
            'app_secret' => $gateway->parameters->app_secret,
        ];

        $response = Http::withHeaders([
            'Content-Type' => 'application/json',
            'username' => $gateway->parameters->username,
            'password' => $gateway->parameters->password,
        ])
            ->acceptJson()
            ->post($uri, $data);

        if ($response->status() == 200) {
            $bkashResponse = json_decode($response->body());
            throw_if(!isset($bkashResponse->id_token), "Something went wrong while trying to pay with bKash.");

            $token = $bkashResponse->id_token;
            return $token;
        }
        throw new Exception("Something went wrong while trying to pay with bKash.");
    }

    public function executePayment($paymentID, $gateway)
    {
        throw_if(empty($gateway->parameters->app_key ?? "") || empty($gateway->parameters->app_secret ?? ""), "Unable to process with bKash.");
        $token = $this->getToken($gateway);
        $uri = $this->getBaseURL($gateway->environment) . "/execute";
        $data = array(
            'paymentID' => $paymentID
        );
        $response = $this->sentHttpRequestToBkash($token, $gateway->parameters->app_key, $uri, $data);

        if ($response->status() == 200) {
            $bkashResponse = json_decode($response->body());
            return $bkashResponse;
        }
        throw new Exception("Something went wrong while trying to verify your payment with bKash.");
    }

    public function queryPayment($paymentID, $gateway)
    {
        throw_if(empty($gateway->parameters->app_key ?? "") || empty($gateway->parameters->app_secret ?? ""), "Unable to process with bKash.");
        $token = $this->getToken($gateway);
        $uri = $this->getBaseURL($gateway->environment) . "/payment/status";
        $data = array(
            'paymentID' => $paymentID
        );
        $response = $this->sentHttpRequestToBkash($token, $gateway->parameters->app_key, $uri, $data);
        if ($response->status() == 200) {
            $bkashResponse = json_decode($response->body());
            return $bkashResponse;
        }
        throw new Exception("Something went wrong while trying to verify your payment with bKash.");
    }

    public function updateAndMessage($deposit, $note, $msg)
    {
        $deposit->update([
            'status' => 3,
            'note' => $note
        ]);
        $data['status'] = 'error';
        $data['msg'] = $msg;
        $data['redirect'] = route('failed');
        return $data;
    }
}
