APIv4 - Add Address::getCoordinates action
[civicrm-core.git] / CRM / Core / Payment.php
index 6b599e0bf30563cdbc4ff2e789be9a8ac06dc201..34d6363005ad1457f393bd67416146fb96241bca 100644 (file)
@@ -1155,20 +1155,20 @@ abstract class CRM_Core_Payment {
   }
 
   /**
-   * Legacy. Better for a method to work on its own PropertyBag,
-   * but also, this function does not do very much.
+   * Get the submitted amount, padded to 2 decimal places, if needed.
    *
    * @param array $params
    *
    * @return string
-   * @throws \CRM_Core_Exception
    */
   protected function getAmount($params = []) {
     if (!CRM_Utils_Rule::numeric($params['amount'])) {
       CRM_Core_Error::deprecatedWarning('Passing Amount value that is not numeric is deprecated please report this in gitlab');
       return CRM_Utils_Money::formatUSLocaleNumericRounded(filter_var($params['amount'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), 2);
     }
-    return CRM_Utils_Money::formatUSLocaleNumericRounded($params['amount'], 2);
+    // Amount is already formatted to a machine-friendly format but may NOT have
+    // decimal places - eg. it could be 1000.1 so this would return 1000.10.
+    return Civi::format()->machineMoney($params['amount']);
   }
 
   /**
@@ -1366,14 +1366,12 @@ abstract class CRM_Core_Payment {
   public function doPayment(&$params, $component = 'contribute') {
     $propertyBag = \Civi\Payment\PropertyBag::cast($params);
     $this->_component = $component;
-    $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate');
 
     // If we have a $0 amount, skip call to processor and set payment_status to Completed.
     // Conceivably a processor might override this - perhaps for setting up a token - but we don't
     // have an example of that at the moment.
     if ($propertyBag->getAmount() == 0) {
-      $result['payment_status_id'] = array_search('Completed', $statuses);
-      $result['payment_status'] = 'Completed';
+      $result = $this->setStatusPaymentCompleted([]);
       return $result;
     }
 
@@ -1381,8 +1379,7 @@ abstract class CRM_Core_Payment {
       CRM_Core_Error::deprecatedFunctionWarning('doPayment', 'doTransferCheckout');
       $result = $this->doTransferCheckout($params, $component);
       if (is_array($result) && !isset($result['payment_status_id'])) {
-        $result['payment_status_id'] = array_search('Pending', $statuses);
-        $result['payment_status'] = 'Pending';
+        $result = $this->setStatusPaymentPending($result);
       }
     }
     else {
@@ -1391,12 +1388,10 @@ abstract class CRM_Core_Payment {
       if (is_array($result) && !isset($result['payment_status_id'])) {
         if (!empty($params['is_recur'])) {
           // See comment block.
-          $result['payment_status_id'] = array_search('Pending', $statuses);
-          $result['payment_status'] = 'Pending';
+          $result = $this->setStatusPaymentPending($result);
         }
         else {
-          $result['payment_status_id'] = array_search('Completed', $statuses);
-          $result['payment_status'] = 'Completed';
+          $result = $this->setStatusPaymentCompleted($result);
         }
       }
     }
@@ -1407,6 +1402,30 @@ abstract class CRM_Core_Payment {
     return $result;
   }
 
+  /**
+   * Set the payment status to Pending
+   * @param \Civi\Payment\PropertyBag|array $params
+   *
+   * @return array
+   */
+  protected function setStatusPaymentPending($params) {
+    $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
+    $params['payment_status'] = 'Pending';
+    return $params;
+  }
+
+  /**
+   * Set the payment status to Completed
+   * @param \Civi\Payment\PropertyBag|array $params
+   *
+   * @return array
+   */
+  protected function setStatusPaymentCompleted($params) {
+    $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
+    $params['payment_status'] = 'Completed';
+    return $params;
+  }
+
   /**
    * Cancel a recurring subscription.
    *
@@ -1674,8 +1693,7 @@ abstract class CRM_Core_Payment {
    * it is better to standardise to being here.
    *
    * @param int $invoiceId The ID to check.
-   *
-   * @param null $contributionID
+   * @param int|null $contributionID
    *   If a contribution exists pass in the contribution ID.
    *
    * @return bool
@@ -1693,8 +1711,8 @@ abstract class CRM_Core_Payment {
   /**
    * Get url for users to manage this recurring contribution for this processor.
    *
-   * @param int $entityID
-   * @param null $entity
+   * @param int|null $entityID
+   * @param string|null $entity
    * @param string $action
    *
    * @return string|null
@@ -1724,6 +1742,10 @@ abstract class CRM_Core_Payment {
         }
         $url = 'civicrm/contribute/updaterecur';
         break;
+
+      default:
+        $url = '';
+        break;
     }
 
     $userId = CRM_Core_Session::singleton()->get('userID');