Showing
2 changed files
with
22 additions
and
28 deletions
| ... | @@ -20,7 +20,7 @@ class PayController extends Controller | ... | @@ -20,7 +20,7 @@ class PayController extends Controller |
| 20 | 20 | ||
| 21 | $order = Order::query()->where('order_sn', $order_sn)->first(); | 21 | $order = Order::query()->where('order_sn', $order_sn)->first(); |
| 22 | 22 | ||
| 23 | - if ($order->status !== Order::UNPAID) return false; | 23 | + if ($order->status != Order::UNPAID) return Response::fail('订单错误!'); |
| 24 | 24 | ||
| 25 | // if ($order->pay_amount <= 0) return $this->paid($order_sn); 0元购应该单独写一套 | 25 | // if ($order->pay_amount <= 0) return $this->paid($order_sn); 0元购应该单独写一套 |
| 26 | 26 | ... | ... |
| ... | @@ -35,33 +35,27 @@ class PaypalPayment implements PaymentInterface | ... | @@ -35,33 +35,27 @@ class PaypalPayment implements PaymentInterface |
| 35 | 35 | ||
| 36 | public function __construct() | 36 | public function __construct() |
| 37 | { | 37 | { |
| 38 | - try{ | 38 | + // 初始化时做一些准备工作 |
| 39 | - // 初始化时做一些准备工作 | 39 | + $redis = Redis::connection(); |
| 40 | - $redis = Redis::connection(); | 40 | + $access_token = $redis->get('paypal:access_token'); |
| 41 | - $access_token = $redis->get('paypal:access_token'); | 41 | + if ($access_token){ |
| 42 | - if ($access_token){ | 42 | + $this->accessToken = $access_token; |
| 43 | - Log::channel('daily')->debug($access_token); | 43 | + }else{ |
| 44 | - $this->accessToken = $access_token; | 44 | + $client = new Client([ |
| 45 | - }else{ | 45 | + 'base_uri' => $this->baseUrlSandbox, |
| 46 | - $client = new Client([ | 46 | + 'headers'=>[ |
| 47 | - 'base_uri' => $this->baseUrlSandbox, | 47 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
| 48 | - 'headers'=>[ | 48 | + 'Accept'=>'application/json', |
| 49 | - 'Content-Type' => 'application/x-www-form-urlencoded', | 49 | + ] |
| 50 | - 'Accept'=>'application/json', | 50 | + ]); |
| 51 | - ] | 51 | + $response = $client->post('/v1/oauth2/token',[ |
| 52 | - ]); | 52 | + 'form_params'=>['grant_type' => 'client_credentials'], |
| 53 | - $response = $client->post('/v1/oauth2/token',[ | 53 | + 'auth' => [$this->clientId, $this->secret], |
| 54 | - 'form_params'=>['grant_type' => 'client_credentials'], | 54 | + ]); |
| 55 | - 'auth' => [$this->clientId, $this->secret], | 55 | + $body = $response->getBody(); |
| 56 | - ]); | 56 | + $content = json_decode($body->getContents(),true); |
| 57 | - $body = $response->getBody(); | 57 | + $this->accessToken = $content['access_token']; |
| 58 | - $content = json_decode($body->getContents(),true); | 58 | + $redis->setex('paypal:access_token',$content['expires_in'],$content['access_token']); |
| 59 | - $this->accessToken = $content['access_token']; | ||
| 60 | - Log::channel('daily')->debug($content['access_token']); | ||
| 61 | - $redis->setex('paypal:access_token',$content['expires_in'],$content['access_token']); | ||
| 62 | - } | ||
| 63 | - }catch (\Exception $exception){ | ||
| 64 | - Log::channel('daily')->error($exception->getMessage()); | ||
| 65 | } | 59 | } |
| 66 | } | 60 | } |
| 67 | 61 | ... | ... |
-
Please register or login to post a comment