DirectDebit   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 31
dl 0
loc 115
c 2
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCheckoutRedirectUrl() 0 10 2
A postSaveOrder() 0 22 4
A updateRegistrationWithAddressId() 0 3 1
A getRegistrationIdFromQuote() 0 7 1
A addDataToPayment() 0 4 1
A __construct() 0 8 1
A hasCustomerRegisteredShipmentAddress() 0 3 1
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\HeidelpayTransactionLogTransfer;
13
use Generated\Shared\Transfer\QuoteTransfer;
14
use Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpay;
15
use SprykerEco\Zed\Heidelpay\Business\Payment\DirectDebit\Registration\DirectDebitRegistrationWriterInterface;
16
use SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface;
17
use SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithPostSaveOrderInterface;
18
use SprykerEco\Zed\Heidelpay\Business\Payment\Type\PaymentWithPreSavePaymentInterface;
19
use SprykerEco\Zed\Heidelpay\HeidelpayConfig;
20
21
class DirectDebit extends BaseHeidelpayPaymentMethod implements
22
    PaymentWithPostSaveOrderInterface,
23
    PaymentWithPreSavePaymentInterface
24
{
25
    /**
26
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\DirectDebit\Registration\DirectDebitRegistrationWriterInterface
27
     */
28
    protected $registrationWriter;
29
30
    /**
31
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface $transactionLogManager
32
     * @param \SprykerEco\Zed\Heidelpay\HeidelpayConfig $config
33
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\DirectDebit\Registration\DirectDebitRegistrationWriterInterface $registrationWriter
34
     */
35
    public function __construct(
36
        TransactionLogReaderInterface $transactionLogManager,
37
        HeidelpayConfig $config,
38
        DirectDebitRegistrationWriterInterface $registrationWriter
39
    ) {
40
        parent::__construct($transactionLogManager, $config);
41
42
        $this->registrationWriter = $registrationWriter;
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
47
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
48
     *
49
     * @return void
50
     */
51
    public function postSaveOrder(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): void
52
    {
53
        $debitOnRegistrationTransactionLogTransfer = $this->transactionLogManager
54
            ->findDebitOnRegistrationTransactionLogByIdSalesOrder(
55
                $checkoutResponseTransfer->getSaveOrder()->getIdSalesOrder()
56
            );
57
58
        if ($debitOnRegistrationTransactionLogTransfer === null) {
59
            $checkoutResponseTransfer->setIsSuccess(false);
60
61
            return;
62
        }
63
64
        if ($debitOnRegistrationTransactionLogTransfer->getHeidelpayResponse()->getIsSuccess()
65
            && $this->hasCustomerRegisteredShipmentAddress($quoteTransfer->getShippingAddress())
66
        ) {
67
            $this->updateRegistrationWithAddressId($quoteTransfer);
68
        }
69
70
        $redirectUrl = $this->getCheckoutRedirectUrl($debitOnRegistrationTransactionLogTransfer);
71
72
        $this->setExternalRedirect($redirectUrl, $checkoutResponseTransfer);
73
    }
74
75
    /**
76
     * @param \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpay $paymentEntity
77
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
78
     *
79
     * @return void
80
     */
81
    public function addDataToPayment(SpyPaymentHeidelpay $paymentEntity, QuoteTransfer $quoteTransfer): void
82
    {
83
        $registrationId = $this->getRegistrationIdFromQuote($quoteTransfer);
84
        $paymentEntity->setIdPaymentRegistration($registrationId);
85
    }
86
87
    /**
88
     * @param \Generated\Shared\Transfer\AddressTransfer $shippingAddress
89
     *
90
     * @return bool
91
     */
92
    protected function hasCustomerRegisteredShipmentAddress(AddressTransfer $shippingAddress): bool
93
    {
94
        return $shippingAddress->getIdCustomerAddress() !== null;
95
    }
96
97
    /**
98
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
99
     *
100
     * @return void
101
     */
102
    protected function updateRegistrationWithAddressId(QuoteTransfer $quoteTransfer): void
103
    {
104
        $this->registrationWriter->updateDirectDebitRegistration($quoteTransfer);
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
109
     *
110
     * @return string
111
     */
112
    protected function getRegistrationIdFromQuote(QuoteTransfer $quoteTransfer): string
113
    {
114
        return $quoteTransfer
115
            ->getPayment()
116
            ->getHeidelpayDirectDebit()
117
            ->getSelectedRegistration()
118
            ->getRegistrationUniqueId();
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer $transactionLogTransfer
123
     *
124
     * @return string
125
     */
126
    protected function getCheckoutRedirectUrl(HeidelpayTransactionLogTransfer $transactionLogTransfer): string
127
    {
128
        if ($transactionLogTransfer->getHeidelpayResponse()->getIsSuccess()) {
129
            return $transactionLogTransfer->getHeidelpayResponse()->getPaymentFormUrl();
130
        }
131
132
        $errorCode = $transactionLogTransfer->getHeidelpayResponse()->getError()->getCode();
133
        $paymentFailedUrl = $this->config->getYvesCheckoutPaymentFailedUrl();
134
135
        return sprintf($paymentFailedUrl, $errorCode);
136
    }
137
}
138