Include lower level data when throwing an exception on payment processor.pay
authoreileen <emcnaughton@wikimedia.org>
Tue, 9 Apr 2019 00:51:57 +0000 (12:51 +1200)
committereileen <emcnaughton@wikimedia.org>
Tue, 9 Apr 2019 00:51:57 +0000 (12:51 +1200)
api/v3/PaymentProcessor.php

index 51e401c75e96bfd0f3adfb94859dc3a353c3e2ca..0c8b7c13166265dab6dac8f6cadceae849ba119c 100644 (file)
@@ -122,12 +122,23 @@ function _civicrm_api3_payment_processor_getlist_defaults(&$request) {
  *
  * @return array
  *   API result array.
- * @throws CiviCRM_API3_Exception
+ *
+ * @throws \API_Exception
  */
 function civicrm_api3_payment_processor_pay($params) {
   $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
   $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
-  $result = $processor->doPayment($params);
+  try {
+    $result = $processor->doPayment($params);
+  }
+  catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
+    $code = $e->getErrorCode();
+    $errorData = $e->getErrorData();
+    if (empty($code)) {
+      $code = 'EXTERNAL_FAILURE';
+    }
+    throw new API_Exception('Payment failed', $code, $errorData, $e);
+  }
   return civicrm_api3_create_success(array($result), $params);
 }