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 throw new API_Exception('Payment failed', $code, $errorData, $e);
128 return civicrm_api3_create_success(array($result), $params);
134 * @param array $params
136 function _civicrm_api3_payment_processor_pay_spec(&$params) {
137 $params['payment_processor_id'] = [
138 'api.required' => TRUE,
139 'title' => ts('Payment processor'),
140 'type' => CRM_Utils_Type
::T_INT
,
142 $params['amount'] = [
143 'api.required' => TRUE,
144 'title' => ts('Amount to pay'),
145 'type' => CRM_Utils_Type
::T_MONEY
,
147 $params['contribution_id'] = [
148 'api.required' => TRUE,
149 'title' => ts('Contribution ID'),
150 'type' => CRM_Utils_Type
::T_INT
,
151 'api.aliases' => ['order_id'],
153 $params['contact_id'] = [
154 'title' => ts('Contact ID'),
155 'type' => CRM_Utils_Type
::T_INT
,
157 $params['contribution_recur_id'] = [
158 'title' => ts('Contribution Recur ID'),
159 'type' => CRM_Utils_Type
::T_INT
,
161 $params['invoice_id'] = [
162 'title' => ts('Invoice ID'),
163 'type' => CRM_Utils_Type
::T_STRING
,
170 * @param array $params
175 * @throws \API_Exception
176 * @throws \CiviCRM_API3_Exception
177 * @throws \Civi\Payment\Exception\PaymentProcessorException
179 function civicrm_api3_payment_processor_refund($params) {
180 /** @var \CRM_Core_Payment $processor */
181 $processor = Civi\Payment\System
::singleton()->getById($params['payment_processor_id']);
182 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
183 if (!$processor->supportsRefund()) {
184 throw new API_Exception('Payment Processor does not support refund');
186 $result = $processor->doRefund($params);
187 return civicrm_api3_create_success([$result], $params);
193 * @param array $params
196 function _civicrm_api3_payment_processor_refund_spec(&$params) {
197 $params['payment_processor_id'] = [
198 'api.required' => TRUE,
199 'title' => ts('Payment processor'),
200 'type' => CRM_Utils_Type
::T_INT
,
202 $params['amount'] = [
203 'api.required' => TRUE,
204 'title' => ts('Amount to refund'),
205 'type' => CRM_Utils_Type
::T_MONEY
,