Passed
Branch [email protected] (aae97d)
by Bruno
10:40
created

WalletPaymentDataRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Request;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use Getnet\PaymentMagento\Gateway\Config\ConfigWallet;
14
use Getnet\PaymentMagento\Gateway\SubjectReader;
15
use InvalidArgumentException;
16
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
17
use Magento\Payment\Gateway\Request\BuilderInterface;
18
use Magento\Payment\Model\InfoInterface;
19
20
/**
21
 * Class Wallet Payment Data Request - Payment data structure for Credit Card.
22
 */
23
class WalletPaymentDataRequest implements BuilderInterface
24
{
25
    /**
26
     * Payment - Block name.
27
     */
28
    public const PAYMENTS = 'payments';
29
30
    /**
31
     * Type - Block name.
32
     */
33
    public const TYPE = 'type';
34
35
    /**
36
     * Credit - Block name.
37
     */
38
    public const CREDIT = 'credit';
39
40
    /**
41
     * Transaction Type - Block name.
42
     */
43
    public const TRANSACTION_TYPE = 'transaction_type';
44
45
    /**
46
     * Number Installment - Block name.
47
     */
48
    public const NUMBER_INSTALLMENTS = 'number_installments';
49
50
    /**
51
     * Statement descriptor - Invoice description.
52
     */
53
    public const SOFT_DESCRIPTOR = 'soft_descriptor';
54
55
    /**
56
     * Credit Holder Phone - Block name.
57
     */
58
    public const CARDHOLDER_MOBILE = 'cardholder_mobile';
59
60
    /**
61
     * @var SubjectReader
62
     */
63
    protected $subjectReader;
64
65
    /**
66
     * @var Config
67
     */
68
    protected $config;
69
70
    /**
71
     * @var ConfigCc
72
     */
73
    protected $configCc;
74
75
    /**
76
     * @var ConfigWallet
77
     */
78
    protected $configWallet;
79
80
    /**
81
     * @param SubjectReader $subjectReader
82
     * @param Config        $config
83
     * @param ConfigCc      $configCc
84
     * @param ConfigWallet  $configWallet
85
     */
86
    public function __construct(
87
        SubjectReader $subjectReader,
88
        Config $config,
89
        ConfigCc $configCc,
90
        ConfigWallet $configWallet
91
    ) {
92
        $this->subjectReader = $subjectReader;
93
        $this->config = $config;
94
        $this->configCc = $configCc;
95
        $this->configWallet = $configWallet;
96
    }
97
98
    /**
99
     * Build.
100
     *
101
     * @param array $buildSubject
102
     */
103
    public function build(array $buildSubject)
104
    {
105
        if (!isset($buildSubject['payment'])
106
            || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
107
        ) {
108
            throw new InvalidArgumentException('Payment data object should be provided');
109
        }
110
111
        $paymentDO = $buildSubject['payment'];
112
        $payment = $paymentDO->getPayment();
113
        $order = $paymentDO->getOrder();
114
        $storeId = $order->getStoreId();
115
        $result = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
116
117
        $result = $this->getDataPaymetWallet($payment, $storeId);
118
119
        return $result;
120
    }
121
122
    /**
123
     * Data for CC.
124
     *
125
     * @param InfoInterface $payment
126
     * @param int           $storeId
127
     *
128
     * @return array
129
     */
130
    public function getDataPaymetWallet($payment, $storeId)
131
    {
132
        $instruction = [];
133
        $cardType = $payment->getAdditionalInformation('wallet_card_type');
134
        $phone = $payment->getAdditionalInformation('wallet_payer_phone');
135
136
        if ($cardType === 'credit') {
137
            $installment = $payment->getAdditionalInformation('cc_installments') ?: 1;
138
            $transactionType = $this->configCc->getTypeInterestByInstallment($installment, $storeId);
139
140
            $instruction[self::PAYMENTS][] = [
141
                self::TYPE                 => 'CREDIT',
142
                self::SOFT_DESCRIPTOR      => $this->config->getStatementDescriptor($storeId),
143
                self::TRANSACTION_TYPE     => $transactionType,
144
                self::NUMBER_INSTALLMENTS  => $installment,
145
                self::CARDHOLDER_MOBILE    => preg_replace('/[^0-9]/', '', $phone),
146
            ];
147
148
            return $instruction;
149
        }
150
151
        if ($cardType === 'debit') {
152
            $instruction[self::PAYMENTS][] = [
153
                self::TYPE                 => 'DEBIT',
154
                self::SOFT_DESCRIPTOR      => $this->config->getStatementDescriptor($storeId),
155
                self::CARDHOLDER_MOBILE    => preg_replace('/[^0-9]/', '', $phone),
156
            ];
157
158
            return $instruction;
159
        }
160
161
        return $instruction;
162
    }
163
}
164