diff --git a/CRM/Core/Payment/Stripe.php b/CRM/Core/Payment/Stripe.php index 3c856783ca06d991a7c6265d75ed88e183467475..8e04d134d4374c9842561ef29d9e9a27cce4233a 100644 --- a/CRM/Core/Payment/Stripe.php +++ b/CRM/Core/Payment/Stripe.php @@ -725,14 +725,11 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment { } if (($stripeCharge['currency'] !== $stripeBalanceTransaction['currency']) && (!empty($stripeBalanceTransaction['exchange_rate']))) { - $newParams['fee_amount'] = ($stripeBalanceTransaction->fee / $stripeBalanceTransaction['exchange_rate']) / 100; + $newParams['fee_amount'] = CRM_Stripe_Api::currencyConversion($stripeBalanceTransaction->fee, $stripeBalanceTransaction['exchange_rate'], $stripeCharge['currency']); } else { $newParams['fee_amount'] = $stripeBalanceTransaction->fee / 100; } - // We must round to currency precision otherwise payments may fail because Contribute BAO saves but then - // can't retrieve because it tries to use the full unrounded number when it only got saved with 2dp. - $newParams['fee_amount'] = round($newParams['fee_amount'], CRM_Utils_Money::getCurrencyPrecision($stripeCharge['currency'])); // Success! // Set the desired contribution status which will be set later (do not set on the contribution here!) $params['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); diff --git a/CRM/Core/Payment/StripeIPN.php b/CRM/Core/Payment/StripeIPN.php index 6dcb1b591cbed9094fc1690da6c60ab5cd14ff4d..89eab6b79d5559eebc13722831da343ae44570c1 100644 --- a/CRM/Core/Payment/StripeIPN.php +++ b/CRM/Core/Payment/StripeIPN.php @@ -442,9 +442,15 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN { $this->fee = 0.0; if ($balanceTransactionID) { try { + $currency = $this->retrieve('currency', 'String', FALSE); $balanceTransaction = \Stripe\BalanceTransaction::retrieve($balanceTransactionID); - $this->amount = $balanceTransaction->amount / 100; - $this->fee = $balanceTransaction->fee / 100; + if ($currency !== $balanceTransaction->currency && !empty($balanceTransaction->exchange_rate)) { + $this->amount = CRM_Stripe_Api::currencyConversion($balanceTransaction->amount, $balanceTransaction->exchange_rate, $currency); + $this->fee = CRM_Stripe_Api::currencyConversion($balanceTransaction->fee, $balanceTransaction->exchange_rate, $currency); + } else { + $this->amount = $balanceTransaction->amount / 100; + $this->fee = $balanceTransaction->fee / 100; + } } catch(Exception $e) { $this->exception('Error retrieving balance transaction. ' . $e->getMessage()); diff --git a/CRM/Stripe/Api.php b/CRM/Stripe/Api.php index c0a1a96f7160a9b122d0568ed83617935793c1d7..239bfcac59c9d6381c49c52a4e9b8d2e1d63f231 100644 --- a/CRM/Stripe/Api.php +++ b/CRM/Stripe/Api.php @@ -192,4 +192,20 @@ class CRM_Stripe_Api { } return NULL; } + + /** + * Convert amount to a new currency + * + * @param type $amount + * @param type $exchangeRate + * @param type $currency + * @return type + */ + public static function currencyConversion($amount, $exchangeRate, $currency) { + $amount = ($amount / $exchangeRate) / 100; + // We must round to currency precision otherwise payments may fail because Contribute BAO saves but then + // can't retrieve because it tries to use the full unrounded number when it only got saved with 2dp. + $amount = round($amount, CRM_Utils_Money::getCurrencyPrecision($currency)); + return $amount; + } }