Created
December 7, 2023 13:58
-
-
Save charlesteh/98d285285cefc6cd3aa2e31f8910b981 to your computer and use it in GitHub Desktop.
Stripe amount to standard Currency + Amount and vice-versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This function converts a normal currency amount to a Stripe-compatible amount. | |
// To be used on creating Stripe Checkout Stations | |
// e.g. USD 10.00 to 1000 | |
if (! function_exists('convert_normal_to_stripe_amount')) { | |
function convert_normal_to_stripe_amount($currency, $amount) | |
{ | |
$stripe_amount = $amount; | |
$currency = strtolower(trim($currency)); | |
if (in_array($currency, ['bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw'])) { | |
$stripe_amount = number_format(ceil($stripe_amount), 0, '', ''); | |
} else { | |
$stripe_amount = number_format(($stripe_amount * 100), 0, '', ''); | |
} | |
return $stripe_amount; | |
} | |
} | |
// This function converts Stripe amounts back to normal currency amounts. | |
// To be used on parsing Stripe Webhooks | |
// e.g. 1000 to 10.00 | |
function convert_stripe_to_normal_amount($currency, $stripe_amount) | |
{ | |
$amount = $stripe_amount; | |
$currency = strtolower(trim($currency)); | |
if (in_array($currency, ['bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw'])) { | |
$amount = number_format((float) $amount, 0, '.', ''); | |
} else { | |
$amount = number_format((float) $amount / 100, 2, '.', ''); | |
} | |
return $amount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment