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