3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM PaymentProcessor.
15 * @package CiviCRM_APIv3
19 * Add/Update a PaymentProcessor.
21 * @param array $params
26 function civicrm_api3_payment_processor_create($params) {
27 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'PaymentProcessor');
31 * Adjust Metadata for Create action.
33 * The metadata is used for setting defaults, documentation & validation.
35 * @param array $params
36 * Array of parameters determined by getfields.
38 function _civicrm_api3_payment_processor_create_spec(&$params) {
39 $params['payment_processor_type_id']['api.required'] = 1;
40 $params['is_default']['api.default'] = 0;
41 $params['is_test']['api.default'] = 0;
42 $params['is_active']['api.default'] = TRUE;
43 $params['domain_id']['api.default'] = CRM_Core_Config
::domainID();
44 $params['financial_account_id']['api.default'] = CRM_Financial_BAO_PaymentProcessor
::getDefaultFinancialAccountID();
45 $params['financial_account_id']['api.required'] = TRUE;
46 $params['financial_account_id']['type'] = CRM_Utils_Type
::T_INT
;
47 $params['financial_account_id']['title'] = ts('Financial Account for Processor');
48 $params['financial_account_id']['pseudoconstant'] = [
49 'table' => 'civicrm_financial_account',
51 'labelColumn' => 'name',
56 * Deletes an existing PaymentProcessor.
58 * @param array $params
63 function civicrm_api3_payment_processor_delete($params) {
64 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
68 * Retrieve one or more PaymentProcessor.
70 * @param array $params
71 * Array of name/value pairs.
76 function civicrm_api3_payment_processor_get($params) {
77 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
81 * Set default getlist parameters.
83 * @see _civicrm_api3_generic_getlist_defaults
85 * @param array $request
89 function _civicrm_api3_payment_processor_getlist_defaults(&$request) {
91 'description_field' => [
92 'payment_processor_type_id',
105 * @param array $params
110 * @throws \API_Exception
111 * @throws \CiviCRM_API3_Exception
113 function civicrm_api3_payment_processor_pay($params) {
114 /* @var CRM_Core_Payment $processor */
115 $processor = Civi\Payment\System
::singleton()->getById($params['payment_processor_id']);
116 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
118 $result = $processor->doPayment($params);
120 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
121 $code = $e->getErrorCode();
122 $errorData = $e->getErrorData();
124 $code = 'EXTERNAL_FAILURE';
126 $message = $e->getMessage() ??
'Payment Failed';
127 throw new API_Exception($message, $code, $errorData, $e);
129 return civicrm_api3_create_success(array($result), $params);
135 * @param array $params
137 function _civicrm_api3_payment_processor_pay_spec(&$params) {
138 $params['payment_processor_id'] = [
139 'api.required' => TRUE,
140 'title' => ts('Payment processor'),
141 'type' => CRM_Utils_Type
::T_INT
,
143 $params['amount'] = [
144 'api.required' => TRUE,
145 'title' => ts('Amount to pay'),
146 'type' => CRM_Utils_Type
::T_MONEY
,
148 $params['contribution_id'] = [
149 'api.required' => TRUE,
150 'title' => ts('Contribution ID'),
151 'type' => CRM_Utils_Type
::T_INT
,
152 'api.aliases' => ['order_id'],
154 $params['contact_id'] = [
155 'title' => ts('Contact ID'),
156 'type' => CRM_Utils_Type
::T_INT
,
158 $params['contribution_recur_id'] = [
159 'title' => ts('Contribution Recur ID'),
160 'type' => CRM_Utils_Type
::T_INT
,
162 $params['invoice_id'] = [
163 'title' => ts('Invoice ID'),
164 'type' => CRM_Utils_Type
::T_STRING
,
171 * @param array $params
176 * @throws \API_Exception
177 * @throws \CiviCRM_API3_Exception
178 * @throws \Civi\Payment\Exception\PaymentProcessorException
180 function civicrm_api3_payment_processor_refund($params) {
181 /** @var \CRM_Core_Payment $processor */
182 $processor = Civi\Payment\System
::singleton()->getById($params['payment_processor_id']);
183 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
184 if (!$processor->supportsRefund()) {
185 throw new API_Exception('Payment Processor does not support refund');
187 $result = $processor->doRefund($params);
188 return civicrm_api3_create_success([$result], $params);
194 * @param array $params
197 function _civicrm_api3_payment_processor_refund_spec(&$params) {
198 $params['payment_processor_id'] = [
199 'api.required' => TRUE,
200 'title' => ts('Payment processor'),
201 'type' => CRM_Utils_Type
::T_INT
,
203 $params['amount'] = [
204 'api.required' => TRUE,
205 'title' => ts('Amount to refund'),
206 'type' => CRM_Utils_Type
::T_MONEY
,