/** * @Route("/{id}") * @Method("DELETE") */ public function removeCard($id) { $customer = $this->getDoctrine()->getRepository(Customer::getEntityClassByUser($this->getUser()))->findOneBy(['user' => $this->getUser()]); if ($customer) { $this->get('civix_core.stripe')->removeCard($customer, $id); } return $this->createJSONResponse(null, 204); }
/** * @Route("/") * @Template("CivixFrontBundle:PaymentSettings:index.html.twig") * @Method("GET") */ public function indexAction() { $cards = null; $bankAccounts = null; /* @var Customer $customer */ $customer = $this->getDoctrine()->getRepository(Customer::getEntityClassByUser($this->getUser()))->findOneBy(['user' => $this->getUser()]); if ($customer) { $cards = $customer->getCards(); } /* @var Account $account */ $account = $this->getDoctrine()->getRepository(Account::getEntityClassByUser($this->getUser()))->findOneBy(['user' => $this->getUser()]); if ($account) { $bankAccounts = $account->getBankAccounts(); } return ['bankAccounts' => $bankAccounts, 'cards' => $cards, 'customer' => $customer, 'return_path' => $this->generateUrl('civix_front_' . $this->getUser()->getType() . '_paymentsettings_index')]; }
protected function execute(InputInterface $input, OutputInterface $output) { /** @var PaymentRequest $paymentRequest */ $paymentRequest = $this->getContainer()->get('doctrine')->getRepository(PaymentRequest::class)->find($input->getArgument('id')); if (!$paymentRequest || !$paymentRequest->getIsCrowdfunding()) { return $output->writeln('<error>Cannot find payment request.</error>'); } if (!$paymentRequest->isCrowdfundingDeadline()) { return $output->writeln('<error>Deadline is not reached.</error>'); } /** @var Stripe $stripe */ $stripe = $this->getContainer()->get('civix_core.stripe'); /** @var \Doctrine\ORM\EntityRepository $chargeRepository */ $chargeRepository = $this->getContainer()->get('doctrine')->getRepository(Charge::class); /** @var \Doctrine\ORM\EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); if ($paymentRequest->getCrowdfundingGoalAmount() > $paymentRequest->getCrowdfundingPledgedAmount()) { $paymentRequest->setIsCrowdfundingCompleted(true); $em->flush($paymentRequest); return; } /** @var Answer $answer */ foreach ($paymentRequest->getAnswers() as $answer) { $customer = $this->getContainer()->get('doctrine')->getRepository(Customer::getEntityClassByUser($answer->getUser()))->findOneBy(['user' => $answer->getUser()]); if (!$customer) { continue; } $charge = $chargeRepository->findOneBy(['questionId' => $answer->getQuestion()->getId(), 'fromCustomer' => $customer]); if ($answer->getOption()->getPaymentAmount() && !$charge) { try { $stripe->chargeToPaymentRequest($paymentRequest, $answer, $answer->getUser()); $output->writeln("<comment>User {$answer->getUser()->getId()} has charged</comment>"); } catch (\Exception $e) { $output->writeln("<error>{$e->getMessage()}</error>"); } } else { $output->writeln("<comment> Already paid: {$answer->getUser()->getId()} </comment>"); } } $paymentRequest->setIsCrowdfundingCompleted(true); $em->flush($paymentRequest); $output->writeln('Charged.'); }
/** * @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); }
private function createCustomer(UserInterface $user) { $entityClass = Customer::getEntityClassByUser($user); /* @var $customer CustomerInterface */ $customer = new $entityClass(); $customer->setUser($user); $response = \Stripe\Customer::create(['description' => $user->getOfficialName(), 'email' => $user->getEmail()]); $customer->setStripeId($response->id); $this->em->persist($customer); $this->em->flush($customer); return $customer; }
/** * @Route("/answers/{id}/charges/") * @Method("GET") */ public function charges(Answer $answer) { if ($answer->getUser() !== $this->getUser()) { throw $this->createNotFoundException(); } $customer = $this->getDoctrine()->getRepository(Customer::getEntityClassByUser($this->getUser()))->findOneBy(['user' => $this->getUser()]); if (!$customer) { throw $this->createNotFoundException(); } $charge = $this->getDoctrine()->getRepository(Charge::class)->findOneBy(['questionId' => $answer->getQuestion()->getId(), 'fromCustomer' => $customer]); return $this->createJSONResponse($this->jmsSerialization($charge->toArray(), ['api-answer-private'])); }