Completed
Push — master ( 5dadda...07493f )
by Cesar
30s queued 10s
created

AuthenticatedController::subscriptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Controller\Paypal;
4
5
use PayPal\Api\Invoice;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
12
/**
13
 * Class AuthenticatedController
14
 * @package App\Controller\Paypal
15
 *
16
 * @Route("/paypal/logged-in", name="paypal-authenticated-")
17
 */
18
class AuthenticatedController extends AbstractController
19
{
20
    /**
21
     * @Route("/", name="index", methods={"GET"})
22
     *
23
     * @return Response | RedirectResponse
24
     */
25
    public function index()
26
    {
27
        if (!$this->sessionService->isActive()) {
28
            return $this->redirectToRoute('paypal-index');
29
        }
30
        $refreshToken = $this->sessionService->getRefreshToken();
31
        $myTransactions = $this->paypalService
32
            ->getReportingService()
33
            ->getUserTransactionsFromRefreshToken($refreshToken);
34
        if ($refreshToken !== null) {
35
            $userInfo = $this->paypalService
36
                ->getIdentityService()
37
                ->getUserInfoFromRefreshToken($refreshToken);
38
            if ($userInfo) {
39
                return $this->render('paypal/authenticated/account.html.twig', [
40
                    'name' => $userInfo->getName(),
41
                    'email' => $userInfo->getEmail(),
42
                    'userInfo' => $userInfo,
43
                    'transactions' => $myTransactions,
44
                ]);
45
            }
46
        }
47
        return $this->redirectToRoute('paypal-index');
48
    }
49
50
    /**
51
     * @Route("/payments", name="payments", methods={"GET"})
52
     *
53
     * @return Response | RedirectResponse
54
     */
55
    public function payments()
56
    {
57
        if (!$this->sessionService->isActive()) {
58
            return $this->redirectToRoute('paypal-index');
59
        }
60
        return $this->render('paypal/authenticated/payments.html.twig', [
61
            'PAYPAL_SDK_CLIENT_ID' =>
62
                $this->sessionService->session->get('PAYPAL_SDK_CLIENT_ID') ??
63
                $this->getParameter('PAYPAL_SDK_CLIENT_ID'),
64
        ]);
65
    }
66
67
    /**
68
     * @Route("/payments/{paymentId}/capture", name="payments-capture", methods={"POST"})
69
     * @param string $paymentId
70
     * @return Response | RedirectResponse
71
     */
72 View Code Duplication
    public function paymentsCapture(string $paymentId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        if (!$this->sessionService->isActive()) {
75
            return $this->redirectToRoute('paypal-index');
76
        }
77
        $capture = $this->paypalService->getPaymentService()->capturePayment($paymentId);
78
        return $this->render('paypal/authenticated/result.html.twig', [
79
            'raw_result' => false,
80
            'result' => $capture,
81
            'result_id' => 'payment-id'
82
        ]);
83
    }
84
85
    /**
86
     * @Route("/payouts", name="payouts", methods={"GET"})
87
     *
88
     * @return Response | RedirectResponse
89
     */
90
    public function payouts()
91
    {
92
        if (!$this->sessionService->isActive()) {
93
            return $this->redirectToRoute('paypal-index');
94
        }
95
        return $this->render('paypal/authenticated/payouts.html.twig');
96
    }
97
98
    /**
99
     * @Route("/payouts", name="payouts-create", methods={"POST"})
100
     *
101
     * @return Response | RedirectResponse
102
     */
103
    public function payoutsCreate()
104
    {
105
        if (!$this->sessionService->isActive()) {
106
            return $this->redirectToRoute('paypal-index');
107
        }
108
        $request = Request::createFromGlobals();
109
        $subject = $request->request->get('subject', null);
110
        $note = $request->request->get('note', null);
111
        $email = $request->request->get('email', null);
112
        $itemId = $request->request->get('item-id', null);
113
        $currency = $request->request->get('currency', null);
114
        $amount = $request->request->get('amount', null);
115
        $payout = $this->paypalService
116
            ->getPayoutService()
117
            ->createPayout($subject, $note, $email, $itemId, $amount, $currency);
118
        return $this->render('paypal/authenticated/result.html.twig', [
119
            'raw_result' => false,
120
            'result' => $payout,
121
            'result_id' => $payout->getBatchHeader()->getPayoutBatchId()
122
        ]);
123
    }
124
125
    /**
126
     * @Route("/payouts/{statusId}", name="payouts-refresh", methods={"GET"})
127
     * @param string $statusId
128
     * @return Response | RedirectResponse
129
     */
130 View Code Duplication
    public function payoutsRefresh(string $statusId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        if (!$this->sessionService->isActive()) {
133
            return $this->redirectToRoute('paypal-index');
134
        }
135
        $payout = $this->paypalService->getPayoutService()->getPayout($statusId);
136
        return $this->render('paypal/authenticated/result.html.twig', [
137
            'raw_result' => false,
138
            'result' => $payout,
139
            'result_id' => $payout->getBatchHeader()->getPayoutBatchId()
140
        ]);
141
    }
142
143
    /**
144
     * @Route("/invoices", name="invoices", methods={"GET"})
145
     *
146
     * @return Response | RedirectResponse
147
     */
148
    public function invoices()
149
    {
150
        if (!$this->sessionService->isActive()) {
151
            return $this->redirectToRoute('paypal-index');
152
        }
153
        return $this->render('paypal/authenticated/invoices.html.twig');
154
    }
155
156
    /**
157
     * @Route("/invoices", name="invoices-create", methods={"POST"})
158
     *
159
     * @return Response | RedirectResponse
160
     */
161
    public function invoicesCreate()
162
    {
163
        if (!$this->sessionService->isActive()) {
164
            return $this->redirectToRoute('paypal-index');
165
        }
166
        $request = Request::createFromGlobals();
167
        $inputForm = $request->request->all();
168
        $invoice = $this->paypalService->getInvoiceService()
169
            ->createInvoice($inputForm);
170
        if ($invoice instanceof Invoice) {
171
            $this->paypalService->getInvoiceService()->sendInvoice($invoice);
172
            $invoiceQR = $this->paypalService->getInvoiceService()
173
                ->getInvoiceQRHTML($invoice);
174
        }
175
        if (isset($invoiceQR)) {
176
            return $this->render('paypal/authenticated/result.html.twig', [
177
                'raw_result' => true,
178
                'result' => $invoiceQR,
179
                'result_id' => $invoice->getId()
180
            ]);
181
        }
182
        return new JsonResponse('Error creating the Invoice, please check the logs');
183
    }
184
185
    /**
186
     * @Route("/invoices/{invoiceId}", name="invoices-refresh", methods={"GET"})
187
     * @param string $invoiceId
188
     * @return Response | RedirectResponse
189
     */
190 View Code Duplication
    public function invoicesRefresh(string $invoiceId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        if (!$this->sessionService->isActive()) {
193
            return $this->redirectToRoute('paypal-index');
194
        }
195
        $invoice = $this->paypalService->getInvoiceService()->getInvoice($invoiceId);
196
        return $this->render('paypal/authenticated/result.html.twig', [
197
            'raw_result' => false,
198
            'result' => $invoice,
199
            'result_id' => $invoice->getId()
200
        ]);
201
    }
202
203
    /**
204
     * @Route("/subscriptions", name="subscriptions", methods={"GET"})
205
     *
206
     * @return Response | RedirectResponse
207
     */
208
    public function subscriptions()
209
    {
210
        if (!$this->sessionService->isActive()) {
211
            return $this->redirectToRoute('paypal-index');
212
        }
213
        return $this->render('paypal/authenticated/subscriptions.html.twig');
214
    }
215
}
216