PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.31.1
GiveWP – Donation Plugin and Fundraising Platform v2.31.1
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / PaymentGateways / PayPalCommerce / AjaxRequestHandler.php
give / src / PaymentGateways / PayPalCommerce Last commit date
DataTransferObjects 4 years ago Exceptions 3 years ago Migrations 2 years ago Models 3 years ago PayPalCheckoutSdk 2 years ago Repositories 2 years ago Webhooks 4 years ago AccountAdminNotices.php 4 years ago AdminSettingFields.php 3 years ago AdvancedCardFields.php 4 years ago AjaxRequestHandler.php 2 years ago DonationDetailsPage.php 4 years ago DonationFormPaymentMethod.php 4 years ago PayPalClient.php 3 years ago PayPalCommerce.php 3 years ago RefreshToken.php 2 years ago RefundPaymentHandler.php 4 years ago ScriptLoader.php 2 years ago Utils.php 4 years ago onBoardingRedirectHandler.php 2 years ago
AjaxRequestHandler.php
387 lines
1 <?php
2
3 namespace Give\PaymentGateways\PayPalCommerce;
4
5 use Give\Framework\Http\ConnectServer\Client\ConnectClient;
6 use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail;
7 use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails;
8 use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalAuth;
9 use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalOrder;
10 use Give\PaymentGateways\PayPalCommerce\Repositories\Settings;
11 use Give\PaymentGateways\PayPalCommerce\Repositories\Webhooks;
12
13 /**
14 * Class AjaxRequestHandler
15 * @package Give\PaymentGateways\PaypalCommerce
16 *
17 * @sicne 2.9.0
18 */
19 class AjaxRequestHandler
20 {
21 /**
22 * @since 2.9.0
23 *
24 * @var Webhooks
25 */
26 private $webhooksRepository;
27
28 /**
29 * @since 2.9.0
30 *
31 * @var MerchantDetail
32 */
33 private $merchantDetails;
34
35 /**
36 * @since 2.9.0
37 *
38 * @var PayPalAuth
39 */
40 private $payPalAuth;
41
42 /**
43 * @since 2.9.0
44 *
45 * @var MerchantDetails
46 */
47 private $merchantRepository;
48
49 /**
50 * @since 2.9.0
51 *
52 * @var ConnectClient
53 */
54 private $refreshToken;
55
56 /**
57 * @since 2.9.0
58 *
59 * @var Settings
60 */
61 private $settings;
62
63 /**
64 * AjaxRequestHandler constructor.
65 *
66 * @since 2.9.0
67 *
68 * @param Webhooks $webhooksRepository
69 * @param MerchantDetail $merchantDetails
70 * @param MerchantDetails $merchantRepository
71 * @param RefreshToken $refreshToken
72 * @param Settings $settings
73 * @param PayPalAuth $payPalAuth
74 */
75 public function __construct(
76 Webhooks $webhooksRepository,
77 MerchantDetail $merchantDetails,
78 MerchantDetails $merchantRepository,
79 RefreshToken $refreshToken,
80 Settings $settings,
81 PayPalAuth $payPalAuth
82 ) {
83 $this->webhooksRepository = $webhooksRepository;
84 $this->merchantDetails = $merchantDetails;
85 $this->merchantRepository = $merchantRepository;
86 $this->refreshToken = $refreshToken;
87 $this->settings = $settings;
88 $this->payPalAuth = $payPalAuth;
89 }
90
91 /**
92 * give_paypal_commerce_user_onboarded ajax action handler
93 *
94 * @since 2.9.0
95 */
96 public function onBoardedUserAjaxRequestHandler()
97 {
98 $this->validateAdminRequest();
99
100 if (empty($_GET['mode']) || ! in_array($_GET['mode'], ['sandbox', 'live'])) {
101 wp_send_json_error('Must include valid mode');
102 }
103
104 $mode = sanitize_text_field(wp_unslash($_GET['mode']));
105
106 // Set PayPal client mode.
107 give(PayPalClient::class)->setMode($mode);
108
109 $partnerLinkInfo = $this->settings->getPartnerLinkDetails();
110
111 $payPalResponse = $this->payPalAuth->getTokenFromAuthorizationCode(
112 give_clean($_GET['authCode']),
113 give_clean($_GET['sharedId']),
114 $partnerLinkInfo['nonce']
115 );
116
117 if (! $payPalResponse || array_key_exists('error', $payPalResponse)) {
118 wp_send_json_error();
119 }
120
121 $this->settings->updateAccessToken($payPalResponse);
122
123 // Set cron job to refresh token.
124 $refreshToken = give(RefreshToken::class);
125 $refreshToken->setMode($mode);
126 $refreshToken->registerCronJobToRefreshToken($payPalResponse['expiresIn']);
127
128 wp_send_json_success();
129 }
130
131 /**
132 * This function handle ajax request with give_paypal_commerce_get_partner_url action.
133 *
134 * @since 2.30.0 Add support for mode param.
135 * @since 2.9.0
136 */
137 public function onGetPartnerUrlAjaxRequestHandler()
138 {
139 $this->validateAdminRequest();
140
141 if (empty($country = $_GET['countryCode']) || ! isset(give_get_country_list()[$country])) {
142 wp_send_json_error('Must include valid 2-character country code');
143 }
144
145 if (empty($_GET['mode']) || ! in_array($_GET['mode'], ['sandbox', 'live'])) {
146 wp_send_json_error('Must include valid mode');
147 }
148
149 $country = sanitize_text_field(wp_unslash($_GET['countryCode']));
150 $mode = sanitize_text_field(wp_unslash($_GET['mode']));
151 $redirectUrl = add_query_arg(
152 [
153 'tab' => 'gateways',
154 'section' => 'paypal',
155 'group' => 'paypal-commerce',
156 'mode' => $mode
157 ],
158 admin_url('edit.php?post_type=give_forms&page=give-settings')
159 );
160
161 // Set PayPal client mode.
162 give(PayPalClient::class)->setMode($mode);
163
164 $data = $this->payPalAuth->getSellerPartnerLink($redirectUrl, $country);
165
166 if (! $data) {
167 wp_send_json_error();
168 }
169
170 $this->settings->updateAccountCountry($country);
171 $this->settings->updatePartnerLinkDetails($data);
172
173 wp_send_json_success($data);
174 }
175
176 /**
177 * give_paypal_commerce_disconnect_account ajax request handler.
178 *
179 * @since 2.30.0 Add support for mode param.
180 * @since 2.25.0 Remove merchant seller token.
181 * @since 2.9.0
182 */
183 public function removePayPalAccount()
184 {
185 if (! current_user_can('manage_give_settings')) {
186 wp_send_json_error(['error' => esc_html__('You are not allowed to perform this action.', 'give')]);
187 }
188
189 try {
190 $mode = give_clean($_POST['mode']);
191 $this->webhooksRepository->setMode($mode);
192 $this->merchantRepository->setMode($mode);
193 $this->refreshToken->setMode($mode);
194 $this->settings->setMode($mode);
195
196 $this->validateAdminRequest();
197
198 // Remove the webhook from PayPal if there is one
199 if ($webhookConfig = $this->webhooksRepository->getWebhookConfig()) {
200 $this->webhooksRepository->deleteWebhook($this->merchantDetails->accessToken, $webhookConfig->id);
201 $this->webhooksRepository->deleteWebhookConfig();
202 }
203
204 $this->merchantRepository->delete();
205 $this->merchantRepository->deleteAccountErrors();
206 $this->merchantRepository->deleteClientToken();
207 $this->settings->deleteSellerAccessToken();
208 $this->refreshToken->deleteRefreshTokenCronJob();
209
210 wp_send_json_success();
211 } catch (\Exception $exception) {
212 wp_send_json_error(['error' => $exception->getMessage()]);
213 }
214 }
215
216 /**
217 * Create order.
218 *
219 * @todo: handle payment create error on frontend.
220 *
221 * @since 2.9.0
222 */
223 public function createOrder()
224 {
225 $this->validateFrontendRequest();
226
227 $postData = give_clean($_POST);
228 $formId = absint($postData['give-form-id']);
229
230 $data = [
231 'formId' => $formId,
232 'formTitle' => give_payment_gateway_item_title(['post_data' => $postData], 127),
233 'donationAmount' => isset($postData['give-amount']) ?
234 (float)apply_filters(
235 'give_donation_total',
236 give_maybe_sanitize_amount(
237 $postData['give-amount'],
238 ['currency' => give_get_currency($formId)]
239 )
240 ) :
241 '0.00',
242 'payer' => [
243 'firstName' => $postData['give_first'],
244 'lastName' => $postData['give_last'],
245 'email' => $postData['give_email'],
246 'address' => $this->getDonorAddressFromPostedDataForPaypalOrder($postData),
247 ],
248 'application_context' => [
249 'shipping_preference' => 'NO_SHIPPING',
250 ],
251 ];
252
253 try {
254 $result = give(PayPalOrder::class)->createOrder($data);
255
256 wp_send_json_success(
257 [
258 'id' => $result,
259 ]
260 );
261 } catch (\Exception $ex) {
262 wp_send_json_error(
263 [
264 'error' => json_decode($ex->getMessage(), true),
265 ]
266 );
267 }
268 }
269
270 /**
271 * Approve order.
272 *
273 * @todo: handle payment capture error on frontend.
274 *
275 * @since 2.9.0
276 */
277 public function approveOrder()
278 {
279 $this->validateFrontendRequest();
280
281 $orderId = give_clean($_GET['order']);
282
283 try {
284 $result = give(PayPalOrder::class)->approveOrder($orderId);
285 wp_send_json_success(
286 [
287 'order' => $result,
288 ]
289 );
290 } catch (\Exception $ex) {
291 wp_send_json_error(
292 [
293 'error' => json_decode($ex->getMessage(), true),
294 ]
295 );
296 }
297 }
298
299 /**
300 * Return on boarding trouble notice.
301 *
302 * @since 2.9.6
303 */
304 public function onBoardingTroubleNotice()
305 {
306 if (! current_user_can('manage_give_settings')) {
307 wp_die();
308 }
309
310 /* @var AdminSettingFields $adminSettingFields */
311 $adminSettingFields = give(AdminSettingFields::class);
312
313 $actionList = sprintf(
314 '<ol><li>%1$s</li><li>%2$s</li><li>%3$s</li></ol>',
315 esc_html__(
316 'Make sure to complete the entire PayPal process. Do not close the window until you have finished the process.',
317 'give'
318 ),
319 esc_html__(
320 'The last screen of the PayPal connect process includes a button to be sent back to your site. It is important you click this and do not close the window yourself.',
321 'give'
322 ),
323 esc_html__(
324 'If you’re still having problems connecting: ',
325 'give'
326 ) . $adminSettingFields->getAdminGuidanceNotice(false)
327 );
328
329 $standardError = sprintf(
330 '<div id="give-paypal-onboarding-trouble-notice" class="give-hidden"><p class="error-message">%1$s</p><p>%2$s</p></div>',
331 esc_html__('Having trouble connecting to PayPal?', 'give'),
332 $actionList
333 );
334
335 wp_send_json_success($standardError);
336 }
337
338 /**
339 * Validate admin ajax request.
340 *
341 * @since 2.9.0
342 */
343 private function validateAdminRequest()
344 {
345 if (! current_user_can('manage_give_settings')) {
346 wp_die();
347 }
348 }
349
350 /**
351 * Validate frontend ajax request.
352 *
353 * @since 2.9.0
354 */
355 private function validateFrontendRequest()
356 {
357 $formId = absint($_POST['give-form-id']);
358
359 if (! $formId || ! give_verify_donation_form_nonce(give_clean($_POST['give-form-hash']), $formId)) {
360 wp_die();
361 }
362 }
363
364 /**
365 * @since 2.11.1
366 *
367 * @param array $postedData
368 *
369 * @return array
370 */
371 private function getDonorAddressFromPostedDataForPaypalOrder($postedData)
372 {
373 if (! $this->settings->canCollectBillingInformation()) {
374 return [];
375 }
376
377 $address['address_line1'] = ! empty($postedData['card_address']) ? $postedData['card_address'] : '';
378 $address['address_line_2'] = ! empty($postedData['card_address_2']) ? $postedData['card_address_2'] : '';
379 $address['admin_line_1'] = ! empty($postedData['card_city']) ? $postedData['card_city'] : '';
380 $address['admin_line_2'] = ! empty($postedData['card_state']) ? $postedData['card_state'] : '';
381 $address['postal_code'] = ! empty($postedData['card_zip']) ? $postedData['card_zip'] : '';
382 $address['country_code'] = ! empty($postedData['billing_country']) ? $postedData['billing_country'] : '';
383
384 return $address;
385 }
386 }
387