· 9 years ago · Oct 11, 2016, 03:32 PM
1<?php
2
3namespace Lgn\CoreBundle\Controller;
4
5use Lgn\CoreBundle\Entity\PriceGroup;
6use Lgn\CoreBundle\Entity\SubscriptionPlan;
7use Lgn\CoreBundle\Entity\User;
8use Lgn\CoreBundle\Entity\UserBilling;
9use Lgn\CoreBundle\Helper\InstagramMobile;
10use Lgn\CoreBundle\Helper\Curl;
11use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14use Symfony\Component\HttpFoundation\Request;
15use Symfony\Component\HttpFoundation\JsonResponse;
16use Symfony\Component\HttpFoundation\Response;
17use Lgn\CoreBundle\Helper\Instagram;
18use Abraham\TwitterOAuth\TwitterOAuth;
19use JMS\DiExtraBundle\Annotation as DI;
20
21/**
22 * @Route("/inftool")
23 */
24class InfluencerBillingController extends Controller
25{
26
27 /**
28 * @Route("/billing", name="_inf_billing")
29 * @Template("LgnCoreBundle:Influencer:billing/billing.html.twig")
30 */
31 public function billingAction(Request $request)
32 {
33 $stripe = [
34 'secret_key' => $this->container->getParameter('stripe_secret_key'),
35 'publishable_key' => $this->container->getParameter('stripe_publishable_key')
36 ];
37
38 $user = $this->getUser();
39 $blog = $user->getBrandBlog();
40
41 $subscription_plan = $this->getSubscriptionSet($blog);
42 $subscription_plan['range'] = json_decode($subscription_plan['range'], true);
43
44 $followers = $blog->getTotalFollowersCount();
45
46 $current_plan = array_values(array_filter($subscription_plan['range'], function($ar) use ($followers) {
47 return $ar['from'] <= $followers && (empty($ar['to']) || $ar['to'] > $followers);
48 }))[0];
49
50 $invoices = [];
51
52 try {
53 \Stripe\Stripe::setApiKey($stripe['secret_key']);
54 if ($user->getPaymentCustomerId()) {
55 $invoices = \Stripe\Invoice::all(['customer' => $user->getPaymentCustomerId(), "limit" => 12]);
56 }
57 } catch (Exception $e) {
58 $this->addFlash(
59 'stripe_error',
60 $e->getMessage()
61 );
62 }
63
64 $invoiceList = [];
65 if ($invoices) {
66 foreach ($invoices->data as $invoice) {
67
68 $invoiceData = end($invoice->lines->data);
69
70 $invoiceList[] = [
71 'id' => $invoice['id'],
72 'amount' => $invoice['total']/100,
73 'date' => $invoice['date'],
74 'paid' => $invoice['paid'],
75 'period_end' => $invoice['period_end'],
76 'period_start' => $invoice['period_start'],
77 'plan_name' => User::selectPlanName($invoiceData->plan->id),
78 'plan_interval' => $invoiceData->plan->interval,
79 'plan_currency' => strtoupper($invoiceData->plan->currency)
80 ];
81 }
82 }
83
84 ksort($invoiceList);
85
86 return [
87 'stripe' => $stripe,
88 'user' => $user,
89 'current_plan' => $current_plan,
90 'subscription_plan' => $subscription_plan,
91 'invoices' => $invoiceList
92 ];
93 }