<?php
namespace App\Service;
use Stripe\Customer;
use Stripe\Stripe;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\RouterInterface;
class PaymentMethodService {
protected $tokenStorage;
protected $entityManager;
protected $router;
protected $logger;
protected $stripeSecret;
public function __construct($stripeSecret, TokenStorageInterface $tokenStorage, EntityManagerInterface $entityManager, RouterInterface $router, LoggerInterface $logger) {
$this->tokenStorage = $tokenStorage;
$this->em = $entityManager;
$this->logger = $logger;
$this->router = $router;
$this->stripeSecret = $stripeSecret;
}
public function getPaymentMethodForStripeCardId($stripeCardId) {
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$repoPaymentMethod = $this->em->getRepository('App\Entity\PaymentMethod');
return $repoPaymentMethod->findOneBy(['user' => $user, 'stripeCardId' => $stripeCardId]);
}
public function getStripeCardForId($stripeCardId) {
Stripe::setApiKey($this->stripeSecret);
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$customer = $user->getStripeCustomerId() ? Customer::retrieve($user->getStripeCustomerId()) : null;
return $customer->retrieveSource($user->getStripeCustomerId(), $stripeCardId);
}
}