diff --git a/BillingBundle/Resources/config/services.yaml b/BillingBundle/Resources/config/services.yaml index 44bf68f..de486e1 100644 --- a/BillingBundle/Resources/config/services.yaml +++ b/BillingBundle/Resources/config/services.yaml @@ -3,7 +3,8 @@ services: resource: "../../Service/*" public: true arguments: - $container: "@service_container" + $container: "@service_container", + $manager: "@doctrine.orm.entity_manager" $env: '%kernel.environment%' Bluesquare\BillingBundle\Controller\: diff --git a/BillingBundle/Service/BillingSrv.php b/BillingBundle/Service/BillingSrv.php index c44dfa1..9312158 100644 --- a/BillingBundle/Service/BillingSrv.php +++ b/BillingBundle/Service/BillingSrv.php @@ -7,12 +7,15 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class BillingSrv { private $container; + private $manager; private $url_prefix; - public function __construct(ContainerInterface $container, $env) + public function __construct(ContainerInterface $container, EntityManager $manager, $env) { $this->container = $container; + $this->manager = $manager; + $ngrok_prefix = $this->container->getParameter('dev_ngrok_prefix'); $base_url = $this->container->getParameter('prod_prefix'); @@ -33,11 +36,33 @@ class BillingSrv $line_items = []; + if (empty($customer->getStripeCustomerId())) + { + $customer = \Stripe\Customer::create(array( + "description" => "Customer for ".$customer->getUsername(), + "email" => $customer->getUsername(), + "metadata" => [ + "Prénom" => $customer->getFirstname(), + "Nom" => $customer->getLastname(), + "Entreprise" => $customer->getCompany() + ] + )); + + $customerId = $customer["id"]; + $customer->setStripeCustomerId($customerId); + $this->manager->persist($customer); + $this->manager->flush(); + + $stripeCustomerId = $customerId; + } + else + $stripeCustomerId = $customer->getStripeCustomerId(); + foreach ($items as $item) $line_items[] = $cbFormatter($item); return ( \Stripe\Checkout\Session::create([ - 'customer' => $customer, + 'customer' => $stripeCustomerId, 'payment_method_types' => $this->container->getParameter('payment_method'), 'line_items' => $line_items, 'success_url' => $this->url_prefix . $this->container->getParameter('stripe_success_url'),