EventInfo template cleanup.
[civicrm-core.git] / api / v3 / Payment.php
index 3ab01d934cf0980554ad3249b7496727b9f07d18..bf10d26f1546f9ea5684aede88cae3ca94b23fb1 100644 (file)
  * @throws \CiviCRM_API3_Exception
  */
 function civicrm_api3_payment_get($params) {
-  $financialTrxn = [];
-  $limit = '';
-  if (isset($params['options']) && !empty($params['options']['limit'])) {
-    $limit = $params['options']['limit'] ?? NULL;
-  }
-  $params['options']['limit'] = 0;
-  if (isset($params['trxn_id'])) {
-    $params['financial_trxn_id.trxn_id'] = $params['trxn_id'];
+  $params['is_payment'] = TRUE;
+  $contributionID = $params['entity_id'] ?? NULL;
+
+  // In order to support contribution id we need to do an extra lookup.
+  if ($contributionID) {
+    $eftParams = [
+      'entity_id' => $contributionID,
+      'entity_table' => 'civicrm_contribution',
+      'options' => ['limit' => 0],
+      'financial_trxn_id.is_payment' => 1,
+    ];
+    $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams)['values'];
+    if (empty($eft)) {
+      return civicrm_api3_create_success([], $params, 'Payment', 'get');
+    }
+    foreach ($eft as $entityFinancialTrxn) {
+      $params['financial_trxn_id']['IN'][] = $entityFinancialTrxn['financial_trxn_id'];
+    }
   }
-  $eftParams = $params;
-  unset($eftParams['return']);
-  // @todo - why do we fetch EFT params at all?
-  $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams);
-  if (!empty($eft['values'])) {
-    $eftIds = [];
-    foreach ($eft['values'] as $efts) {
-      if (empty($efts['financial_trxn_id'])) {
-        continue;
-      }
-      $eftIds[] = $efts['financial_trxn_id'];
-      $map[$efts['financial_trxn_id']] = $efts['entity_id'];
+
+  $financialTrxn = civicrm_api3('FinancialTrxn', 'get', array_merge($params, ['sequential' => FALSE]))['values'];
+  if ($contributionID) {
+    foreach ($financialTrxn as &$values) {
+      $values['contribution_id'] = $contributionID;
     }
-    if (!empty($eftIds)) {
-      $ftParams = [
-        'id' => ['IN' => $eftIds],
-        'is_payment' => 1,
-      ];
-      if ($limit) {
-        $ftParams['options']['limit'] = $limit;
-      }
-      $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
-      foreach ($financialTrxn['values'] as &$values) {
-        $values['contribution_id'] = $map[$values['id']];
-      }
+  }
+  elseif (!empty($financialTrxn)) {
+    $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', ['financial_trxn_id' => ['IN' => array_keys($financialTrxn)], 'entity_table' => 'civicrm_contribution', 'options' => ['limit' => 0]])['values'];
+    foreach ($entityFinancialTrxns as $entityFinancialTrxn) {
+      $financialTrxn[$entityFinancialTrxn['financial_trxn_id']]['contribution_id'] = $entityFinancialTrxn['entity_id'];
     }
   }
-  return civicrm_api3_create_success(CRM_Utils_Array::value('values', $financialTrxn, []), $params, 'Payment', 'get');
+
+  return civicrm_api3_create_success($financialTrxn, $params, 'Payment', 'get');
 }
 
 /**
@@ -103,7 +100,7 @@ function civicrm_api3_payment_cancel($params) {
   $paymentParams = [
     'total_amount' => -$entity['amount'],
     'contribution_id' => $entity['entity_id'],
-    'trxn_date' => CRM_Utils_Array::value('trxn_date', $params, 'now'),
+    'trxn_date' => $params['trxn_date'] ?? 'now',
     'cancelled_payment_id' => $params['id'],
   ];
 
@@ -276,6 +273,22 @@ function _civicrm_api3_payment_create_spec(&$params) {
         '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',
+      ],
+    ],
     'check_number' => [
       'name' => 'check_number',
       'type' => CRM_Utils_Type::T_STRING,
@@ -325,10 +338,6 @@ function _civicrm_api3_payment_get_spec(&$params) {
       'title' => ts('Contribution ID'),
       'type' => CRM_Utils_Type::T_INT,
     ],
-    'entity_table' => [
-      'title' => ts('Entity Table'),
-      'api.default' => 'civicrm_contribution',
-    ],
     'entity_id' => [
       'title' => ts('Entity ID'),
       'type' => CRM_Utils_Type::T_INT,