Merge pull request #23645 from jmcclelland/profile-require-tag
[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 $message = $e->getMessage() ?? 'Payment Failed';
127 throw new API_Exception($message, $code, $errorData, $e);
128 }
129 return civicrm_api3_create_success(array($result), $params);
130 }
131
132 /**
133 * Action payment.
134 *
135 * @param array $params
136 */
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,
142 ];
143 $params['amount'] = [
144 'api.required' => TRUE,
145 'title' => ts('Amount to pay'),
146 'type' => CRM_Utils_Type::T_MONEY,
147 ];
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'],
153 ];
154 $params['contact_id'] = [
155 'title' => ts('Contact ID'),
156 'type' => CRM_Utils_Type::T_INT,
157 ];
158 $params['contribution_recur_id'] = [
159 'title' => ts('Contribution Recur ID'),
160 'type' => CRM_Utils_Type::T_INT,
161 ];
162 $params['invoice_id'] = [
163 'title' => ts('Invoice ID'),
164 'type' => CRM_Utils_Type::T_STRING,
165 ];
166 }
167
168 /**
169 * Action refund.
170 *
171 * @param array $params
172 *
173 * @return array
174 * API result array.
175 *
176 * @throws \API_Exception
177 * @throws \CiviCRM_API3_Exception
178 * @throws \Civi\Payment\Exception\PaymentProcessorException
179 */
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');
186 }
187 $result = $processor->doRefund($params);
188 return civicrm_api3_create_success([$result], $params);
189 }
190
191 /**
192 * Action Refund.
193 *
194 * @param array $params
195 *
196 */
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,
202 ];
203 $params['amount'] = [
204 'api.required' => TRUE,
205 'title' => ts('Amount to refund'),
206 'type' => CRM_Utils_Type::T_MONEY,
207 ];
208 }