TransactionController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 44
c 4
b 0
f 0
dl 0
loc 78
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A triggerEventsOnSuccess() 0 18 3
A statusUpdateAction() 0 46 3
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Communication\Controller;
9
10
use Generated\Shared\Transfer\PayoneTransactionStatusUpdateTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ionStatusUpdateTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
13
use SprykerEco\Shared\Payone\PayoneConstants;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @method \SprykerEco\Zed\Payone\Business\PayoneFacadeInterface getFacade()
18
 * @method \SprykerEco\Zed\Payone\Communication\PayoneCommunicationFactory getFactory()
19
 * @method \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface getQueryContainer()
20
 * @method \SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface getRepository()
21
 */
22
class TransactionController extends AbstractController
23
{
24
    /**
25
     * @param \Symfony\Component\HttpFoundation\Request $request
26
     *
27
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
28
     */
29
    public function statusUpdateAction(Request $request)
30
    {
31
        //Payone always sends status updates in ISO-8859-1. We transform them to utf8.
32
        $requestParameters = $request->request->all();
33
34
        $map = [
35
            // TransferObject (internal) => POST request (external)
36
            'key' => 'key',
37
            'aid' => 'aid',
38
            'mode' => 'mode',
39
            'customerid' => null,
40
            'portalid' => 'portalid',
41
            'sequencenumber' => 'sequencenumber',
42
            'txaction' => 'txaction',
43
            'receivable' => 'receivable',
44
            'price' => 'price',
45
            'balance' => 'balance',
46
            'currency' => 'currency',
47
            'txid' => 'txid',
48
            'userid' => 'userid',
49
            'txtime' => 'txtime',
50
            'clearingtype' => 'clearingtype',
51
            'reference' => 'reference',
52
            'reminderlevel' => 'reminderlevel',
53
        ];
54
55
        $dataArray = [];
56
        foreach ($map as $transferObjectKey => $postDataKey) {
57
            if (!isset($requestParameters[$postDataKey])) {
58
                continue;
59
            }
60
            $dataArray[$transferObjectKey] = utf8_encode($requestParameters[$postDataKey]);
61
        }
62
63
        $payoneTransactionStatusUpdateTransfer = new PayoneTransactionStatusUpdateTransfer();
64
        $payoneTransactionStatusUpdateTransfer->fromArray($dataArray);
65
66
        $payoneTransactionStatusUpdateTransfer = $this->getFacade()->processTransactionStatusUpdate($payoneTransactionStatusUpdateTransfer);
67
68
        $this->triggerEventsOnSuccess($payoneTransactionStatusUpdateTransfer);
69
70
        $callback = function () use ($payoneTransactionStatusUpdateTransfer): void {
71
            echo $payoneTransactionStatusUpdateTransfer->getResponse();
72
        };
73
74
        return $this->streamedResponse($callback);
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\PayoneTransactionStatusUpdateTransfer $payoneTransactionStatusUpdateTransfer
79
     *
80
     * @return void
81
     */
82
    protected function triggerEventsOnSuccess(PayoneTransactionStatusUpdateTransfer $payoneTransactionStatusUpdateTransfer): void
83
    {
84
        if (!$payoneTransactionStatusUpdateTransfer->getIsSuccess()) {
85
            return;
86
        }
87
88
        $orderItems = SpySalesOrderItemQuery::create()
89
            ->useOrderQuery()
90
            ->useSpyPaymentPayoneQuery()
91
                ->filterByTransactionId($payoneTransactionStatusUpdateTransfer->getTxid())
92
            ->endUse()
93
            ->endUse()
94
            ->find();
95
96
        $this->getFactory()->getOmsFacade()->triggerEvent('PaymentNotificationReceived', $orderItems, []);
97
98
        if ($payoneTransactionStatusUpdateTransfer->getTxaction() === PayoneConstants::PAYONE_TXACTION_APPOINTED) {
99
            $this->getFactory()->getOmsFacade()->triggerEvent('RedirectResponseAppointed', $orderItems, []);
100
        }
101
    }
102
}
103