dev/financial#38 : Support refund payment using payment processor
authorMonish Deb <deb.monish@gmail.com>
Fri, 8 Mar 2019 12:48:33 +0000 (18:18 +0530)
committereileen <emcnaughton@wikimedia.org>
Thu, 4 Apr 2019 01:03:35 +0000 (14:03 +1300)
CRM/Core/Payment.php
CRM/Core/Payment/Dummy.php
api/v3/PaymentProcessor.php

index fb465033e8a5faef76189755b7a13d3f774522f1..1aaa4aa5f5c379b105dacb4cb95a1319bb2ef474 100644 (file)
@@ -336,6 +336,15 @@ abstract class CRM_Core_Payment {
     return TRUE;
   }
 
+  /**
+   * Does this payment processor support refund?
+   *
+   * @return bool
+   */
+  public function supportsRefund() {
+    return FALSE;
+  }
+
   /**
    * Should the first payment date be configurable when setting up back office recurring payments.
    *
@@ -1258,6 +1267,17 @@ abstract class CRM_Core_Payment {
     return $result;
   }
 
+  /**
+   * Refunds payment
+   *
+   * Payment processors should set payment_status_id if it set the status to Refunded in case the transaction is successful
+   *
+   * @param array $params
+   *
+   * @throws \Civi\Payment\Exception\PaymentProcessorException
+   */
+  public function doRefund(&$params) {}
+
   /**
    * Query payment processor for details about a transaction.
    *
index 08b5486433975856e0324dd1458595ba214ca0c0..92f932c273cc726f9dc3815f55fa43f74db9d95a 100644 (file)
@@ -137,6 +137,25 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
     return TRUE;
   }
 
+  /**
+   * Does this payment processor support refund?
+   *
+   * @return bool
+   */
+  public function supportsRefund() {
+    return TRUE;
+  }
+
+  /**
+   * Submit a refund payment
+   *
+   * @throws \Civi\Payment\Exception\PaymentProcessorException
+   *
+   * @param array $params
+   *   Assoc array of input parameters for this transaction.
+   */
+  public function doRefund(&$params) {}
+
   /**
    * Generate error object.
    *
index 5faa1318f4786a0f868cfd422abe185d14a8dddb..d2c5b0ec29597b0e26dfa67757b90aa551c38f44 100644 (file)
@@ -115,3 +115,41 @@ function _civicrm_api3_payment_processor_getlist_defaults(&$request) {
     ],
   ];
 }
+
+/**
+ * Action refund.
+ *
+ * @param array $params
+ *
+ * @return array
+ *   API result array.
+ * @throws CiviCRM_API3_Exception
+ */
+function civicrm_api3_payment_processor_refund($params) {
+  $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
+  $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $params['payment_processor_id'])));
+  if (!$processor->supportsRefund()) {
+    throw API_Exception('Payment Processor does not support refund');
+  }
+  $result = $processor->doRefund($params);
+  return civicrm_api3_create_success(array($result), $params);
+}
+
+/**
+ * Action Refund.
+ *
+ * @param array $params
+ *
+ */
+function _civicrm_api3_payment_processor_refund_spec(&$params) {
+  $params['payment_processor_id'] = [
+    'api.required' => 1,
+    'title' => ts('Payment processor'),
+    'type' => CRM_Utils_Type::T_INT,
+  ];
+  $params['amount'] = [
+    'api.required' => TRUE,
+    'title' => ts('Amount to refund'),
+    'type' => CRM_Utils_Type::T_MONEY,
+  ];
+}