Completed
Pull Request — master (#24)
by Rafał
03:08
created

PurchaseController::methodsAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
namespace Newscoop\PaywallBundle\Controller;
9
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
17
/**
18
 * It handles purchase actions.
19
 */
20
class PurchaseController extends BaseController
21
{
22
    /**
23
     * @Route("/paywall/purchase/", name="paywall_plugin_purchase_purchase", options={"expose"=true})
24
     *
25
     * @Method("POST")
26
     */
27
    public function purchaseAction(Request $request)
28
    {
29
        $translator = $this->get('translator');
30
        $currencyProvider = $this->get('newscoop_paywall.currency_provider');
31
        $currencyContext = $this->get('newscoop_paywall.currency_context');
32
        $currencyContext->setCurrency($currencyProvider->getDefaultCurrency()->getCode());
33
        $items = $request->getSession()->get('paywall_purchase', array());
34
35
        if (empty($items)) {
36
            return $this->loadErrorTemplate($translator->trans('paywall.error.noitems'));
37
        }
38
39
        $method = $request->request->get('paymentMethod');
40
        $paymentMethodContext = $this->get('newscoop_paywall.payment_method_context');
41
        $paymentMethodContext->setMethod($method);
42
        $purchaseService = $this->get('newscoop_paywall.services.purchase');
43
        $response = $purchaseService->startPurchase($items);
44
        if ($response && $response->isRedirect()) {
45
            $response->redirect();
46
        }
47
48
        if ($response && !$response->isSuccessful()) {
49
            return $this->loadErrorTemplate($response->getMessage());
50
        }
51
52
        return $this->redirectToThankYou();
53
    }
54
55
    /**
56
     * @Route("/paywall/subscriptions/order-batch/{currency}", name="paywall_subscribe_order_batch", options={"expose"=true})
57
     *
58
     * @Method("POST")
59
     */
60
    public function batchOrderAction(Request $request, $currency)
61
    {
62
        $items = $request->request->get('batchorder', array());
63
        $response = new JsonResponse();
64
        if (empty($items)) {
65
            $response->setStatusCode(404);
66
67
            return $response;
68
        }
69
70
        $request->getSession()->set('paywall_purchase', $items);
71
72
        $purchaseService = $this->get('newscoop_paywall.services.purchase');
73
        $result = $purchaseService->startPurchase($items, $currency);
74
75
        $data = $result->getData();
76
        if (isset($data['ACK']) && 'Success' === $data['ACK']) {
77
            $response->headers->set('X-Location', $result->getRedirectUrl());
78
            $response->setStatusCode(302);
79
        } else {
80
            $response->setStatusCode(502);
81
        }
82
83
        return $response;
84
    }
85
86
    /**
87
     * @Route("/paywall/purchase/methods/", name="paywall_plugin_purchase_methods", options={"expose"=true})
88
     */
89
    public function methodsAction(Request $request)
90
    {
91
        $templatesService = $this->get('newscoop.templates.service');
92
        $translator = $this->get('translator');
93
94
        $items = $request->query->get('batchorder', array());
95
        if (empty($items)) {
96
            return $this->loadErrorTemplate($translator->trans('paywall.error.noitems'));
97
        }
98
99
        $request->getSession()->set('paywall_purchase', $items);
100
101
        $order = $this->get('newscoop_paywall.services.order')->processAndCalculateOrderItems($items);
102
103
        return new Response($templatesService->fetchTemplate(
104
            '_paywall/payment_methods.tpl',
105
            array(
106
                'amount' => $order->getTotal(),
107
                'currency' => $order->getCurrency(),
108
            )
109
        ), 200, array('Content-Type' => 'text/html'));
110
    }
111
112
    /**
113
     * @Route("/paywall/success/", name="paywall_plugin_purchase_return", options={"expose"=true})
114
     *
115
     * @Method("GET")
116
     */
117
    public function returnAction(Request $request)
118
    {
119
        $items = $request->getSession()->get('paywall_purchase', array());
120
        $purchaseService = $this->get('newscoop_paywall.services.purchase');
121
        $response = $purchaseService->finishPurchase($items);
122
        $request->getSession()->remove('paywall_purchase');
123
124
        if (!$response->isSuccessful() && !$response->isRedirect()) {
125
            return $this->loadErrorTemplate($response->getMessage());
126
        }
127
128
        return $this->redirectToThankYou();
129
    }
130
131
    /**
132
     * @Route("/paywall/cancel/", name="paywall_plugin_purchase_cancel", options={"expose"=true})
133
     *
134
     * @Method("GET")
135
     */
136 View Code Duplication
    public function cancelAction()
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...
137
    {
138
        $templatesService = $this->get('newscoop.templates.service');
139
140
        return new Response($templatesService->fetchTemplate(
141
            '_paywall/cancel.tpl'
142
        ), 200, array('Content-Type' => 'text/html'));
143
    }
144
145
    /**
146
     * @Route("/paywall/thank-you/", name="paywall_plugin_purchase_thank_you", options={"expose"=true})
147
     *
148
     * @Method("GET")
149
     */
150 View Code Duplication
    public function thankYouAction()
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...
151
    {
152
        $templatesService = $this->get('newscoop.templates.service');
153
154
        return new Response($templatesService->fetchTemplate(
155
            '_paywall/thankyou.tpl'
156
        ), 200, array('Content-Type' => 'text/html'));
157
    }
158
159 View Code Duplication
    private function loadErrorTemplate($message = '')
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...
160
    {
161
        $templatesService = $this->get('newscoop.templates.service');
162
163
        return new Response($templatesService->fetchTemplate(
164
            '_paywall/error.tpl',
165
            array('msg' => $message)
166
        ), 200, array('Content-Type' => 'text/html'));
167
    }
168
169
    private function redirectToThankYou()
170
    {
171
        return new RedirectResponse($this->get('router')->generate('paywall_plugin_purchase_thank_you'));
172
    }
173
}
174