3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM Contribution Payment records.
31 * @package CiviCRM_APIv3
35 * Retrieve a set of financial transactions which are payments.
37 * @param array $params
41 * Array of financial transactions which are payments, if error an array with an error id and error message
43 function civicrm_api3_payment_get($params) {
46 if (isset($params['options']) && CRM_Utils_Array
::value('limit', $params['options'])) {
47 $limit = CRM_Utils_Array
::value('limit', $params['options']);
49 $params['options']['limit'] = 0;
50 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
51 if (!empty($eft['values'])) {
53 foreach ($eft['values'] as $efts) {
54 if (empty($efts['financial_trxn_id'])) {
57 $eftIds[] = $efts['financial_trxn_id'];
58 $map[$efts['financial_trxn_id']] = $efts['entity_id'];
60 if (!empty($eftIds)) {
62 'id' => ['IN' => $eftIds],
66 $ftParams['options']['limit'] = $limit;
68 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
69 foreach ($financialTrxn['values'] as &$values) {
70 $values['contribution_id'] = $map[$values['id']];
74 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $financialTrxn, []), $params, 'Payment', 'get');
80 * @param array $params
83 * @throws API_Exception
87 function civicrm_api3_payment_delete(&$params) {
88 return civicrm_api3('FinancialTrxn', 'delete', $params);
92 * Cancel/Refund a payment for a Contribution.
94 * @param array $params
97 * @throws API_Exception
101 function civicrm_api3_payment_cancel(&$params) {
103 'entity_table' => 'civicrm_contribution',
104 'financial_trxn_id' => $params['id'],
106 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
107 $contributionId = $entity['entity_id'];
108 $params['total_amount'] = $entity['amount'];
109 unset($params['id']);
111 $trxn = CRM_Contribute_BAO_Contribution
::recordAdditionalPayment($contributionId, $params, 'refund', NULL, FALSE);
114 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
115 return civicrm_api3_create_success($values, $params, 'Payment', 'cancel', $trxn);
119 * Add a payment for a Contribution.
121 * @param array $params
124 * @throws API_Exception
128 function civicrm_api3_payment_create(&$params) {
129 // Check if it is an update
130 if (CRM_Utils_Array
::value('id', $params)) {
131 $amount = $params['total_amount'];
132 civicrm_api3('Payment', 'cancel', $params);
133 $params['total_amount'] = $amount;
135 $trxn = CRM_Financial_BAO_Payment
::create($params);
138 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
139 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
143 * Adjust Metadata for Create action.
145 * The metadata is used for setting defaults, documentation & validation.
147 * @param array $params
148 * Array of parameters.
150 function _civicrm_api3_payment_create_spec(&$params) {
152 'contribution_id' => [
153 'api.required' => 1 ,
154 'title' => 'Contribution ID',
155 'type' => CRM_Utils_Type
::T_INT
,
158 'api.required' => 1 ,
159 'title' => 'Total Payment Amount',
160 'type' => CRM_Utils_Type
::T_FLOAT
,
162 'payment_processor_id' => [
163 'title' => 'Payment Processor ID',
164 'type' => CRM_Utils_Type
::T_INT
,
165 'description' => ts('Payment processor ID - required for payment processor payments'),
168 'title' => 'Payment ID',
169 'type' => CRM_Utils_Type
::T_INT
,
170 'api.aliases' => ['payment_id'],
176 * Adjust Metadata for Get action.
178 * The metadata is used for setting defaults, documentation & validation.
180 * @param array $params
181 * Array of parameters determined by getfields.
183 function _civicrm_api3_payment_get_spec(&$params) {
185 'contribution_id' => [
186 'title' => 'Contribution ID',
187 'type' => CRM_Utils_Type
::T_INT
,
190 'title' => 'Entity Table',
191 'api.default' => 'civicrm_contribution',
194 'title' => 'Entity ID',
195 'type' => CRM_Utils_Type
::T_INT
,
196 'api.aliases' => ['contribution_id'],
202 * Adjust Metadata for Delete action.
204 * The metadata is used for setting defaults, documentation & validation.
206 * @param array $params
207 * Array of parameters.
209 function _civicrm_api3_payment_delete_spec(&$params) {
212 'api.required' => 1 ,
213 'title' => 'Payment ID',
214 'type' => CRM_Utils_Type
::T_INT
,
215 'api.aliases' => ['payment_id'],
221 * Adjust Metadata for Cancel action.
223 * The metadata is used for setting defaults, documentation & validation.
225 * @param array $params
226 * Array of parameters.
228 function _civicrm_api3_payment_cancel_spec(&$params) {
231 'api.required' => 1 ,
232 'title' => 'Payment ID',
233 'type' => CRM_Utils_Type
::T_INT
,
234 'api.aliases' => ['payment_id'],
240 * Send a payment confirmation.
242 * @param array $params
248 function civicrm_api3_payment_sendconfirmation($params) {
250 'receipt_from_email',
257 $input = array_intersect_key($params, array_flip($allowedParams));
258 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
259 $result = CRM_Financial_BAO_Payment
::sendConfirmation($input);
260 return civicrm_api3_create_success([
262 'is_sent' => $result[0],
263 'subject' => $result[1],
264 'message_txt' => $result[2],
265 'message_html' => $result[3],
270 * Adjust Metadata for sendconfirmation action.
272 * The metadata is used for setting defaults, documentation & validation.
274 * @param array $params
275 * Array of parameters determined by getfields.
277 function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
280 'title' => ts('Payment ID'),
281 'type' => CRM_Utils_Type
::T_INT
,