Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58
Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler
<?php
namespace App\Service ;
use App\Entity\DeliveryNote ;
use App\Entity\Payment ;
use DateTime ;
use DateTimeInterface ;
use Stripe\Charge ;
use Stripe\Event ;
use Stripe\Exception\ApiErrorException ;
use Stripe\Exception\SignatureVerificationException ;
use Stripe\PaymentIntent ;
use Stripe\Stripe ;
use Stripe\Webhook ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface ;
use Symfony\Component\HttpFoundation\JsonResponse ;
use Throwable ;
class StripeService
{
public const VAT = 1.2 ;
private ParameterBagInterface $parameterBag ;
private GuardChargeService $guardChargeService ;
private BrandService $brandService ;
/**
* @param ParameterBagInterface $parameterBag
* @param GuardChargeService $guardChargeService
* @param BrandService $brandService
*/
public function __construct ( ParameterBagInterface $parameterBag , GuardChargeService $guardChargeService , BrandService $brandService )
{
$this -> parameterBag = $parameterBag ;
$this -> guardChargeService = $guardChargeService ;
$this -> brandService = $brandService ;
}
public function getPaymentPublicKey (): string
{
return $this -> parameterBag -> get ( 'stripe.api_key.payment.public' );
}
public function getBookingShippingPublicKey (): string
{
return $this -> parameterBag -> get ( 'stripe.api_key.delivery.public' );
}
public function getFeesPublicKey (): string
{
return $this -> parameterBag -> get ( 'stripe.api_key.fees.public' );
}
public function getDeliveryPublicKey ( DeliveryNote $deliveryNote , DateTimeInterface $saleDate , DateTimeInterface $bookingDate = null ): string
{
$saleDate = (clone $saleDate )-> setTime ( 0 , 0 );
if ($deliveryNote -> isPaidOnline ()) {
$saleDate -> modify ( '+' . $this -> guardChargeService -> getFreeDays () . ' days' )-> modify ( '-1 microsecond' );
}
$date = $bookingDate ;
if (!$date ) {
$date = new DateTime ();
}
$dateDiff = $saleDate -> diff ( $date );
if ($dateDiff -> format ( '%R' ) == '+' ) {
return $this -> getFeesPublicKey ();
} else {
return $this -> getBookingShippingPublicKey ();
}
}
public function getWebhookSecret ( string $type , DateTimeInterface $saleDate , DeliveryNote $deliveryNote , DateTimeInterface $bookingDate = null ): string
{
if ($type == Payment :: TYPE_PAYMENT ) {
return $this -> parameterBag -> get ( 'stripe.webhook.payment.secret' );
} else {
$saleDate = (clone $saleDate )-> setTime ( 0 , 0 );
if ($deliveryNote -> isPaidOnline ()) {
$saleDate -> modify ( '+' . $this -> guardChargeService -> getFreeDays () . ' days' )-> modify ( '-1 microsecond' );
}
$date = $bookingDate ;
if(!$date ) {
$date = new DateTime ();
}
$dateDiff = $saleDate -> diff ( $date );
if ($dateDiff -> format ( '%R' ) == '+' ) {
return $this -> parameterBag -> get ( 'stripe.webhook.payment_fees.secret' );
} else {
return $this -> parameterBag -> get ( 'stripe.webhook.payment_delivery.secret' );
}
}
}
public function getFeesSecret (): string {
return $this -> parameterBag -> get ( 'stripe.webhook.payment_fees.secret' );
}
public function setDeliveryKey ( DeliveryNote $deliveryNote , DateTimeInterface $saleDate , DateTimeInterface $bookingDate = null ): void
{
$saleDate = (clone $saleDate )-> setTime ( 0 , 0 );
if ($deliveryNote -> isPaidOnline ()) {
$saleDate -> modify ( '+' . $this -> guardChargeService -> getFreeDays () . ' days' )-> modify ( '-1 microsecond' );
}
$date = $bookingDate ;
if (!$date ) {
$date = new DateTime ();
}
$dateDiff = $saleDate -> diff ( $date );
if ($dateDiff -> format ( '%R' ) == '+' ) {
$this -> setFeesKey ();
} else {
$this -> setBookingShippingKey ();
}
}
public function setPaymentKey (): void
{
Stripe :: setApiKey ( $this -> parameterBag -> get ( 'stripe.api_key.payment.secret' ));
}
public function setBookingShippingKey (): void
{
Stripe :: setApiKey ( $this -> parameterBag -> get ( 'stripe.api_key.delivery.secret' ));
}
public function setFeesKey (): void
{
Stripe :: setApiKey ( $this -> parameterBag -> get ( 'stripe.api_key.fees.secret' ));
}
/**
* @param float $amount
* @param string $reference
* @param string $type
* @param array $metadata
* @return PaymentIntent
* @throws ApiErrorException
*/
public function createInstance ( float $amount , string $reference , string $type , array $metadata = array()): PaymentIntent
{
$time = microtime ( true );
$paymentIntentOptions = [
'amount' => $amount * 100 ,
'currency' => 'EUR' ,
'automatic_payment_methods' => [
'enabled' => false ,
],
'metadata' => array(
'brand' => $this -> brandService -> getBrand ()-> getCode (),
'code' => $reference ,
'type' => $type ,
'reference' => $reference . '_' . $type . '_' . $time ,
'microtime' => $time ,
'datetime' => (new DateTime ())-> format ( 'Y-m-d H:i:s' ),
)
];
if (!empty($metadata )) {
$paymentIntentOptions [ 'metadata' ] = array_merge ( $paymentIntentOptions [ 'metadata' ], $metadata );
}
return PaymentIntent :: create ( $paymentIntentOptions );
}
/**
* @param string $intentId
* @param float $amount
* @return PaymentIntent
* @throws ApiErrorException
*/
public function updateAmount ( string $intentId , float $amount ): PaymentIntent
{
$paymentIntentOptions = [
'amount' => $amount * 100 ,
];
return PaymentIntent :: update ( $intentId , $paymentIntentOptions );
}
/**
* @param string $instanceId
* @return PaymentIntent
* @throws ApiErrorException
*/
public function getInstance ( string $instanceId ): PaymentIntent
{
return PaymentIntent :: retrieve ( $instanceId );
}
/**
* @param $params
* @return PaymentIntent[]
* @throws ApiErrorException
*/
public function search ( $params ): array
{
$results = array();
$intents = PaymentIntent :: search ( array_merge ( $params , array( 'limit' => 100 )));
do {
$results = array_merge ( $results , $intents -> data );
$hasMore = $intents -> has_more ;
if ($hasMore ) {
$intents = $intents -> nextPage ();
}
} while ($hasMore );
return $results ;
}
/**
* @param PaymentIntent $paymentIntent
* @return mixed
* @throws ApiErrorException
*/
public function get3DS ( PaymentIntent $paymentIntent ): mixed
{
$paymentCharge = Charge :: retrieve ( $paymentIntent -> latest_charge );
return $paymentCharge -> payment_method_details -> card -> three_d_secure ;
}
/**
* @param PaymentIntent $paymentIntent
* @return bool
*/
public function validPayment ( PaymentIntent $paymentIntent ): bool
{
return $paymentIntent -> status == PaymentIntent :: STATUS_SUCCEEDED ;
}
/**
* @param PaymentIntent $paymentIntent
* @return bool
*/
public function requiresActionPayment ( PaymentIntent $paymentIntent ): bool
{
return $paymentIntent -> status == PaymentIntent :: STATUS_REQUIRES_ACTION ;
}
/**
* @param PaymentIntent $paymentIntent
* @return bool
*/
public function pendingPayment ( PaymentIntent $paymentIntent ): bool
{
return $paymentIntent -> status == PaymentIntent :: STATUS_REQUIRES_PAYMENT_METHOD ;
}
/**
* @param PaymentIntent $intent
* @return array
*/
public function formatIntentOutput ( PaymentIntent $intent ): array
{
return array(
'clientSecret' => $intent -> client_secret
);
}
/**
* @param string $payload
* @param string $endpointSecret
* @return PaymentIntent|JsonResponse
*/
public function hookPayment ( string $payload , string $endpointSecret ): PaymentIntent | JsonResponse
{
$event = null ;
try {
$event = Event :: constructFrom ( json_decode ( $payload , true ));
} catch (Throwable $e ) {
return new JsonResponse ( 'Webhook error while parsing basic request' , 400 );
}
if ($endpointSecret ) {
$sig_header = $_SERVER [ 'HTTP_STRIPE_SIGNATURE' ];
try {
$event = Webhook :: constructEvent ( $payload , $sig_header , $endpointSecret );
} catch (SignatureVerificationException $e ) {
return new JsonResponse ( 'Webhook error while validating signature' , 400 );
}
}
if ($event -> type == 'payment_intent.succeeded' ) {
/** @var PaymentIntent $paymentIntent */
$paymentIntent = $event -> data -> object ;
return $paymentIntent ;
}
return new JsonResponse ( 'No event triggered' );
}
public static function getPriceExcludedTaxes ( float $price ): float
{
return round ( $price / self :: VAT , 2 );
}
public static function getPriceIncludedTaxes ( float $price ): float
{
return round ( $price * self :: VAT , 2 );
}
}