Compare commits

...

17 Commits
1.0 ... master

3 changed files with 60 additions and 10 deletions

View File

@ -37,7 +37,7 @@ class BillingController extends AbstractController
list($service, $function) = explode('::', $serviceToCall); list($service, $function) = explode('::', $serviceToCall);
$srv = new $service(); $srv = $this->container->get($service);
$srv->{$function}($session); $srv->{$function}($session);

View File

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

View File

@ -2,37 +2,87 @@
namespace Bluesquare\BillingBundle\Service; namespace Bluesquare\BillingBundle\Service;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
class BillingSrv class BillingSrv
{ {
private $container; private $container;
private $manager;
private $url_prefix;
public function __construct(ContainerInterface $container) 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');
$base_url = $this->container->getParameter('prod_prefix');
$this->url_prefix = ($env == "dev" && !empty($ngrok_prefix)) ? $ngrok_prefix : $base_url;
} }
public function createPurchase($user_email, $items, $cbFormatter) private function setApiKey()
{ {
$apiKey = $this->container->getParameter('stripe_api_key_secret'); $apiKey = $this->container->getParameter('stripe_api_key_secret');
\Stripe\Stripe::setApiKey($apiKey); \Stripe\Stripe::setApiKey($apiKey);
}
public function createPurchase($customer, $items, $cbFormatter)
{
$this->setApiKey();
$line_items = []; $line_items = [];
foreach ($items as $item) $line_items[] = $cbFormatter($item); if (empty($customer->getStripeCustomerId()))
{
$stripeCustomer = \Stripe\Customer::create(array(
"description" => "Customer for ".$customer->getUsername(),
"email" => $customer->getUsername(),
"metadata" => [
"Prénom" => $customer->getFirstname(),
"Nom" => $customer->getLastname(),
"Entreprise" => $customer->getCompany()
]
));
$ngrok_url = $this->container->getParameter('dev_ngrok_prefix'); $customerId = $stripeCustomer["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 ( return (
\Stripe\Checkout\Session::create([ \Stripe\Checkout\Session::create([
'customer_email' => $user_email, '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' => $ngrok_url . $this->container->getParameter('stripe_success_url'), 'success_url' => $this->url_prefix . $this->container->getParameter('stripe_success_url'),
'cancel_url' => $ngrok_url . $this->container->getParameter('stripe_cancel_url'), 'cancel_url' => $this->url_prefix . $this->container->getParameter('stripe_cancel_url'),
]) ])
); );
} }
public function confirmPurchase($paimentIntent)
{
$infoIntent = $this->retrievePurchase($paimentIntent);
return ($infoIntent['amount'] == $infoIntent['amount_received'] && $infoIntent['amount_capturable'] == 0);
}
public function retrievePurchase($paimentIntent)
{
$this->setApiKey();
return \Stripe\PaymentIntent::retrieve($paimentIntent);
}
} }