diff --git a/includes/gateways/class-stripe-gateway.php b/includes/gateways/class-stripe-gateway.php index 07ad99d29..68dd965ef 100644 --- a/includes/gateways/class-stripe-gateway.php +++ b/includes/gateways/class-stripe-gateway.php @@ -435,7 +435,7 @@ private function get_session_parameters( $payment ) { array( 'name' => esc_attr( $checkout_title ? $checkout_title : get_bloginfo( 'name' ) ), 'description' => $payment->summary, - 'amount' => $this->formated_amount( $payment->amount ), + 'amount' => $this->formated_amount( $payment->amount, $payment->currency_code ), 'currency' => strtolower( $payment->currency_code ), 'quantity' => 1, ), @@ -471,7 +471,7 @@ private function get_discounts( $payment ) { $coupon = WPBDPStrpConnectHelper::create_coupon( array( - 'amount_off' => abs( (int) $this->formated_amount( $discount['amount'] ) ), + 'amount_off' => abs( (int) $this->formated_amount( $discount['amount'], $payment->currency_code ) ), 'currency' => $payment->currency_code, 'duration' => 'once', ) @@ -524,7 +524,7 @@ private function get_stripe_plan( $payment ) { */ private function get_recurring_plan_fingerprint( $recurring, $payment ) { $params = array( - 'amount' => $this->formated_amount( $recurring['amount'] ), + 'amount' => $this->formated_amount( $recurring['amount'], $payment->currency_code ), 'currency' => strtolower( $payment->currency_code ), 'interval' => 'day', 'interval_count' => intval( $recurring['fee_days'] ), @@ -565,7 +565,7 @@ private function get_stripe_plan_fingerprint( $plan ) { */ private function create_stripe_plan( $id, $recurring, $payment ) { $plan = array( - 'amount' => $this->formated_amount( $recurring['amount'] ), + 'amount' => $this->formated_amount( $recurring['amount'], $payment->currency_code ), 'currency' => strtolower( $payment->currency_code ), 'interval' => 'day', 'interval_count' => $recurring['fee_days'], @@ -611,11 +611,21 @@ private function create_stripe_session( $payment ) { /** * @since 6.4.9 * - * @param float $amount Amount to be formatted. + * @param float $amount Amount to be formatted. + * @param string $currency Currency code. * * @return float */ - private function formated_amount( $amount ) { + private function formated_amount( $amount, $currency ) { + if ( WPBDPStrpAppHelper::is_zero_decimal_currency( $currency ) ) { + return round( $amount, 0 ); + } + + if ( in_array( strtoupper( (string) $currency ), array( 'ISK', 'UGX' ), true ) ) { + // Stripe takes these as two-decimal values, but without fractions. + return round( $amount, 0 ) * 100; + } + return round( $amount * 100, 0 ); } diff --git a/includes/gateways/stripe/controllers/classStrpEventsController.php b/includes/gateways/stripe/controllers/classStrpEventsController.php index 1656f21d7..903c33b68 100644 --- a/includes/gateways/stripe/controllers/classStrpEventsController.php +++ b/includes/gateways/stripe/controllers/classStrpEventsController.php @@ -457,7 +457,7 @@ private function process_payment_succeeded( $subscription, $parent_payment ) { // An installment. $subscription->record_payment( array( - 'amount' => $invoice->total / 100.0, + 'amount' => WPBDPStrpAppHelper::amount_from_stripe( $invoice->total, $invoice->currency ?? $parent_payment->currency_code ), 'gateway_tx_id' => $invoice->charge, 'created_at' => gmdate( 'Y-m-d H:i:s', $invoice->created ), ) diff --git a/includes/gateways/stripe/helpers/classStrpAppHelper.php b/includes/gateways/stripe/helpers/classStrpAppHelper.php index eb91e82b8..0e948cd1c 100644 --- a/includes/gateways/stripe/helpers/classStrpAppHelper.php +++ b/includes/gateways/stripe/helpers/classStrpAppHelper.php @@ -72,4 +72,41 @@ public static function customer_meta_name() { $test_mode = wpbdp_get_option( 'payments-test-mode' ); return '_wpbdp_stripe_customer_id' . ( $test_mode ? '_test' : '' ); } + + /** + * Stripe expects amounts in these currencies in whole units. + * + * @since x.x + * @see https://docs.stripe.com/currencies#zero-decimal + * + * @param string $currency Currency code. + * + * @return bool + */ + public static function is_zero_decimal_currency( $currency ) { + $zero_decimal = array( + 'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', + 'PYG', 'RWF', 'VND', 'VUV', 'XAF', 'XOF', 'XPF', + ); + + return in_array( strtoupper( (string) $currency ), $zero_decimal, true ); + } + + /** + * Convert an amount in Stripe's smallest currency unit back to a regular amount. + * + * @since x.x + * + * @param int $amount Amount from Stripe. + * @param string $currency Currency code. + * + * @return float + */ + public static function amount_from_stripe( $amount, $currency ) { + if ( self::is_zero_decimal_currency( $currency ) ) { + return (float) $amount; + } + + return $amount / 100.0; + } }