Banners
2 years ago
DataTransferObjects
4 years ago
Exceptions
3 years ago
Migrations
2 years ago
Models
2 years ago
PayPalCheckoutSdk
2 years ago
Repositories
2 years ago
Webhooks
2 years ago
AccountAdminNotices.php
4 years ago
AdminSettingFields.php
2 years ago
AdvancedCardFields.php
4 years ago
AjaxRequestHandler.php
2 years ago
DonationDetailsPage.php
4 years ago
DonationFormPaymentMethod.php
4 years ago
PayPalClient.php
2 years ago
PayPalCommerce.php
2 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
388 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.32.0 Return error response on exception when fetch access token from authorization code. |
| 95 | * @since 2.9.0 |
| 96 | */ |
| 97 | public function onBoardedUserAjaxRequestHandler() |
| 98 | { |
| 99 | $this->validateAdminRequest(); |
| 100 | |
| 101 | if (empty($_GET['mode']) || ! in_array($_GET['mode'], ['sandbox', 'live'])) { |
| 102 | wp_send_json_error('Must include valid mode'); |
| 103 | } |
| 104 | |
| 105 | $mode = sanitize_text_field(wp_unslash($_GET['mode'])); |
| 106 | |
| 107 | // Set PayPal client mode. |
| 108 | give(PayPalClient::class)->setMode($mode); |
| 109 | |
| 110 | $partnerLinkInfo = $this->settings->getPartnerLinkDetails(); |
| 111 | |
| 112 | try{ |
| 113 | $payPalResponse = $this->payPalAuth->getTokenFromAuthorizationCode( |
| 114 | give_clean($_GET['authCode']), |
| 115 | give_clean($_GET['sharedId']), |
| 116 | $partnerLinkInfo['nonce'] |
| 117 | ); |
| 118 | } catch ( \Exception $exception ) { |
| 119 | wp_send_json_error(); |
| 120 | } |
| 121 | |
| 122 | $this->settings->updateAccessToken($payPalResponse); |
| 123 | |
| 124 | // Set cron job to refresh token. |
| 125 | $refreshToken = give(RefreshToken::class); |
| 126 | $refreshToken->setMode($mode); |
| 127 | $refreshToken->registerCronJobToRefreshToken($payPalResponse['expiresIn']); |
| 128 | |
| 129 | wp_send_json_success(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * This function handle ajax request with give_paypal_commerce_get_partner_url action. |
| 134 | * |
| 135 | * @since 2.30.0 Add support for mode param. |
| 136 | * @since 2.9.0 |
| 137 | */ |
| 138 | public function onGetPartnerUrlAjaxRequestHandler() |
| 139 | { |
| 140 | $this->validateAdminRequest(); |
| 141 | |
| 142 | if (empty($country = $_GET['countryCode']) || ! isset(give_get_country_list()[$country])) { |
| 143 | wp_send_json_error('Must include valid 2-character country code'); |
| 144 | } |
| 145 | |
| 146 | if (empty($_GET['mode']) || ! in_array($_GET['mode'], ['sandbox', 'live'])) { |
| 147 | wp_send_json_error('Must include valid mode'); |
| 148 | } |
| 149 | |
| 150 | $country = sanitize_text_field(wp_unslash($_GET['countryCode'])); |
| 151 | $mode = sanitize_text_field(wp_unslash($_GET['mode'])); |
| 152 | $redirectUrl = add_query_arg( |
| 153 | [ |
| 154 | 'tab' => 'gateways', |
| 155 | 'section' => 'paypal', |
| 156 | 'group' => 'paypal-commerce', |
| 157 | 'mode' => $mode |
| 158 | ], |
| 159 | admin_url('edit.php?post_type=give_forms&page=give-settings') |
| 160 | ); |
| 161 | |
| 162 | // Set PayPal client mode. |
| 163 | give(PayPalClient::class)->setMode($mode); |
| 164 | |
| 165 | $data = $this->payPalAuth->getSellerPartnerLink($redirectUrl, $country); |
| 166 | |
| 167 | if (! $data) { |
| 168 | wp_send_json_error(); |
| 169 | } |
| 170 | |
| 171 | $this->settings->updateAccountCountry($country); |
| 172 | $this->settings->updatePartnerLinkDetails($data); |
| 173 | |
| 174 | wp_send_json_success($data); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * give_paypal_commerce_disconnect_account ajax request handler. |
| 179 | * |
| 180 | * @since 2.30.0 Add support for mode param. |
| 181 | * @since 2.25.0 Remove merchant seller token. |
| 182 | * @since 2.9.0 |
| 183 | */ |
| 184 | public function removePayPalAccount() |
| 185 | { |
| 186 | if (! current_user_can('manage_give_settings')) { |
| 187 | wp_send_json_error(['error' => esc_html__('You are not allowed to perform this action.', 'give')]); |
| 188 | } |
| 189 | |
| 190 | try { |
| 191 | $mode = give_clean($_POST['mode']); |
| 192 | $this->webhooksRepository->setMode($mode); |
| 193 | $this->merchantRepository->setMode($mode); |
| 194 | $this->refreshToken->setMode($mode); |
| 195 | $this->settings->setMode($mode); |
| 196 | |
| 197 | $this->validateAdminRequest(); |
| 198 | |
| 199 | // Remove the webhook from PayPal if there is one |
| 200 | if ($webhookConfig = $this->webhooksRepository->getWebhookConfig()) { |
| 201 | $this->webhooksRepository->deleteWebhook($this->merchantDetails->accessToken, $webhookConfig->id); |
| 202 | $this->webhooksRepository->deleteWebhookConfig(); |
| 203 | } |
| 204 | |
| 205 | $this->merchantRepository->delete(); |
| 206 | $this->merchantRepository->deleteAccountErrors(); |
| 207 | $this->merchantRepository->deleteClientToken(); |
| 208 | $this->settings->deleteSellerAccessToken(); |
| 209 | $this->refreshToken->deleteRefreshTokenCronJob(); |
| 210 | |
| 211 | wp_send_json_success(); |
| 212 | } catch (\Exception $exception) { |
| 213 | wp_send_json_error(['error' => $exception->getMessage()]); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Create order. |
| 219 | * |
| 220 | * @todo: handle payment create error on frontend. |
| 221 | * |
| 222 | * @since 2.9.0 |
| 223 | */ |
| 224 | public function createOrder() |
| 225 | { |
| 226 | $this->validateFrontendRequest(); |
| 227 | |
| 228 | $postData = give_clean($_POST); |
| 229 | $formId = absint($postData['give-form-id']); |
| 230 | |
| 231 | $data = [ |
| 232 | 'formId' => $formId, |
| 233 | 'formTitle' => give_payment_gateway_item_title(['post_data' => $postData], 127), |
| 234 | 'donationAmount' => isset($postData['give-amount']) ? |
| 235 | (float)apply_filters( |
| 236 | 'give_donation_total', |
| 237 | give_maybe_sanitize_amount( |
| 238 | $postData['give-amount'], |
| 239 | ['currency' => give_get_currency($formId)] |
| 240 | ) |
| 241 | ) : |
| 242 | '0.00', |
| 243 | 'payer' => [ |
| 244 | 'firstName' => $postData['give_first'], |
| 245 | 'lastName' => $postData['give_last'], |
| 246 | 'email' => $postData['give_email'], |
| 247 | 'address' => $this->getDonorAddressFromPostedDataForPaypalOrder($postData), |
| 248 | ], |
| 249 | 'application_context' => [ |
| 250 | 'shipping_preference' => 'NO_SHIPPING', |
| 251 | ], |
| 252 | ]; |
| 253 | |
| 254 | try { |
| 255 | $result = give(PayPalOrder::class)->createOrder($data); |
| 256 | |
| 257 | wp_send_json_success( |
| 258 | [ |
| 259 | 'id' => $result, |
| 260 | ] |
| 261 | ); |
| 262 | } catch (\Exception $ex) { |
| 263 | wp_send_json_error( |
| 264 | [ |
| 265 | 'error' => json_decode($ex->getMessage(), true), |
| 266 | ] |
| 267 | ); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Approve order. |
| 273 | * |
| 274 | * @todo: handle payment capture error on frontend. |
| 275 | * |
| 276 | * @since 2.9.0 |
| 277 | */ |
| 278 | public function approveOrder() |
| 279 | { |
| 280 | $this->validateFrontendRequest(); |
| 281 | |
| 282 | $orderId = give_clean($_GET['order']); |
| 283 | |
| 284 | try { |
| 285 | $result = give(PayPalOrder::class)->approveOrder($orderId); |
| 286 | wp_send_json_success( |
| 287 | [ |
| 288 | 'order' => $result, |
| 289 | ] |
| 290 | ); |
| 291 | } catch (\Exception $ex) { |
| 292 | wp_send_json_error( |
| 293 | [ |
| 294 | 'error' => json_decode($ex->getMessage(), true), |
| 295 | ] |
| 296 | ); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Return on boarding trouble notice. |
| 302 | * |
| 303 | * @since 2.9.6 |
| 304 | */ |
| 305 | public function onBoardingTroubleNotice() |
| 306 | { |
| 307 | if (! current_user_can('manage_give_settings')) { |
| 308 | wp_die(); |
| 309 | } |
| 310 | |
| 311 | /* @var AdminSettingFields $adminSettingFields */ |
| 312 | $adminSettingFields = give(AdminSettingFields::class); |
| 313 | |
| 314 | $actionList = sprintf( |
| 315 | '<ol><li>%1$s</li><li>%2$s</li><li>%3$s</li></ol>', |
| 316 | esc_html__( |
| 317 | 'Make sure to complete the entire PayPal process. Do not close the window until you have finished the process.', |
| 318 | 'give' |
| 319 | ), |
| 320 | esc_html__( |
| 321 | '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.', |
| 322 | 'give' |
| 323 | ), |
| 324 | esc_html__( |
| 325 | 'If you’re still having problems connecting: ', |
| 326 | 'give' |
| 327 | ) . $adminSettingFields->getAdminGuidanceNotice(false) |
| 328 | ); |
| 329 | |
| 330 | $standardError = sprintf( |
| 331 | '<div id="give-paypal-onboarding-trouble-notice" class="give-hidden"><p class="error-message">%1$s</p><p>%2$s</p></div>', |
| 332 | esc_html__('Having trouble connecting to PayPal?', 'give'), |
| 333 | $actionList |
| 334 | ); |
| 335 | |
| 336 | wp_send_json_success($standardError); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Validate admin ajax request. |
| 341 | * |
| 342 | * @since 2.9.0 |
| 343 | */ |
| 344 | private function validateAdminRequest() |
| 345 | { |
| 346 | if (! current_user_can('manage_give_settings')) { |
| 347 | wp_die(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Validate frontend ajax request. |
| 353 | * |
| 354 | * @since 2.9.0 |
| 355 | */ |
| 356 | private function validateFrontendRequest() |
| 357 | { |
| 358 | $formId = absint($_POST['give-form-id']); |
| 359 | |
| 360 | if (! $formId || ! give_verify_donation_form_nonce(give_clean($_POST['give-form-hash']), $formId)) { |
| 361 | wp_die(); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * @since 2.11.1 |
| 367 | * |
| 368 | * @param array $postedData |
| 369 | * |
| 370 | * @return array |
| 371 | */ |
| 372 | private function getDonorAddressFromPostedDataForPaypalOrder($postedData) |
| 373 | { |
| 374 | if (! $this->settings->canCollectBillingInformation()) { |
| 375 | return []; |
| 376 | } |
| 377 | |
| 378 | $address['address_line1'] = ! empty($postedData['card_address']) ? $postedData['card_address'] : ''; |
| 379 | $address['address_line_2'] = ! empty($postedData['card_address_2']) ? $postedData['card_address_2'] : ''; |
| 380 | $address['admin_line_1'] = ! empty($postedData['card_city']) ? $postedData['card_city'] : ''; |
| 381 | $address['admin_line_2'] = ! empty($postedData['card_state']) ? $postedData['card_state'] : ''; |
| 382 | $address['postal_code'] = ! empty($postedData['card_zip']) ? $postedData['card_zip'] : ''; |
| 383 | $address['country_code'] = ! empty($postedData['billing_country']) ? $postedData['billing_country'] : ''; |
| 384 | |
| 385 | return $address; |
| 386 | } |
| 387 | } |
| 388 |