$params['invoiceID'] = md5(uniqid(rand(), TRUE));
// We want to move away from passing in amount as it is calculated by the actually-submitted params.
- $params['amount'] = $params['amount'] ?? $form->getMainContributionAmount($params);
+ if ($form->getMainContributionAmount($params)) {
+ $params['amount'] = $form->getMainContributionAmount($params);
+ }
$paramsProcessedForForm = $form->_params = self::getFormParams($params['id'], $params);
- $form->_amount = $params['amount'];
+
+ $order = new CRM_Financial_BAO_Order();
+ $order->setPriceSelectionFromUnfilteredInput($params);
+ if (isset($params['amount'])) {
+ // @todo deprecate receiving amount, calculate on the form.
+ $order->setOverrideTotalAmount($params['amount']);
+ }
+ $amount = $order->getTotalAmount();
+ $form->_amount = $params['amount'] = $form->_params['amount'] = $params['amount'] ?? $amount;
// hack these in for test support.
$form->_fields['billing_first_name'] = 1;
$form->_fields['billing_last_name'] = 1;
+--------------------------------------------------------------------+
*/
+use Civi\Api4\PriceField;
+
/**
*
* @package CRM
}
foreach ($this->getPriceOptions() as $fieldID => $valueID) {
+ if (!isset($this->priceSetID)) {
+ $this->setPriceSetID(PriceField::get()->addSelect('price_set_id')->addWhere('id', '=', $fieldID)->execute()->first()['price_set_id']);
+ }
$throwAwayArray = [];
// @todo - still using getLine for now but better to bring it to this class & do a better job.
- $lineItems[$valueID] = CRM_Price_BAO_PriceSet::getLine($params, $throwAwayArray, $this->getPriceSetID(), $this->getPriceFieldSpec($fieldID), $fieldID, 0)[1][$valueID];
+ $newLines = CRM_Price_BAO_PriceSet::getLine($params, $throwAwayArray, $this->getPriceSetID(), $this->getPriceFieldSpec($fieldID), $fieldID, 0)[1];
+ foreach ($newLines as $newLine) {
+ $lineItems[$newLine['price_field_value_id']] = $newLine;
+ }
}
foreach ($lineItems as &$lineItem) {
}
/**
- * Get the total tax amount for the order.
+ * Get the total amount for the order.
*
* @return float
*
return $amount;
}
+ /**
+ * Get the total tax amount for the order.
+ *
+ * @return float
+ *
+ * @throws \CiviCRM_API3_Exception
+ */
+ public function getTotalAmount() :float {
+ $amount = 0.0;
+ foreach ($this->getLineItems() as $lineItem) {
+ $amount += $lineItem['line_total'] ?? 0.0;
+ }
+ return $amount;
+ }
+
/**
* Get the tax rate for the given financial type.
*
* @throws \CRM_Core_Exception
*/
protected function validateAllContributions() {
- $contributions = $this->callAPISuccess('Contribution', 'get', [])['values'];
+ $contributions = $this->callAPISuccess('Contribution', 'get', ['return' => ['tax_amount', 'total_amount']])['values'];
foreach ($contributions as $contribution) {
$lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values'];
$total = 0;
+ $taxTotal = 0;
foreach ($lineItems as $lineItem) {
$total += $lineItem['line_total'];
+ $taxTotal += (float) ($lineItem['tax_amount'] ?? 0);
}
+ $this->assertEquals($taxTotal, (float) ($contribution['tax_amount'] ?? 0));
$this->assertEquals($total, $contribution['total_amount']);
}
}
$submitParams = [
'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
'id' => (int) $this->_ids['contribution_page'],
- 'amount' => 80,
'first_name' => 'Billy',
'last_name' => 'Gruff',
'email' => 'billy@goat.gruff',
];
$this->addPriceFields($submitParams);
- $this->callAPISuccess('contribution_page', 'submit', $submitParams);
+ $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
$contribution = $this->callAPISuccessGetSingle('contribution', [
'contribution_page_id' => $this->_ids['contribution_page'],
'contribution_status_id' => 'Pending',
// Set quantity for our test
$submitParams['price_' . $priceFieldId] = 110;
- // contribution_page submit requires amount and tax_amount - and that's ok we're not testing that - we're testing at the LineItem level
- $submitParams['amount'] = $this->formatMoneyInput(180 * 16.95 + 110 * 2.95);
// This is the correct Tax Amount - use it later to compare to what the CiviCRM Core came up with at the LineItem level
$submitParams['tax_amount'] = (180 * 16.95 * 0.10 + 110 * 2.95 * 0.10);
- $this->callAPISuccess('contribution_page', 'submit', $submitParams);
+ $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
+ $this->validateAllContributions();
+
$contribution = $this->callAPISuccessGetSingle('contribution', [
'contribution_page_id' => $this->_ids['contribution_page'],
]);
]);
$finalContribution = $this->callAPISuccess('Contribution', 'getsingle', ['id' => $contribution['id'], 'return' => ['tax_amount', 'total_amount']]);
-
$this->assertEquals($lineItem1['line_total'] + $lineItem2['line_total'], round(180 * 16.95 + 110 * 2.95, 2), 'Line Item Total is incorrect.');
- $this->assertEquals($lineItem1['line_total'] + $lineItem2['line_total'], $finalContribution['total_amount'], 'Contribution total should match line item totals');
-
$this->assertEquals(round($lineItem1['tax_amount'] + $lineItem2['tax_amount'], 2), round(180 * 16.95 * 0.10 + 110 * 2.95 * 0.10, 2), 'Wrong Sales Tax Amount is calculated and stored.');
- $this->assertEquals(round($lineItem1['tax_amount'] + $lineItem2['tax_amount'], 2), $finalContribution['tax_amount'], 'Sales Tax Amount on Contribution does not match sum of Sales Tax Amount across the Line Items.');
-
}
/**
/**
* Test to ensure mail is sent on chosing pay later
*/
- public function testpayLater() {
+ public function testPayLater() {
$mut = new CiviMailUtils($this, TRUE);
$this->swapMessageTemplateForTestTemplate();
$this->createLoggedInUser();
'description' => 'Online Contribution: Help Support CiviCRM!',
'price_set_id' => $priceSet['id'],
];
- $this->callAPISuccess('contribution_page', 'submit', $params);
+ $this->callAPISuccess('ContributionPage', 'submit', $params);
$mut->checkMailLog([
'is_pay_later:::1',