diff --git a/Api/StripeBase.php b/Api/StripeBase.php index 91803f5..2e61f10 100644 --- a/Api/StripeBase.php +++ b/Api/StripeBase.php @@ -42,21 +42,30 @@ public function customer($email) } /** - * Get All Invoices + * Get All Invoices * @return StripeBase */ public function invoices() { $customerIds = array_map(function ($item) { return $item->id; - }, $this->result->data); + }, data_get($this->result, ['data'])); $this->result = []; + foreach ($customerIds as $customerId) { + $invoices = $this->stripe->invoices->all(["customer" => $customerId, "limit" => 5]); + foreach ($invoices->data as $invoice) { - $productId = $invoice->lines->data[0]->plan->product; + $productId = data_get($invoice, ['lines', 'data', 0, 'plan', 'product']); + + if(empty($productId)) { + continue; + } + $product = $this->getProduct($productId); + if (!isset($this->result[$product->name])) { $this->result[$product->name] = []; } @@ -78,14 +87,21 @@ public function subscriptions() { $customerIds = array_map(function ($item) { return $item->id; - }, $this->result->data); + }, data_get($this->result, ['data'])); $this->result = []; foreach ($customerIds as $customerId) { $subscriptions = $this->stripe->subscriptions->all(["customer" => $customerId, "limit" => 20]); + foreach ($subscriptions->data as $subscription) { - $productId = $subscription->items->data[0]->plan->product; + $productId = data_get($subscription, ['items', 'data', 0, 'plan', 'product']); + + if(empty($productId)) { + continue; + } + $product = $this->getProduct($productId); + if (!isset($this->result[$product->name])) { $this->result[$product->name] = []; } diff --git a/Providers/StripeServiceProvider.php b/Providers/StripeServiceProvider.php index bee8d2e..d05e995 100644 --- a/Providers/StripeServiceProvider.php +++ b/Providers/StripeServiceProvider.php @@ -56,11 +56,11 @@ protected function registerMigration() __DIR__ . '/../Database/Migrations/2023_02_07_091128_create_stripe_settings_table.php' => database_path('migrations/'. date('Y_m_d_His', time()).'_create_stripe_settings_table.php'), ], 'stripe-migration'); } - /** - * Register config. - * - * @return void - */ + /** + * Register config. + * + * @return void + */ protected function registerConfig() { $this->publishes([ @@ -95,20 +95,24 @@ public function hooks() */ public function customerProfileExtra($customer) { - $productWithInvoices = $this->getStripeInvoices($customer->getMainEmail()); - $productWithSubscriptions = $this->getStripeSubscriptions($customer->getMainEmail()); + $stripeSecret = $this->getStripeSecretKey($customer->getMainEmail()); - echo View::make('stripe::customer_fields_view', [ - 'productWithInvoices' => $productWithInvoices, - 'productWithSubscriptions' => $productWithSubscriptions - ])->render(); + if(!empty($stripeSecret)) { + $productWithInvoices = $this->getStripeInvoices($customer->getMainEmail(), $stripeSecret); + $productWithSubscriptions = $this->getStripeSubscriptions($customer->getMainEmail(), $stripeSecret); + + echo View::make('stripe::customer_fields_view', [ + 'productWithInvoices' => $productWithInvoices, + 'productWithSubscriptions' => $productWithSubscriptions + ])->render(); + } } public function getStripeSecretKey($email) { $customer = Conversation::where('customer_email', $email)->first(); - $mailboxID = isset($customer->mailbox) ? $customer->mailbox->id : ''; + $stripeSettings = StripeSetting::select('stripe_secret_key')->where('mailbox_id', $mailboxID)->first(); if (isset($stripeSettings)) { @@ -123,18 +127,13 @@ public function getStripeSecretKey($email) * @param mixed $email * @return mixed */ - public function getStripeInvoices($email) + public function getStripeInvoices($email, $stripeSecret) { - $stripeSecret = $this->getStripeSecretKey($email); - - if (! empty($stripeSecret)) { - $stripe = new Stripe($stripeSecret); - $stripeInvoices = $stripe->getInvoices($email); + $stripe = new Stripe($stripeSecret); + $stripeInvoices = $stripe->getInvoices($email); - return $stripeInvoices; - } + return $stripeInvoices; - return false; } /** @@ -142,24 +141,20 @@ public function getStripeInvoices($email) * @param mixed $email * @return mixed */ - public function getStripeSubscriptions($email) + public function getStripeSubscriptions($email, $stripeSecret) { - $stripeSecret = $this->getStripeSecretKey($email); - if (! empty($stripeSecret)) { - $stripe = new Stripe($stripeSecret); - $stripeSubscriptions = $stripe->getSubscriptions($email); + $stripe = new Stripe($stripeSecret); + $stripeSubscriptions = $stripe->getSubscriptions($email); - return $stripeSubscriptions; - } + return $stripeSubscriptions; - return false; } - /** - * Show data in customer Profile Extra section - * @param mixed $customer - * @return void - */ + /** + * Show data in customer Profile Extra section + * @param mixed $customer + * @return void + */ public function mailboxSettingsMenu($mailbox) { echo View::make('stripe::mailbox_settings_menu', [ @@ -198,7 +193,7 @@ public function registerAssets() ], 'public'); } - + /** * Register translations. * @@ -211,7 +206,7 @@ public function registerTranslations() if (is_dir($langPath)) { $this->loadTranslationsFrom($langPath, 'stripe'); } else { - $this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'stripe'); + $this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'stripe'); } } diff --git a/Public/css/stripe.css b/Public/css/stripe.css index 67f0dac..5472bd3 100644 --- a/Public/css/stripe.css +++ b/Public/css/stripe.css @@ -1,131 +1,132 @@ #conv-layout-customer { - overflow: auto; + overflow: auto; } .customer-extra { - margin-top: 10px; + margin-top: 10px; } .customer-data { - padding: 0; + padding: 0; } .stripe-container { - padding: 5px 2px 5px 0; - font-family: "system-ui", "-apple-system", "BlinkMacSystemFont", "Segoe UI", - "Roboto", "Helvetica Neue", "Arial", "Noto Sans", "sans-serif", - "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; + padding: 5px 2px 5px 0; + font-family: "system-ui", "-apple-system", "BlinkMacSystemFont", "Segoe UI", + "Roboto", "Helvetica Neue", "Arial", "Noto Sans", "sans-serif", + "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", + "Noto Color Emoji"; } .stripe-heading .stripe-title h4 { - color: #2a3b47; - text-decoration: none; - font-size: 12px; - font-weight: bold; + color: #2a3b47; + text-decoration: none; + font-size: 14px; + font-weight: bold; } .stripe-container .stripe-product input { - position: absolute; - opacity: 0; - z-index: -1; + position: absolute; + opacity: 0; + z-index: -1; } .stripe-container .stripe-product .tabs { - border-radius: 5px; - overflow: hidden; - box-shadow: 0 2px 2px -1px rgba(238, 238, 234, 0.1); + border-radius: 5px; + overflow: hidden; + box-shadow: 0 2px 2px -1px rgba(238, 238, 234, 0.1); } .stripe-container .stripe-product .tab { - width: 100%; - color: #000; - overflow: hidden; + width: 100%; + color: #000; + overflow: hidden; } .stripe-container .stripe-product .tab-label { - display: flex; - justify-content: space-between; - padding: 1em; - background: rgb(236, 237, 239); - font-weight: bold; - cursor: pointer; - margin: 0px; - text-transform: capitalize; - font-size: 12px; + display: flex; + justify-content: space-between; + padding: 1em; + background: rgb(236, 237, 239); + font-weight: bold; + cursor: pointer; + margin: 0px; + text-transform: capitalize; + font-size: 12px; } .stripe-container .stripe-product .tab-label:hover { - background: #d7d6d6; + background: #d7d6d6; } .stripe-container .stripe-product .tab-label::after { - content: "\276F"; - width: 1em; - height: 1em; - text-align: center; - transition: all 0.35s; + content: "\276F"; + width: 1em; + height: 1em; + text-align: center; + transition: all 0.35s; } .stripe-container .stripe-product .tab-content { - max-height: 0; - padding: 0 1em; - color: #000; - background: #f8f9f9; - transition: all 0.35s; + max-height: 0; + padding: 0 1em; + color: #000; + background: #f8f9f9; + transition: all 0.35s; } .stripe-container .stripe-product .tab-close { - display: flex; - justify-content: flex-end; - padding: 1em; - font-size: 0.75em; - background: #f5f5f5; - cursor: pointer; + display: flex; + justify-content: flex-end; + padding: 1em; + font-size: 0.75em; + background: #f5f5f5; + cursor: pointer; } .stripe-container .stripe-product .tab-close:hover { - background: #cdcccc; + background: #cdcccc; } .stripe-container .stripe-product input:checked + .tab-label { - background: #f1f1f1; + background: #f1f1f1; } .stripe-container .stripe-product input:checked + .tab-label::after { - transform: rotate(90deg); + transform: rotate(90deg); } .stripe-container .stripe-product input:checked ~ .tab-content { - max-height: 100vh; - padding: 1em; + max-height: 100vh; + padding: 1em; } .stripe-container .stripe-product .tab-content .subscription-title, .stripe-container .stripe-product .tab-content .invoice-title { - font-size: 12px; - font-weight: bold; + font-size: 12px; + font-weight: bold; } .stripe-container .stripe-product .tab-content .subscriptions, .stripe-container .stripe-product .tab-content .invoices { - margin-top: 6px; + margin-top: 6px; } .stripe-container .stripe-product .first-invoice, .stripe-container .stripe-product .first-subscription { - border-top: 1px solid rgb(205, 201, 201); + border-top: 1px solid rgb(205, 201, 201); } .stripe-container .stripe-product .tab-content .subscriptions .subscription, .stripe-container .stripe-product .tab-content .invoices .invoice { - padding: 5px 0px; - font-size: 12px; - display: block; - border-bottom: 1px solid rgb(205, 201, 201); + padding: 5px 0px; + font-size: 12px; + display: block; + border-bottom: 1px solid rgb(205, 201, 201); } .stripe-container .stripe-product .status { - display: inline-block; - padding: 1px 7px; - background-color: #bced94; - border-radius: 5px; - color: #000; - font-weight: 500; - font-size: 14px; - text-transform: capitalize; + display: inline-block; + padding: 1px 7px; + background-color: #bced94; + border-radius: 5px; + color: #000; + font-weight: 500; + font-size: 14px; + text-transform: capitalize; } .stripe-container .stripe-product .tab-content .price { - display: inline-block; - font-weight: bold; + display: inline-block; + font-weight: bold; } .stripe-container .stripe-product .tab-content .date-time { - display: inline-block; - font-size: 11px; + display: inline-block; + font-size: 11px; } diff --git a/Resources/views/customer_fields_view.blade.php b/Resources/views/customer_fields_view.blade.php index 3cbe7e0..a13e56a 100644 --- a/Resources/views/customer_fields_view.blade.php +++ b/Resources/views/customer_fields_view.blade.php @@ -1,5 +1,4 @@ -
{{__('Subscriptions')}}
- @if(isset($productWithSubscriptions[$productName])) - @foreach($productWithSubscriptions[$productName] as $key => $subscription) -
{{__('No Subscription found')}}
- @endif -{{__('Subscriptions')}}
+ @if(isset($productWithSubscriptions[$productName])) + @foreach($productWithSubscriptions[$productName] as $key => $subscription) +
{{__('Payments')}}
- @forelse($invoices as $key => $invoice) - @if($loop->iteration <= 5) -
{{__('No Subscription found')}}
@endif - @empty -{{ __('No Invoice found') }}
- @endforelse +{{__('Payments')}}
+ @forelse($invoices as $key => $invoice) + @if($loop->iteration <= 5) +
{{ __('No Invoice found') }}
+ @endforelse +{{ __('No Stripe data found') }}
+ @endforelse{{ __('No Stripe data found') }}
- @endforelse -{{ __('No Stripe data found') }}
@endif