@@ -11,674 +11,674 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | // Quickfix to address https://events.codebasehq.com/projects/event-espresso/tickets/11089 ASAP |
| 13 | 13 | if (! function_exists('mb_strcut')) { |
| 14 | - /** |
|
| 15 | - * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
|
| 16 | - * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
|
| 17 | - * @param $string |
|
| 18 | - * @param $start |
|
| 19 | - * @param $length |
|
| 20 | - * @return bool|string |
|
| 21 | - */ |
|
| 22 | - function mb_strcut($string, $start, $length = null) |
|
| 23 | - { |
|
| 24 | - return mb_substr($string, $start, $length); |
|
| 25 | - } |
|
| 14 | + /** |
|
| 15 | + * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
|
| 16 | + * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
|
| 17 | + * @param $string |
|
| 18 | + * @param $start |
|
| 19 | + * @param $length |
|
| 20 | + * @return bool|string |
|
| 21 | + */ |
|
| 22 | + function mb_strcut($string, $start, $length = null) |
|
| 23 | + { |
|
| 24 | + return mb_substr($string, $start, $length); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | class EEG_Paypal_Express extends EE_Offsite_Gateway |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Merchant API Username. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $_api_username; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Merchant API Password. |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - protected $_api_password; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * API Signature. |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - protected $_api_signature; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Request Shipping address on PP checkout page. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $_request_shipping_addr; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Business/personal logo. |
|
| 60 | - * |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - protected $_image_url; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * gateway URL variable |
|
| 67 | - * |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - protected $_base_gateway_url = ''; |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * EEG_Paypal_Express constructor. |
|
| 76 | - */ |
|
| 77 | - public function __construct() |
|
| 78 | - { |
|
| 79 | - $this->_currencies_supported = array( |
|
| 80 | - 'USD', |
|
| 81 | - 'AUD', |
|
| 82 | - 'BRL', |
|
| 83 | - 'CAD', |
|
| 84 | - 'CZK', |
|
| 85 | - 'DKK', |
|
| 86 | - 'EUR', |
|
| 87 | - 'HKD', |
|
| 88 | - 'HUF', |
|
| 89 | - 'ILS', |
|
| 90 | - 'JPY', |
|
| 91 | - 'MYR', |
|
| 92 | - 'MXN', |
|
| 93 | - 'NOK', |
|
| 94 | - 'NZD', |
|
| 95 | - 'PHP', |
|
| 96 | - 'PLN', |
|
| 97 | - 'GBP', |
|
| 98 | - 'RUB', |
|
| 99 | - 'SGD', |
|
| 100 | - 'SEK', |
|
| 101 | - 'CHF', |
|
| 102 | - 'TWD', |
|
| 103 | - 'THB', |
|
| 104 | - 'TRY', |
|
| 105 | - 'INR', |
|
| 106 | - ); |
|
| 107 | - parent::__construct(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
| 114 | - * |
|
| 115 | - * @param array $settings_array |
|
| 116 | - */ |
|
| 117 | - public function set_settings($settings_array) |
|
| 118 | - { |
|
| 119 | - parent::set_settings($settings_array); |
|
| 120 | - // Redirect URL. |
|
| 121 | - $this->_base_gateway_url = $this->_debug_mode |
|
| 122 | - ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
| 123 | - : 'https://api-3t.paypal.com/nvp'; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param EEI_Payment $payment |
|
| 130 | - * @param array $billing_info |
|
| 131 | - * @param string $return_url |
|
| 132 | - * @param string $notify_url |
|
| 133 | - * @param string $cancel_url |
|
| 134 | - * @return \EE_Payment|\EEI_Payment |
|
| 135 | - * @throws \EE_Error |
|
| 136 | - */ |
|
| 137 | - public function set_redirection_info( |
|
| 138 | - $payment, |
|
| 139 | - $billing_info = array(), |
|
| 140 | - $return_url = null, |
|
| 141 | - $notify_url = null, |
|
| 142 | - $cancel_url = null |
|
| 143 | - ) { |
|
| 144 | - if (! $payment instanceof EEI_Payment) { |
|
| 145 | - $payment->set_gateway_response( |
|
| 146 | - esc_html__( |
|
| 147 | - 'Error. No associated payment was found.', |
|
| 148 | - 'event_espresso' |
|
| 149 | - ) |
|
| 150 | - ); |
|
| 151 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 152 | - return $payment; |
|
| 153 | - } |
|
| 154 | - $transaction = $payment->transaction(); |
|
| 155 | - if (! $transaction instanceof EEI_Transaction) { |
|
| 156 | - $payment->set_gateway_response( |
|
| 157 | - esc_html__( |
|
| 158 | - 'Could not process this payment because it has no associated transaction.', |
|
| 159 | - 'event_espresso' |
|
| 160 | - ) |
|
| 161 | - ); |
|
| 162 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 163 | - return $payment; |
|
| 164 | - } |
|
| 165 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
| 166 | - $order_description = mb_strcut($gateway_formatter->formatOrderDescription($payment), 0, 127); |
|
| 167 | - $primary_registration = $transaction->primary_registration(); |
|
| 168 | - $primary_attendee = $primary_registration instanceof EE_Registration |
|
| 169 | - ? $primary_registration->attendee() |
|
| 170 | - : false; |
|
| 171 | - $locale = explode('-', get_bloginfo('language')); |
|
| 172 | - // Gather request parameters. |
|
| 173 | - $token_request_dtls = array( |
|
| 174 | - 'METHOD' => 'SetExpressCheckout', |
|
| 175 | - 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
| 176 | - 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
| 177 | - 'PAYMENTREQUEST_0_DESC' => $order_description, |
|
| 178 | - 'RETURNURL' => $return_url, |
|
| 179 | - 'CANCELURL' => $cancel_url, |
|
| 180 | - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
| 181 | - // Buyer does not need to create a PayPal account to check out. |
|
| 182 | - // This is referred to as PayPal Account Optional. |
|
| 183 | - 'SOLUTIONTYPE' => 'Sole', |
|
| 184 | - // Locale of the pages displayed by PayPal during Express Checkout. |
|
| 185 | - 'LOCALECODE' => $locale[1] |
|
| 186 | - ); |
|
| 187 | - // Show itemized list. |
|
| 188 | - $itemized_list = $this->itemize_list($payment, $transaction); |
|
| 189 | - $token_request_dtls = array_merge($token_request_dtls, $itemized_list); |
|
| 190 | - // Automatically filling out shipping and contact information. |
|
| 191 | - if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
| 192 | - // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
| 193 | - $token_request_dtls['NOSHIPPING'] = '2'; |
|
| 194 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
| 195 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
| 196 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
| 197 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
| 198 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
| 199 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
| 200 | - $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
| 201 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
| 202 | - } elseif (! $this->_request_shipping_addr) { |
|
| 203 | - // Do not request shipping details on the PP Checkout page. |
|
| 204 | - $token_request_dtls['NOSHIPPING'] = '1'; |
|
| 205 | - $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
|
| 206 | - } |
|
| 207 | - // Used a business/personal logo on the PayPal page. |
|
| 208 | - if (! empty($this->_image_url)) { |
|
| 209 | - $token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
| 210 | - } |
|
| 211 | - $token_request_dtls = apply_filters( |
|
| 212 | - 'FHEE__EEG_Paypal_Express__set_redirection_info__arguments', |
|
| 213 | - $token_request_dtls, |
|
| 214 | - $this |
|
| 215 | - ); |
|
| 216 | - // Request PayPal token. |
|
| 217 | - $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
| 218 | - $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
| 219 | - $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) |
|
| 220 | - ? $token_rstatus['args'] |
|
| 221 | - : array(); |
|
| 222 | - if ($token_rstatus['status']) { |
|
| 223 | - // We got the Token so we may continue with the payment and redirect the client. |
|
| 224 | - $payment->set_details($response_args); |
|
| 225 | - $gateway_url = $this->_debug_mode ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
| 226 | - $payment->set_redirect_url( |
|
| 227 | - $gateway_url |
|
| 228 | - . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' |
|
| 229 | - . $response_args['TOKEN'] |
|
| 230 | - ); |
|
| 231 | - } else { |
|
| 232 | - if (isset($response_args['L_ERRORCODE'])) { |
|
| 233 | - $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
| 234 | - } else { |
|
| 235 | - $payment->set_gateway_response( |
|
| 236 | - esc_html__( |
|
| 237 | - 'Error occurred while trying to setup the Express Checkout.', |
|
| 238 | - 'event_espresso' |
|
| 239 | - ) |
|
| 240 | - ); |
|
| 241 | - } |
|
| 242 | - $payment->set_details($response_args); |
|
| 243 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 244 | - } |
|
| 245 | - return $payment; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param array $update_info { |
|
| 252 | - * @type string $gateway_txn_id |
|
| 253 | - * @type string status an EEMI_Payment status |
|
| 254 | - * } |
|
| 255 | - * @param EEI_Transaction $transaction |
|
| 256 | - * @return EEI_Payment |
|
| 257 | - */ |
|
| 258 | - public function handle_payment_update($update_info, $transaction) |
|
| 259 | - { |
|
| 260 | - $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : null; |
|
| 261 | - if ($payment instanceof EEI_Payment) { |
|
| 262 | - $this->log(array('Return from Authorization' => $update_info), $payment); |
|
| 263 | - $transaction = $payment->transaction(); |
|
| 264 | - if (! $transaction instanceof EEI_Transaction) { |
|
| 265 | - $payment->set_gateway_response( |
|
| 266 | - esc_html__( |
|
| 267 | - 'Could not process this payment because it has no associated transaction.', |
|
| 268 | - 'event_espresso' |
|
| 269 | - ) |
|
| 270 | - ); |
|
| 271 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 272 | - return $payment; |
|
| 273 | - } |
|
| 274 | - $primary_registrant = $transaction->primary_registration(); |
|
| 275 | - $payment_details = $payment->details(); |
|
| 276 | - // Check if we still have the token. |
|
| 277 | - if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
| 278 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 279 | - return $payment; |
|
| 280 | - } |
|
| 281 | - $cdetails_request_dtls = array( |
|
| 282 | - 'METHOD' => 'GetExpressCheckoutDetails', |
|
| 283 | - 'TOKEN' => $payment_details['TOKEN'], |
|
| 284 | - ); |
|
| 285 | - // Request Customer Details. |
|
| 286 | - $cdetails_request_response = $this->_ppExpress_request( |
|
| 287 | - $cdetails_request_dtls, |
|
| 288 | - 'Customer Details', |
|
| 289 | - $payment |
|
| 290 | - ); |
|
| 291 | - $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
| 292 | - $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) |
|
| 293 | - ? $cdetails_rstatus['args'] |
|
| 294 | - : array(); |
|
| 295 | - if ($cdetails_rstatus['status']) { |
|
| 296 | - // We got the PayerID so now we can Complete the transaction. |
|
| 297 | - $docheckout_request_dtls = array( |
|
| 298 | - 'METHOD' => 'DoExpressCheckoutPayment', |
|
| 299 | - 'PAYERID' => $cdata_response_args['PAYERID'], |
|
| 300 | - 'TOKEN' => $payment_details['TOKEN'], |
|
| 301 | - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
| 302 | - 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
| 303 | - 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
| 304 | - ); |
|
| 305 | - // Include itemized list. |
|
| 306 | - $itemized_list = $this->itemize_list( |
|
| 307 | - $payment, |
|
| 308 | - $transaction, |
|
| 309 | - $cdata_response_args |
|
| 310 | - ); |
|
| 311 | - $docheckout_request_dtls = array_merge($docheckout_request_dtls, $itemized_list); |
|
| 312 | - // Payment Checkout/Capture. |
|
| 313 | - $docheckout_request_response = $this->_ppExpress_request( |
|
| 314 | - $docheckout_request_dtls, |
|
| 315 | - 'Do Payment', |
|
| 316 | - $payment |
|
| 317 | - ); |
|
| 318 | - $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
| 319 | - $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) |
|
| 320 | - ? $docheckout_rstatus['args'] |
|
| 321 | - : array(); |
|
| 322 | - if ($docheckout_rstatus['status']) { |
|
| 323 | - // All is well, payment approved. |
|
| 324 | - $primary_registration_code = $primary_registrant instanceof EE_Registration ? |
|
| 325 | - $primary_registrant->reg_code() |
|
| 326 | - : ''; |
|
| 327 | - $payment->set_extra_accntng($primary_registration_code); |
|
| 328 | - $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) |
|
| 329 | - ? (float) $docheckout_response_args['PAYMENTINFO_0_AMT'] |
|
| 330 | - : 0); |
|
| 331 | - $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
| 332 | - ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] |
|
| 333 | - : null); |
|
| 334 | - $payment->set_details($cdata_response_args); |
|
| 335 | - $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) |
|
| 336 | - ? $docheckout_response_args['PAYMENTINFO_0_ACK'] |
|
| 337 | - : ''); |
|
| 338 | - $payment->set_status($this->_pay_model->approved_status()); |
|
| 339 | - } else { |
|
| 340 | - if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
| 341 | - $payment->set_gateway_response( |
|
| 342 | - $docheckout_response_args['L_ERRORCODE'] |
|
| 343 | - . '; ' |
|
| 344 | - . $docheckout_response_args['L_SHORTMESSAGE'] |
|
| 345 | - ); |
|
| 346 | - } else { |
|
| 347 | - $payment->set_gateway_response( |
|
| 348 | - esc_html__( |
|
| 349 | - 'Error occurred while trying to Capture the funds.', |
|
| 350 | - 'event_espresso' |
|
| 351 | - ) |
|
| 352 | - ); |
|
| 353 | - } |
|
| 354 | - $payment->set_details($docheckout_response_args); |
|
| 355 | - $payment->set_status($this->_pay_model->declined_status()); |
|
| 356 | - } |
|
| 357 | - } else { |
|
| 358 | - if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
| 359 | - $payment->set_gateway_response( |
|
| 360 | - $cdata_response_args['L_ERRORCODE'] |
|
| 361 | - . '; ' |
|
| 362 | - . $cdata_response_args['L_SHORTMESSAGE'] |
|
| 363 | - ); |
|
| 364 | - } else { |
|
| 365 | - $payment->set_gateway_response( |
|
| 366 | - esc_html__( |
|
| 367 | - 'Error occurred while trying to get payment Details from PayPal.', |
|
| 368 | - 'event_espresso' |
|
| 369 | - ) |
|
| 370 | - ); |
|
| 371 | - } |
|
| 372 | - $payment->set_details($cdata_response_args); |
|
| 373 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 374 | - } |
|
| 375 | - } else { |
|
| 376 | - $payment->set_gateway_response( |
|
| 377 | - esc_html__( |
|
| 378 | - 'Error occurred while trying to process the payment.', |
|
| 379 | - 'event_espresso' |
|
| 380 | - ) |
|
| 381 | - ); |
|
| 382 | - $payment->set_status($this->_pay_model->failed_status()); |
|
| 383 | - } |
|
| 384 | - return $payment; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Make a list of items that are in the giver transaction. |
|
| 391 | - * |
|
| 392 | - * @param EEI_Payment $payment |
|
| 393 | - * @param EEI_Transaction $transaction |
|
| 394 | - * @param array $request_response_args Data from a previous communication with PP. |
|
| 395 | - * @return array |
|
| 396 | - */ |
|
| 397 | - public function itemize_list(EEI_Payment $payment, EEI_Transaction $transaction, $request_response_args = array()) |
|
| 398 | - { |
|
| 399 | - $itemized_list = array(); |
|
| 400 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
| 401 | - // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
|
| 402 | - if (! empty($request_response_args) |
|
| 403 | - && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
|
| 404 | - && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
|
| 405 | - ) { |
|
| 406 | - foreach ($request_response_args as $arg_key => $arg_val) { |
|
| 407 | - if (strpos($arg_key, 'PAYMENTREQUEST_') !== false |
|
| 408 | - && strpos($arg_key, 'NOTIFYURL') === false |
|
| 409 | - ) { |
|
| 410 | - $itemized_list[ $arg_key ] = $arg_val; |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - // If we got only a few Items then something is not right. |
|
| 414 | - if (count($itemized_list) > 2) { |
|
| 415 | - return $itemized_list; |
|
| 416 | - } else { |
|
| 417 | - if (WP_DEBUG) { |
|
| 418 | - throw new EE_Error( |
|
| 419 | - sprintf( |
|
| 420 | - esc_html__( |
|
| 421 | - // @codingStandardsIgnoreStart |
|
| 422 | - 'Unable to continue with the checkout because a proper purchase list could not be generated. The purchased list we could have sent was %1$s', |
|
| 423 | - // @codingStandardsIgnoreEnd |
|
| 424 | - 'event_espresso' |
|
| 425 | - ), |
|
| 426 | - wp_json_encode($itemized_list) |
|
| 427 | - ) |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - // Reset the list and log an error, maybe allow to try and generate a new list (below). |
|
| 431 | - $itemized_list = array(); |
|
| 432 | - $this->log( |
|
| 433 | - array( |
|
| 434 | - (string) esc_html__( |
|
| 435 | - 'Could not generate a proper item list with:', |
|
| 436 | - 'event_espresso' |
|
| 437 | - ) => $request_response_args |
|
| 438 | - ), |
|
| 439 | - $payment |
|
| 440 | - ); |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - // ...otherwise we generate a new list for this transaction. |
|
| 444 | - if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
| 445 | - $item_num = 0; |
|
| 446 | - $itemized_sum = 0; |
|
| 447 | - $total_line_items = $transaction->total_line_item(); |
|
| 448 | - // Go through each item in the list. |
|
| 449 | - foreach ($total_line_items->get_items() as $line_item) { |
|
| 450 | - if ($line_item instanceof EE_Line_Item) { |
|
| 451 | - // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
| 452 | - if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
| 453 | - continue; |
|
| 454 | - } |
|
| 455 | - $unit_price = $line_item->unit_price(); |
|
| 456 | - $line_item_quantity = $line_item->quantity(); |
|
| 457 | - // This is a discount. |
|
| 458 | - if ($line_item->is_percent()) { |
|
| 459 | - $unit_price = $line_item->total(); |
|
| 460 | - $line_item_quantity = 1; |
|
| 461 | - } |
|
| 462 | - // Item Name. |
|
| 463 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
| 464 | - $gateway_formatter->formatLineItemName($line_item, $payment), |
|
| 465 | - 0, |
|
| 466 | - 127 |
|
| 467 | - ); |
|
| 468 | - // Item description. |
|
| 469 | - $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = mb_strcut( |
|
| 470 | - $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
| 471 | - 0, |
|
| 472 | - 127 |
|
| 473 | - ); |
|
| 474 | - // Cost of individual item. |
|
| 475 | - $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency($unit_price); |
|
| 476 | - // Item Number. |
|
| 477 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
| 478 | - // Item quantity. |
|
| 479 | - $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = $line_item_quantity; |
|
| 480 | - // Digital item is sold. |
|
| 481 | - $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
| 482 | - $itemized_sum += $line_item->total(); |
|
| 483 | - ++$item_num; |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - // Item's sales S/H and tax amount. |
|
| 487 | - $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
| 488 | - $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
| 489 | - $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
| 490 | - $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
| 491 | - $itemized_sum_diff_from_txn_total = round( |
|
| 492 | - $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), |
|
| 493 | - 2 |
|
| 494 | - ); |
|
| 495 | - // If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
| 496 | - // add the difference as an extra line item. |
|
| 497 | - if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
|
| 498 | - // Item Name. |
|
| 499 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
| 500 | - esc_html__( |
|
| 501 | - 'Other (promotion/surcharge/cancellation)', |
|
| 502 | - 'event_espresso' |
|
| 503 | - ), |
|
| 504 | - 0, |
|
| 505 | - 127 |
|
| 506 | - ); |
|
| 507 | - // Item description. |
|
| 508 | - $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = ''; |
|
| 509 | - // Cost of individual item. |
|
| 510 | - $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency( |
|
| 511 | - $itemized_sum_diff_from_txn_total |
|
| 512 | - ); |
|
| 513 | - // Item Number. |
|
| 514 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
| 515 | - // Item quantity. |
|
| 516 | - $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = 1; |
|
| 517 | - // Digital item is sold. |
|
| 518 | - $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
| 519 | - $item_num++; |
|
| 520 | - } |
|
| 521 | - } else { |
|
| 522 | - // Just one Item. |
|
| 523 | - // Item Name. |
|
| 524 | - $itemized_list['L_PAYMENTREQUEST_0_NAME0'] = mb_strcut( |
|
| 525 | - $gateway_formatter->formatPartialPaymentLineItemName($payment), |
|
| 526 | - 0, |
|
| 527 | - 127 |
|
| 528 | - ); |
|
| 529 | - // Item description. |
|
| 530 | - $itemized_list['L_PAYMENTREQUEST_0_DESC0'] = mb_strcut( |
|
| 531 | - $gateway_formatter->formatPartialPaymentLineItemDesc($payment), |
|
| 532 | - 0, |
|
| 533 | - 127 |
|
| 534 | - ); |
|
| 535 | - // Cost of individual item. |
|
| 536 | - $itemized_list['L_PAYMENTREQUEST_0_AMT0'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
| 537 | - // Item Number. |
|
| 538 | - $itemized_list['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
| 539 | - // Item quantity. |
|
| 540 | - $itemized_list['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
| 541 | - // Digital item is sold. |
|
| 542 | - $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
|
| 543 | - // Item's sales S/H and tax amount. |
|
| 544 | - $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
| 545 | - $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
| 546 | - $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
| 547 | - $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
| 548 | - } |
|
| 549 | - return $itemized_list; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Make the Express checkout request. |
|
| 556 | - * |
|
| 557 | - * @param array $request_params |
|
| 558 | - * @param string $request_text |
|
| 559 | - * @param EEI_Payment $payment |
|
| 560 | - * @return mixed |
|
| 561 | - */ |
|
| 562 | - public function _ppExpress_request($request_params, $request_text, $payment) |
|
| 563 | - { |
|
| 564 | - $request_dtls = array( |
|
| 565 | - 'VERSION' => '204.0', |
|
| 566 | - 'USER' => $this->_api_username, |
|
| 567 | - 'PWD' => $this->_api_password, |
|
| 568 | - 'SIGNATURE' => $this->_api_signature, |
|
| 569 | - // EE will blow up if you change this |
|
| 570 | - 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
| 571 | - ); |
|
| 572 | - $dtls = array_merge($request_dtls, $request_params); |
|
| 573 | - $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
| 574 | - // Request Customer Details. |
|
| 575 | - $request_response = wp_remote_post( |
|
| 576 | - $this->_base_gateway_url, |
|
| 577 | - array( |
|
| 578 | - 'method' => 'POST', |
|
| 579 | - 'timeout' => 45, |
|
| 580 | - 'httpversion' => '1.1', |
|
| 581 | - 'cookies' => array(), |
|
| 582 | - 'headers' => array(), |
|
| 583 | - 'body' => http_build_query($dtls, '', '&'), |
|
| 584 | - ) |
|
| 585 | - ); |
|
| 586 | - // Log the response. |
|
| 587 | - $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
| 588 | - return $request_response; |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Check the response status. |
|
| 595 | - * |
|
| 596 | - * @param mixed $request_response |
|
| 597 | - * @return array |
|
| 598 | - */ |
|
| 599 | - public function _ppExpress_check_response($request_response) |
|
| 600 | - { |
|
| 601 | - if (is_wp_error($request_response) || empty($request_response['body'])) { |
|
| 602 | - // If we got here then there was an error in this request. |
|
| 603 | - return array('status' => false, 'args' => $request_response); |
|
| 604 | - } |
|
| 605 | - $response_args = array(); |
|
| 606 | - parse_str(urldecode($request_response['body']), $response_args); |
|
| 607 | - if (! isset($response_args['ACK'])) { |
|
| 608 | - return array('status' => false, 'args' => $request_response); |
|
| 609 | - } |
|
| 610 | - if (( |
|
| 611 | - isset($response_args['PAYERID']) |
|
| 612 | - || isset($response_args['TOKEN']) |
|
| 613 | - || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
| 614 | - || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
| 615 | - ) |
|
| 616 | - && in_array($response_args['ACK'], array('Success', 'SuccessWithWarning'), true) |
|
| 617 | - ) { |
|
| 618 | - // Response status OK, return response parameters for further processing. |
|
| 619 | - return array('status' => true, 'args' => $response_args); |
|
| 620 | - } |
|
| 621 | - $errors = $this->_get_errors($response_args); |
|
| 622 | - return array('status' => false, 'args' => $errors); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * Log a "Cleared" request. |
|
| 629 | - * |
|
| 630 | - * @param array $request |
|
| 631 | - * @param EEI_Payment $payment |
|
| 632 | - * @param string $info |
|
| 633 | - * @return void |
|
| 634 | - */ |
|
| 635 | - private function _log_clean_request($request, $payment, $info) |
|
| 636 | - { |
|
| 637 | - $cleaned_request_data = $request; |
|
| 638 | - unset($cleaned_request_data['PWD'], $cleaned_request_data['USER'], $cleaned_request_data['SIGNATURE']); |
|
| 639 | - $this->log(array($info => $cleaned_request_data), $payment); |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Get error from the response data. |
|
| 646 | - * |
|
| 647 | - * @param array $data_array |
|
| 648 | - * @return array |
|
| 649 | - */ |
|
| 650 | - private function _get_errors($data_array) |
|
| 651 | - { |
|
| 652 | - $errors = array(); |
|
| 653 | - $n = 0; |
|
| 654 | - while (isset($data_array[ "L_ERRORCODE{$n}" ])) { |
|
| 655 | - $l_error_code = isset($data_array[ "L_ERRORCODE{$n}" ]) |
|
| 656 | - ? $data_array[ "L_ERRORCODE{$n}" ] |
|
| 657 | - : ''; |
|
| 658 | - $l_severity_code = isset($data_array[ "L_SEVERITYCODE{$n}" ]) |
|
| 659 | - ? $data_array[ "L_SEVERITYCODE{$n}" ] |
|
| 660 | - : ''; |
|
| 661 | - $l_short_message = isset($data_array[ "L_SHORTMESSAGE{$n}" ]) |
|
| 662 | - ? $data_array[ "L_SHORTMESSAGE{$n}" ] |
|
| 663 | - : ''; |
|
| 664 | - $l_long_message = isset($data_array[ "L_LONGMESSAGE{$n}" ]) |
|
| 665 | - ? $data_array[ "L_LONGMESSAGE{$n}" ] |
|
| 666 | - : ''; |
|
| 667 | - if ($n === 0) { |
|
| 668 | - $errors = array( |
|
| 669 | - 'L_ERRORCODE' => $l_error_code, |
|
| 670 | - 'L_SHORTMESSAGE' => $l_short_message, |
|
| 671 | - 'L_LONGMESSAGE' => $l_long_message, |
|
| 672 | - 'L_SEVERITYCODE' => $l_severity_code, |
|
| 673 | - ); |
|
| 674 | - } else { |
|
| 675 | - $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
| 676 | - $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
| 677 | - $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
| 678 | - $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
| 679 | - } |
|
| 680 | - $n++; |
|
| 681 | - } |
|
| 682 | - return $errors; |
|
| 683 | - } |
|
| 30 | + /** |
|
| 31 | + * Merchant API Username. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $_api_username; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Merchant API Password. |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + protected $_api_password; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * API Signature. |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + protected $_api_signature; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Request Shipping address on PP checkout page. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $_request_shipping_addr; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Business/personal logo. |
|
| 60 | + * |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + protected $_image_url; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * gateway URL variable |
|
| 67 | + * |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + protected $_base_gateway_url = ''; |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * EEG_Paypal_Express constructor. |
|
| 76 | + */ |
|
| 77 | + public function __construct() |
|
| 78 | + { |
|
| 79 | + $this->_currencies_supported = array( |
|
| 80 | + 'USD', |
|
| 81 | + 'AUD', |
|
| 82 | + 'BRL', |
|
| 83 | + 'CAD', |
|
| 84 | + 'CZK', |
|
| 85 | + 'DKK', |
|
| 86 | + 'EUR', |
|
| 87 | + 'HKD', |
|
| 88 | + 'HUF', |
|
| 89 | + 'ILS', |
|
| 90 | + 'JPY', |
|
| 91 | + 'MYR', |
|
| 92 | + 'MXN', |
|
| 93 | + 'NOK', |
|
| 94 | + 'NZD', |
|
| 95 | + 'PHP', |
|
| 96 | + 'PLN', |
|
| 97 | + 'GBP', |
|
| 98 | + 'RUB', |
|
| 99 | + 'SGD', |
|
| 100 | + 'SEK', |
|
| 101 | + 'CHF', |
|
| 102 | + 'TWD', |
|
| 103 | + 'THB', |
|
| 104 | + 'TRY', |
|
| 105 | + 'INR', |
|
| 106 | + ); |
|
| 107 | + parent::__construct(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
| 114 | + * |
|
| 115 | + * @param array $settings_array |
|
| 116 | + */ |
|
| 117 | + public function set_settings($settings_array) |
|
| 118 | + { |
|
| 119 | + parent::set_settings($settings_array); |
|
| 120 | + // Redirect URL. |
|
| 121 | + $this->_base_gateway_url = $this->_debug_mode |
|
| 122 | + ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
| 123 | + : 'https://api-3t.paypal.com/nvp'; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param EEI_Payment $payment |
|
| 130 | + * @param array $billing_info |
|
| 131 | + * @param string $return_url |
|
| 132 | + * @param string $notify_url |
|
| 133 | + * @param string $cancel_url |
|
| 134 | + * @return \EE_Payment|\EEI_Payment |
|
| 135 | + * @throws \EE_Error |
|
| 136 | + */ |
|
| 137 | + public function set_redirection_info( |
|
| 138 | + $payment, |
|
| 139 | + $billing_info = array(), |
|
| 140 | + $return_url = null, |
|
| 141 | + $notify_url = null, |
|
| 142 | + $cancel_url = null |
|
| 143 | + ) { |
|
| 144 | + if (! $payment instanceof EEI_Payment) { |
|
| 145 | + $payment->set_gateway_response( |
|
| 146 | + esc_html__( |
|
| 147 | + 'Error. No associated payment was found.', |
|
| 148 | + 'event_espresso' |
|
| 149 | + ) |
|
| 150 | + ); |
|
| 151 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 152 | + return $payment; |
|
| 153 | + } |
|
| 154 | + $transaction = $payment->transaction(); |
|
| 155 | + if (! $transaction instanceof EEI_Transaction) { |
|
| 156 | + $payment->set_gateway_response( |
|
| 157 | + esc_html__( |
|
| 158 | + 'Could not process this payment because it has no associated transaction.', |
|
| 159 | + 'event_espresso' |
|
| 160 | + ) |
|
| 161 | + ); |
|
| 162 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 163 | + return $payment; |
|
| 164 | + } |
|
| 165 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
| 166 | + $order_description = mb_strcut($gateway_formatter->formatOrderDescription($payment), 0, 127); |
|
| 167 | + $primary_registration = $transaction->primary_registration(); |
|
| 168 | + $primary_attendee = $primary_registration instanceof EE_Registration |
|
| 169 | + ? $primary_registration->attendee() |
|
| 170 | + : false; |
|
| 171 | + $locale = explode('-', get_bloginfo('language')); |
|
| 172 | + // Gather request parameters. |
|
| 173 | + $token_request_dtls = array( |
|
| 174 | + 'METHOD' => 'SetExpressCheckout', |
|
| 175 | + 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
| 176 | + 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
| 177 | + 'PAYMENTREQUEST_0_DESC' => $order_description, |
|
| 178 | + 'RETURNURL' => $return_url, |
|
| 179 | + 'CANCELURL' => $cancel_url, |
|
| 180 | + 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
| 181 | + // Buyer does not need to create a PayPal account to check out. |
|
| 182 | + // This is referred to as PayPal Account Optional. |
|
| 183 | + 'SOLUTIONTYPE' => 'Sole', |
|
| 184 | + // Locale of the pages displayed by PayPal during Express Checkout. |
|
| 185 | + 'LOCALECODE' => $locale[1] |
|
| 186 | + ); |
|
| 187 | + // Show itemized list. |
|
| 188 | + $itemized_list = $this->itemize_list($payment, $transaction); |
|
| 189 | + $token_request_dtls = array_merge($token_request_dtls, $itemized_list); |
|
| 190 | + // Automatically filling out shipping and contact information. |
|
| 191 | + if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
| 192 | + // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
| 193 | + $token_request_dtls['NOSHIPPING'] = '2'; |
|
| 194 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
| 195 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
| 196 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
| 197 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
| 198 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
| 199 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
| 200 | + $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
| 201 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
| 202 | + } elseif (! $this->_request_shipping_addr) { |
|
| 203 | + // Do not request shipping details on the PP Checkout page. |
|
| 204 | + $token_request_dtls['NOSHIPPING'] = '1'; |
|
| 205 | + $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
|
| 206 | + } |
|
| 207 | + // Used a business/personal logo on the PayPal page. |
|
| 208 | + if (! empty($this->_image_url)) { |
|
| 209 | + $token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
| 210 | + } |
|
| 211 | + $token_request_dtls = apply_filters( |
|
| 212 | + 'FHEE__EEG_Paypal_Express__set_redirection_info__arguments', |
|
| 213 | + $token_request_dtls, |
|
| 214 | + $this |
|
| 215 | + ); |
|
| 216 | + // Request PayPal token. |
|
| 217 | + $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
| 218 | + $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
| 219 | + $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) |
|
| 220 | + ? $token_rstatus['args'] |
|
| 221 | + : array(); |
|
| 222 | + if ($token_rstatus['status']) { |
|
| 223 | + // We got the Token so we may continue with the payment and redirect the client. |
|
| 224 | + $payment->set_details($response_args); |
|
| 225 | + $gateway_url = $this->_debug_mode ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
| 226 | + $payment->set_redirect_url( |
|
| 227 | + $gateway_url |
|
| 228 | + . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' |
|
| 229 | + . $response_args['TOKEN'] |
|
| 230 | + ); |
|
| 231 | + } else { |
|
| 232 | + if (isset($response_args['L_ERRORCODE'])) { |
|
| 233 | + $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
| 234 | + } else { |
|
| 235 | + $payment->set_gateway_response( |
|
| 236 | + esc_html__( |
|
| 237 | + 'Error occurred while trying to setup the Express Checkout.', |
|
| 238 | + 'event_espresso' |
|
| 239 | + ) |
|
| 240 | + ); |
|
| 241 | + } |
|
| 242 | + $payment->set_details($response_args); |
|
| 243 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 244 | + } |
|
| 245 | + return $payment; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param array $update_info { |
|
| 252 | + * @type string $gateway_txn_id |
|
| 253 | + * @type string status an EEMI_Payment status |
|
| 254 | + * } |
|
| 255 | + * @param EEI_Transaction $transaction |
|
| 256 | + * @return EEI_Payment |
|
| 257 | + */ |
|
| 258 | + public function handle_payment_update($update_info, $transaction) |
|
| 259 | + { |
|
| 260 | + $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : null; |
|
| 261 | + if ($payment instanceof EEI_Payment) { |
|
| 262 | + $this->log(array('Return from Authorization' => $update_info), $payment); |
|
| 263 | + $transaction = $payment->transaction(); |
|
| 264 | + if (! $transaction instanceof EEI_Transaction) { |
|
| 265 | + $payment->set_gateway_response( |
|
| 266 | + esc_html__( |
|
| 267 | + 'Could not process this payment because it has no associated transaction.', |
|
| 268 | + 'event_espresso' |
|
| 269 | + ) |
|
| 270 | + ); |
|
| 271 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 272 | + return $payment; |
|
| 273 | + } |
|
| 274 | + $primary_registrant = $transaction->primary_registration(); |
|
| 275 | + $payment_details = $payment->details(); |
|
| 276 | + // Check if we still have the token. |
|
| 277 | + if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
| 278 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 279 | + return $payment; |
|
| 280 | + } |
|
| 281 | + $cdetails_request_dtls = array( |
|
| 282 | + 'METHOD' => 'GetExpressCheckoutDetails', |
|
| 283 | + 'TOKEN' => $payment_details['TOKEN'], |
|
| 284 | + ); |
|
| 285 | + // Request Customer Details. |
|
| 286 | + $cdetails_request_response = $this->_ppExpress_request( |
|
| 287 | + $cdetails_request_dtls, |
|
| 288 | + 'Customer Details', |
|
| 289 | + $payment |
|
| 290 | + ); |
|
| 291 | + $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
| 292 | + $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) |
|
| 293 | + ? $cdetails_rstatus['args'] |
|
| 294 | + : array(); |
|
| 295 | + if ($cdetails_rstatus['status']) { |
|
| 296 | + // We got the PayerID so now we can Complete the transaction. |
|
| 297 | + $docheckout_request_dtls = array( |
|
| 298 | + 'METHOD' => 'DoExpressCheckoutPayment', |
|
| 299 | + 'PAYERID' => $cdata_response_args['PAYERID'], |
|
| 300 | + 'TOKEN' => $payment_details['TOKEN'], |
|
| 301 | + 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
| 302 | + 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
| 303 | + 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
| 304 | + ); |
|
| 305 | + // Include itemized list. |
|
| 306 | + $itemized_list = $this->itemize_list( |
|
| 307 | + $payment, |
|
| 308 | + $transaction, |
|
| 309 | + $cdata_response_args |
|
| 310 | + ); |
|
| 311 | + $docheckout_request_dtls = array_merge($docheckout_request_dtls, $itemized_list); |
|
| 312 | + // Payment Checkout/Capture. |
|
| 313 | + $docheckout_request_response = $this->_ppExpress_request( |
|
| 314 | + $docheckout_request_dtls, |
|
| 315 | + 'Do Payment', |
|
| 316 | + $payment |
|
| 317 | + ); |
|
| 318 | + $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
| 319 | + $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) |
|
| 320 | + ? $docheckout_rstatus['args'] |
|
| 321 | + : array(); |
|
| 322 | + if ($docheckout_rstatus['status']) { |
|
| 323 | + // All is well, payment approved. |
|
| 324 | + $primary_registration_code = $primary_registrant instanceof EE_Registration ? |
|
| 325 | + $primary_registrant->reg_code() |
|
| 326 | + : ''; |
|
| 327 | + $payment->set_extra_accntng($primary_registration_code); |
|
| 328 | + $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) |
|
| 329 | + ? (float) $docheckout_response_args['PAYMENTINFO_0_AMT'] |
|
| 330 | + : 0); |
|
| 331 | + $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
| 332 | + ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] |
|
| 333 | + : null); |
|
| 334 | + $payment->set_details($cdata_response_args); |
|
| 335 | + $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) |
|
| 336 | + ? $docheckout_response_args['PAYMENTINFO_0_ACK'] |
|
| 337 | + : ''); |
|
| 338 | + $payment->set_status($this->_pay_model->approved_status()); |
|
| 339 | + } else { |
|
| 340 | + if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
| 341 | + $payment->set_gateway_response( |
|
| 342 | + $docheckout_response_args['L_ERRORCODE'] |
|
| 343 | + . '; ' |
|
| 344 | + . $docheckout_response_args['L_SHORTMESSAGE'] |
|
| 345 | + ); |
|
| 346 | + } else { |
|
| 347 | + $payment->set_gateway_response( |
|
| 348 | + esc_html__( |
|
| 349 | + 'Error occurred while trying to Capture the funds.', |
|
| 350 | + 'event_espresso' |
|
| 351 | + ) |
|
| 352 | + ); |
|
| 353 | + } |
|
| 354 | + $payment->set_details($docheckout_response_args); |
|
| 355 | + $payment->set_status($this->_pay_model->declined_status()); |
|
| 356 | + } |
|
| 357 | + } else { |
|
| 358 | + if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
| 359 | + $payment->set_gateway_response( |
|
| 360 | + $cdata_response_args['L_ERRORCODE'] |
|
| 361 | + . '; ' |
|
| 362 | + . $cdata_response_args['L_SHORTMESSAGE'] |
|
| 363 | + ); |
|
| 364 | + } else { |
|
| 365 | + $payment->set_gateway_response( |
|
| 366 | + esc_html__( |
|
| 367 | + 'Error occurred while trying to get payment Details from PayPal.', |
|
| 368 | + 'event_espresso' |
|
| 369 | + ) |
|
| 370 | + ); |
|
| 371 | + } |
|
| 372 | + $payment->set_details($cdata_response_args); |
|
| 373 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 374 | + } |
|
| 375 | + } else { |
|
| 376 | + $payment->set_gateway_response( |
|
| 377 | + esc_html__( |
|
| 378 | + 'Error occurred while trying to process the payment.', |
|
| 379 | + 'event_espresso' |
|
| 380 | + ) |
|
| 381 | + ); |
|
| 382 | + $payment->set_status($this->_pay_model->failed_status()); |
|
| 383 | + } |
|
| 384 | + return $payment; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Make a list of items that are in the giver transaction. |
|
| 391 | + * |
|
| 392 | + * @param EEI_Payment $payment |
|
| 393 | + * @param EEI_Transaction $transaction |
|
| 394 | + * @param array $request_response_args Data from a previous communication with PP. |
|
| 395 | + * @return array |
|
| 396 | + */ |
|
| 397 | + public function itemize_list(EEI_Payment $payment, EEI_Transaction $transaction, $request_response_args = array()) |
|
| 398 | + { |
|
| 399 | + $itemized_list = array(); |
|
| 400 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
| 401 | + // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
|
| 402 | + if (! empty($request_response_args) |
|
| 403 | + && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
|
| 404 | + && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
|
| 405 | + ) { |
|
| 406 | + foreach ($request_response_args as $arg_key => $arg_val) { |
|
| 407 | + if (strpos($arg_key, 'PAYMENTREQUEST_') !== false |
|
| 408 | + && strpos($arg_key, 'NOTIFYURL') === false |
|
| 409 | + ) { |
|
| 410 | + $itemized_list[ $arg_key ] = $arg_val; |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + // If we got only a few Items then something is not right. |
|
| 414 | + if (count($itemized_list) > 2) { |
|
| 415 | + return $itemized_list; |
|
| 416 | + } else { |
|
| 417 | + if (WP_DEBUG) { |
|
| 418 | + throw new EE_Error( |
|
| 419 | + sprintf( |
|
| 420 | + esc_html__( |
|
| 421 | + // @codingStandardsIgnoreStart |
|
| 422 | + 'Unable to continue with the checkout because a proper purchase list could not be generated. The purchased list we could have sent was %1$s', |
|
| 423 | + // @codingStandardsIgnoreEnd |
|
| 424 | + 'event_espresso' |
|
| 425 | + ), |
|
| 426 | + wp_json_encode($itemized_list) |
|
| 427 | + ) |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + // Reset the list and log an error, maybe allow to try and generate a new list (below). |
|
| 431 | + $itemized_list = array(); |
|
| 432 | + $this->log( |
|
| 433 | + array( |
|
| 434 | + (string) esc_html__( |
|
| 435 | + 'Could not generate a proper item list with:', |
|
| 436 | + 'event_espresso' |
|
| 437 | + ) => $request_response_args |
|
| 438 | + ), |
|
| 439 | + $payment |
|
| 440 | + ); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + // ...otherwise we generate a new list for this transaction. |
|
| 444 | + if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
| 445 | + $item_num = 0; |
|
| 446 | + $itemized_sum = 0; |
|
| 447 | + $total_line_items = $transaction->total_line_item(); |
|
| 448 | + // Go through each item in the list. |
|
| 449 | + foreach ($total_line_items->get_items() as $line_item) { |
|
| 450 | + if ($line_item instanceof EE_Line_Item) { |
|
| 451 | + // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
| 452 | + if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
| 453 | + continue; |
|
| 454 | + } |
|
| 455 | + $unit_price = $line_item->unit_price(); |
|
| 456 | + $line_item_quantity = $line_item->quantity(); |
|
| 457 | + // This is a discount. |
|
| 458 | + if ($line_item->is_percent()) { |
|
| 459 | + $unit_price = $line_item->total(); |
|
| 460 | + $line_item_quantity = 1; |
|
| 461 | + } |
|
| 462 | + // Item Name. |
|
| 463 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
| 464 | + $gateway_formatter->formatLineItemName($line_item, $payment), |
|
| 465 | + 0, |
|
| 466 | + 127 |
|
| 467 | + ); |
|
| 468 | + // Item description. |
|
| 469 | + $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = mb_strcut( |
|
| 470 | + $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
| 471 | + 0, |
|
| 472 | + 127 |
|
| 473 | + ); |
|
| 474 | + // Cost of individual item. |
|
| 475 | + $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency($unit_price); |
|
| 476 | + // Item Number. |
|
| 477 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
| 478 | + // Item quantity. |
|
| 479 | + $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = $line_item_quantity; |
|
| 480 | + // Digital item is sold. |
|
| 481 | + $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
| 482 | + $itemized_sum += $line_item->total(); |
|
| 483 | + ++$item_num; |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + // Item's sales S/H and tax amount. |
|
| 487 | + $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
| 488 | + $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
| 489 | + $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
| 490 | + $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
| 491 | + $itemized_sum_diff_from_txn_total = round( |
|
| 492 | + $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), |
|
| 493 | + 2 |
|
| 494 | + ); |
|
| 495 | + // If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
| 496 | + // add the difference as an extra line item. |
|
| 497 | + if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
|
| 498 | + // Item Name. |
|
| 499 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
| 500 | + esc_html__( |
|
| 501 | + 'Other (promotion/surcharge/cancellation)', |
|
| 502 | + 'event_espresso' |
|
| 503 | + ), |
|
| 504 | + 0, |
|
| 505 | + 127 |
|
| 506 | + ); |
|
| 507 | + // Item description. |
|
| 508 | + $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = ''; |
|
| 509 | + // Cost of individual item. |
|
| 510 | + $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency( |
|
| 511 | + $itemized_sum_diff_from_txn_total |
|
| 512 | + ); |
|
| 513 | + // Item Number. |
|
| 514 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
| 515 | + // Item quantity. |
|
| 516 | + $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = 1; |
|
| 517 | + // Digital item is sold. |
|
| 518 | + $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
| 519 | + $item_num++; |
|
| 520 | + } |
|
| 521 | + } else { |
|
| 522 | + // Just one Item. |
|
| 523 | + // Item Name. |
|
| 524 | + $itemized_list['L_PAYMENTREQUEST_0_NAME0'] = mb_strcut( |
|
| 525 | + $gateway_formatter->formatPartialPaymentLineItemName($payment), |
|
| 526 | + 0, |
|
| 527 | + 127 |
|
| 528 | + ); |
|
| 529 | + // Item description. |
|
| 530 | + $itemized_list['L_PAYMENTREQUEST_0_DESC0'] = mb_strcut( |
|
| 531 | + $gateway_formatter->formatPartialPaymentLineItemDesc($payment), |
|
| 532 | + 0, |
|
| 533 | + 127 |
|
| 534 | + ); |
|
| 535 | + // Cost of individual item. |
|
| 536 | + $itemized_list['L_PAYMENTREQUEST_0_AMT0'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
| 537 | + // Item Number. |
|
| 538 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
| 539 | + // Item quantity. |
|
| 540 | + $itemized_list['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
| 541 | + // Digital item is sold. |
|
| 542 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
|
| 543 | + // Item's sales S/H and tax amount. |
|
| 544 | + $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
| 545 | + $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
| 546 | + $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
| 547 | + $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
| 548 | + } |
|
| 549 | + return $itemized_list; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Make the Express checkout request. |
|
| 556 | + * |
|
| 557 | + * @param array $request_params |
|
| 558 | + * @param string $request_text |
|
| 559 | + * @param EEI_Payment $payment |
|
| 560 | + * @return mixed |
|
| 561 | + */ |
|
| 562 | + public function _ppExpress_request($request_params, $request_text, $payment) |
|
| 563 | + { |
|
| 564 | + $request_dtls = array( |
|
| 565 | + 'VERSION' => '204.0', |
|
| 566 | + 'USER' => $this->_api_username, |
|
| 567 | + 'PWD' => $this->_api_password, |
|
| 568 | + 'SIGNATURE' => $this->_api_signature, |
|
| 569 | + // EE will blow up if you change this |
|
| 570 | + 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
| 571 | + ); |
|
| 572 | + $dtls = array_merge($request_dtls, $request_params); |
|
| 573 | + $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
| 574 | + // Request Customer Details. |
|
| 575 | + $request_response = wp_remote_post( |
|
| 576 | + $this->_base_gateway_url, |
|
| 577 | + array( |
|
| 578 | + 'method' => 'POST', |
|
| 579 | + 'timeout' => 45, |
|
| 580 | + 'httpversion' => '1.1', |
|
| 581 | + 'cookies' => array(), |
|
| 582 | + 'headers' => array(), |
|
| 583 | + 'body' => http_build_query($dtls, '', '&'), |
|
| 584 | + ) |
|
| 585 | + ); |
|
| 586 | + // Log the response. |
|
| 587 | + $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
| 588 | + return $request_response; |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Check the response status. |
|
| 595 | + * |
|
| 596 | + * @param mixed $request_response |
|
| 597 | + * @return array |
|
| 598 | + */ |
|
| 599 | + public function _ppExpress_check_response($request_response) |
|
| 600 | + { |
|
| 601 | + if (is_wp_error($request_response) || empty($request_response['body'])) { |
|
| 602 | + // If we got here then there was an error in this request. |
|
| 603 | + return array('status' => false, 'args' => $request_response); |
|
| 604 | + } |
|
| 605 | + $response_args = array(); |
|
| 606 | + parse_str(urldecode($request_response['body']), $response_args); |
|
| 607 | + if (! isset($response_args['ACK'])) { |
|
| 608 | + return array('status' => false, 'args' => $request_response); |
|
| 609 | + } |
|
| 610 | + if (( |
|
| 611 | + isset($response_args['PAYERID']) |
|
| 612 | + || isset($response_args['TOKEN']) |
|
| 613 | + || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
| 614 | + || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
| 615 | + ) |
|
| 616 | + && in_array($response_args['ACK'], array('Success', 'SuccessWithWarning'), true) |
|
| 617 | + ) { |
|
| 618 | + // Response status OK, return response parameters for further processing. |
|
| 619 | + return array('status' => true, 'args' => $response_args); |
|
| 620 | + } |
|
| 621 | + $errors = $this->_get_errors($response_args); |
|
| 622 | + return array('status' => false, 'args' => $errors); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * Log a "Cleared" request. |
|
| 629 | + * |
|
| 630 | + * @param array $request |
|
| 631 | + * @param EEI_Payment $payment |
|
| 632 | + * @param string $info |
|
| 633 | + * @return void |
|
| 634 | + */ |
|
| 635 | + private function _log_clean_request($request, $payment, $info) |
|
| 636 | + { |
|
| 637 | + $cleaned_request_data = $request; |
|
| 638 | + unset($cleaned_request_data['PWD'], $cleaned_request_data['USER'], $cleaned_request_data['SIGNATURE']); |
|
| 639 | + $this->log(array($info => $cleaned_request_data), $payment); |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Get error from the response data. |
|
| 646 | + * |
|
| 647 | + * @param array $data_array |
|
| 648 | + * @return array |
|
| 649 | + */ |
|
| 650 | + private function _get_errors($data_array) |
|
| 651 | + { |
|
| 652 | + $errors = array(); |
|
| 653 | + $n = 0; |
|
| 654 | + while (isset($data_array[ "L_ERRORCODE{$n}" ])) { |
|
| 655 | + $l_error_code = isset($data_array[ "L_ERRORCODE{$n}" ]) |
|
| 656 | + ? $data_array[ "L_ERRORCODE{$n}" ] |
|
| 657 | + : ''; |
|
| 658 | + $l_severity_code = isset($data_array[ "L_SEVERITYCODE{$n}" ]) |
|
| 659 | + ? $data_array[ "L_SEVERITYCODE{$n}" ] |
|
| 660 | + : ''; |
|
| 661 | + $l_short_message = isset($data_array[ "L_SHORTMESSAGE{$n}" ]) |
|
| 662 | + ? $data_array[ "L_SHORTMESSAGE{$n}" ] |
|
| 663 | + : ''; |
|
| 664 | + $l_long_message = isset($data_array[ "L_LONGMESSAGE{$n}" ]) |
|
| 665 | + ? $data_array[ "L_LONGMESSAGE{$n}" ] |
|
| 666 | + : ''; |
|
| 667 | + if ($n === 0) { |
|
| 668 | + $errors = array( |
|
| 669 | + 'L_ERRORCODE' => $l_error_code, |
|
| 670 | + 'L_SHORTMESSAGE' => $l_short_message, |
|
| 671 | + 'L_LONGMESSAGE' => $l_long_message, |
|
| 672 | + 'L_SEVERITYCODE' => $l_severity_code, |
|
| 673 | + ); |
|
| 674 | + } else { |
|
| 675 | + $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
| 676 | + $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
| 677 | + $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
| 678 | + $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
| 679 | + } |
|
| 680 | + $n++; |
|
| 681 | + } |
|
| 682 | + return $errors; |
|
| 683 | + } |
|
| 684 | 684 | } |
@@ -19,201 +19,201 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class PayPalSettingsForm extends EE_Payment_Method_Form |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var string of HTML being the help tab link |
|
| 24 | - */ |
|
| 25 | - protected $helpTabLink; |
|
| 22 | + /** |
|
| 23 | + * @var string of HTML being the help tab link |
|
| 24 | + */ |
|
| 25 | + protected $helpTabLink; |
|
| 26 | 26 | |
| 27 | - public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 28 | - { |
|
| 29 | - $this->helpTabLink = $help_tab_link; |
|
| 30 | - $options_array = array_replace_recursive( |
|
| 31 | - array( |
|
| 32 | - 'extra_meta_inputs' => array( |
|
| 33 | - 'api_username' => new EE_Text_Input( |
|
| 34 | - array( |
|
| 35 | - 'html_label_text' => sprintf( |
|
| 36 | - // translators: %s link to help doc |
|
| 37 | - esc_html__('API Username %s', 'event_espresso'), |
|
| 38 | - $help_tab_link |
|
| 39 | - ), |
|
| 40 | - 'required' => true, |
|
| 41 | - ) |
|
| 42 | - ), |
|
| 43 | - 'api_password' => new EE_Text_Input( |
|
| 44 | - array( |
|
| 45 | - 'html_label_text' => sprintf( |
|
| 46 | - // translators: %s link to help doc |
|
| 47 | - esc_html__('API Password %s', 'event_espresso'), |
|
| 48 | - $help_tab_link |
|
| 49 | - ), |
|
| 50 | - 'required' => true, |
|
| 51 | - ) |
|
| 52 | - ), |
|
| 53 | - 'api_signature' => new EE_Text_Input( |
|
| 54 | - array( |
|
| 55 | - 'html_label_text' => sprintf( |
|
| 56 | - // translators: %s link to help doc |
|
| 57 | - esc_html__('API Signature %s', 'event_espresso'), |
|
| 58 | - $help_tab_link |
|
| 59 | - ), |
|
| 60 | - 'required' => true, |
|
| 61 | - ) |
|
| 62 | - ), |
|
| 63 | - ) |
|
| 64 | - ), |
|
| 65 | - $options_array |
|
| 66 | - ); |
|
| 67 | - parent::__construct($options_array); |
|
| 68 | - } |
|
| 27 | + public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 28 | + { |
|
| 29 | + $this->helpTabLink = $help_tab_link; |
|
| 30 | + $options_array = array_replace_recursive( |
|
| 31 | + array( |
|
| 32 | + 'extra_meta_inputs' => array( |
|
| 33 | + 'api_username' => new EE_Text_Input( |
|
| 34 | + array( |
|
| 35 | + 'html_label_text' => sprintf( |
|
| 36 | + // translators: %s link to help doc |
|
| 37 | + esc_html__('API Username %s', 'event_espresso'), |
|
| 38 | + $help_tab_link |
|
| 39 | + ), |
|
| 40 | + 'required' => true, |
|
| 41 | + ) |
|
| 42 | + ), |
|
| 43 | + 'api_password' => new EE_Text_Input( |
|
| 44 | + array( |
|
| 45 | + 'html_label_text' => sprintf( |
|
| 46 | + // translators: %s link to help doc |
|
| 47 | + esc_html__('API Password %s', 'event_espresso'), |
|
| 48 | + $help_tab_link |
|
| 49 | + ), |
|
| 50 | + 'required' => true, |
|
| 51 | + ) |
|
| 52 | + ), |
|
| 53 | + 'api_signature' => new EE_Text_Input( |
|
| 54 | + array( |
|
| 55 | + 'html_label_text' => sprintf( |
|
| 56 | + // translators: %s link to help doc |
|
| 57 | + esc_html__('API Signature %s', 'event_espresso'), |
|
| 58 | + $help_tab_link |
|
| 59 | + ), |
|
| 60 | + 'required' => true, |
|
| 61 | + ) |
|
| 62 | + ), |
|
| 63 | + ) |
|
| 64 | + ), |
|
| 65 | + $options_array |
|
| 66 | + ); |
|
| 67 | + parent::__construct($options_array); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Tests the the PayPal API credentials work ok |
|
| 72 | - * @return string of an error using the credentials, otherwise, if the credentials work, returns a blank string |
|
| 73 | - * @throws EE_Error |
|
| 74 | - */ |
|
| 75 | - protected function checkForCredentialsErrors() |
|
| 76 | - { |
|
| 77 | - $request_params = array( |
|
| 78 | - 'METHOD' => 'GetBalance', |
|
| 79 | - 'VERSION' => '204.0', |
|
| 80 | - 'USER' => $this->get_input_value('api_username'), |
|
| 81 | - 'PWD' => $this->get_input_value('api_password'), |
|
| 82 | - 'SIGNATURE' => $this->get_input_value('api_signature'), |
|
| 83 | - ); |
|
| 84 | - $gateway_url = $this->get_input_value('PMD_debug_mode') |
|
| 85 | - ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
| 86 | - : 'https://api-3t.paypal.com/nvp'; |
|
| 87 | - // Request Customer Details. |
|
| 88 | - $response = wp_remote_post( |
|
| 89 | - $gateway_url, |
|
| 90 | - array( |
|
| 91 | - 'method' => 'POST', |
|
| 92 | - 'timeout' => 45, |
|
| 93 | - 'httpversion' => '1.1', |
|
| 94 | - 'cookies' => array(), |
|
| 95 | - 'headers' => array(), |
|
| 96 | - 'body' => http_build_query($request_params, '', '&'), |
|
| 97 | - ) |
|
| 98 | - ); |
|
| 99 | - if (is_wp_error($response) || empty($response['body'])) { |
|
| 100 | - // If we got here then there was an error in this request. |
|
| 101 | - // maybe is turned off. We don't know the credentials are invalid |
|
| 102 | - EE_Error::add_error( |
|
| 103 | - sprintf( |
|
| 104 | - // translators: %1$s Error message received from PayPal |
|
| 105 | - esc_html__( |
|
| 106 | - // @codingStandardsIgnoreStart |
|
| 107 | - 'Your PayPal credentials could not be verified. The following error occurred while communicating with PayPal: %1$s', |
|
| 108 | - // @codingStandardsIgnoreEnd |
|
| 109 | - 'event_espresso' |
|
| 110 | - ), |
|
| 111 | - $response->get_error_message() |
|
| 112 | - ), |
|
| 113 | - __FILE__, |
|
| 114 | - __FUNCTION__, |
|
| 115 | - __LINE__ |
|
| 116 | - ); |
|
| 117 | - } |
|
| 118 | - $response_args = array(); |
|
| 119 | - parse_str(urldecode($response['body']), $response_args); |
|
| 70 | + /** |
|
| 71 | + * Tests the the PayPal API credentials work ok |
|
| 72 | + * @return string of an error using the credentials, otherwise, if the credentials work, returns a blank string |
|
| 73 | + * @throws EE_Error |
|
| 74 | + */ |
|
| 75 | + protected function checkForCredentialsErrors() |
|
| 76 | + { |
|
| 77 | + $request_params = array( |
|
| 78 | + 'METHOD' => 'GetBalance', |
|
| 79 | + 'VERSION' => '204.0', |
|
| 80 | + 'USER' => $this->get_input_value('api_username'), |
|
| 81 | + 'PWD' => $this->get_input_value('api_password'), |
|
| 82 | + 'SIGNATURE' => $this->get_input_value('api_signature'), |
|
| 83 | + ); |
|
| 84 | + $gateway_url = $this->get_input_value('PMD_debug_mode') |
|
| 85 | + ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
| 86 | + : 'https://api-3t.paypal.com/nvp'; |
|
| 87 | + // Request Customer Details. |
|
| 88 | + $response = wp_remote_post( |
|
| 89 | + $gateway_url, |
|
| 90 | + array( |
|
| 91 | + 'method' => 'POST', |
|
| 92 | + 'timeout' => 45, |
|
| 93 | + 'httpversion' => '1.1', |
|
| 94 | + 'cookies' => array(), |
|
| 95 | + 'headers' => array(), |
|
| 96 | + 'body' => http_build_query($request_params, '', '&'), |
|
| 97 | + ) |
|
| 98 | + ); |
|
| 99 | + if (is_wp_error($response) || empty($response['body'])) { |
|
| 100 | + // If we got here then there was an error in this request. |
|
| 101 | + // maybe is turned off. We don't know the credentials are invalid |
|
| 102 | + EE_Error::add_error( |
|
| 103 | + sprintf( |
|
| 104 | + // translators: %1$s Error message received from PayPal |
|
| 105 | + esc_html__( |
|
| 106 | + // @codingStandardsIgnoreStart |
|
| 107 | + 'Your PayPal credentials could not be verified. The following error occurred while communicating with PayPal: %1$s', |
|
| 108 | + // @codingStandardsIgnoreEnd |
|
| 109 | + 'event_espresso' |
|
| 110 | + ), |
|
| 111 | + $response->get_error_message() |
|
| 112 | + ), |
|
| 113 | + __FILE__, |
|
| 114 | + __FUNCTION__, |
|
| 115 | + __LINE__ |
|
| 116 | + ); |
|
| 117 | + } |
|
| 118 | + $response_args = array(); |
|
| 119 | + parse_str(urldecode($response['body']), $response_args); |
|
| 120 | 120 | |
| 121 | - if (empty($response_args['ACK'])) { |
|
| 122 | - EE_Error::add_error( |
|
| 123 | - esc_html__( |
|
| 124 | - 'Your PayPal credentials could not be verified. Part of their response was missing.', |
|
| 125 | - 'event_espresso' |
|
| 126 | - ), |
|
| 127 | - __FILE__, |
|
| 128 | - __FUNCTION__, |
|
| 129 | - __LINE__ |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - if (in_array( |
|
| 133 | - $response_args['ACK'], |
|
| 134 | - array( |
|
| 135 | - 'Success', |
|
| 136 | - 'SuccessWithWarning' |
|
| 137 | - ), |
|
| 138 | - true |
|
| 139 | - ) |
|
| 140 | - ) { |
|
| 141 | - return ''; |
|
| 142 | - } else { |
|
| 143 | - return sprintf( |
|
| 144 | - // translators: %1$s: PayPal response message, %2$s: PayPal response code |
|
| 145 | - esc_html__( |
|
| 146 | - // @codingStandardsIgnoreStart |
|
| 147 | - 'Your PayPal API credentials appear to be invalid. PayPal said "%1$s (%2$s)". Please see tips below.', |
|
| 148 | - // @codingStandardsIgnoreEnd |
|
| 149 | - 'event_espresso' |
|
| 150 | - ), |
|
| 151 | - isset($response_args['L_LONGMESSAGE0']) |
|
| 152 | - ? $response_args['L_LONGMESSAGE0'] |
|
| 153 | - : esc_html__('No error message received from PayPal', 'event_espresso'), |
|
| 154 | - isset($response_args['L_ERRORCODE0']) ? $response_args['L_ERRORCODE0'] : 0 |
|
| 155 | - ); |
|
| 156 | - } |
|
| 157 | - } |
|
| 121 | + if (empty($response_args['ACK'])) { |
|
| 122 | + EE_Error::add_error( |
|
| 123 | + esc_html__( |
|
| 124 | + 'Your PayPal credentials could not be verified. Part of their response was missing.', |
|
| 125 | + 'event_espresso' |
|
| 126 | + ), |
|
| 127 | + __FILE__, |
|
| 128 | + __FUNCTION__, |
|
| 129 | + __LINE__ |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + if (in_array( |
|
| 133 | + $response_args['ACK'], |
|
| 134 | + array( |
|
| 135 | + 'Success', |
|
| 136 | + 'SuccessWithWarning' |
|
| 137 | + ), |
|
| 138 | + true |
|
| 139 | + ) |
|
| 140 | + ) { |
|
| 141 | + return ''; |
|
| 142 | + } else { |
|
| 143 | + return sprintf( |
|
| 144 | + // translators: %1$s: PayPal response message, %2$s: PayPal response code |
|
| 145 | + esc_html__( |
|
| 146 | + // @codingStandardsIgnoreStart |
|
| 147 | + 'Your PayPal API credentials appear to be invalid. PayPal said "%1$s (%2$s)". Please see tips below.', |
|
| 148 | + // @codingStandardsIgnoreEnd |
|
| 149 | + 'event_espresso' |
|
| 150 | + ), |
|
| 151 | + isset($response_args['L_LONGMESSAGE0']) |
|
| 152 | + ? $response_args['L_LONGMESSAGE0'] |
|
| 153 | + : esc_html__('No error message received from PayPal', 'event_espresso'), |
|
| 154 | + isset($response_args['L_ERRORCODE0']) ? $response_args['L_ERRORCODE0'] : 0 |
|
| 155 | + ); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Gets the HTML to show the link to the help tab |
|
| 161 | - * @return string |
|
| 162 | - */ |
|
| 163 | - protected function helpTabLink() |
|
| 164 | - { |
|
| 165 | - return $this->helpTabLink; |
|
| 166 | - } |
|
| 159 | + /** |
|
| 160 | + * Gets the HTML to show the link to the help tab |
|
| 161 | + * @return string |
|
| 162 | + */ |
|
| 163 | + protected function helpTabLink() |
|
| 164 | + { |
|
| 165 | + return $this->helpTabLink; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Does the normal validation, but also verifies the PayPal API credentials work. |
|
| 170 | - * If they don't, sets a validation error on the entire form, and adds validation errors (which are really more |
|
| 171 | - * tips) on each of the inputs that could be the cause of the problem. |
|
| 172 | - * @throws EE_Error |
|
| 173 | - */ |
|
| 174 | - public function _validate() |
|
| 175 | - { |
|
| 176 | - parent::_validate(); |
|
| 177 | - $credentials_message = $this->checkForCredentialsErrors(); |
|
| 178 | - if ($credentials_message !== '') { |
|
| 179 | - $this->add_validation_error($credentials_message); |
|
| 180 | - $this->get_input('PMD_debug_mode')->add_validation_error( |
|
| 181 | - esc_html__( |
|
| 182 | - // @codingStandardsIgnoreStart |
|
| 183 | - 'If you are using PayPal Sandbox (test) credentials, Debug mode should be set to "Yes". Otherwise, if you are using live PayPal credentials, set this to "No".', |
|
| 184 | - // @codingStandardsIgnoreEnd |
|
| 185 | - 'event_espresso' |
|
| 186 | - ) |
|
| 187 | - ); |
|
| 188 | - $this->get_input('api_username')->add_validation_error( |
|
| 189 | - sprintf( |
|
| 190 | - // translators: $1$s HTML for a link to the help tab |
|
| 191 | - esc_html__( |
|
| 192 | - 'Are you sure this is your API username, not your login username? %1$s', |
|
| 193 | - 'event_espresso' |
|
| 194 | - ), |
|
| 195 | - $this->helpTabLink() |
|
| 196 | - ) |
|
| 197 | - ); |
|
| 198 | - $this->get_input('api_password')->add_validation_error( |
|
| 199 | - sprintf( |
|
| 200 | - // translators: $1$s HTML for a link to the help tab |
|
| 201 | - esc_html__( |
|
| 202 | - 'Are you sure this is your API password, not your login password? %1$s', |
|
| 203 | - 'event_espresso' |
|
| 204 | - ), |
|
| 205 | - $this->helpTabLink() |
|
| 206 | - ) |
|
| 207 | - ); |
|
| 208 | - $this->get_input('api_signature')->add_validation_error( |
|
| 209 | - sprintf( |
|
| 210 | - // translators: $1$s HTML for a link to the help tab |
|
| 211 | - esc_html__('Please verify your API signature is correct. %1$s', 'event_espresso'), |
|
| 212 | - $this->helpTabLink() |
|
| 213 | - ) |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - } |
|
| 168 | + /** |
|
| 169 | + * Does the normal validation, but also verifies the PayPal API credentials work. |
|
| 170 | + * If they don't, sets a validation error on the entire form, and adds validation errors (which are really more |
|
| 171 | + * tips) on each of the inputs that could be the cause of the problem. |
|
| 172 | + * @throws EE_Error |
|
| 173 | + */ |
|
| 174 | + public function _validate() |
|
| 175 | + { |
|
| 176 | + parent::_validate(); |
|
| 177 | + $credentials_message = $this->checkForCredentialsErrors(); |
|
| 178 | + if ($credentials_message !== '') { |
|
| 179 | + $this->add_validation_error($credentials_message); |
|
| 180 | + $this->get_input('PMD_debug_mode')->add_validation_error( |
|
| 181 | + esc_html__( |
|
| 182 | + // @codingStandardsIgnoreStart |
|
| 183 | + 'If you are using PayPal Sandbox (test) credentials, Debug mode should be set to "Yes". Otherwise, if you are using live PayPal credentials, set this to "No".', |
|
| 184 | + // @codingStandardsIgnoreEnd |
|
| 185 | + 'event_espresso' |
|
| 186 | + ) |
|
| 187 | + ); |
|
| 188 | + $this->get_input('api_username')->add_validation_error( |
|
| 189 | + sprintf( |
|
| 190 | + // translators: $1$s HTML for a link to the help tab |
|
| 191 | + esc_html__( |
|
| 192 | + 'Are you sure this is your API username, not your login username? %1$s', |
|
| 193 | + 'event_espresso' |
|
| 194 | + ), |
|
| 195 | + $this->helpTabLink() |
|
| 196 | + ) |
|
| 197 | + ); |
|
| 198 | + $this->get_input('api_password')->add_validation_error( |
|
| 199 | + sprintf( |
|
| 200 | + // translators: $1$s HTML for a link to the help tab |
|
| 201 | + esc_html__( |
|
| 202 | + 'Are you sure this is your API password, not your login password? %1$s', |
|
| 203 | + 'event_espresso' |
|
| 204 | + ), |
|
| 205 | + $this->helpTabLink() |
|
| 206 | + ) |
|
| 207 | + ); |
|
| 208 | + $this->get_input('api_signature')->add_validation_error( |
|
| 209 | + sprintf( |
|
| 210 | + // translators: $1$s HTML for a link to the help tab |
|
| 211 | + esc_html__('Please verify your API signature is correct. %1$s', 'event_espresso'), |
|
| 212 | + $this->helpTabLink() |
|
| 213 | + ) |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | } |
| 218 | 218 | // End of file PayPalSettingsForm.php |
| 219 | 219 | // Location: ${NAMESPACE}/PayPalSettingsForm.php |