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