|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nikandlv\LaravelCafebazaar; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
class LaravelCafebazaar { |
|
10
|
|
|
|
|
11
|
|
|
function __construct() { |
|
|
|
|
|
|
12
|
|
|
$this->guzzle = new \GuzzleHttp\Client(["base_uri" => "https://pardakht.cafebazaar.ir/devapi/v2/"]); |
|
|
|
|
|
|
13
|
|
|
$this->data = $this->getCache(); |
|
|
|
|
|
|
14
|
|
|
$this->updateToken(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
protected function updateToken() { |
|
18
|
|
|
|
|
19
|
|
|
if(empty($this->code)) { |
|
|
|
|
|
|
20
|
|
|
throw new Exception("Code not found. run php artisan Cafebazaar code"); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if(empty($this->data)) { |
|
24
|
|
|
$response = $this->guzzle->post("auth/token", [ |
|
25
|
|
|
"grant_type" => 'authorization_code', |
|
26
|
|
|
'code' => $this->code, |
|
27
|
|
|
"client_id" => config('laravel-cafebazaar.client_id'), |
|
28
|
|
|
"client_secret" => config('laravel-cafebazaar.client_secret'), |
|
29
|
|
|
"redirect_uri" => config('laravel-cafebazaar.redirect_uri'), |
|
30
|
|
|
]); |
|
31
|
|
|
$this->data = $response->getBody(); |
|
32
|
|
|
setCache($this->data); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function getCache() { |
|
37
|
|
|
return Cache::get('laravel-cafebazaar'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function setCache($cache) { |
|
41
|
|
|
Cache::put('laravel-cafebazaar', $cache, 60); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function getCode() { |
|
45
|
|
|
return Cache::get('laravel-cafebazaar-code'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function verifyPurchase($package_id, $product_id, $purchase_token) { |
|
49
|
|
|
$this->updateToken(); |
|
50
|
|
|
$data = $this->guzzle->get("api/validate/$package_id/inapp/$product_id/purchases/$purchase_token"); |
|
51
|
|
|
return new CafebazaarPurchase($data); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function handleRedirect(Request $request) { |
|
55
|
|
|
$code = $request->input('code'); |
|
56
|
|
|
if(!empty($code)) { |
|
57
|
|
|
Cache::forever('laravel-cafebazaar-code', $code); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
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.