PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.1
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.1, at app/Modules/PaymentMethods/PayPalGateway/API/PayPalPartner.php

290 lines 8.9 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 = admin_url('admin.php?fct_app_authenticator&method=paypal&mode=' . $this->mode);
146 $capabilities = [];
147 $products = [
148 'EXPRESS_CHECKOUT'
149 ];
150
151 $store = (new StoreSettings())->get();
152 $currency = Arr::get($store, 'currency');
153 $country = Arr::get($store, 'store_country');
154
155 if (empty($country) || empty($currency)) {
156 return new Wp_Error('store_country_currency_not_set', 'Please set store country and currency first!');
157 }
158
159 $inAcdcCountry = (new DccApplies($country, $currency))->forCountryCurrency();
160
161
162 if ($inAcdcCountry) {
163 $products = array('PPCP', 'ADVANCED_VAULTING');
164 $capabilities[] = 'PAYPAL_WALLET_VAULTING_ADVANCED';
165 }
166
167 $payload = [
168 // we will use it to generate the tracking ID
169 'tracking_id' => 'fluent_cart_seller_' . md5(site_url()),
170 "operations" => [
171 [
172 "operation" => "API_INTEGRATION",
173 "api_integration_preference" => [
174 "rest_api_integration" => [
175 "integration_method" => "PAYPAL",
176 "integration_type" => "FIRST_PARTY",
177 "first_party_details" => [
178 'features' => [
179 'PAYMENT',
180 'REFUND',
181 'ADVANCED_TRANSACTIONS_SEARCH',
182 'TRACKING_SHIPMENT_READWRITE',
183 'BILLING_AGREEMENT',
184 // 'FUTURE_PAYMENT',
185 // 'VAULT'
186 ],
187 "seller_nonce" => $this->nonce()
188 ]
189 ]
190 ]
191 ]
192 ],
193 'partner_config_override' => [
194 'return_url' => $return_url,
195 'return_url_description' => 'Return to FluentCart',
196 'show_add_credit_card' => true
197 ],
198 "products" => $products,
199 "legal_consents" => [
200 [
201 "type" => "SHARE_DATA_CONSENT",
202 "granted" => true
203 ]
204 ]
205 ];
206
207 if (!empty($capabilities)) {
208 $payload['capabilities'] = $capabilities;
209 }
210
211 //todo: will decide later
212 //$response = $this->post('/v2/customer/partner-referrals', $payload);
213 $response = $this->getRemoteAuthenticator($payload, $this->mode);
214
215 $openModeParams = '&displayMode=minibrowser';
216 if (isset($response['links'])) {
217 foreach ($response['links'] as $link) {
218 if ('action_url' === $link['rel']) {
219 return (string)$link['href'] . $openModeParams;
220 }
221 }
222 }
223
224 return '';
225 }
226
227 public function getRemoteAuthenticator($payload, $mode)
228 {
229 $url = $this->apiBase;
230 $response = wp_remote_post(
231 static::$fluentCartAPI . 'client_id=' . $this->getClientId() . '&endpoint=' . $url . '&environment=' . $mode,
232 [
233 'method' => 'POST',
234 'body' => json_encode($payload),
235 'headers' => [
236 'Content-Type' => 'application/json'
237 ],
238 ]
239 );
240
241 if (is_wp_error($response)) {
242 throw new \Exception(esc_html($response->get_error_message()), 400);
243 }
244
245 $body = wp_remote_retrieve_body($response);
246 $response = json_decode($body, true);
247 if (isset($response['data'])) {
248 return $response['data'];
249 }
250 return $response;
251 }
252
253 public function nonce(): string
254 {
255 return 'Zmx1ZW50Y2FydHBheXBhbG5vbmNlX2RvbnRkZWNvZGV0aGlz';
256 }
257
258 public function getBNCode()
259 {
260 return 'FLUENTCART_SP_PPCP';
261 }
262
263 private function sendRequest($method, $url, $payload = null)
264 {
265 $headers = [
266 'Content-Type' => 'application/json',
267 'Authorization' => 'Bearer ' . $this->accessToken,
268 'PayPal-Partner-Attribution-ID' => 'FLUENTCART_SP_PPCP'
269 ];
270
271 $args = [
272 'method' => $method,
273 'headers' => $headers
274 ];
275
276 if ($payload) {
277 $args['body'] = $payload;
278 }
279
280 $response = wp_remote_request($url, $args);
281
282 if (is_wp_error($response)) {
283 return ['error' => $response->get_error_message()];
284 }
285
286 $body = wp_remote_retrieve_body($response);
287 return json_decode($body, true);
288 }
289 }
290