Merge pull request #15825 from seamuslee001/dev_core_183_logging
[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 if (empty($params['id']) && empty($params['payment_instrument_id'])) {
28 $params['payment_instrument_id'] = civicrm_api3('PaymentProcessorType', 'getvalue', [
29 'id' => $params['payment_processor_type_id'],
30 'return' => 'payment_instrument_id',
31 ]);
32 }
33 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'PaymentProcessor');
34 }
35
36 /**
37 * Adjust Metadata for Create action.
38 *
39 * The metadata is used for setting defaults, documentation & validation.
40 *
41 * @param array $params
42 * Array of parameters determined by getfields.
43 */
44 function _civicrm_api3_payment_processor_create_spec(&$params) {
45 $params['payment_processor_type_id']['api.required'] = 1;
46 $params['is_default']['api.default'] = 0;
47 $params['is_test']['api.default'] = 0;
48 $params['is_active']['api.default'] = TRUE;
49 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
50 $params['financial_account_id']['api.default'] = CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID();
51 $params['financial_account_id']['api.required'] = TRUE;
52 $params['financial_account_id']['type'] = CRM_Utils_Type::T_INT;
53 $params['financial_account_id']['title'] = ts('Financial Account for Processor');
54 $params['financial_account_id']['pseudoconstant'] = [
55 'table' => 'civicrm_financial_account',
56 'keyColumn' => 'id',
57 'labelColumn' => 'name',
58 ];
59 }
60
61 /**
62 * Deletes an existing PaymentProcessor.
63 *
64 * @param array $params
65 *
66 * @return array
67 * API result array
68 */
69 function civicrm_api3_payment_processor_delete($params) {
70 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
71 }
72
73 /**
74 * Retrieve one or more PaymentProcessor.
75 *
76 * @param array $params
77 * Array of name/value pairs.
78 *
79 * @return array
80 * API result array
81 */
82 function civicrm_api3_payment_processor_get($params) {
83 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
84 }
85
86 /**
87 * Set default getlist parameters.
88 *
89 * @see _civicrm_api3_generic_getlist_defaults
90 *
91 * @param array $request
92 *
93 * @return array
94 */
95 function _civicrm_api3_payment_processor_getlist_defaults(&$request) {
96 return [
97 'description_field' => [
98 'payment_processor_type_id',
99 'description',
100 ],
101 'params' => [
102 'is_test' => 0,
103 'is_active' => 1,
104 ],
105 ];
106 }
107
108 /**
109 * Action payment.
110 *
111 * @param array $params
112 *
113 * @return array
114 * API result array.
115 *
116 * @throws \API_Exception
117 * @throws \CiviCRM_API3_Exception
118 */
119 function civicrm_api3_payment_processor_pay($params) {
120 /* @var CRM_Core_Payment $processor */
121 $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
122 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
123 try {
124 $result = $processor->doPayment($params);
125 }
126 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
127 $code = $e->getErrorCode();
128 $errorData = $e->getErrorData();
129 if (empty($code)) {
130 $code = 'EXTERNAL_FAILURE';
131 }
132 throw new API_Exception('Payment failed', $code, $errorData, $e);
133 }
134 return civicrm_api3_create_success(array($result), $params);
135 }
136
137 /**
138 * Action payment.
139 *
140 * @param array $params
141 */
142 function _civicrm_api3_payment_processor_pay_spec(&$params) {
143 $params['payment_processor_id'] = [
144 'api.required' => TRUE,
145 'title' => ts('Payment processor'),
146 'type' => CRM_Utils_Type::T_INT,
147 ];
148 $params['amount'] = [
149 'api.required' => TRUE,
150 'title' => ts('Amount to pay'),
151 'type' => CRM_Utils_Type::T_MONEY,
152 ];
153 $params['contribution_id'] = [
154 'api.required' => TRUE,
155 'title' => ts('Contribution ID'),
156 'type' => CRM_Utils_Type::T_INT,
157 'api.aliases' => ['order_id'],
158 ];
159 $params['contact_id'] = [
160 'title' => ts('Contact ID'),
161 'type' => CRM_Utils_Type::T_INT,
162 ];
163 $params['contribution_recur_id'] = [
164 'title' => ts('Contribution Recur ID'),
165 'type' => CRM_Utils_Type::T_INT,
166 ];
167 $params['invoice_id'] = [
168 'title' => ts('Invoice ID'),
169 'type' => CRM_Utils_Type::T_STRING,
170 ];
171 }
172
173 /**
174 * Action refund.
175 *
176 * @param array $params
177 *
178 * @return array
179 * API result array.
180 *
181 * @throws \API_Exception
182 * @throws \CiviCRM_API3_Exception
183 * @throws \Civi\Payment\Exception\PaymentProcessorException
184 */
185 function civicrm_api3_payment_processor_refund($params) {
186 /** @var \CRM_Core_Payment $processor */
187 $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
188 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
189 if (!$processor->supportsRefund()) {
190 throw new API_Exception('Payment Processor does not support refund');
191 }
192 $result = $processor->doRefund($params);
193 return civicrm_api3_create_success([$result], $params);
194 }
195
196 /**
197 * Action Refund.
198 *
199 * @param array $params
200 *
201 */
202 function _civicrm_api3_payment_processor_refund_spec(&$params) {
203 $params['payment_processor_id'] = [
204 'api.required' => TRUE,
205 'title' => ts('Payment processor'),
206 'type' => CRM_Utils_Type::T_INT,
207 ];
208 $params['amount'] = [
209 'api.required' => TRUE,
210 'title' => ts('Amount to refund'),
211 'type' => CRM_Utils_Type::T_MONEY,
212 ];
213 }