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() { |
|
|
|
|
13
|
|
|
$this->guzzle = new \GuzzleHttp\Client(["base_uri" => "https://pardakht.cafebazaar.ir/devapi/v2/"]); |
|
|
|
|
14
|
|
|
$this->data = $this->getCache(); |
|
|
|
|
15
|
|
|
$this->code = $this->getCode(); |
|
|
|
|
16
|
|
|
$this->updateToken(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function updateToken() { |
20
|
|
|
|
21
|
|
|
if(empty($this->code)) { |
22
|
|
|
throw new Exception("Code not found. run php artisan Cafebazaar code"); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if(empty($this->data)) { |
26
|
|
|
try { |
27
|
|
|
$response = $this->guzzle->post("auth/token/", [ |
28
|
|
|
'form_params' => [ |
29
|
|
|
"grant_type" => 'authorization_code', |
30
|
|
|
'code' => $this->code, |
31
|
|
|
"client_id" => config('laravel-cafebazaar.client_id'), |
32
|
|
|
"client_secret" => config('laravel-cafebazaar.client_secret'), |
33
|
|
|
"redirect_uri" => config('laravel-cafebazaar.redirect_uri'), |
34
|
|
|
], |
35
|
|
|
]); |
36
|
|
|
$this->data = json_decode($response->getBody()->getContents()); |
37
|
|
|
$this->setCache($this->data); |
38
|
|
|
} catch (RequestException $exception) { |
39
|
|
|
throw $exception; |
40
|
|
|
} |
41
|
|
|
} else { |
42
|
|
|
try { |
43
|
|
|
if(!isset($this->data->refresh_token)) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
$refresh_token = $this->data->refresh_token; |
47
|
|
|
$response = $this->guzzle->post("auth/token/", [ |
48
|
|
|
'form_params' => [ |
49
|
|
|
"grant_type" => 'refresh_token', |
50
|
|
|
"client_id" => config('laravel-cafebazaar.client_id'), |
51
|
|
|
"client_secret" => config('laravel-cafebazaar.client_secret'), |
52
|
|
|
"refresh_token" => $refresh_token |
53
|
|
|
], |
54
|
|
|
]); |
55
|
|
|
$this->data = json_decode($response->getBody()->getContents()); |
56
|
|
|
$this->data->refresh_token = $refresh_token; |
57
|
|
|
$this->setCache($this->data); |
58
|
|
|
} catch (RequestException $exception) { |
59
|
|
|
throw $exception; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getCache() { |
65
|
|
|
return Cache::get('laravel-cafebazaar'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function setCache($cache) { |
69
|
|
|
Cache::forever('laravel-cafebazaar', $cache); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getCode() { |
73
|
|
|
return Cache::get('laravel-cafebazaar-code'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function verifyPurchase($package_id, $product_id, $purchase_token) { |
77
|
|
|
$this->updateToken(); |
78
|
|
|
$response = $this->guzzle->get("api/validate/$package_id/inapp/$product_id/purchases/$purchase_token?access_token=".$this->data->access_token); |
79
|
|
|
$data = json_decode($response->getBody()->getContents()); |
80
|
|
|
return new CafebazaarPurchase($data); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function handleRedirect(Request $request) { |
84
|
|
|
$code = $request->input('code'); |
85
|
|
|
if(!empty($code)) { |
86
|
|
|
Cache::forever('laravel-cafebazaar-code', $code); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.