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

src/Service/StripeService.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\DeliveryNote;
  4. use App\Entity\Payment;
  5. use DateTime;
  6. use DateTimeInterface;
  7. use Stripe\Charge;
  8. use Stripe\Event;
  9. use Stripe\Exception\ApiErrorException;
  10. use Stripe\Exception\SignatureVerificationException;
  11. use Stripe\PaymentIntent;
  12. use Stripe\Stripe;
  13. use Stripe\Webhook;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Throwable;
  17. class StripeService
  18. {
  19.     public const VAT 1.2;
  20.     private ParameterBagInterface $parameterBag;
  21.     private GuardChargeService $guardChargeService;
  22.     private BrandService $brandService;
  23.     /**
  24.      * @param ParameterBagInterface $parameterBag
  25.      * @param GuardChargeService $guardChargeService
  26.      * @param BrandService $brandService
  27.      */
  28.     public function __construct(ParameterBagInterface $parameterBagGuardChargeService $guardChargeServiceBrandService $brandService)
  29.     {
  30.         $this->parameterBag $parameterBag;
  31.         $this->guardChargeService $guardChargeService;
  32.         $this->brandService $brandService;
  33.     }
  34.     public function getPaymentPublicKey(): string
  35.     {
  36.         return $this->parameterBag->get('stripe.api_key.payment.public');
  37.     }
  38.     public function getBookingShippingPublicKey(): string
  39.     {
  40.         return $this->parameterBag->get('stripe.api_key.delivery.public');
  41.     }
  42.     public function getFeesPublicKey(): string
  43.     {
  44.         return $this->parameterBag->get('stripe.api_key.fees.public');
  45.     }
  46.     public function getDeliveryPublicKey(DeliveryNote $deliveryNoteDateTimeInterface $saleDateDateTimeInterface $bookingDate null): string
  47.     {
  48.         $saleDate = (clone $saleDate)->setTime(00);
  49.         if ($deliveryNote->isPaidOnline()) {
  50.             $saleDate->modify('+' $this->guardChargeService->getFreeDays() . ' days')->modify('-1 microsecond');
  51.         }
  52.         $date $bookingDate;
  53.         if (!$date) {
  54.             $date = new DateTime();
  55.         }
  56.         $dateDiff $saleDate->diff($date);
  57.         if ($dateDiff->format('%R') == '+') {
  58.             return $this->getFeesPublicKey();
  59.         } else {
  60.             return $this->getBookingShippingPublicKey();
  61.         }
  62.     }
  63.     public function getWebhookSecret(string $typeDateTimeInterface $saleDateDeliveryNote $deliveryNoteDateTimeInterface $bookingDate null): string
  64.     {
  65.         if ($type == Payment::TYPE_PAYMENT) {
  66.             return $this->parameterBag->get('stripe.webhook.payment.secret');
  67.         } else {
  68.             $saleDate = (clone $saleDate)->setTime(00);
  69.             if ($deliveryNote->isPaidOnline()) {
  70.                 $saleDate->modify('+' $this->guardChargeService->getFreeDays() . ' days')->modify('-1 microsecond');
  71.             }
  72.             $date $bookingDate;
  73.             if(!$date) {
  74.                 $date = new DateTime();
  75.             }
  76.             $dateDiff $saleDate->diff($date);
  77.             if ($dateDiff->format('%R') == '+') {
  78.                 return $this->parameterBag->get('stripe.webhook.payment_fees.secret');
  79.             } else {
  80.                 return $this->parameterBag->get('stripe.webhook.payment_delivery.secret');
  81.             }
  82.         }
  83.     }
  84.     public function getFeesSecret(): string {
  85.         return $this->parameterBag->get('stripe.webhook.payment_fees.secret');
  86.     }
  87.     public function setDeliveryKey(DeliveryNote $deliveryNoteDateTimeInterface $saleDateDateTimeInterface $bookingDate null): void
  88.     {
  89.         $saleDate = (clone $saleDate)->setTime(00);
  90.         if ($deliveryNote->isPaidOnline()) {
  91.             $saleDate->modify('+' $this->guardChargeService->getFreeDays() . ' days')->modify('-1 microsecond');
  92.         }
  93.         $date $bookingDate;
  94.         if (!$date) {
  95.             $date = new DateTime();
  96.         }
  97.         $dateDiff $saleDate->diff($date);
  98.         if ($dateDiff->format('%R') == '+') {
  99.             $this->setFeesKey();
  100.         } else {
  101.             $this->setBookingShippingKey();
  102.         }
  103.     }
  104.     public function setPaymentKey(): void
  105.     {
  106.         Stripe::setApiKey($this->parameterBag->get('stripe.api_key.payment.secret'));
  107.     }
  108.     public function setBookingShippingKey(): void
  109.     {
  110.         Stripe::setApiKey($this->parameterBag->get('stripe.api_key.delivery.secret'));
  111.     }
  112.     public function setFeesKey(): void
  113.     {
  114.         Stripe::setApiKey($this->parameterBag->get('stripe.api_key.fees.secret'));
  115.     }
  116.     /**
  117.      * @param float $amount
  118.      * @param string $reference
  119.      * @param string $type
  120.      * @param array $metadata
  121.      * @return PaymentIntent
  122.      * @throws ApiErrorException
  123.      */
  124.     public function createInstance(float $amountstring $referencestring $type, array $metadata = array()): PaymentIntent
  125.     {
  126.         $time microtime(true);
  127.         $paymentIntentOptions = [
  128.             'amount' => $amount 100,
  129.             'currency' => 'EUR',
  130.             'automatic_payment_methods' => [
  131.                 'enabled' => false,
  132.             ],
  133.             'metadata' => array(
  134.                 'brand' => $this->brandService->getBrand()->getCode(),
  135.                 'code' => $reference,
  136.                 'type' => $type,
  137.                 'reference' => $reference '_' $type '_' $time,
  138.                 'microtime' => $time,
  139.                 'datetime' => (new DateTime())->format('Y-m-d H:i:s'),
  140.             )
  141.         ];
  142.         if (!empty($metadata)) {
  143.             $paymentIntentOptions['metadata'] = array_merge($paymentIntentOptions['metadata'], $metadata);
  144.         }
  145.         return PaymentIntent::create($paymentIntentOptions);
  146.     }
  147.     /**
  148.      * @param string $intentId
  149.      * @param float $amount
  150.      * @return PaymentIntent
  151.      * @throws ApiErrorException
  152.      */
  153.     public function updateAmount(string $intentIdfloat $amount): PaymentIntent
  154.     {
  155.         $paymentIntentOptions = [
  156.             'amount' => $amount 100,
  157.         ];
  158.         return PaymentIntent::update($intentId$paymentIntentOptions);
  159.     }
  160.     /**
  161.      * @param string $instanceId
  162.      * @return PaymentIntent
  163.      * @throws ApiErrorException
  164.      */
  165.     public function getInstance(string $instanceId): PaymentIntent
  166.     {
  167.         return PaymentIntent::retrieve($instanceId);
  168.     }
  169.     /**
  170.      * @param $params
  171.      * @return PaymentIntent[]
  172.      * @throws ApiErrorException
  173.      */
  174.     public function search($params): array
  175.     {
  176.         $results = array();
  177.         $intents PaymentIntent::search(array_merge($params, array('limit' => 100)));
  178.         do {
  179.             $results array_merge($results$intents->data);
  180.             $hasMore $intents->has_more;
  181.             if ($hasMore) {
  182.                 $intents $intents->nextPage();
  183.             }
  184.         } while ($hasMore);
  185.         return $results;
  186.     }
  187.     /**
  188.      * @param PaymentIntent $paymentIntent
  189.      * @return mixed
  190.      * @throws ApiErrorException
  191.      */
  192.     public function get3DS(PaymentIntent $paymentIntent): mixed
  193.     {
  194.         $paymentCharge Charge::retrieve($paymentIntent->latest_charge);
  195.         return $paymentCharge->payment_method_details->card->three_d_secure;
  196.     }
  197.     /**
  198.      * @param PaymentIntent $paymentIntent
  199.      * @return bool
  200.      */
  201.     public function validPayment(PaymentIntent $paymentIntent): bool
  202.     {
  203.         return $paymentIntent->status == PaymentIntent::STATUS_SUCCEEDED;
  204.     }
  205.     /**
  206.      * @param PaymentIntent $paymentIntent
  207.      * @return bool
  208.      */
  209.     public function requiresActionPayment(PaymentIntent $paymentIntent): bool
  210.     {
  211.         return $paymentIntent->status == PaymentIntent::STATUS_REQUIRES_ACTION;
  212.     }
  213.     /**
  214.      * @param PaymentIntent $paymentIntent
  215.      * @return bool
  216.      */
  217.     public function pendingPayment(PaymentIntent $paymentIntent): bool
  218.     {
  219.         return $paymentIntent->status == PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD;
  220.     }
  221.     /**
  222.      * @param PaymentIntent $intent
  223.      * @return array
  224.      */
  225.     public function formatIntentOutput(PaymentIntent $intent): array
  226.     {
  227.         return array(
  228.             'clientSecret' => $intent->client_secret
  229.         );
  230.     }
  231.     /**
  232.      * @param string $payload
  233.      * @param string $endpointSecret
  234.      * @return PaymentIntent|JsonResponse
  235.      */
  236.     public function hookPayment(string $payloadstring $endpointSecret): PaymentIntent|JsonResponse
  237.     {
  238.         $event null;
  239.         try {
  240.             $event Event::constructFrom(json_decode($payloadtrue));
  241.         } catch (Throwable $e) {
  242.             return new JsonResponse('Webhook error while parsing basic request'400);
  243.         }
  244.         if ($endpointSecret) {
  245.             $sig_header $_SERVER['HTTP_STRIPE_SIGNATURE'];
  246.             try {
  247.                 $event Webhook::constructEvent($payload$sig_header$endpointSecret);
  248.             } catch (SignatureVerificationException $e) {
  249.                 return new JsonResponse('Webhook error while validating signature'400);
  250.             }
  251.         }
  252.         if ($event->type == 'payment_intent.succeeded') {
  253.             /** @var PaymentIntent $paymentIntent */
  254.             $paymentIntent $event->data->object;
  255.             return $paymentIntent;
  256.         }
  257.         return new JsonResponse('No event triggered');
  258.     }
  259.     public static function getPriceExcludedTaxes(float $price): float
  260.     {
  261.         return round($price self::VAT2);
  262.     }
  263.     public static function getPriceIncludedTaxes(float $price): float
  264.     {
  265.         return round($price self::VAT2);
  266.     }
  267. }