add checkout customer registration

This commit is contained in:
Cyprian Beauvois 2019-09-17 12:09:35 +02:00
parent 3553562f62
commit 44ab2846df
2 changed files with 29 additions and 3 deletions

View File

@ -3,7 +3,8 @@ services:
resource: "../../Service/*" resource: "../../Service/*"
public: true public: true
arguments: arguments:
$container: "@service_container" $container: "@service_container",
$manager: "@doctrine.orm.entity_manager"
$env: '%kernel.environment%' $env: '%kernel.environment%'
Bluesquare\BillingBundle\Controller\: Bluesquare\BillingBundle\Controller\:

View File

@ -7,12 +7,15 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class BillingSrv class BillingSrv
{ {
private $container; private $container;
private $manager;
private $url_prefix; private $url_prefix;
public function __construct(ContainerInterface $container, $env) public function __construct(ContainerInterface $container, EntityManager $manager, $env)
{ {
$this->container = $container; $this->container = $container;
$this->manager = $manager;
$ngrok_prefix = $this->container->getParameter('dev_ngrok_prefix'); $ngrok_prefix = $this->container->getParameter('dev_ngrok_prefix');
$base_url = $this->container->getParameter('prod_prefix'); $base_url = $this->container->getParameter('prod_prefix');
@ -33,11 +36,33 @@ class BillingSrv
$line_items = []; $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); foreach ($items as $item) $line_items[] = $cbFormatter($item);
return ( return (
\Stripe\Checkout\Session::create([ \Stripe\Checkout\Session::create([
'customer' => $customer, 'customer' => $stripeCustomerId,
'payment_method_types' => $this->container->getParameter('payment_method'), 'payment_method_types' => $this->container->getParameter('payment_method'),
'line_items' => $line_items, 'line_items' => $line_items,
'success_url' => $this->url_prefix . $this->container->getParameter('stripe_success_url'), 'success_url' => $this->url_prefix . $this->container->getParameter('stripe_success_url'),