/**
  * @Route("/petitions/buy-emails/{petition}")
  * @ParamConverter(
  *      "petition",
  *      class="CivixCoreBundle:Poll\Question\Petition",
  *      options={"repository_method" = "getPublishPetitonById"}
  * )
  * @Template("CivixFrontBundle:Payment:buy-petition-emails.html.twig")
  */
 public function buyEmailsAction(Request $request, Petition $petition)
 {
     if ($petition->getUser() !== $this->getUser()) {
         throw new AccessDeniedHttpException();
     }
     /* @var Customer $customer */
     $customer = $this->getDoctrine()->getRepository(Customer::getEntityClassByUser($this->getUser()))->findOneBy(['user' => $this->getUser()]);
     //get count of public emails
     $emailCount = $this->getDoctrine()->getRepository('CivixCoreBundle:Poll\\Question\\Petition')->getPetitionEmailsCount($petition);
     $amount = $emailCount * $this->get('civix_core.subscription_manager')->getPackage($this->getUser())->getPetitionDataEmailPrice();
     $form = $this->createForm('form', null, ['label' => 'Buy ' . $emailCount . ' email(s) (' . $amount / 100 . '$)']);
     if ('POST' === $request->getMethod()) {
         if (intval($request->get('amount')) !== $amount) {
             $this->get('session')->getFlashBag()->add('notice', 'Emails amount has changed. Please review.');
         } else {
             if ($amount < 50) {
                 $this->get('session')->getFlashBag()->add('notice', 'Amount must be at least 50 cents.');
             } else {
                 if ($form->submit($request)->isValid()) {
                     $entityManager = $this->getDoctrine()->getManager();
                     $charge = $this->get('civix_core.stripe')->chargeCustomer($customer, $amount, 'PowerlinePay', 'Powerline Payment:  Petition Data');
                     if (!$charge->isSucceeded()) {
                         $this->get('session')->getFlashBag()->add('error', $charge->getStatus());
                     } else {
                         $emails = $entityManager->getRepository('CivixCoreBundle:Poll\\Question\\Petition')->getPetitionEmails($petition);
                         //save transaction
                         $transaction = new Transaction();
                         $transaction->setReferencePayment(uniqid('emails_'));
                         $transaction->setStripeCustomer($customer);
                         $transaction->setData(serialize($emails));
                         $entityManager->persist($transaction);
                         $entityManager->flush($transaction);
                         return $this->redirect($this->generateUrl("civix_front_{$this->getUser()->getType()}_payment_success", ['reference' => $transaction->getReferencePayment()]));
                     }
                 }
             }
         }
     }
     return array('petition' => $petition->getId(), 'form' => $form->createView(), 'amount' => $amount, 'card' => $customer && count($customer->getCards()) ? $customer->getCards()[0] : null);
 }
 public function getCreatedAt()
 {
     $this->__load();
     return parent::getCreatedAt();
 }