2019-07-16 11:58:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Bluesquare\BillingBundle\Service;
|
|
|
|
|
2019-07-16 14:44:22 +02:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
2019-07-16 11:58:38 +02:00
|
|
|
class BillingSrv
|
|
|
|
{
|
2019-07-16 14:44:22 +02:00
|
|
|
private $container;
|
2019-07-22 11:28:50 +02:00
|
|
|
private $url_prefix;
|
2019-07-16 14:44:22 +02:00
|
|
|
|
2019-07-22 11:28:50 +02:00
|
|
|
public function __construct(ContainerInterface $container, $env)
|
2019-07-16 11:58:38 +02:00
|
|
|
{
|
2019-07-16 14:44:22 +02:00
|
|
|
$this->container = $container;
|
2019-07-22 11:28:50 +02:00
|
|
|
|
|
|
|
$this->url_prefix = $env->isDebug() ? $this->container->getParameter('dev_ngrok_prefix')
|
|
|
|
: $container->get('router')->getContext()->getBaseUrl();
|
2019-07-16 14:44:22 +02:00
|
|
|
}
|
2019-07-16 14:58:49 +02:00
|
|
|
|
2019-07-19 17:54:27 +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);
|
2019-07-19 17:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function createPurchase($customer, $items, $cbFormatter)
|
|
|
|
{
|
|
|
|
$this->setApiKey();
|
2019-07-16 11:58:38 +02:00
|
|
|
|
2019-07-16 18:08:09 +02:00
|
|
|
$line_items = [];
|
|
|
|
|
2019-07-16 18:23:05 +02:00
|
|
|
foreach ($items as $item) $line_items[] = $cbFormatter($item);
|
2019-07-16 18:08:09 +02:00
|
|
|
|
2019-07-16 14:44:22 +02:00
|
|
|
return (
|
|
|
|
\Stripe\Checkout\Session::create([
|
2019-07-19 17:54:27 +02:00
|
|
|
'customer' => $customer,
|
2019-07-16 18:08:09 +02:00
|
|
|
'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-16 11:58:38 +02:00
|
|
|
}
|
2019-07-19 17:54:27 +02:00
|
|
|
|
2019-07-19 18:14:21 +02:00
|
|
|
public function confirmPayment($paimentIntent)
|
|
|
|
{
|
|
|
|
$infoIntent = $this->retrievePurchase($paimentIntent);
|
|
|
|
|
|
|
|
return ($infoIntent['amount'] == $infoIntent['amount_received'] && $infoIntent['amount_capturable'] == 0);
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:54:27 +02:00
|
|
|
public function retrievePurchase($paimentIntent)
|
|
|
|
{
|
|
|
|
$this->setApiKey();
|
|
|
|
|
2019-07-19 18:00:44 +02:00
|
|
|
return \Stripe\PaymentIntent::retrieve($paimentIntent);
|
2019-07-19 17:54:27 +02:00
|
|
|
}
|
2019-07-16 11:58:38 +02:00
|
|
|
}
|