Update Paypal invokeApi to only throw exceptions.
authoreileen <emcnaughton@wikimedia.org>
Tue, 10 Dec 2019 11:11:41 +0000 (00:11 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 11 Dec 2019 10:53:58 +0000 (23:53 +1300)
Currently invokeAPI will return an error for a curl function and throw a processorException for an outcome other than success.

In just about every instance where it is called the Core_Error is converted into an exception anyway - this
can all be cleaned up as a follow up. The exceptions are

doQuery -  called by doPayment
doDirectPayment - this is called by doPayment which converts it into an exception. Calling doDirectPayment directly was deprecated in 4.6
createRecurringPayments - called by doExpressCheckout which in already throws exceptions in other scenarios,
updateSubscriptionBillingInfo - we can update the one place that calls this in core as a follow up like https://github.com/civicrm/civicrm-core/pull/15676

However, note that an exception was already thrown in all these places in the more common error scenario of the action not succeeding as opposed
to the rare curl error scenario

Follow ons to consider
1) introduce more nuance into PaymentProcessorExceptions - extend the exception class with PaymentProcessorConfigException, PaymentProcessingException or similar to differentiate
2) remove all the handling for invokeApi to have returned an error object.
3) fix the code that calls updateSubscriptionBillingInfo
4) ensure docs reflect that doPayment should throw an exception. AFAIK we have not yet documented other 'do' functions & I'd prefer to stdise them first

CRM/Core/Payment/PayPalImpl.php

index 21f9fd37b2d1def245e4d12cfcebce378726d877..6310c5003e39e23ee1de3a260099492dce11ab95 100644 (file)
@@ -1016,13 +1016,13 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
    * @param null $url
    *
    * @return array|object
-   * @throws \Exception
+   * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
   public function invokeAPI($args, $url = NULL) {
 
     if ($url === NULL) {
       if (empty($this->_paymentProcessor['url_api'])) {
-        CRM_Core_Error::fatal(ts('Please set the API URL. Please refer to the documentation for more details'));
+        throw new PaymentProcessorException(ts('Please set the API URL. Please refer to the documentation for more details'));
       }
 
       $url = $this->_paymentProcessor['url_api'] . 'nvp';
@@ -1062,16 +1062,9 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
     $result = self::deformat($response);
 
     if (curl_errno($ch)) {
-      $e = CRM_Core_Error::singleton();
-      $e->push(curl_errno($ch),
-        0, NULL,
-        curl_error($ch)
-      );
-      return $e;
-    }
-    else {
-      curl_close($ch);
+      throw new PaymentProcessorException(ts('Network error') . ' ' . curl_error($ch) . curl_errno($ch), curl_errno($ch));
     }
+    curl_close($ch);
 
     $outcome = strtolower(CRM_Utils_Array::value('ack', $result));