Completed
Push — master ( 1beb5a...c55f61 )
by Joachim
16:30
created

ApiController::findPaymentByOrderIdOrAltapayId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Controller;
4
5
use FOS\RestBundle\Controller\Annotations;
6
use FOS\RestBundle\Controller\FOSRestController;
7
use Loevgaard\AltaPay\Client;
8
use Loevgaard\AltaPay\Payload\CaptureReservation;
9
use Loevgaard\AltaPay\Payload\RefundCapturedReservation;
10
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class ApiController extends FOSRestController
15
{
16
    /**
17
     * @Annotations\Get("/payment/{id}/capture")
18
     *
19
     * @param string|int $id Can be both altapay id or order id
20
     * @return Response
21
     */
22 View Code Duplication
    public function paymentCaptureAction($id)
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...
23
    {
24
        $data = ['error' => false];
25
26
        $payment = $this->findPaymentByOrderIdOrAltapayId($id);
27
28
        $altapay = $this->getAltapayClient();
29
30
        $payload = new CaptureReservation($payment->getAltapayId());
31
        $res = $altapay->captureReservation($payload);
32
33
        if($res->isSuccessful()) {
34
            $transactions = $res->getTransactions();
35
            if(count($transactions)) {
36
                $data['captured_amount'] = $transactions[0]->getCapturedAmount();
37
            }
38
        } else {
39
            $data['error'] = true;
40
        }
41
42
        $view = $this->view($data);
43
        $view->setTemplate('@LoevgaardDandomainAltapay/api/payment_capture.html.twig');
44
45
        return $this->handleView($view);
46
    }
47
48
    /**
49
     * @Annotations\Post("/payment/{id}/refund")
50
     *
51
     * @param Request $request
52
     * @param string|int $id Can be both altapay id or order id
53
     * @return Response
54
     */
55 View Code Duplication
    public function paymentRefundAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
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...
56
    {
57
        $data = ['error' => false];
58
59
        $payment = $this->findPaymentByOrderIdOrAltapayId($id);
60
61
        $altapay = $this->getAltapayClient();
62
63
        $payload = new RefundCapturedReservation($payment->getAltapayId());
64
        $res = $altapay->refundCapturedReservation($payload);
65
66
        if($res->isSuccessful()) {
67
            $transactions = $res->getTransactions();
68
            if(count($transactions)) {
69
                $data['refunded_amount'] = $transactions[0]->getRefundedAmount();
70
            }
71
        } else {
72
            $data['error'] = true;
73
        }
74
75
        $view = $this->view($data);
76
        $view->setTemplate('@LoevgaardDandomainAltapay/api/payment_capture.html.twig');
77
78
        return $this->handleView($view);
79
    }
80
81
    /**
82
     * @param string|int $id
83
     * @return Payment
84
     */
85
    protected function findPaymentByOrderIdOrAltapayId($id) : Payment
86
    {
87
        $paymentManager = $this->get('loevgaard_dandomain_altapay.payment_manager');
88
        $payment = $paymentManager->findByOrderIdOrAltapayId($id);
89
        if(!$payment) {
90
            throw $this->createNotFoundException();
91
        }
92
93
        return $payment;
94
    }
95
96
    /**
97
     * @return Client
98
     */
99
    protected function getAltapayClient() : Client
100
    {
101
        return $this->get('loevgaard_dandomain_altapay.altapay_client');
102
    }
103
}
104