CreditCardSecure::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Heidelpay\Business\Payment;
9
10
use Generated\Shared\Transfer\AddressTransfer;
11
use Generated\Shared\Transfer\CheckoutResponseTransfer;
12
use Generated\Shared\Transfer\QuoteTransfer;
13
use Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpay;
14
use SprykerEco\Zed\Heidelpay\Business\Payment\CreditCard\Registration\RegistrationWriterInterface;
15
use SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface;
16
use SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithPostSaveOrderInterface;
17
use SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithPreSavePaymentInterface;
18
use SprykerEco\Zed\Heidelpay\HeidelpayConfig;
19
20
class CreditCardSecure extends BaseHeidelpayPaymentMethod implements
21
    PaymentWithPostSaveOrderInterface,
22
    PaymentWithPreSavePaymentInterface
23
{
24
    /**
25
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\CreditCard\Registration\RegistrationWriterInterface
26
     */
27
    protected $registrationWriter;
28
29
    /**
30
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface $transactionLogManager
31
     * @param \SprykerEco\Zed\Heidelpay\HeidelpayConfig $config
32
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\CreditCard\Registration\RegistrationWriterInterface $registrationWriter
33
     */
34
    public function __construct(
35
        TransactionLogReaderInterface $transactionLogManager,
36
        HeidelpayConfig $config,
37
        RegistrationWriterInterface $registrationWriter
38
    ) {
39
        parent::__construct($transactionLogManager, $config);
40
41
        $this->registrationWriter = $registrationWriter;
42
    }
43
44
    /**
45
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
46
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
47
     *
48
     * @return void
49
     */
50
    public function postSaveOrder(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): void
51
    {
52
        $authorizeTransactionLogTransfer = $this->findOrderAuthorizeTransactionLog(
53
            $checkoutResponseTransfer->getSaveOrder()->getIdSalesOrder()
54
        );
55
56
        if ($this->isAuthorizeTransactionSentSuccessfully($authorizeTransactionLogTransfer) &&
57
            $this->hasCustomerRegisteredShipmentAddress($quoteTransfer->getShippingAddress())
58
        ) {
59
            $this->updateCreditCardRegistrationWithAddressId($quoteTransfer);
60
        }
61
62
        $redirectUrl = $this->getCheckoutRedirectUrlFromAuthorizeTransactionLog(
63
            $checkoutResponseTransfer->getSaveOrder()->getIdSalesOrder()
64
        );
65
66
        $this->setExternalRedirect($redirectUrl, $checkoutResponseTransfer);
67
    }
68
69
    /**
70
     * @param \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpay $paymentEntity
71
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
72
     *
73
     * @return void
74
     */
75
    public function addDataToPayment(SpyPaymentHeidelpay $paymentEntity, QuoteTransfer $quoteTransfer): void
76
    {
77
        $registrationId = $this->getRegistrationIdFromQuote($quoteTransfer);
78
        $paymentEntity->setIdPaymentRegistration($registrationId);
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\AddressTransfer $shippingAddress
83
     *
84
     * @return bool
85
     */
86
    protected function hasCustomerRegisteredShipmentAddress(AddressTransfer $shippingAddress): bool
87
    {
88
        return $shippingAddress->getIdCustomerAddress() !== null;
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
93
     *
94
     * @return void
95
     */
96
    protected function updateCreditCardRegistrationWithAddressId(QuoteTransfer $quoteTransfer): void
97
    {
98
        $this->registrationWriter->updateRegistrationWithAddressIdFromQuote($quoteTransfer);
99
    }
100
101
    /**
102
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
103
     *
104
     * @return string
105
     */
106
    protected function getRegistrationIdFromQuote(QuoteTransfer $quoteTransfer): string
107
    {
108
        return $quoteTransfer
109
            ->getPayment()
110
            ->getHeidelpayCreditCardSecure()
111
            ->getSelectedRegistration()
112
            ->getRegistrationNumber();
113
    }
114
}
115