symfony-billing/BillingBundle/Service/BillingSrv.php

89 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Bluesquare\BillingBundle\Service;
use Doctrine\ORM\EntityManager;
2019-07-16 14:44:22 +02:00
use Symfony\Component\DependencyInjection\ContainerInterface;
class BillingSrv
{
2019-07-16 14:44:22 +02:00
private $container;
2019-09-17 12:09:35 +02:00
private $manager;
2019-07-22 11:28:50 +02:00
private $url_prefix;
2019-07-16 14:44:22 +02:00
2019-09-17 12:09:35 +02:00
public function __construct(ContainerInterface $container, EntityManager $manager, $env)
{
2019-07-16 14:44:22 +02:00
$this->container = $container;
2019-07-22 11:28:50 +02:00
2019-09-17 12:09:35 +02:00
$this->manager = $manager;
2019-07-26 16:41:04 +02:00
$ngrok_prefix = $this->container->getParameter('dev_ngrok_prefix');
2019-07-26 16:55:49 +02:00
$base_url = $this->container->getParameter('prod_prefix');
$this->url_prefix = ($env == "dev" && !empty($ngrok_prefix)) ? $ngrok_prefix : $base_url;
2019-07-16 14:44:22 +02:00
}
2019-07-16 14:58:49 +02:00
private function setApiKey()
2019-07-16 14:44:22 +02:00
{
$apiKey = $this->container->getParameter('stripe_api_key_secret');
\Stripe\Stripe::setApiKey($apiKey);
}
public function createPurchase($customer, $items, $cbFormatter)
{
$this->setApiKey();
$line_items = [];
2019-09-17 12:09:35 +02:00
if (empty($customer->getStripeCustomerId()))
{
2019-09-18 16:24:38 +02:00
$stripeCustomer = \Stripe\Customer::create(array(
2019-09-17 12:09:35 +02:00
"description" => "Customer for ".$customer->getUsername(),
"email" => $customer->getUsername(),
"metadata" => [
"Prénom" => $customer->getFirstname(),
"Nom" => $customer->getLastname(),
"Entreprise" => $customer->getCompany()
]
));
2019-09-18 16:24:38 +02:00
$customerId = $stripeCustomer["id"];
2019-09-17 12:09:35 +02:00
$customer->setStripeCustomerId($customerId);
$this->manager->persist($customer);
$this->manager->flush();
$stripeCustomerId = $customerId;
}
else
$stripeCustomerId = $customer->getStripeCustomerId();
2019-07-16 18:23:05 +02:00
foreach ($items as $item) $line_items[] = $cbFormatter($item);
2019-07-16 14:44:22 +02:00
return (
\Stripe\Checkout\Session::create([
2019-09-17 12:09:35 +02:00
'customer' => $stripeCustomerId,
'payment_method_types' => $this->container->getParameter('payment_method'),
'line_items' => $line_items,
2019-07-22 11:28:50 +02:00
'success_url' => $this->url_prefix . $this->container->getParameter('stripe_success_url'),
'cancel_url' => $this->url_prefix . $this->container->getParameter('stripe_cancel_url'),
2019-07-16 14:44:22 +02:00
])
);
}
2019-07-22 12:20:32 +02:00
public function confirmPurchase($paimentIntent)
2019-07-19 18:14:21 +02:00
{
$infoIntent = $this->retrievePurchase($paimentIntent);
return ($infoIntent['amount'] == $infoIntent['amount_received'] && $infoIntent['amount_capturable'] == 0);
}
public function retrievePurchase($paimentIntent)
{
$this->setApiKey();
2019-07-19 18:00:44 +02:00
return \Stripe\PaymentIntent::retrieve($paimentIntent);
}
}