Skip to content
Snippets Groups Projects
Commit 587e7407 authored by mattwire's avatar mattwire
Browse files

Switch to getAmountForStripeAPI() function

parent 828a2122
Branches
Tags
1 merge request!217Implement Stripe Checkout (with support for SEPA and ACH)
......@@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/
use Brick\Money\Money;
use Civi\Api4\PaymentprocessorWebhook;
use CRM_Stripe_ExtensionUtil as E;
use Civi\Payment\PropertyBag;
......@@ -240,13 +241,22 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
*
* @return string
*/
public function getAmount($params = []): string {
protected function getAmount($params = []) {
$amount = number_format((float) $params['amount'] ?? 0.0, CRM_Utils_Money::getCurrencyPrecision($this->getCurrency($params)), '.', '');
// Stripe amount required in cents.
$amount = preg_replace('/[^\d]/', '', strval($amount));
return $amount;
}
/**
* @param \Civi\Payment\PropertyBag $propertyBag
*
* @throws \Brick\Money\Exception\UnknownCurrencyException
*/
public function getAmountFormattedForStripeAPI(PropertyBag $propertyBag): string {
return Money::of($propertyBag->getAmount(), $propertyBag->getCurrency())->getMinorAmount()->getIntegralPart();
}
/**
* Set API parameters for Stripe (such as identifier, api version, api key)
*/
......@@ -550,7 +560,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
*/
public function doPayment(&$paymentParams, $component = 'contribute') {
/* @var \Civi\Payment\PropertyBag $propertyBag */
$propertyBag = \Civi\Payment\PropertyBag::cast($paymentParams);
$propertyBag = PropertyBag::cast($paymentParams);
$zeroAmountPayment = $this->processZeroAmountPayment($propertyBag);
if ($zeroAmountPayment) {
......@@ -583,10 +593,11 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$newParams = [];
CRM_Utils_Hook::alterPaymentProcessorParams($this, $propertyBag, $newParams);
$amountFormattedForStripe = $this->getAmountFormattedForStripeAPI($propertyBag);
// @fixme DO NOT SET ANYTHING ON $propertyBag or $params BELOW THIS LINE (we are reading from both)
$params = $this->getPropertyBagAsArray($propertyBag);
$amountFormattedForStripe = self::getAmount($params);
$email = $this->getBillingEmail($params, $propertyBag->getContactID());
// See if we already have a stripe customer
......@@ -687,8 +698,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$paymentIntentID = $propertyBag->getCustomProperty('paymentIntentID');
}
$intent = $this->stripeClient->paymentIntents->retrieve($paymentIntentID);
if ($intent->amount != $this->getAmount($params)) {
$intentParams['amount'] = $this->getAmount($params);
if ($intent->amount != $this->getAmountFormattedForStripeAPI($propertyBag)) {
$intentParams['amount'] = $this->getAmountFormattedForStripeAPI($propertyBag);
}
$intent = $this->stripeClient->paymentIntents->update($intent->id, $intentParams);
}
......@@ -979,10 +990,12 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
}
$propertyBag = PropertyBag::cast($params);
$refundParams = [
'charge' => $params['trxn_id'],
];
$refundParams['amount'] = $this->getAmount($params);
$refundParams['amount'] = $this->getAmountFormattedForStripeAPI($propertyBag);
try {
$refund = $this->stripeClient->refunds->create($refundParams);
// Stripe does not refund fees - see https://support.stripe.com/questions/understanding-fees-for-refunded-payments
......@@ -1203,7 +1216,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
* @return array|null[]
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function doCancelRecurring(\Civi\Payment\PropertyBag $propertyBag) {
public function doCancelRecurring(PropertyBag $propertyBag) {
// By default we always notify the processor and we don't give the user the option
// because supportsCancelRecurringNotifyOptional() = FALSE
if (!$propertyBag->has('isNotifyProcessorOnCancelRecur')) {
......
......@@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/
use Civi\Payment\PropertyBag;
use CRM_Stripe_ExtensionUtil as E;
/**
......@@ -456,7 +457,7 @@ class CRM_Stripe_PaymentIntent {
if (!empty($params['moto'])) {
$intentParams['payment_method_options']['card']['moto'] = TRUE;
}
$intentParams['amount'] = $this->paymentProcessor->getAmount(['amount' => $params['amount'], 'currency' => $params['currency']]);
$intentParams['amount'] = $this->paymentProcessor->getAmountFormattedForStripeAPI(PropertyBag::cast(['amount' => $params['amount'], 'currency' => $params['currency']]));
$intentParams['currency'] = $params['currency'];
// authorize the amount but don't take from card yet
$intentParams['capture_method'] = 'manual';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment