From 008e3e5207960ec8004ca6be3984c9c272eeb383 Mon Sep 17 00:00:00 2001 From: rabol Date: Wed, 18 Aug 2021 10:54:12 +0200 Subject: [PATCH] check if the user have the taxid --- src/LaravelSimplesubscriptionStripe.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/LaravelSimplesubscriptionStripe.php b/src/LaravelSimplesubscriptionStripe.php index 21a2cba..3e57a6d 100644 --- a/src/LaravelSimplesubscriptionStripe.php +++ b/src/LaravelSimplesubscriptionStripe.php @@ -78,22 +78,30 @@ public static function updateTaxIdOnCustomer(User $user, string $value, string $ $taxIds = self::stripe()->customers->allTaxIds($user->stripe_id); $addNewTaxId = true; + // check if the user have the TaxId if ($taxIds && $taxIds->count() != 0) { foreach ($taxIds as $taxId) { - if ($taxId->type == $type && $taxId->value == $value) { + if ($taxId->type == $type && ($taxId->value == $value || $taxId->value == str_replace('-', '', $value))) { $addNewTaxId = false; } } } + // Does not seem like the TaxId is attached to the user, so let's attach it if ($addNewTaxId) { - $taxId = self::stripe()->customers->createTaxId($user->stripe_id, [ - 'type' => $type, - 'value' => $value, - ]); - - $user->stripe_tax_id = $taxId->id; - $user->save(); + try { + $taxId = self::stripe()->customers->createTaxId($user->stripe_id, [ + 'type' => $type, + 'value' => $value, + ]); + + if($taxId) { + $user->stripe_tax_id = $taxId->id; + $user->save(); + } + } catch (\Exception $e) { + Log::error($e->getMessage()); + } } } }