Mjwpayment.php 8.49 KB
Newer Older
mattwire's avatar
mattwire committed
1
<?php
2
3
4
5
6
7
8
9
/*
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC. All rights reserved.                        |
 |                                                                    |
 | This work is published under the GNU AGPLv3 license with some      |
 | permitted exceptions and without any warranty. For full license    |
 | and copyright information, see https://civicrm.org/licensing       |
 +--------------------------------------------------------------------+
mattwire's avatar
mattwire committed
10
11
 */

12
13
require_once('api/v3/Payment.php');

mattwire's avatar
mattwire committed
14
15
16
17
18
/**
 * @todo mjwpayment.get_contribution is a replacement for Contribution.get
 *   which support querying by contribution/payment trxn_id per https://github.com/civicrm/civicrm-core/pull/14748
 *   - These API functions should be REMOVED once core has the above PR merged and we increment the min version for the extension.
 *   - The change is small, but to re-implement them here we have to copy quite a lot over.
mattwire's avatar
mattwire committed
19
20
 * @todo mjwpayment.get_payment is a replacement for Payment.get:
 *   https://github.com/civicrm/civicrm-core/pull/16603
mattwire's avatar
mattwire committed
21
22
23
24
25
26
27
28
29
30
 */
/**
 * Adjust Metadata for Get action.
 *
 * The metadata is used for setting defaults, documentation & validation.
 *
 * @param array $params
 *   Array of parameters determined by getfields.
 */
function _civicrm_api3_mjwpayment_get_contribution_spec(&$params) {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  $params = [
    'contribution_test' => [
      'api.default' => 0,
      'type' => CRM_Utils_Type::T_BOOLEAN,
      'title' => 'Get Test Contributions?',
      'api.aliases' => ['is_test'],
    ],
    'trxn_id' => [
      'name' => 'trxn_id',
      'type' => CRM_Utils_Type::T_STRING,
      'title' => ts('Transaction ID'),
      'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
      'maxlength' => 255,
      'size' => 10,
      'where' => 'civicrm_financial_trxn.trxn_id',
      'table_name' => 'civicrm_financial_trxn',
      'entity' => 'FinancialTrxn',
      'bao' => 'CRM_Financial_DAO_FinancialTrxn',
      'localizable' => 0,
      'html' => [
        'type' => 'Text',
      ],
    ],
    'order_reference' => [
      'name' => 'order_reference',
      'type' => CRM_Utils_Type::T_STRING,
      'title' => 'Order Reference',
      'description' => 'Payment Processor external order reference',
      'maxlength' => 255,
      'size' => 25,
      'where' => 'civicrm_financial_trxn.order_reference',
      'table_name' => 'civicrm_financial_trxn',
      'entity' => 'FinancialTrxn',
      'bao' => 'CRM_Financial_DAO_FinancialTrxn',
      'localizable' => 0,
      'html' => [
        'type' => 'Text',
      ],
    ],
mattwire's avatar
mattwire committed
70
71
72
73
74
75
76
77
78
79
  ];
}

/**
 * Retrieve a set of contributions.
 *
 * @param array $params
 *  Input parameters.
 *
 * @return array
mattwire's avatar
mattwire committed
80
 *   Array of contribution, if error an array with an error id and error message
mattwire's avatar
mattwire committed
81
82
 */
function civicrm_api3_mjwpayment_get_contribution($params) {
83
  $payments = civicrm_api3('Mjwpayment', 'get_payment', $params);
mattwire's avatar
mattwire committed
84

85
86
87
88
89
90
91
92
93
94
  if ($payments['count'] > 0) {
    // We found at least one payment for the params we were given.
    // We may have more than one payment (eg. A payment + a refund payment)
    // Return the contribution of the FIRST payment (all found payments SHOULD reference the same contribution)
    $contributionID = reset($payments['values'])['contribution_id'];
    $contribution = civicrm_api3('Contribution', 'getsingle', [
      'id' => $contributionID,
      'contribution_test' => $params['contribution_test'],
    ]);
    $contribution['payments'] = $payments['values'];
mattwire's avatar
mattwire committed
95
  }
96
97
98
99
100
101
102
  else {
    $contributionParams = [
      'options' => ['limit' => 1, 'sort' => 'id DESC'],
      'contribution_test' => $params['contribution_test'],
    ];
    if (isset($params['order_reference'])) {
      $contributionParams['trxn_id'][] = $params['order_reference'];
mattwire's avatar
mattwire committed
103
    }
104
105
    if (isset($params['trxn_id'])) {
      $contributionParams['trxn_id'][] = $params['trxn_id'];
mattwire's avatar
mattwire committed
106
    }
107
108
109
    if (isset($contributionParams['trxn_id'])) {
      $contributionParams['trxn_id'] = ['IN' => $contributionParams['trxn_id']];
    }
mattwire's avatar
mattwire committed
110
111
    $contribution = civicrm_api3('Contribution', 'get', $contributionParams)['values'];
    $contribution = reset($contribution);
mattwire's avatar
mattwire committed
112
  }
mattwire's avatar
mattwire committed
113
114
115
116
117
  $result = [];
  if ($contribution) {
    $result = [$contribution['id'] => $contribution];
  }
  return civicrm_api3_create_success($result, $params, 'Mjwpayment', 'get_contribution');
mattwire's avatar
mattwire committed
118
119
120
121
122
123
}

