Skip to content
Snippets Groups Projects
Commit 7c4e3063 authored by mattwire's avatar mattwire
Browse files

Resolve issues with backend forms and tax amounts. Also resolve issues with...

Resolve issues with backend forms and tax amounts. Also resolve issues with money formats that don't use a dot as decimal separator.
parent d90a4bab
No related branches found
No related tags found
No related merge requests found
......@@ -171,13 +171,14 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
*
* @param $params
*
* @return string
* @return int
*/
public function getAmount($params) {
public function getAmount($params): int {
$amount = $params['amount'] ?? 0.0;
$amount = number_format($amount, CRM_Utils_Money::getCurrencyPrecision($this->getCurrency($params)), '.', '');
// Stripe amount required in cents.
$amount = number_format($params['amount'], 2, '.', '');
$amount = (int) preg_replace('/[^\d]/', '', strval($amount));
return $amount;
$amount = preg_replace('/[^\d]/', '', strval($amount));
return (int) $amount;
}
/**
......@@ -517,9 +518,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// This is where we actually charge the customer
try {
$intent = \Stripe\PaymentIntent::retrieve($params['paymentIntentID']);
if ($intent->amount !== $this->getAmount($params)) {
if ($intent->amount != $this->getAmount($params)) {
$intentParams['amount'] = $this->getAmount($params);
//$this->handleError('Amount differs', E::ts('Amount is different from the authorised amount (%1, %2)', [1 => $intent->amount, 2=> $this->getAmount($params)]), $params['stripe_error_url']);
}
$intent = \Stripe\PaymentIntent::update($intent->id, $intentParams);
}
......
......@@ -41,7 +41,7 @@ class CRM_Stripe_AJAX {
public static function confirmPayment() {
$paymentMethodID = CRM_Utils_Request::retrieveValue('payment_method_id', 'String');
$paymentIntentID = CRM_Utils_Request::retrieveValue('payment_intent_id', 'String');
$amount = CRM_Utils_Request::retrieveValue('amount', 'Money');
$amount = CRM_Utils_Request::retrieveValue('amount', 'String');
$capture = CRM_Utils_Request::retrieveValue('capture', 'Boolean', FALSE);
$title = CRM_Utils_Request::retrieveValue('description', 'String');
$confirm = TRUE;
......@@ -70,7 +70,7 @@ class CRM_Stripe_AJAX {
try {
$intent = \Stripe\PaymentIntent::create([
'payment_method' => $paymentMethodID,
'amount' => $processor->getAmount(['amount' => $amount]),
'amount' => $processor->getAmount(['amount' => $amount, 'currency' => $currency]),
'currency' => $currency,
'confirmation_method' => 'manual',
'capture_method' => 'manual',
......
......@@ -453,18 +453,20 @@ CRM.$(function($) {
}
function getTotalAmount() {
var totalFee = null;
var totalFee = 0.0;
if ((document.getElementById('additional_participants') !== null) &&
(document.getElementById('additional_participants').value.length !== 0)) {
debugging('Cannot setup paymentIntent because we don\'t know the final price');
return totalFee;
debugging('We don\'t know the final price - registering additional participants');
}
else if (document.getElementById('totalTaxAmount') !== null) {
totalFee = parseFloat(calculateTaxAmount());
debugging('Calculated amount using internal calculateTaxAmount()');
}
if (typeof calculateTotalFee == 'function') {
else if (typeof calculateTotalFee == 'function') {
// This is ONLY triggered in the following circumstances on a CiviCRM contribution page:
// - With a priceset that allows a 0 amount to be selected.
// - When Stripe is the ONLY payment processor configured on the page.
totalFee = calculateTotalFee();
totalFee = parseFloat(calculateTotalFee());
}
else if (getIsDrupalWebform()) {
// This is how webform civicrm calculates the amount in webform_civicrm_payment.js
......@@ -474,9 +476,25 @@ CRM.$(function($) {
}
else if (document.getElementById('total_amount')) {
// The input#total_amount field exists on backend contribution forms
totalFee = document.getElementById('total_amount').value;
totalFee = parseFloat(document.getElementById('total_amount').value);
}
debugging('getTotalAmount: ' + totalFee.toFixed(2));
return totalFee.toFixed(2);
}
// This is calculated in CRM/Contribute/Form/Contribution.tpl and is used to calculate the total
// amount with tax on backend submit contribution forms.
// The only way we can get the amount is by parsing the text field and extracting the final bit after the space.
// eg. "Amount including Tax: $ 4.50" gives us 4.50.
// The PHP side is responsible for converting money formats (we just parse to cents and remove any ,. chars).
function calculateTaxAmount() {
var totalTaxAmount = 0;
if (document.getElementById('totalTaxAmount') === null) {
return totalTaxAmount;
}
return totalFee;
totalTaxAmount = document.getElementById('totalTaxAmount').textContent.split(' ').pop();
return totalTaxAmount;
}
function getIsRecur() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment