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\PaymentMethodsTokens; |
||
19 | use PayPalAPI\PaymentAuthorizations; |
||
20 | use PayPalAPI\PaymentCaptures; |
||
21 | use PayPalAPI\PaymentRefunds; |
||
22 | use PayPalAPI\Payouts; |
||
23 | use PayPalAPI\ReferencedPayouts; |
||
24 | use PayPalAPI\BillingAgreements; |
||
25 | use PayPalAPI\BillingPlans; |
||
26 | use PayPalAPI\Subscriptions; |
||
27 | use PayPalAPI\Reporting; |
||
28 | use PayPalAPI\WebHooks; |
||
29 | use PayPalAPI\WebHooksVerification; |
||
30 | use PayPalAPI\WebHooksEvents; |
||
31 | |||
32 | /** |
||
33 | * Login through PayPal API to get access token. |
||
34 | * |
||
35 | * @throws \Throwable |
||
36 | * |
||
37 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
38 | * |
||
39 | * @see https://developer.paypal.com/docs/api/get-an-access-token-curl/ |
||
40 | * @see https://developer.paypal.com/docs/api/get-an-access-token-postman/ |
||
41 | */ |
||
42 | public function getAccessToken() |
||
43 | { |
||
44 | $this->apiEndPoint = 'v1/oauth2/token'; |
||
45 | |||
46 | $this->options['auth'] = [$this->config['client_id'], $this->config['client_secret']]; |
||
47 | $this->options[$this->httpBodyParam] = [ |
||
48 | 'grant_type' => 'client_credentials', |
||
49 | ]; |
||
50 | |||
51 | $response = $this->doPayPalRequest(); |
||
52 | |||
53 | unset($this->options['auth']); |
||
54 | unset($this->options[$this->httpBodyParam]); |
||
55 | |||
56 | if (isset($response['access_token'])) { |
||
57 | $this->setAccessToken($response); |
||
58 | } |
||
59 | |||
60 | return $response; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Set PayPal Rest API access token. |
||
65 | * |
||
66 | * @param array $response |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function setAccessToken(array $response) |
||
71 | { |
||
72 | $this->access_token = $response['access_token']; |
||
73 | |||
74 | $this->setPayPalAppId($response); |
||
75 | |||
76 | $this->setRequestHeader('Authorization', "{$response['token_type']} {$this->access_token}"); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Set PayPal App ID. |
||
81 | * |
||
82 | * @param array $response |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | private function setPayPalAppId(array $response) |
||
87 | { |
||
88 | $app_id = empty($response['app_id']) ? $this->config['app_id'] : $response['app_id']; |
||
89 | |||
90 | $this->config['app_id'] = $app_id; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set records per page for list resources API calls. |
||
95 | * |
||
96 | * @param int $size |
||
97 | * |
||
98 | * @return \Srmklive\PayPal\Services\PayPal |
||
99 | */ |
||
100 | public function setPageSize(int $size): \Srmklive\PayPal\Services\PayPal |
||
101 | { |
||
102 | $this->page_size = $size; |
||
0 ignored issues
–
show
|
|||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Set the current page for list resources API calls. |
||
109 | * |
||
110 | * @param int $size |
||
111 | * |
||
112 | * @return \Srmklive\PayPal\Services\PayPal |
||
113 | */ |
||
114 | public function setCurrentPage(int $page): \Srmklive\PayPal\Services\PayPal |
||
115 | { |
||
116 | $this->current_page = $page; |
||
0 ignored issues
–
show
|
|||
117 | |||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Toggle whether totals for list resources are returned after every API call. |
||
123 | * |
||
124 | * @param bool $totals |
||
125 | * |
||
126 | * @return \Srmklive\PayPal\Services\PayPal |
||
127 | */ |
||
128 | public function showTotals(bool $totals): \Srmklive\PayPal\Services\PayPal |
||
129 | { |
||
130 | $this->show_totals = $totals ? 'true' : 'false'; |
||
0 ignored issues
–
show
|
|||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | } |
||
135 |