Skip to content

Commit

Permalink
Refactor Stripe Base File and Service Provider Code
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Sep 12, 2023
1 parent 5ac4bb2 commit 5fadf0b
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 159 deletions.
26 changes: 21 additions & 5 deletions Api/StripeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand All @@ -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] = [];
}
Expand Down
67 changes: 31 additions & 36 deletions Providers/StripeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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)) {
Expand All @@ -123,43 +127,34 @@ 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;
}

/**
* Get Stripe Invoice
* @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', [
Expand Down Expand Up @@ -198,7 +193,7 @@ public function registerAssets()
], 'public');
}


/**
* Register translations.
*
Expand All @@ -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');
}
}

Expand Down
143 changes: 72 additions & 71 deletions Public/css/stripe.css
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 5fadf0b

Please sign in to comment.