Merge pull request #17522 from seamuslee001/remove_deprecated_methods
[civicrm-core.git] / api / v3 / PaymentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM PaymentProcessor.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Add/Update a PaymentProcessor.
20 *
21 * @param array $params
22 *
23 * @return array
24 * API result array
25 */
26 function civicrm_api3_payment_processor_create($params) {
27 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'PaymentProcessor');
28 }
29
30 /**
31 * Adjust Metadata for Create action.
32 *
33 * The metadata is used for setting defaults, documentation & validation.
34 *
35 * @param array $params
36 * Array of parameters determined by getfields.
37 */
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',
50 'keyColumn' => 'id',
51 'labelColumn' => 'name',
52 ];
53 }
54
55 /**
56 * Deletes an existing PaymentProcessor.
57 *
58 * @param array $params
59 *
60 * @return array
61 * API result array
62 */
63 function civicrm_api3_payment_processor_delete($params) {
64 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
65 }
66
67 /**
68 * Retrieve one or more PaymentProcessor.
69 *
70 * @param array $params
71 * Array of name/value pairs.
72 *
73 * @return array
74 * API result array
75 */
76 function civicrm_api3_payment_processor_get($params) {
77 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
78 }
79
80 /**
81 * Set default getlist parameters.
82 *
83 * @see _civicrm_api3_generic_getlist_defaults
84 *
85 * @param array $request
86 *
87 * @return array
88 */
89 function _civicrm_api3_payment_processor_getlist_defaults(&$request) {
90 return [
91 'description_field' => [
92 'payment_processor_type_id',
93 'description',
94 ],
95 'params' => [
96 'is_test' => 0,
97 'is_active' => 1,
98 ],
99 ];
100 }
101
102 /**
103 * Action payment.
104 *
105 * @param array $params
106 *
107 * @return array
108 * API result array.
109 *
110 * @throws \API_Exception
111 * @throws \CiviCRM_API3_Exception
112 */
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']]));
117 try {
118 $result = $processor->doPayment($params);
119 }
120 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
121 $code = $e->getErrorCode();
122 $errorData = $e->getErrorData();
123 if (empty($code)) {
124 $code = 'EXTERNAL_FAILURE';
125 }
126 throw new API_Exception('Payment failed', $code, $errorData, $e);
127 }
128 return civicrm_api3_create_success(array($result), $params);
129 }
130
131 /**
132 * Action payment.
133 *
134 * @param array $params
135 */
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,
141 ];
142 $params['amount'] = [
143 'api.required' => TRUE,
144 'title' => ts('Amount to pay'),
145 'type' => CRM_Utils_Type::T_MONEY,
146 ];
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'],
152 ];
153 $params['contact_id'] = [
154 'title' => ts('Contact ID'),
155 'type' => CRM_Utils_Type::T_INT,
156 ];
157 $params['contribution_recur_id'] = [
158 'title' => ts('Contribution Recur ID'),
159 'type' => CRM_Utils_Type::T_INT,
160 ];
161 $params['invoice_id'] = [
162 'title' => ts('Invoice ID'),
163 'type' => CRM_Utils_Type::T_STRING,
164 ];
165 }
166
167 /**
168 * Action refund.
169 *
170 * @param array $params
171 *
172 * @return array
173 * API result array.
174 *
175 * @throws \API_Exception
176 * @throws \CiviCRM_API3_Exception
177 * @throws \Civi\Payment\Exception\PaymentProcessorException
178 */
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');
185 }
186 $result = $processor->doRefund($params);
187 return civicrm_api3_create_success([$result], $params);
188 }
189
190 /**
191 * Action Refund.
192 *
193 * @param array $params
194 *
195 */
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,
201 ];
202 $params['amount'] = [
203 'api.required' => TRUE,
204 'title' => ts('Amount to refund'),
205 'type' => CRM_Utils_Type::T_MONEY,
206 ];
207 }