From 51bc7fea2b6beb5f53d87bc095a22d87e0f9f377 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Thu, 8 Jun 2023 11:24:56 +0100 Subject: [PATCH] Add function to handle stripe currency format while sending request --- app/Helpers/utils.php | 23 +++++++++++++++++++++++ app/Http/Controllers/StripeController.php | 8 ++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/Helpers/utils.php b/app/Helpers/utils.php index 7bbfa19..c9f55bf 100644 --- a/app/Helpers/utils.php +++ b/app/Helpers/utils.php @@ -12,3 +12,26 @@ if(!function_exists('randomString')){ return $randomString; } } + + +if(!function_exists('stripeFormatCurrency')){ + function stripeFormatCurrency($amount, $currency = "USD"){ + // https://stripe.com/docs/currencies#zero-decimal + if(in_array($currency,['BIF','CLP','DJF','GNF','JPY','KMF','KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'])) { + return number_format(ceil($amount) , 0, '', ''); + } + + if(in_array($currency,['BHD','JOD','KWD','OMR','TND'])) { + $number = $amount*1000; + $remainder = $number % 10; + if ($remainder != 0) { + $number += 10 - $remainder; + } + return number_format($number , 0, '', ''); + } + + return number_format(($amount*100) , 0, '', ''); + + } + +} diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php index 6738b94..bcf638b 100644 --- a/app/Http/Controllers/StripeController.php +++ b/app/Http/Controllers/StripeController.php @@ -143,7 +143,7 @@ class StripeController extends Controller // Init payment $charge = $this->client->charges->create([ - "amount" => round($transaction->amount,2), + "amount" => stripeFormatCurrency(round($transaction->amount,2), $transaction->currency), "currency" => $transaction->currency, "description" => $transaction->reason, // "customer" => 15, @@ -192,7 +192,7 @@ class StripeController extends Controller return $code; } - + public function refund(Request $request) { $this->validate($request, [ @@ -321,7 +321,7 @@ class StripeController extends Controller // Create payment intent // https://stripe.com/docs/api/payment_intents/create $paymentIntent = $this->client->paymentIntents->create([ - "amount" => $amount, + "amount" => stripeFormatCurrency($amount, $request->input('currency')), "currency" => $request->input('currency'), "description" => $request->input('reason'), 'payment_method_types' => ['card'], @@ -462,7 +462,7 @@ class StripeController extends Controller // Create payout in Stripe // https://stripe.com/docs/api/payouts/create $payout = $this->client->payouts->create([ - "amount" => $amount, + "amount" => stripeFormatCurrency($amount, $currency), "currency" => $currency, 'description' => $request->input('reason'), "destination" => $destination->id,