LaravelCafebazaar::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Nikandlv\LaravelCafebazaar;
4
5
use Exception;
6
use GuzzleHttp\Exception\RequestException;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Http\Request;
9
10
class LaravelCafebazaar {
11
12
    protected $data;
13
    protected $code;
14
    protected $guzzle;
15
    
16
    public function __construct() {
17
        
18
        if(empty($this->code)) {  
19
            throw new Exception("Code not found. run php artisan Cafebazaar code");
20
        }
21
22
        $this->guzzle = new \GuzzleHttp\Client(["base_uri" => "https://pardakht.cafebazaar.ir/devapi/v2/"]);
23
        $this->data = $this->getCache();
24
        $this->code = $this->getCode();
25
26
        if(empty($this->data)) {
27
            $this->updateToken();
28
        } else {
29
            $this->refreshToken();
30
        }
31
    }
32
33
    protected function updateToken() {
34
        if(empty($this->data)) {
35
            try {
36
                $response = $this->guzzle->post("auth/token/", [
37
                    'form_params' => [
38
                        "grant_type" => 'authorization_code',
39
                        'code' => $this->code,
40
                        "client_id" => config('laravel-cafebazaar.client_id'),
41
                        "client_secret" => config('laravel-cafebazaar.client_secret'),
42
                        "redirect_uri" => config('laravel-cafebazaar.redirect_uri'),    
43
                   ],
44
                ]);
45
                $this->data = json_decode($response->getBody()->getContents());
46
                $this->setCache($this->data);
47
            } catch (RequestException $exception) {
48
                throw $exception;
49
            }
50
        }
51
    }
52
53
    protected function refreshToken() {
54
        try {
55
            if(!isset($this->data->refresh_token)) {
56
                return;
57
            }
58
            $refresh_token = $this->data->refresh_token;
59
            $response = $this->guzzle->post("auth/token/", [
60
                'form_params' => [
61
                    "grant_type" => 'refresh_token',
62
                    "client_id" => config('laravel-cafebazaar.client_id'),
63
                    "client_secret" => config('laravel-cafebazaar.client_secret'),
64
                    "refresh_token" => $refresh_token
65
               ],
66
            ]);
67
            $this->data = json_decode($response->getBody()->getContents());
68
            $this->data->refresh_token = $refresh_token;
69
            $this->setCache($this->data);
70
        } catch (RequestException $exception) {
71
            throw $exception;
72
        }
73
    }    
74
75
    protected function getCache() {
76
        return Cache::get('laravel-cafebazaar');
77
    }
78
79
    protected function setCache($cache) {
80
        Cache::forever('laravel-cafebazaar', $cache);
81
    }
82
83
    protected function getCode() {
84
        return Cache::get('laravel-cafebazaar-code');
85
    }
86
    
87
    public function verifyPurchase($package_id, $product_id, $purchase_token) {
88
        $this->updateToken();
89
        try {
90
            $response = $this->guzzle->get("api/validate/$package_id/inapp/$product_id/purchases/$purchase_token?access_token=".$this->data->access_token);
91
            $data = json_decode($response->getBody()->getContents());
92
            return new CafebazaarPurchase($data);
93
        } catch(Exception $exception) {
94
            return new CafebazaarPurchase(new \stdClass());
95
        }
96
    }
97
98
    public static function handleRedirect(Request $request) {
99
        $code = $request->input('code');
100
        if(!empty($code)) {
101
            Cache::forever('laravel-cafebazaar-code', $code);
102
        }
103
    }
104
}
105