DataTransferObjects
5 years ago
Models
5 years ago
Repositories
5 years ago
Webhooks
5 years ago
AccountAdminNotices.php
5 years ago
AdminSettingFields.php
5 years ago
AdvancedCardFields.php
5 years ago
AjaxRequestHandler.php
5 years ago
DonationDetailsPage.php
5 years ago
DonationFormPaymentMethod.php
5 years ago
DonationProcessor.php
5 years ago
PayPalClient.php
5 years ago
PayPalCommerce.php
5 years ago
RefreshToken.php
5 years ago
RefundPaymentHandler.php
5 years ago
ScriptLoader.php
5 years ago
Utils.php
5 years ago
onBoardingRedirectHandler.php
5 years ago
AjaxRequestHandler.php
292 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\Settings; |
| 10 | use Give\PaymentGateways\PayPalCommerce\Repositories\Webhooks; |
| 11 | use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalOrder; |
| 12 | |
| 13 | /** |
| 14 | * Class AjaxRequestHandler |
| 15 | * @package Give\PaymentGateways\PaypalCommerce |
| 16 | * |
| 17 | * @sicne 2.9.0 |
| 18 | */ |
| 19 | class AjaxRequestHandler { |
| 20 | /** |
| 21 | * @since 2.9.0 |
| 22 | * |
| 23 | * @var Webhooks |
| 24 | */ |
| 25 | private $webhooksRepository; |
| 26 | |
| 27 | /** |
| 28 | * @since 2.9.0 |
| 29 | * |
| 30 | * @var MerchantDetail |
| 31 | */ |
| 32 | private $merchantDetails; |
| 33 | |
| 34 | /** |
| 35 | * @since 2.9.0 |
| 36 | * |
| 37 | * @var PayPalAuth |
| 38 | */ |
| 39 | private $payPalAuth; |
| 40 | |
| 41 | /** |
| 42 | * @since 2.9.0 |
| 43 | * |
| 44 | * @var MerchantDetails |
| 45 | */ |
| 46 | private $merchantRepository; |
| 47 | |
| 48 | /** |
| 49 | * @since 2.9.0 |
| 50 | * |
| 51 | * @var ConnectClient |
| 52 | */ |
| 53 | private $refreshToken; |
| 54 | |
| 55 | /** |
| 56 | * @since 2.9.0 |
| 57 | * |
| 58 | * @var Settings |
| 59 | */ |
| 60 | private $settings; |
| 61 | |
| 62 | /** |
| 63 | * AjaxRequestHandler constructor. |
| 64 | * |
| 65 | * @since 2.9.0 |
| 66 | * |
| 67 | * @param Webhooks $webhooksRepository |
| 68 | * @param MerchantDetail $merchantDetails |
| 69 | * @param MerchantDetails $merchantRepository |
| 70 | * @param RefreshToken $refreshToken |
| 71 | * @param Settings $settings |
| 72 | * @param PayPalAuth $payPalAuth |
| 73 | */ |
| 74 | public function __construct( |
| 75 | Webhooks $webhooksRepository, |
| 76 | MerchantDetail $merchantDetails, |
| 77 | MerchantDetails $merchantRepository, |
| 78 | RefreshToken $refreshToken, |
| 79 | Settings $settings, |
| 80 | PayPalAuth $payPalAuth |
| 81 | ) { |
| 82 | $this->webhooksRepository = $webhooksRepository; |
| 83 | $this->merchantDetails = $merchantDetails; |
| 84 | $this->merchantRepository = $merchantRepository; |
| 85 | $this->refreshToken = $refreshToken; |
| 86 | $this->settings = $settings; |
| 87 | $this->payPalAuth = $payPalAuth; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * give_paypal_commerce_user_onboarded ajax action handler |
| 92 | * |
| 93 | * @since 2.9.0 |
| 94 | */ |
| 95 | public function onBoardedUserAjaxRequestHandler() { |
| 96 | $this->validateAdminRequest(); |
| 97 | |
| 98 | $partnerLinkInfo = $this->settings->getPartnerLinkDetails(); |
| 99 | |
| 100 | $payPalResponse = $this->payPalAuth->getTokenFromAuthorizationCode( |
| 101 | give_clean( $_GET['authCode'] ), |
| 102 | give_clean( $_GET['sharedId'] ), |
| 103 | $partnerLinkInfo['nonce'] |
| 104 | ); |
| 105 | |
| 106 | if ( ! $payPalResponse || array_key_exists( 'error', $payPalResponse ) ) { |
| 107 | wp_send_json_error(); |
| 108 | } |
| 109 | |
| 110 | $this->settings->updateAccessToken( $payPalResponse ); |
| 111 | |
| 112 | give( RefreshToken::class )->registerCronJobToRefreshToken( $payPalResponse['expiresIn'] ); |
| 113 | |
| 114 | wp_send_json_success(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * give_paypal_commerce_get_partner_url action handler |
| 119 | * |
| 120 | * @since 2.9.0 |
| 121 | */ |
| 122 | public function onGetPartnerUrlAjaxRequestHandler() { |
| 123 | $this->validateAdminRequest(); |
| 124 | |
| 125 | if ( empty( $country = $_GET['countryCode'] ) || ! isset( give_get_country_list()[ $country ] ) ) { |
| 126 | wp_send_json_error( 'Must include valid 2-character country code' ); |
| 127 | } |
| 128 | |
| 129 | $data = $this->payPalAuth->getSellerPartnerLink( |
| 130 | admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=paypal&group=paypal-commerce' ), |
| 131 | $country |
| 132 | ); |
| 133 | |
| 134 | if ( ! $data ) { |
| 135 | wp_send_json_error(); |
| 136 | } |
| 137 | |
| 138 | $this->settings->updateAccountCountry( $country ); |
| 139 | $this->settings->updatePartnerLinkDetails( $data ); |
| 140 | |
| 141 | wp_send_json_success( $data ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * give_paypal_commerce_disconnect_account ajax request handler. |
| 146 | * |
| 147 | * @since 2.9.0 |
| 148 | */ |
| 149 | public function removePayPalAccount() { |
| 150 | $this->validateAdminRequest(); |
| 151 | |
| 152 | // Remove the webhook from PayPal if there is one |
| 153 | if ( $webhookConfig = $this->webhooksRepository->getWebhookConfig() ) { |
| 154 | $this->webhooksRepository->deleteWebhook( $this->merchantDetails->accessToken, $webhookConfig->id ); |
| 155 | $this->webhooksRepository->deleteWebhookConfig(); |
| 156 | } |
| 157 | |
| 158 | $this->merchantRepository->delete(); |
| 159 | $this->merchantRepository->deleteAccountErrors(); |
| 160 | $this->merchantRepository->deleteClientToken(); |
| 161 | $this->refreshToken->deleteRefreshTokenCronJob(); |
| 162 | |
| 163 | wp_send_json_success(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Create order. |
| 168 | * |
| 169 | * @todo: handle payment create error on frontend. |
| 170 | * |
| 171 | * @since 2.9.0 |
| 172 | */ |
| 173 | public function createOrder() { |
| 174 | $this->validateFrontendRequest(); |
| 175 | |
| 176 | $postData = give_clean( $_POST ); |
| 177 | $formId = absint( $postData['give-form-id'] ); |
| 178 | |
| 179 | $data = [ |
| 180 | 'formId' => $formId, |
| 181 | 'formTitle' => give_payment_gateway_item_title( [ 'post_data' => $postData ], 127 ), |
| 182 | 'donationAmount' => isset( $postData['give-amount'] ) ? (float) apply_filters( 'give_donation_total', give_maybe_sanitize_amount( $postData['give-amount'], [ 'currency' => give_get_currency( $formId ) ] ) ) : '0.00', |
| 183 | 'payer' => [ |
| 184 | 'firstName' => $postData['give_first'], |
| 185 | 'lastName' => $postData['give_last'], |
| 186 | 'email' => $postData['give_email'], |
| 187 | ], |
| 188 | 'application_context' => [ |
| 189 | 'shipping_preference' => 'NO_SHIPPING', |
| 190 | ], |
| 191 | ]; |
| 192 | |
| 193 | try { |
| 194 | $result = give( PayPalOrder::class )->createOrder( $data ); |
| 195 | |
| 196 | wp_send_json_success( |
| 197 | [ |
| 198 | 'id' => $result, |
| 199 | ] |
| 200 | ); |
| 201 | } catch ( \Exception $ex ) { |
| 202 | wp_send_json_error( |
| 203 | [ |
| 204 | 'error' => json_decode( $ex->getMessage(), true ), |
| 205 | ] |
| 206 | ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Approve order. |
| 212 | * |
| 213 | * @todo: handle payment capture error on frontend. |
| 214 | * |
| 215 | * @since 2.9.0 |
| 216 | */ |
| 217 | public function approveOrder() { |
| 218 | $this->validateFrontendRequest(); |
| 219 | |
| 220 | $orderId = give_clean( $_GET['order'] ); |
| 221 | |
| 222 | try { |
| 223 | $result = give( PayPalOrder::class )->approveOrder( $orderId ); |
| 224 | wp_send_json_success( |
| 225 | [ |
| 226 | 'order' => $result, |
| 227 | ] |
| 228 | ); |
| 229 | } catch ( \Exception $ex ) { |
| 230 | wp_send_json_error( |
| 231 | [ |
| 232 | 'error' => json_decode( $ex->getMessage(), true ), |
| 233 | ] |
| 234 | ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Return on boarding trouble notice. |
| 240 | * |
| 241 | * @since 2.9.6 |
| 242 | */ |
| 243 | public function onBoardingTroubleNotice() { |
| 244 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 245 | wp_die(); |
| 246 | } |
| 247 | |
| 248 | /* @var AdminSettingFields $adminSettingFields */ |
| 249 | $adminSettingFields = give( AdminSettingFields::class ); |
| 250 | |
| 251 | $actionList = sprintf( |
| 252 | '<ol><li>%1$s</li><li>%2$s</li><li>%3$s</li></ol>', |
| 253 | esc_html__( 'Make sure to complete the entire PayPal process. Do not close the window until you have finished the process.', 'give' ), |
| 254 | esc_html__( '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.', 'give' ), |
| 255 | esc_html__( 'If you’re still having problems connecting: ', 'give' ) . $adminSettingFields->getAdminGuidanceNotice( false ) |
| 256 | ); |
| 257 | |
| 258 | $standardError = sprintf( |
| 259 | '<div id="give-paypal-onboarding-trouble-notice" class="give-hidden"><p class="error-message">%1$s</p><p>%2$s</p></div>', |
| 260 | esc_html__( 'Having trouble connecting to PayPal?', 'give' ), |
| 261 | $actionList |
| 262 | ); |
| 263 | |
| 264 | wp_send_json_success( $standardError ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Validate admin ajax request. |
| 269 | * |
| 270 | * @since 2.9.0 |
| 271 | */ |
| 272 | private function validateAdminRequest() { |
| 273 | |
| 274 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 275 | wp_die(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Validate frontend ajax request. |
| 281 | * |
| 282 | * @since 2.9.0 |
| 283 | */ |
| 284 | private function validateFrontendRequest() { |
| 285 | $formId = absint( $_POST['give-form-id'] ); |
| 286 | |
| 287 | if ( ! $formId || ! give_verify_donation_form_nonce( give_clean( $_POST['give-form-hash'] ), $formId ) ) { |
| 288 | wp_die(); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 |