Skip to content

Commit

Permalink
Merge pull request #296 from a-solaris/pr/fix-rounding-error
Browse files Browse the repository at this point in the history
fix: Fix a potential rounding error
  • Loading branch information
nfourtythree committed Mar 28, 2024
2 parents df0770e + 6347b3d commit c19b378
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Stripe for Craft Commerce

## Unreleased

- Fixed a bug where floating point rounding precision could cause payments/refunds to fail. ([#296](https://github.com/craftcms/commerce-stripe/pull/296))

## 4.1.2 - 2024-03-25

- Fixed a bug where redirects could break when adding a new payment source. ([#259](https://github.com/craftcms/commerce-stripe/issues/259), [#289](https://github.com/craftcms/commerce-stripe/issues/289))
Expand Down
6 changes: 3 additions & 3 deletions src/gateways/PaymentIntents.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function refund(Transaction $transaction): RequestResponseInterface
try {
$request = [
'charge' => $transaction->reference,
'amount' => $transaction->paymentAmount * (10 ** $currency->minorUnit),
'amount' => (int) bcmul($transaction->paymentAmount, 10 ** $currency->minorUnit),
];
$refund = $this->getStripeClient()->refunds->create($request);

Expand All @@ -303,7 +303,7 @@ public function refund(Transaction $transaction): RequestResponseInterface
if ($stripePaymentIntent->status == 'succeeded') {
$refund = $this->getStripeClient()->refunds->create([
'payment_intent' => $stripePaymentIntent->id,
'amount' => $transaction->paymentAmount * (10 ** $currency->minorUnit),
'amount' => (int) bcmul($transaction->paymentAmount, 10 ** $currency->minorUnit),
]);

return $this->createPaymentResponseFromApiResource($refund);
Expand Down Expand Up @@ -638,7 +638,7 @@ protected function authorizeOrPurchase(Transaction $transaction, PaymentIntentFo
}

// Normalized amount for Stripe into minor units
$amount = $transaction->paymentAmount * (10 ** $currency->minorUnit);
$amount = (int) bcmul($transaction->paymentAmount, 10 ** $currency->minorUnit);

/** @var PaymentIntentForm $form */
if ($form->paymentFormType == self::PAYMENT_FORM_TYPE_CHECKOUT) {
Expand Down

0 comments on commit c19b378

Please sign in to comment.