PaymentDetailsController::startTokenProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 5
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Billing\Controller;
23
24
use Parthenon\Billing\Config\FrontendConfig;
25
use Parthenon\Billing\CustomerProviderInterface;
26
use Parthenon\Billing\Entity\PaymentCard;
27
use Parthenon\Billing\PaymentMethod\DefaultPaymentManagerInterface;
28
use Parthenon\Billing\PaymentMethod\DeleterInterface;
29
use Parthenon\Billing\PaymentMethod\FrontendAddProcessorInterface;
30
use Parthenon\Billing\Repository\PaymentCardRepositoryInterface;
31
use Parthenon\Common\Exception\NoEntityFoundException;
32
use Psr\Log\LoggerInterface;
33
use Symfony\Component\HttpFoundation\JsonResponse;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\Routing\Attribute\Route;
36
use Symfony\Component\Serializer\SerializerInterface;
37
38
class PaymentDetailsController
39
{
40
    #[Route('/billing/payment-method', name: 'parthenon_billing_paymentdetails_fetch_payment_details', methods: ['GET'])]
41
    public function fetchPaymentDetails(
42
        Request $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
        /** @scrutinizer ignore-unused */ Request $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
        CustomerProviderInterface $customerProvider,
44
        PaymentCardRepositoryInterface $paymentDetailsRepository,
45
        SerializerInterface $serializer,
46
    ) {
47
        $customer = $customerProvider->getCurrentCustomer();
48
        $paymentDetails = $paymentDetailsRepository->getPaymentCardForCustomer($customer);
49
        $returnData = $serializer->serialize(['payment_details' => $paymentDetails], 'json');
50
51
        return JsonResponse::fromJsonString($returnData);
52
    }
53
54
    #[Route('/billing/payment-method/token/start', name: 'parthenon_billing_paymentdetails_starttokenprocess', methods: ['GET'])]
55
    public function startTokenProcess(
56
        Request $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
        /** @scrutinizer ignore-unused */ Request $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
        LoggerInterface $logger,
58
        CustomerProviderInterface $customerProvider,
59
        FrontendConfig $config,
60
        FrontendAddProcessorInterface $addCardByTokenDriver,
61
    ) {
62
        $logger->info('Starting the card token process');
63
64
        $customer = $customerProvider->getCurrentCustomer();
65
        $token = $addCardByTokenDriver->startTokenProcess($customer);
66
67
        return new JsonResponse([
68
            'token' => $token,
69
            'api_info' => $config->getApiInfo(),
70
        ]);
71
    }
72
73
    #[Route('/billing/payment-method/token/add', name: 'parthenon_billing_paymentdetails_addcardbytoken', methods: ['POST'])]
74
    public function addCardByToken(
75
        Request $request,
76
        CustomerProviderInterface $customerProvider,
77
        SerializerInterface $serializer,
78
        FrontendAddProcessorInterface $addCardByTokenDriver,
79
    ) {
80
        $customer = $customerProvider->getCurrentCustomer();
81
82
        $data = json_decode($request->getContent(), true);
83
84
        $paymentDetails = $addCardByTokenDriver->createPaymentDetailsFromToken($customer, $data['token']);
85
        $json = $serializer->serialize(['success' => true, 'payment_details' => $paymentDetails], 'json');
86
87
        return JsonResponse::fromJsonString($json, JsonResponse::HTTP_ACCEPTED);
88
    }
89
90
    #[Route('/billing/payment-method/{id}', name: 'parthenon_billing_paymentdetails_deletecard', methods: ['DELETE'])]
91
    public function deleteCard(Request $request, PaymentCardRepositoryInterface $paymentDetailsRepository, DeleterInterface $deleter)
92
    {
93
        try {
94
            /** @var PaymentCard $paymentDetails */
95
            $paymentDetails = $paymentDetailsRepository->findById($request->get('id'));
96
        } catch (NoEntityFoundException $exception) {
97
            return new JsonResponse(['success' => false], JsonResponse::HTTP_NOT_FOUND);
98
        }
99
        $deleter->delete($paymentDetails);
100
101
        return new JsonResponse(['success' => true], JsonResponse::HTTP_ACCEPTED);
102
    }
103
104
    #[Route('/billing/payment-method/{id}/default', name: 'parthenon_billing_paymentdetails_defaultcard', methods: ['POST'])]
105
    public function defaultCard(Request $request, CustomerProviderInterface $customerProvider, PaymentCardRepositoryInterface $paymentDetailsRepository, DefaultPaymentManagerInterface $defaultPaymentManager)
106
    {
107
        $customer = $customerProvider->getCurrentCustomer();
108
        try {
109
            /** @var PaymentCard $paymentDetails */
110
            $paymentDetails = $paymentDetailsRepository->findById($request->get('id'));
111
        } catch (NoEntityFoundException $exception) {
112
            return new JsonResponse(['success' => false], JsonResponse::HTTP_NOT_FOUND);
113
        }
114
115
        $defaultPaymentManager->makePaymentDetailsDefault($customer, $paymentDetails);
116
117
        return new JsonResponse(['success' => true], JsonResponse::HTTP_ACCEPTED);
118
    }
119
}
120