Completed
Push — master ( 2e8356...c80ba7 )
by Nikan
02:04 queued 01:04
created

LaravelCafebazaar::verifyPurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
    function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
        
14
        if(empty($this->code)) {  
0 ignored issues
show
Bug introduced by
The property code does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
            throw new Exception("Code not found. run php artisan Cafebazaar code");
16
        }
17
18
        $this->guzzle = new \GuzzleHttp\Client(["base_uri" => "https://pardakht.cafebazaar.ir/devapi/v2/"]);
0 ignored issues
show
Bug introduced by
The property guzzle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        $this->data = $this->getCache();
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        $this->code = $this->getCode();
21
22
        if(empty($this->data)) {
23
            $this->updateToken();
24
        } else {
25
            $this->refreshToken();
26
        }
27
    }
28
29
    protected function updateToken() {
30
        if(empty($this->data)) {
31
            try {
32
                $response = $this->guzzle->post("auth/token/", [
33
                    'form_params' => [
34
                        "grant_type" => 'authorization_code',
35
                        'code' => $this->code,
36
                        "client_id" => config('laravel-cafebazaar.client_id'),
37
                        "client_secret" => config('laravel-cafebazaar.client_secret'),
38
                        "redirect_uri" => config('laravel-cafebazaar.redirect_uri'),    
39
                   ],
40
                ]);
41
                $this->data = json_decode($response->getBody()->getContents());
42
                $this->setCache($this->data);
43
            } catch (RequestException $exception) {
44
                throw $exception;
45
            }
46
        }
47
    }
48
49
    protected function refreshToken() {
50
        try {
51
            if(!isset($this->data->refresh_token)) {
52
                return;
53
            }
54
            $refresh_token = $this->data->refresh_token;
55
            $response = $this->guzzle->post("auth/token/", [
56
                'form_params' => [
57
                    "grant_type" => 'refresh_token',
58
                    "client_id" => config('laravel-cafebazaar.client_id'),
59
                    "client_secret" => config('laravel-cafebazaar.client_secret'),
60
                    "refresh_token" => $refresh_token
61
               ],
62
            ]);
63
            $this->data = json_decode($response->getBody()->getContents());
64
            $this->data->refresh_token = $refresh_token;
65
            $this->setCache($this->data);
66
        } catch (RequestException $exception) {
67
            throw $exception;
68
        }
69
    }    
70
71
    protected function getCache() {
72
        return Cache::get('laravel-cafebazaar');
73
    }
74
75
    protected function setCache($cache) {
76
        Cache::forever('laravel-cafebazaar', $cache);
77
    }
78
79
    protected function getCode() {
80
        return Cache::get('laravel-cafebazaar-code');
81
    }
82
    
83
    public function verifyPurchase($package_id, $product_id, $purchase_token) {
84
        $this->updateToken();
85
        $response = $this->guzzle->get("api/validate/$package_id/inapp/$product_id/purchases/$purchase_token?access_token=".$this->data->access_token);
86
        $data = json_decode($response->getBody()->getContents());
87
        return new CafebazaarPurchase($data);
88
    }
89
90
    public static function handleRedirect(Request $request) {
91
        $code = $request->input('code');
92
        if(!empty($code)) {
93
            Cache::forever('laravel-cafebazaar-code', $code);
94
        }
95
    }
96
}
97