From 6bc775cf0493f74b018580d4ddef48bd1a395cca Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Fri, 8 Mar 2019 18:18:33 +0530 Subject: [PATCH] dev/financial#38 : Support refund payment using payment processor --- CRM/Core/Payment.php | 20 +++++++++++++++++++ CRM/Core/Payment/Dummy.php | 19 +++++++++++++++++++ api/v3/PaymentProcessor.php | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index fb465033e8..1aaa4aa5f5 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -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. * diff --git a/CRM/Core/Payment/Dummy.php b/CRM/Core/Payment/Dummy.php index 08b5486433..92f932c273 100644 --- a/CRM/Core/Payment/Dummy.php +++ b/CRM/Core/Payment/Dummy.php @@ -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. * diff --git a/api/v3/PaymentProcessor.php b/api/v3/PaymentProcessor.php index 5faa1318f4..d2c5b0ec29 100644 --- a/api/v3/PaymentProcessor.php +++ b/api/v3/PaymentProcessor.php @@ -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, + ]; +} -- 2.25.1