Issues (23)

src/Action/CaptureAction.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eres\SyliusIyzicoPlugin\Action;
6
7
use Eres\SyliusIyzicoPlugin\Bridge\IyzicoBridgeInterface;
8
use Eres\SyliusIyzicoPlugin\Form\Type\CreditCardType;
9
use Payum\Core\Exception\UnsupportedApiException;
10
use Payum\Core\Action\ActionInterface;
11
use Payum\Core\ApiAwareInterface;
12
use Payum\Core\GatewayAwareInterface;
13
use Payum\Core\GatewayAwareTrait;
14
use Payum\Core\Model\CreditCardInterface;
15
use Payum\Core\Reply\HttpResponse;
16
use Payum\Core\Request\Capture;
17
use Payum\Core\Exception\RequestNotSupportedException;
18
use Payum\Core\Bridge\Spl\ArrayObject;
19
use Payum\Core\Request\RenderTemplate;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\HttpFoundation\RequestStack;
23
24
final class CaptureAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
25
{
26
    use GatewayAwareTrait;
27
28
    const ERRORS = [
29
      -1 => "eres_sylius_iyzico_plugin.payment.invalid_3d_secure_signature_or_verification",
30
      2=>"eres_sylius_iyzico_plugin.payment.card_holder_or_issuer_not_registered_to_3d_secure_network",
31
      3=> "eres_sylius_iyzico_plugin.payment.issuer_is_not_registered_to_3d_secure_network",
32
      4=>"eres_sylius_iyzico_plugin.payment.verification_is_not_possible_card_holder_chosen_to_register_later_on_system",
33
      5=>"eres_sylius_iyzico_plugin.payment.verification_is_not_possbile",
34
      6=>"eres_sylius_iyzico_plugin.payment.3d_secure_error",
35
      7=>"eres_sylius_iyzico_plugin.payment.system_error",
36
      8=>"eres_sylius_iyzico_plugin.payment.unknown_card"
37
    ];
38
39
    /**
40
     * @var IyzicoBridgeInterface
41
     */
42
    private $iyzicoBridge;
43
44
    /**
45
     * @var FormFactoryInterface
46
     */
47
    protected $formFactory;
48
49
    /**
50
     * @var RequestStack
51
     */
52
    private $requestStack;
53
54
    private $threeds;
55
56
    public function __construct(
57
        IyzicoBridgeInterface $iyzicoBridge,
58
        FormFactoryInterface $formFactory,
59
        RequestStack $requestStack
60
    )
61
    {
62
        $this->iyzicoBridge = $iyzicoBridge;
63
        $this->formFactory = $formFactory;
64
        $this->requestStack = $requestStack;
65
    }
66
67
    public function setApi($api)
68
    {
69
        if (false === is_array($api)) {
70
            throw new UnsupportedApiException('Not supported. Expected to be set as array.');
71
        }
72
73
        $this->iyzicoBridge->setAuthorizationData($api['api_key'], $api['secret_key'], $api['environment']);
74
        $this->threeds = $api['threeds'];
75
    }
76
77
    public function supports($request): bool
78
    {
79
        return
80
            $request instanceof Capture &&
81
            $request->getModel() instanceof \ArrayAccess;
82
    }
83
84
    public function execute($request): void
85
    {
86
        RequestNotSupportedException::assertSupports($this, $request);
87
88
        $model = ArrayObject::ensureArrayObject($request->getModel());
89
90
        $mdStatus = $this->requestStack->getMasterRequest()->request->get('mdStatus');
91
        if ($this->threeds && isset($mdStatus)) {
92
            $status = $this->requestStack->getMasterRequest()->request->get('status');
93
            if ($status === IyzicoBridgeInterface::COMPLETED_STATUS) {
94
                $data = $this->requestStack->getMasterRequest()->request->all();
95
                $data['iyzico_local_code'] = $model['iyzico_local_code'];
96
                $payment = $this->iyzicoBridge->createThreeds($data);
97
                $model['iyzico_status'] = $payment['status'];
98
                $model['iyzico_error_message'] = $payment['error_message'];
99
            } else {
100
                $model['iyzico_status'] = IyzicoBridgeInterface::FAILED_STATUS;
101
                $model['iyzico_error_message'] = isset(self::ERRORS[$mdStatus])?self::ERRORS[$mdStatus]:self::ERRORS[-1];
102
            }
103
104
            return;
105
        }
106
107
        $form = $this->createCreditCardForm();
108
        $form->handleRequest($this->requestStack->getMasterRequest());
109
        if ($form->isSubmitted()) {
110
            /** @var CreditCardInterface $card */
111
            $card = $form->getData();
112
            $card->secure();
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\CreditCardInterface::secure() has been deprecated: the method will be removed in v2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

112
            /** @scrutinizer ignore-deprecated */ $card->secure();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
113
114
            if ($form->isValid()) {
115
                $model['iyzico_card'] = $card;
116
                $model['iyzico_threeds'] = $this->threeds;
117
                $payment = $this->iyzicoBridge->create($model);
118
                $model['iyzico_status'] = $payment['status'];
119
                $model['iyzico_error_message'] = $payment['error_message'];
120
121
                if ($model['iyzico_threeds'] && $model['iyzico_status'] === 'success') {
122
                    $model['iyzico_html_content'] = $payment['html_content'];
123
                    throw new HttpResponse($payment['html_content'], 200, [
124
                        'Cache-Control' => 'no-store, no-cache, max-age=0, post-check=0, pre-check=0',
125
                        'X-Status-Code' => 200,
126
                        'Pragma' => 'no-cache',
127
                    ]);
128
                }
129
                return;
130
            }
131
        }
132
133
        $this->gateway->execute(
134
            $renderTemplate =
135
                new RenderTemplate("@EresSyliusIyzicoPlugin\credit_card_form.html.twig", [
136
                    'form' => $form->createView(),
137
                ]));
138
139
        throw new HttpResponse($renderTemplate->getResult(), 200, [
140
            'Cache-Control' => 'no-store, no-cache, max-age=0, post-check=0, pre-check=0',
141
            'X-Status-Code' => 200,
142
            'Pragma' => 'no-cache',
143
        ]);
144
145
146
    }
147
148
    /**
149
     * @return FormInterface
150
     */
151
    protected function createCreditCardForm()
152
    {
153
        return $this->formFactory->create(CreditCardType::class);
154
    }
155
}
156