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