PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Modules / PaymentMethods / PayPalGateway / API / PayPalPartner.php

PayPalPartner.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.5, at app/Modules/PaymentMethods/PayPalGateway/API/PayPalPartner.php

295 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\PaymentMethods\PayPalGateway\API;
4
5 use FluentCart\Api\StoreSettings;
6 use FluentCart\App\App;
7 use FluentCart\Framework\Support\Arr;
8 use WP_Error;
9
10 class PayPalPartner
11 {
12 private $mode;
13 private $apiBase;
14 private $accessToken;
15
16 private static $fluentCartAPI = '';
17 protected static $testClientId = 'AUaPp5Il9xr2gUhAUtHDcx5qVHIgyyY21Q22XHn_gdKCR-E_S4-r3D4IqZxahb_zpqL46_pT_z2jnHXt';
18 protected static $liveClientId = 'AV2vFALQ_Okaw1HbNAWFbEstDofcsTGdetUNjIMniFTB5xRwnc2Kw2PLADEoRn-gZ5jLPnzwEZD6pQM3';
19
20 protected static $testPartnerMerchantId = 'TVZ979WK7888J';
21
22 protected static $partnerMerchantId = '8VQ7BA4DWP6TA'; // LIVE ACCOUNT ID
23
24 public function __construct($mode = 'live')
25 {
26 static::$fluentCartAPI = App::config()->get('paypal.connect_api');
27 $this->mode = $mode;
28 $this->apiBase = $mode === 'live' ? 'https://api-m.paypal.com' : 'https://api-m.sandbox.paypal.com';
29 }
30
31 public function getPartnerId($mode = ''): string
32 {
33 if (!$mode) {
34 $mode = $this->mode;
35 }
36
37 if ($mode === 'test') {
38 return static::$testPartnerMerchantId;
39 }
40
41 return static::$partnerMerchantId;
42 }
43
44 public function getClientId(): string
45 {
46 if ($this->mode == 'test') {
47 return static::$testClientId;
48 }
49 return static::$liveClientId;
50 }
51
52 public function withToken($token)
53 {
54 $this->accessToken = $token;
55 return $this;
56 }
57
58 public function get($endpoint, $params = [])
59 {
60 $url = $this->apiBase . $endpoint;
61 if (!empty($params)) {
62 $url .= '?' . http_build_query($params);
63 }
64 return $this->sendRequest('GET', $url);
65 }
66
67 public function exchangeAuthCode($sharedId, $authCode)
68 {
69 $endpoint = '/v1/oauth2/token';
70 $url = $this->apiBase . $endpoint;
71
72 $payload = http_build_query([
73 'grant_type' => 'authorization_code',
74 'code' => $authCode,
75 'code_verifier' => $this->nonce()
76 ]);
77 return $this->sendTokenRequest($url, $payload, $sharedId);
78 }
79
80 public function verifyMerchant($sellerAccessToken, $merchantIdInPayPal)
81 {
82 $endpoint = "/v1/customer/partners/" . $this->getPartnerId() . "/merchant-integrations/" . $merchantIdInPayPal;
83 return $this->makeMerchantRequest($endpoint, $sellerAccessToken);
84 }
85
86 public function makeMerchantRequest($endpoint, $sellerAccessToken, $method = 'GET')
87 {
88 $url = $this->apiBase . $endpoint;
89
90 $args = [
91 'headers' => [
92 'Content-Type' => 'application/json',
93 'Authorization' => 'Bearer ' . $sellerAccessToken,
94 'PayPal-Partner-Attribution-ID' => 'FLUENTCART_SP_PPCP'
95 ],
96 'method' => $method
97 ];
98
99 $response = wp_remote_get($url, $args);
100
101 if (is_wp_error($response)) {
102 return $response;
103 }
104
105 $body = wp_remote_retrieve_body($response);
106 $bodyFormatted = json_decode($body, true);
107
108 $code = Arr::get($response, 'response.code');
109
110 if ($code > 299) {
111 return new \WP_Error($code, Arr::get($bodyFormatted, 'message'));
112 }
113
114 return $bodyFormatted;
115 }
116
117
118 private function sendTokenRequest($url, $payload, $sharedId)
119 {
120 $args = [
121 'method' => 'POST',
122 'body' => $payload,
123 'headers' => [
124 'Content-Type' => 'application/x-www-form-urlencoded',
125 'PayPal-Partner-Attribution-ID' => 'FLUENTCART_SP_PPCP',
126 'Authorization' => 'Basic ' . base64_encode($sharedId . ':')
127 ]
128 ];
129
130 $response = wp_remote_post($url, $args);
131
132 if (is_wp_error($response)) {
133 return ['error' => $response->get_error_message()];
134 }
135
136 $body = wp_remote_retrieve_body($response);
137 return json_decode($body, true);
138 }
139
140 /**
141 * @throws \Exception
142 */
143 public function sellerOnboarding()
144 {
145 $return_url = add_query_arg([
146 'fluent-cart' => 'paypal_connect',
147 'intent' => 'return',
148 'mode' => $this->mode,
149 '_wpnonce' => wp_create_nonce('fluent_cart_paypal_connect_return_' . $this->mode),
150 ], home_url());
151 $capabilities = [];
152 $products = [
153 'EXPRESS_CHECKOUT'
154 ];
155
156 $store = (new StoreSettings())->get();
157 $currency = Arr::get($store, 'currency');
158 $country = Arr::get($store, 'store_country');
159
160 if (empty($country) || empty($currency)) {
161 return new Wp_Error('store_country_currency_not_set', 'Please set store country and currency first!');
162 }
163
164 $inAcdcCountry = (new DccApplies($country, $currency))->forCountryCurrency();
165
166
167 if ($inAcdcCountry) {
168 $products = array('PPCP', 'ADVANCED_VAULTING');
169 $capabilities[] = 'PAYPAL_WALLET_VAULTING_ADVANCED';
170 }
171
172 $payload = [
173 // we will use it to generate the tracking ID
174 'tracking_id' => 'fluent_cart_seller_' . md5(site_url()),
175 "operations" => [
176 [
177 "operation" => "API_INTEGRATION",
178 "api_integration_preference" => [
179 "rest_api_integration" => [
180 "integration_method" => "PAYPAL",
181 "integration_type" => "FIRST_PARTY",
182 "first_party_details" => [
183 'features' => [
184 'PAYMENT',
185 'REFUND',
186 'ADVANCED_TRANSACTIONS_SEARCH',
187 'TRACKING_SHIPMENT_READWRITE',
188 'BILLING_AGREEMENT',
189 // 'FUTURE_PAYMENT',
190 // 'VAULT'
191 ],
192 "seller_nonce" => $this->nonce()
193 ]
194 ]
195 ]
196 ]
197 ],
198 'partner_config_override' => [
199 'return_url' => $return_url,
200 'return_url_description' => 'Return to FluentCart',
201 'show_add_credit_card' => true
202 ],
203 "products" => $products,
204 "legal_consents" => [
205 [
206 "type" => "SHARE_DATA_CONSENT",
207 "granted" => true
208 ]
209 ]
210 ];
211
212 if (!empty($capabilities)) {
213 $payload['capabilities'] = $capabilities;
214 }
215
216 //todo: will decide later
217 //$response = $this->post('/v2/customer/partner-referrals', $payload);
218 $response = $this->getRemoteAuthenticator($payload, $this->mode);
219
220 $openModeParams = '&displayMode=minibrowser';
221 if (isset($response['links'])) {
222 foreach ($response['links'] as $link) {
223 if ('action_url' === $link['rel']) {
224 return (string)$link['href'] . $openModeParams;
225 }
226 }
227 }
228
229 return '';
230 }
231
232 public function getRemoteAuthenticator($payload, $mode)
233 {
234 $url = $this->apiBase;
235 $response = wp_remote_post(
236 static::$fluentCartAPI . 'client_id=' . $this->getClientId() . '&endpoint=' . $url . '&environment=' . $mode,
237 [
238 'method' => 'POST',
239 'body' => json_encode($payload),
240 'headers' => [
241 'Content-Type' => 'application/json'
242 ],
243 ]
244 );
245
246 if (is_wp_error($response)) {
247 throw new \Exception(esc_html($response->get_error_message()), 400);
248 }
249
250 $body = wp_remote_retrieve_body($response);
251 $response = json_decode($body, true);
252 if (isset($response['data'])) {
253 return $response['data'];
254 }
255 return $response;
256 }
257
258 public function nonce(): string
259 {
260 return 'Zmx1ZW50Y2FydHBheXBhbG5vbmNlX2RvbnRkZWNvZGV0aGlz';
261 }
262
263 public function getBNCode()
264 {
265 return 'FLUENTCART_SP_PPCP';
266 }
267
268 private function sendRequest($method, $url, $payload = null)
269 {
270 $headers = [
271 'Content-Type' => 'application/json',
272 'Authorization' => 'Bearer ' . $this->accessToken,
273 'PayPal-Partner-Attribution-ID' => 'FLUENTCART_SP_PPCP'
274 ];
275
276 $args = [
277 'method' => $method,
278 'headers' => $headers
279 ];
280
281 if ($payload) {
282 $args['body'] = $payload;
283 }
284
285 $response = wp_remote_request($url, $args);
286
287 if (is_wp_error($response)) {
288 return ['error' => $response->get_error_message()];
289 }
290
291 $body = wp_remote_retrieve_body($response);
292 return json_decode($body, true);
293 }
294 }
295