Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions includes/gateways/class-stripe-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down Expand Up @@ -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',
)
Expand Down Expand Up @@ -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'] ),
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
)
Expand Down
37 changes: 37 additions & 0 deletions includes/gateways/stripe/helpers/classStrpAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading