ApiController::paymentCaptureAction()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 3
eloc 18
nc 3
nop 2
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Controller;
4
5
use FOS\RestBundle\Controller\Annotations;
6
use FOS\RestBundle\Controller\FOSRestController;
7
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
8
use Loevgaard\DandomainAltapayBundle\Handler\PaymentHandler;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class ApiController extends FOSRestController
13
{
14
    /**
15
     * @Annotations\Get("/api/payment/{id}/capture")
16
     *
17
     * @param Request    $request
18
     * @param string|int $id      Can be both altapay id or order id
19
     *
20
     * @return Response
21
     */
22
    public function paymentCaptureAction(Request $request, $id)
23
    {
24
        $data = [
25
            'error' => false,
26
            'captured_amount' => null,
27
            'refunded_amount' => null,
28
        ];
29
30
        $payment = $this->findPaymentByOrderIdOrAltapayId($id);
31
        $paymentHandler = $this->getPaymentHandler();
32
        $res = $paymentHandler->capture($payment, $request->query->get('amount'));
33
34
        if ($res->isSuccessful()) {
35
            $transactions = $res->getTransactions();
36
            if (count($transactions)) {
37
                $data['captured_amount'] = $transactions[0]->getCapturedAmount();
38
                $data['refunded_amount'] = $transactions[0]->getRefundedAmount();
39
            }
40
        } else {
41
            $data['error'] = true;
42
        }
43
44
        $view = $this->view($data);
45
        $view->setTemplate('@LoevgaardDandomainAltapay/api/payment_capture.html.twig');
46
47
        return $this->handleView($view);
48
    }
49
50
    /**
51
     * @Annotations\Post("/api/payment/{id}/refund")
52
     *
53
     * @param Request    $request
54
     * @param string|int $id      Can be both altapay id or order id
55
     *
56
     * @return Response
57
     */
58
    public function paymentRefundAction(Request $request, $id)
59
    {
60
        $data = [
61
            'error' => false,
62
            'captured_amount' => null,
63
            'refunded_amount' => null,
64
        ];
65
66
        $payment = $this->findPaymentByOrderIdOrAltapayId($id);
67
        $paymentHandler = $this->getPaymentHandler();
68
        $res = $paymentHandler->refund($payment, $request->query->get('amount'));
69
70
        if ($res->isSuccessful()) {
71
            $transactions = $res->getTransactions();
72
            if (count($transactions)) {
73
                $data['captured_amount'] = $transactions[0]->getCapturedAmount();
74
                $data['refunded_amount'] = $transactions[0]->getRefundedAmount();
75
            }
76
        } else {
77
            $data['error'] = true;
78
        }
79
80
        $view = $this->view($data);
81
        $view->setTemplate('@LoevgaardDandomainAltapay/api/payment_capture.html.twig');
82
83
        return $this->handleView($view);
84
    }
85
86
    /**
87
     * @param string|int $id
88
     *
89
     * @return Payment
90
     */
91 View Code Duplication
    private function findPaymentByOrderIdOrAltapayId($id): Payment
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...
92
    {
93
        $paymentRepository = $this->get('loevgaard_dandomain_altapay.payment_repository');
94
        $payment = $paymentRepository->findByOrderIdOrAltapayId($id);
95
        if (!$payment) {
96
            throw $this->createNotFoundException();
97
        }
98
99
        return $payment;
100
    }
101
102
    /**
103
     * @return PaymentHandler
104
     */
105
    private function getPaymentHandler(): PaymentHandler
106
    {
107
        return $this->get('loevgaard_dandomain_altapay.payment_handler');
108
    }
109
}
110