Commit | Line | Data |
---|---|---|
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 | */ |
26 | function civicrm_api3_payment_processor_create($params) { | |
a1936a29 | 27 | if (empty($params['id']) && empty($params['payment_instrument_id'])) { |
cf8f0fff | 28 | $params['payment_instrument_id'] = civicrm_api3('PaymentProcessorType', 'getvalue', [ |
a1936a29 EM |
29 | 'id' => $params['payment_processor_type_id'], |
30 | 'return' => 'payment_instrument_id', | |
cf8f0fff | 31 | ]); |
a1936a29 | 32 | } |
08f3bb14 | 33 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'PaymentProcessor'); |
21150aae CW |
34 | } |
35 | ||
36 | /** | |
0aa0303c EM |
37 | * Adjust Metadata for Create action. |
38 | * | |
39 | * The metadata is used for setting defaults, documentation & validation. | |
21150aae | 40 | * |
cf470720 | 41 | * @param array $params |
b081365f | 42 | * Array of parameters determined by getfields. |
21150aae CW |
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; | |
29adc103 | 48 | $params['is_active']['api.default'] = TRUE; |
26f5916f | 49 | $params['domain_id']['api.default'] = CRM_Core_Config::domainID(); |
8563c1ce | 50 | $params['financial_account_id']['api.default'] = CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID(); |
51 | $params['financial_account_id']['api.required'] = TRUE; | |
6cc6cb8c | 52 | $params['financial_account_id']['type'] = CRM_Utils_Type::T_INT; |
8563c1ce | 53 | $params['financial_account_id']['title'] = ts('Financial Account for Processor'); |
6cc6cb8c | 54 | $params['financial_account_id']['pseudoconstant'] = [ |
55 | 'table' => 'civicrm_financial_account', | |
56 | 'keyColumn' => 'id', | |
57 | 'labelColumn' => 'name', | |
58 | ]; | |
21150aae CW |
59 | } |
60 | ||
61 | /** | |
d1b0d05e | 62 | * Deletes an existing PaymentProcessor. |
21150aae | 63 | * |
cf470720 | 64 | * @param array $params |
21150aae | 65 | * |
a6c01b45 | 66 | * @return array |
00f8641b | 67 | * API result array |
21150aae CW |
68 | */ |
69 | function civicrm_api3_payment_processor_delete($params) { | |
70 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
71 | } | |
72 | ||
73 | /** | |
d1b0d05e | 74 | * Retrieve one or more PaymentProcessor. |
21150aae | 75 | * |
cf470720 | 76 | * @param array $params |
d1b0d05e | 77 | * Array of name/value pairs. |
21150aae | 78 | * |
a6c01b45 | 79 | * @return array |
00f8641b | 80 | * API result array |
21150aae CW |
81 | */ |
82 | function civicrm_api3_payment_processor_get($params) { | |
83 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
84 | } | |
6b4c9564 | 85 | |
6b4c9564 CW |
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) { | |
cf8f0fff CW |
96 | return [ |
97 | 'description_field' => [ | |
6b4c9564 CW |
98 | 'payment_processor_type_id', |
99 | 'description', | |
cf8f0fff CW |
100 | ], |
101 | 'params' => [ | |
6b4c9564 CW |
102 | 'is_test' => 0, |
103 | 'is_active' => 1, | |
cf8f0fff CW |
104 | ], |
105 | ]; | |
6b4c9564 | 106 | } |
6bc775cf | 107 | |
71b4fdf9 | 108 | /** |
109 | * Action payment. | |
110 | * | |
111 | * @param array $params | |
112 | * | |
113 | * @return array | |
114 | * API result array. | |
f15d3f21 | 115 | * |
116 | * @throws \API_Exception | |
abf0d662 | 117 | * @throws \CiviCRM_API3_Exception |
71b4fdf9 | 118 | */ |
119 | function civicrm_api3_payment_processor_pay($params) { | |
abf0d662 | 120 | /* @var CRM_Core_Payment $processor */ |
71b4fdf9 | 121 | $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']); |
122 | $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']])); | |
f15d3f21 | 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 | } | |
189e6408 | 134 | return civicrm_api3_create_success(array($result), $params); |
71b4fdf9 | 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'] = [ | |
abf0d662 | 144 | 'api.required' => TRUE, |
71b4fdf9 | 145 | 'title' => ts('Payment processor'), |
146 | 'type' => CRM_Utils_Type::T_INT, | |
147 | ]; | |
148 | $params['amount'] = [ | |
149 | 'api.required' => TRUE, | |
14df3f4a | 150 | 'title' => ts('Amount to pay'), |
71b4fdf9 | 151 | 'type' => CRM_Utils_Type::T_MONEY, |
152 | ]; | |
783b62a7 | 153 | $params['contribution_id'] = [ |
154 | 'api.required' => TRUE, | |
155 | 'title' => ts('Contribution ID'), | |
156 | 'type' => CRM_Utils_Type::T_INT, | |
49cba3ad | 157 | 'api.aliases' => ['order_id'], |
783b62a7 | 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 | ]; | |
71b4fdf9 | 171 | } |
172 | ||
6bc775cf MD |
173 | /** |
174 | * Action refund. | |
175 | * | |
176 | * @param array $params | |
177 | * | |
178 | * @return array | |
179 | * API result array. | |
16b41ddb | 180 | * |
181 | * @throws \API_Exception | |
182 | * @throws \CiviCRM_API3_Exception | |
183 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
6bc775cf MD |
184 | */ |
185 | function civicrm_api3_payment_processor_refund($params) { | |
16b41ddb | 186 | /** @var \CRM_Core_Payment $processor */ |
6bc775cf | 187 | $processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']); |
16b41ddb | 188 | $processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']])); |
6bc775cf | 189 | if (!$processor->supportsRefund()) { |
16b41ddb | 190 | throw new API_Exception('Payment Processor does not support refund'); |
6bc775cf MD |
191 | } |
192 | $result = $processor->doRefund($params); | |
16b41ddb | 193 | return civicrm_api3_create_success([$result], $params); |
6bc775cf MD |
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'] = [ | |
16b41ddb | 204 | 'api.required' => TRUE, |
6bc775cf MD |
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 | } |