<?php declare(strict_types=1);
namespace App\Customer\Controller;
use App\Customer\Entity\Customer;
use App\Framework\Controller\APIController;
use App\Payment\Service\PaymentService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Routing\Annotation\Route;
#[Route(path: '/api/customers/{id}/paymentMethods')]
#[Security("(user == customer.getUser()) or (is_granted('ROLE_ADMIN') or is_granted('ROLE_CALLCENTER') or is_granted('ROLE_CHOFER'))")]
class CustomerPaymentMethodsController extends APIController
{
/**
* @return array<string, mixed>
*/
#[Route(path: '', methods: ['GET'])]
public function paymentMethodsAction(Customer $customer, PaymentService $paymentService): array
{
// Find the available payment methods for this customer
$methods = $paymentService->getPaymentMethodsForCustomer($customer);
// If the request comes from an end user, return their saved methods.
$savedPaymentMethods = [];
if ($this->isGranted('ROLE_USER')) {
$savedPaymentMethods = $paymentService->getSavedPaymentMethodsForCustomer($customer);
}
return [
'methods' => $methods,
'saved' => $savedPaymentMethods,
];
}
}