Showing
1 changed file
with
28 additions
and
24 deletions
... | @@ -14,6 +14,7 @@ use App\Models\User; | ... | @@ -14,6 +14,7 @@ use App\Models\User; |
14 | use App\Models\UserProfile; | 14 | use App\Models\UserProfile; |
15 | use Carbon\Carbon; | 15 | use Carbon\Carbon; |
16 | use GuzzleHttp\Client; | 16 | use GuzzleHttp\Client; |
17 | +use Illuminate\Support\Facades\Log; | ||
17 | use Illuminate\Support\Facades\Redis; | 18 | use Illuminate\Support\Facades\Redis; |
18 | 19 | ||
19 | class PaypalPayment implements PaymentInterface | 20 | class PaypalPayment implements PaymentInterface |
... | @@ -34,29 +35,32 @@ class PaypalPayment implements PaymentInterface | ... | @@ -34,29 +35,32 @@ class PaypalPayment implements PaymentInterface |
34 | 35 | ||
35 | public function __construct() | 36 | public function __construct() |
36 | { | 37 | { |
37 | - // 初始化时做一些准备工作 | 38 | + try{ |
38 | - $redis = Redis::connection(); | 39 | + // 初始化时做一些准备工作 |
39 | - $access_token = $redis->get('paypal:access_token'); | 40 | + $redis = Redis::connection(); |
40 | - if ($access_token){ | 41 | + $access_token = $redis->get('paypal:access_token'); |
41 | - $this->accessToken = $access_token; | 42 | + if ($access_token){ |
42 | - }else{ | 43 | + $this->accessToken = $access_token; |
43 | - $client = new Client([ | 44 | + }else{ |
44 | - 'base_uri' => $this->baseUrlSandbox, | 45 | + $client = new Client([ |
45 | - 'headers'=>[ | 46 | + 'base_uri' => $this->baseUrlSandbox, |
46 | - 'Content-Type' => 'application/x-www-form-urlencoded', | 47 | + 'headers'=>[ |
47 | - 'Accept'=>'application/json', | 48 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
48 | - ] | 49 | + 'Accept'=>'application/json', |
49 | - ]); | 50 | + ] |
50 | - | 51 | + ]); |
51 | - $response = $client->post('/v1/oauth2/token',[ | 52 | + $response = $client->post('/v1/oauth2/token',[ |
52 | - 'form_params'=>['grant_type' => 'client_credentials'], | 53 | + 'form_params'=>['grant_type' => 'client_credentials'], |
53 | - 'auth' => [$this->clientId, $this->secret], | 54 | + 'auth' => [$this->clientId, $this->secret], |
54 | - ]); | 55 | + ]); |
55 | - $body = $response->getBody(); | 56 | + $body = $response->getBody(); |
56 | - $content = json_decode($body->getContents(),true); | 57 | + $content = json_decode($body->getContents(),true); |
57 | - $this->accessToken = $content['access_token']; | 58 | + $this->accessToken = $content['access_token']; |
58 | - | 59 | + |
59 | - $redis->setex('paypal:access_token',$content['expires_in'],$content['access_token']); | 60 | + $redis->setex('paypal:access_token',$content['expires_in'],$content['access_token']); |
61 | + } | ||
62 | + }catch (\Exception $exception){ | ||
63 | + Log::channel('daily')->error($exception->getMessage()); | ||
60 | } | 64 | } |
61 | } | 65 | } |
62 | 66 | ||
... | @@ -98,7 +102,7 @@ class PaypalPayment implements PaymentInterface | ... | @@ -98,7 +102,7 @@ class PaypalPayment implements PaymentInterface |
98 | } | 102 | } |
99 | 103 | ||
100 | public function prepare(Order $order) | 104 | public function prepare(Order $order) |
101 | - {return $this->accessToken; | 105 | + { |
102 | // 在PayPal上创建一个订单,它会返回一个订单对象,它有一个订单id | 106 | // 在PayPal上创建一个订单,它会返回一个订单对象,它有一个订单id |
103 | $client = new Client([ | 107 | $client = new Client([ |
104 | 'base_uri' => $this->baseUrlSandbox, | 108 | 'base_uri' => $this->baseUrlSandbox, | ... | ... |
-
Please register or login to post a comment