|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait PayPalAPI |
|
6
|
|
|
{ |
|
7
|
1 |
|
use PayPalAPI\Trackers; |
|
8
|
|
|
use PayPalAPI\CatalogProducts; |
|
9
|
|
|
use PayPalAPI\Disputes; |
|
10
|
|
|
use PayPalAPI\DisputesActions; |
|
11
|
|
|
use PayPalAPI\Invoices; |
|
12
|
|
|
use PayPalAPI\InvoicesSearch; |
|
13
|
|
|
use PayPalAPI\InvoicesTemplates; |
|
14
|
|
|
use PayPalAPI\PaymentAuthorizations; |
|
15
|
|
|
use PayPalAPI\PaymentCaptures; |
|
16
|
|
|
use PayPalAPI\PaymentRefunds; |
|
17
|
|
|
use PayPalAPI\BillingPlans; |
|
18
|
|
|
use PayPalAPI\Subscriptions; |
|
19
|
|
|
use PayPalAPI\Reporting; |
|
20
|
|
|
use PayPalAPI\WebHooks; |
|
21
|
|
|
use PayPalAPI\WebHooksVerification; |
|
22
|
|
|
use PayPalAPI\WebHooksEvents; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Login through PayPal API to get access token. |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \Throwable |
|
28
|
|
|
* |
|
29
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
30
|
|
|
* |
|
31
|
|
|
* @see https://developer.paypal.com/docs/api/get-an-access-token-curl/ |
|
32
|
|
|
* @see https://developer.paypal.com/docs/api/get-an-access-token-postman/ |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getAccessToken() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->apiEndPoint = 'v1/oauth2/token'; |
|
|
|
|
|
|
37
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->options['auth'] = [$this->config['client_id'], $this->config['client_secret']]; |
|
|
|
|
|
|
40
|
|
|
$this->options[$this->httpBodyParam] = [ |
|
41
|
|
|
'grant_type' => 'client_credentials', |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
1 |
|
$response = $this->doPayPalRequest(); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
if (isset($response['access_token'])) { |
|
47
|
|
|
$this->setAccessToken($response); |
|
48
|
|
|
|
|
49
|
|
|
$this->setPayPalAppId($response); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $response; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set PayPal Rest API access token. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $response |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setAccessToken($response) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->access_token = $response['access_token']; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->options['headers']['Authorization'] = "{$response['token_type']} {$this->access_token}"; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set PayPal App ID. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $response |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
private function setPayPalAppId($response) |
|
77
|
|
|
{ |
|
78
|
|
|
if (empty($this->config['app_id'])) { |
|
79
|
|
|
$this->config['app_id'] = $response['app_id']; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|