/**
 * Adjust Metadata for Get action.
 *
 * The metadata is used for setting defaults, documentation & validation.
124
125
 * @fixme replace with call to _civicrm_api3_payment_get_spec once https://github.com/civicrm/civicrm-core/pull/19449
 * is merged - 5.35?
mattwire's avatar
mattwire committed
126
127
128
129
130
131
132
 *
 * @param array $params
 *   Array of parameters determined by getfields.
 */
function _civicrm_api3_mjwpayment_get_payment_spec(&$params) {
  $params = [
    'contribution_id' => [
mattwire's avatar
mattwire committed
133
      'title' => ts('Contribution ID'),
mattwire's avatar
mattwire committed
134
135
136
      'type' => CRM_Utils_Type::T_INT,
    ],
    'entity_id' => [
mattwire's avatar
mattwire committed
137
      'title' => ts('Entity ID'),
mattwire's avatar
mattwire committed
138
139
140
141
      'type' => CRM_Utils_Type::T_INT,
      'api.aliases' => ['contribution_id'],
    ],
    'trxn_id' => [
mattwire's avatar
mattwire committed
142
143
      'title' => ts('Transaction ID'),
      'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
mattwire's avatar
mattwire committed
144
145
      'type' => CRM_Utils_Type::T_STRING,
    ],
mattwire's avatar
mattwire committed
146
147
148
149
150
    'order_reference' => [
      'title' => 'Order Reference',
      'description' => 'Payment Processor external order reference',
      'type' => CRM_Utils_Type::T_STRING,
    ],
mattwire's avatar
mattwire committed
151
152
153
154
155
156
157
158
159
160
    'trxn_date' => [
      'title' => ts('Payment Date'),
      'type' => CRM_Utils_Type::T_TIMESTAMP,
    ],
    'financial_trxn_id' => [
      'title' => ts('Payment ID'),
      'description' => ts('The ID of the record in civicrm_financial_trxn'),
      'type' => CRM_Utils_Type::T_INT,
      'api.aliases' => ['payment_id', 'id'],
    ],
mattwire's avatar
mattwire committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  ];
}

/**
 * Retrieve a set of financial transactions which are payments.
 *
 * @param array $params
 *  Input parameters.
 *
 * @return array
 *   Array of financial transactions which are payments, if error an array with an error id and error message
 * @throws \CiviCRM_API3_Exception
 */
function civicrm_api3_mjwpayment_get_payment($params) {
175
  return civicrm_api3_payment_get($params);
mattwire's avatar
mattwire committed
176
}
177
178
179
180
181
182
183
184
185
186

/**
 * Adjust Metadata for Create action.
 *
 * The metadata is used for setting defaults, documentation & validation.
 *
 * @param array $params
 *   Array of parameters.
 */
function _civicrm_api3_mjwpayment_create_payment_spec(&$params) {
187
  _civicrm_api3_payment_create_spec($params);
188
189
190
191
}

/**
 * Add a payment for a Contribution.
192
 * @fixme This is a copy of API3 Payment.create including some handling for bugfixes in certain versions.
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
 *
 * @param array $params
 *   Input parameters.
 *
 * @return array
 *   Api result array
 *
 * @throws \CRM_Core_Exception
 * @throws \CiviCRM_API3_Exception
 */
function civicrm_api3_mjwpayment_create_payment($params) {
  if (empty($params['skipCleanMoney'])) {
    foreach (['total_amount', 'net_amount', 'fee_amount'] as $field) {
      if (isset($params[$field])) {
        $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
      }
    }
  }
  if (!empty($params['payment_processor'])) {
    // I can't find evidence this is passed in - I was gonna just remove it but decided to deprecate  as I see getToFinancialAccount
    // also anticipates it.
    CRM_Core_Error::deprecatedFunctionWarning('passing payment_processor is deprecated - use payment_processor_id');
    $params['payment_processor_id'] = $params['payment_processor'];
  }
  // Check if it is an update
  if (!empty($params['id'])) {
    $amount = $params['total_amount'];
    civicrm_api3('Payment', 'cancel', $params);
    $params['total_amount'] = $amount;
  }
223
224
225
226
227
228
229
230
  // @todo #17777 - we store receive_date so we can fix it later
  if (version_compare(CRM_Utils_System::version(), '5.29', '<')) {
    $contributionReceiveDate = (string) civicrm_api3('Contribution', 'getvalue', [
      'id' => $params['contribution_id'],
      'return' => 'receive_date'
    ]);
  }

231
232
  $trxn = CRM_Financial_BAO_Payment::create($params);

233
234
235
236
237
238
239
240
241
  if (version_compare(CRM_Utils_System::version(), '5.29', '<')) {
    // @todo Fix contribution receive date as it should not be updated by Payment.create https://github.com/civicrm/civicrm-core/pull/17777
    $sql = 'UPDATE civicrm_contribution SET receive_date="%2" WHERE id=%1';
    $sqlParams = [
      1 => [$params['contribution_id'], 'Positive'],
      2 => [CRM_Utils_Date::isoToMysql($contributionReceiveDate), 'Date']
    ];
    CRM_Core_DAO::executeQuery($sql, $sqlParams);
  }
242
243
244
245
246

  $values = [];
  _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
  return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
}