Merge pull request #23602 from kainuk/core-3369
[civicrm-core.git] / api / v3 / PaymentProcessor.php
CommitLineData
21150aae
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
21150aae 5 | |
a30c801b
TO
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 |
21150aae 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
21150aae
CW
11
12/**
c28e1768 13 * This api exposes CiviCRM PaymentProcessor.
21150aae
CW
14 *
15 * @package CiviCRM_APIv3
21150aae
CW
16 */
17
18/**
d1b0d05e 19 * Add/Update a PaymentProcessor.
21150aae 20 *
c490a46a 21 * @param array $params
77b97be7 22 *
a6c01b45 23 * @return array
00f8641b 24 * API result array
21150aae
CW
25 */
26function civicrm_api3_payment_processor_create($params) {
08f3bb14 27 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'PaymentProcessor');
21150aae
CW
28}
29
30/**
0aa0303c
EM
31 * Adjust Metadata for Create action.
32 *
33 * The metadata is used for setting defaults, documentation & validation.
21150aae 34 *
cf470720 35 * @param array $params
b081365f 36 * Array of parameters determined by getfields.
21150aae
CW
37 */
38function _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;
29adc103 42 $params['is_active']['api.default'] = TRUE;
26f5916f 43 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
8563c1ce 44 $params['financial_account_id']['api.default'] = CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID();
45 $params['financial_account_id']['api.required'] = TRUE;
6cc6cb8c 46 $params['financial_account_id']['type'] = CRM_Utils_Type::T_INT;
8563c1ce 47 $params['financial_account_id']['title'] = ts('Financial Account for Processor');
6cc6cb8c 48 $params['financial_account_id']['pseudoconstant'] = [
49 'table' => 'civicrm_financial_account',
50 'keyColumn' => 'id',
51 'labelColumn' => 'name',
52 ];
21150aae
CW
53}
54
55/**
d1b0d05e 56 * Deletes an existing PaymentProcessor.
21150aae 57 *
cf470720 58 * @param array $params
21150aae 59 *
a6c01b45 60 * @return array
00f8641b 61 * API result array
21150aae
CW
62 */
63function civicrm_api3_payment_processor_delete($params) {
64 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
65}
66
67/**
d1b0d05e 68 * Retrieve one or more PaymentProcessor.
21150aae 69 *
cf470720 70 * @param array $params
d1b0d05e 71 * Array of name/value pairs.
21150aae 72 *
a6c01b45 73 * @return array
00f8641b 74 * API result array
21150aae
CW
75 */
76function civicrm_api3_payment_processor_get($params) {
77 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
78}
6b4c9564 79
6b4c9564
CW
80/**
81 * Set default getlist parameters.
82 *
83 * @see _civicrm_api3_generic_getlist_defaults
84 *
85 * @param array $request
86 *
87 * @return array
88 */
89function _civicrm_api3_payment_processor_getlist_defaults(&$request) {
cf8f0fff
CW
90 return [
91 'description_field' => [
6b4c9564
CW
92 'payment_processor_type_id',
93 'description',
cf8f0fff
CW
94 ],
95 'params' => [
6b4c9564
CW
96 'is_test' => 0,
97 'is_active' => 1,
cf8f0fff
CW
98 ],
99 ];
6b4c9564 100}
6bc775cf 101
71b4fdf9 102/**
103 * Action payment.
104 *
105 * @param array $params
106 *
107 * @return array
108 * API result array.
f15d3f21 109 *
110 * @throws \API_Exception
abf0d662 111 * @throws \CiviCRM_API3_Exception
71b4fdf9 112 */
113function civicrm_api3_payment_processor_pay($params) {
abf0d662 114 /* @var CRM_Core_Payment $processor */
71b4fdf9 115 $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
116 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
f15d3f21 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 }
fe3055a2
JG
126 $message = $e->getMessage() ?? 'Payment Failed';
127 throw new API_Exception($message, $code, $errorData, $e);
f15d3f21 128 }
189e6408 129 return civicrm_api3_create_success(array($result), $params);
71b4fdf9 130}
131
132/**
133 * Action payment.
134 *
135 * @param array $params
136 */
137function _civicrm_api3_payment_processor_pay_spec(&$params) {
138 $params['payment_processor_id'] = [
abf0d662 139 'api.required' => TRUE,
71b4fdf9 140 'title' => ts('Payment processor'),
141 'type' => CRM_Utils_Type::T_INT,
142 ];
143 $params['amount'] = [
144 'api.required' => TRUE,
14df3f4a 145 'title' => ts('Amount to pay'),
71b4fdf9 146 'type' => CRM_Utils_Type::T_MONEY,
147 ];
783b62a7 148 $params['contribution_id'] = [
149 'api.required' => TRUE,
150 'title' => ts('Contribution ID'),
151 'type' => CRM_Utils_Type::T_INT,
49cba3ad 152 'api.aliases' => ['order_id'],
783b62a7 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 ];
71b4fdf9 166}
167
6bc775cf
MD
168/**
169 * Action refund.
170 *
171 * @param array $params
172 *
173 * @return array
174 * API result array.
16b41ddb 175 *
176 * @throws \API_Exception
177 * @throws \CiviCRM_API3_Exception
178 * @throws \Civi\Payment\Exception\PaymentProcessorException
6bc775cf
MD
179 */
180function civicrm_api3_payment_processor_refund($params) {
16b41ddb 181 /** @var \CRM_Core_Payment $processor */
6bc775cf 182 $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
16b41ddb 183 $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
6bc775cf 184 if (!$processor->supportsRefund()) {
16b41ddb 185 throw new API_Exception('Payment Processor does not support refund');
6bc775cf
MD
186 }
187 $result = $processor->doRefund($params);
16b41ddb 188 return civicrm_api3_create_success([$result], $params);
6bc775cf
MD
189}
190
191/**
192 * Action Refund.
193 *
194 * @param array $params
195 *
196 */
197function _civicrm_api3_payment_processor_refund_spec(&$params) {
198 $params['payment_processor_id'] = [
16b41ddb 199 'api.required' => TRUE,
6bc775cf
MD
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}