Completed
Push — master ( d601ba...2d4509 )
by Cesar
21s queued 15s
created

PaymentsController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 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 PaymentsController
14
 * @package App\Controller\Paypal
15
 *
16
 * @Route("/paypal/payments", name="paypal-payments-")
17
 */
18
class PaymentsController extends AbstractController
19
{
20
    /**
21
     * @Route("/pay-ins", name="pay-ins", methods={"GET"}, defaults={"action" = "pay-ins"})
22
     * @Route("/pay-outs", name="pay-outs", methods={"GET"}, defaults={"action" = "pay-outs"})
23
     * @Route("/bopis", name="bopis", methods={"GET"}, defaults={"action" = "bopis"})
24
     * @Route("/subscriptions", name="subscriptions", methods={"GET"}, defaults={"action" = "subscriptions"})
25
     * @Route("/invoices", name="invoices", methods={"GET"}, defaults={"action" = "invoices"})
26
     * @Route("/ecs", name="ecs", methods={"GET"}, defaults={"action" = "ecs"})
27
     * @Route("/paylater", name="paylater", methods={"GET"}, defaults={"action" = "paylater"})
28
     * @Route("/msp", name="msp", methods={"GET"}, defaults={"action" = "msp"})
29
     *
30
     * @param string $action
31
     *
32
     * @return Response | RedirectResponse
33
     */
34
    public function payments(string $action)
35
    {
36
        return $this->render('paypal/payments/'. $action .'.html.twig');
37
    }
38
39
    /**
40
     * @Route("/{paymentId}/capture", name="capture", methods={"POST"})
41
     * @param string $paymentId
42
     * @return Response | RedirectResponse
43
     */
44
    public function capture(string $paymentId)
45
    {
46
        $capture = $this->paypalService->getPaymentService()->capturePayment($paymentId);
47
        return $this->render('default/dump-input-id.html.twig', [
48
            'raw_result' => false,
49
            'result' => $capture,
50
            'result_id' => 'payment-id'
51
        ]);
52
    }
53
54
    /**
55
     * @Route("/pay-outs", name="pay-outs-create", methods={"POST"})
56
     *
57
     * @return Response | RedirectResponse
58
     */
59
    public function payoutsCreate()
60
    {
61
        $request = Request::createFromGlobals();
62
        $subject = $request->request->get('subject', null);
63
        $note = $request->request->get('note', null);
64
        $email = $request->request->get('email', null);
65
        $itemId = $request->request->get('item-id', null);
66
        $currency = $request->request->get('currency', null);
67
        $amount = $request->request->get('amount', null);
68
        $payout = $this->paypalService
69
            ->getPayoutService()
70
            ->createPayout($subject, $note, $email, $itemId, $amount, $currency);
71
        return $this->render('default/dump-input-id.html.twig', [
72
            'raw_result' => false,
73
            'result' => $payout,
74
            'result_id' => $payout->getBatchHeader()->getPayoutBatchId()
75
        ]);
76
    }
77
78
    /**
79
     * @Route("/pay-outs/{statusId}", name="pay-outs-refresh", methods={"GET"})
80
     *
81
     * @param string $statusId
82
     * @return Response | RedirectResponse
83
     */
84
    public function payoutsRefresh(string $statusId)
85
    {
86
        $payout = $this->paypalService->getPayoutService()->getPayout($statusId);
87
        return $this->render('default/dump-input-id.html.twig', [
88
            'raw_result' => false,
89
            'result' => $payout,
90
            'result_id' => $payout->getBatchHeader()->getPayoutBatchId()
91
        ]);
92
    }
93
94
    /**
95
     * @Route("/invoices", name="invoices-create", methods={"POST"})
96
     *
97
     * @return Response | RedirectResponse
98
     */
99
    public function invoicesCreate()
100
    {
101
        $request = Request::createFromGlobals();
102
        $inputForm = $request->request->all();
103
        $invoice = $this->paypalService->getInvoiceService()->createInvoice($inputForm);
104
        if ($invoice instanceof Invoice) {
105
            $this->paypalService->getInvoiceService()->sendInvoice($invoice);
106
            $invoiceQR = $this->paypalService->getInvoiceService()->getInvoiceQRHTML($invoice);
107
        }
108
        if (isset($invoiceQR)) {
109
            return $this->render('default/dump-input-id.html.twig', [
110
                'raw_result' => true,
111
                'result' => $invoiceQR,
112
                'result_id' => $invoice->getId()
113
            ]);
114
        }
115
        return new JsonResponse('Error creating the Invoice, please check the logs');
116
    }
117
118
    /**
119
     * @Route("/invoices/{invoiceId}", name="invoices-refresh", methods={"GET"})
120
     * @param string $invoiceId
121
     * @return Response | RedirectResponse
122
     */
123
    public function invoicesRefresh(string $invoiceId)
124
    {
125
        $invoice = $this->paypalService->getInvoiceService()->getInvoice($invoiceId);
126
        return $this->render('default/dump-input-id.html.twig', [
127
            'raw_result' => false,
128
            'result' => $invoice,
129
            'result_id' => $invoice->getId()
130
        ]);
131
    }
132
133
    /**
134
     * @Route("/{paymentId}/ecs-result", name="ecs-result", methods={"POST"})
135
     * @param string $paymentId
136
     * @return Response | RedirectResponse
137
     */
138
    public function ecsResult(string $paymentId)
139
    {
140
        $capture = $this->paypalService->getPaymentService()->capturePayment($paymentId);
141
        return $this->render('paypal/payments/ecs-results.twig', [
142
            'capture' => [
143
                'payer' => $capture->result->payer,
0 ignored issues
show
Bug introduced by
The property payer does not exist on string.
Loading history...
144
                'shipping' => $capture->result->purchase_units[0]->shipping,
0 ignored issues
show
Bug introduced by
The property purchase_units does not exist on string.
Loading history...
145
            ],
146
        ]);
147
    }
148
149
    /**
150
     * @Route("/create", name="create", methods={"POST"})
151
     * @return JsonResponse
152
     */
153
    public function create(): JsonResponse
154
    {
155
        $request = Request::createFromGlobals();
156
        $requestBody = $request->getContent();
157
        $headers = ['PayPal-Request-Id' => time()];
158
        $order = $this->paypalService->getPaymentService()->createOrder($requestBody, $headers);
159
        return new JsonResponse($order);
160
    }
161
}
162