PurchaseRequest::setTestEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Omnipay\EpsomAdelante\Message;
4
5
/**
6
 * Epsom connector to Adelante Purchase Request
7
 */
8
class PurchaseRequest extends AbstractRequest
9
{
10 4
    public function getChannel()
11
    {
12 4
        return $this->getParameter('channel');
13
    }
14
15 12
    public function setChannel($value)
16
    {
17 12
        return $this->setParameter('channel', $value);
18
    }
19
20 3
    public function getSessionId()
21
    {
22 3
        return $this->getTransactionId();
23
    }
24
    
25 8
    public function setSessionId($value)
26
    {
27 8
        return $this->setTransactionId($value);
28
    }
29
30
    /**
31
     * Override the abstract method to add length requirements
32
     *
33
     * Max 12 alphanum if used normally [auto appends _nnnnnnn], if CALLERID enabled on channel, 20 chars
34
     * No way to query CALLERID, so restricting to 20 instead of 12
35
     *
36
     * @param string|int $value The transaction ID (sessionid) to set for the transaction
37
     */
38 8
    public function setTransactionId($value)
39
    {
40 8
        return parent::setTransactionId(substr($value, 0, 20));
41
    }
42
43 4
    public function getFundCode()
44
    {
45 4
        return $this->getParameter('fundCode');
46
    }
47
48 9
    public function setFundCode($value)
49
    {
50 9
        return $this->setParameter('fundCode', $value);
51
    }
52
53 4
    public function getReturnMethod()
54
    {
55 4
        return $this->getParameter('returnMethod');
56
    }
57
58 12
    public function setReturnMethod($value)
59
    {
60 12
        return $this->setParameter('returnMethod', $value);
61
    }
62
63 4
    public function getSendMail()
64
    {
65 4
        return $this->getParameter('sendMail');
66
    }
67
68 12
    public function setSendMail($value)
69
    {
70 12
        return $this->setParameter('sendMail', $value);
71
    }
72
73 4
    public function getTestEndpoint()
74
    {
75 4
        return $this->getParameter('testEndpoint');
76
    }
77
78 12
    public function setTestEndpoint($value)
79
    {
80 12
        return $this->setParameter('testEndpoint', $value);
81
    }
82
83 3
    public function getLiveEndpoint()
84
    {
85 3
        return $this->getParameter('liveEndpoint');
86
    }
87
88 12
    public function setLiveEndpoint($value)
89
    {
90 12
        return $this->setParameter('liveEndpoint', $value);
91
    }
92
93 2
    public function getData()
94
    {
95 2
        $this->validate('channel', 'transactionId', 'returnUrl');
96 2
        if ($this->getTestMode()) {
97 1
            $this->validate('testEndpoint');
98 1
        } else {
99 1
            $this->validate('liveEndpoint');
100
        }
101
102
        $data = array(
103
            // mandatory fields
104 2
            'channel'      => $this->getChannel(),
105 2
            'sessionid'    => $this->getSessionId(),
106 2
            'returnurl'    => $this->getReturnUrl(),
107
            // optional fields
108 2
            'returnmethod' => $this->getReturnMethod(),
109 2
            'sendMail'     => $this->getSendMail(),
110 2
        );
111
112
        // get and merge customer data
113 2
        $card = $this->getCard();
114 2
        if ($card) {
115
            $data += array(
116 1
                'cfname'    => $card->getFirstName(),
117 1
                'csname'    => $card->getLastName(),
118 1
                'chouse'    => $card->getAddress1(), // "house name or number"
119 1
                'cadd1'     => $card->getAddress2(), // "street/road"
120 1
                'ctown'     => $card->getCity(),
121 1
                'cpostcode' => $card->getPostcode(),
122 1
                'ccountry'  => $card->getCountry(),
123 1
                'ctel'      => $card->getPhone(),
124 1
                'cemail'    => $card->getEmail(),
125
            );
126 1
        }
127
128
        // get and merge line item data
129 2
        $items = $this->getItems();
130 2
        $fundcode = $this->getFundCode();
131 2
        if (empty($items)) {
132 1
            $this->validate('amount', 'fundCode');
133
            $data += array(
134 1
                'amount'   => $this->getAmountInteger(),
135 1
                'fundcode' => $fundcode,
136 1
                'custref1' => $this->getDescription(),
137
            );
138 1
        } else {
139 1
            foreach ($items as $n => $item) {
140 1
                $suffix = $n > 0 ? '_a'.$n : '';
141
142 1
                $itemFundcode = $item->getFundCode();
143 1
                $itemCustref1 = $item->getCustRef1();
144 1
                $itemCustref2 = $item->getCustRef2();
145 1
                $itemCustref3 = $item->getCustRef3();
146 1
                $itemCustref4 = $item->getCustRef4();
147
148
                // amount, fundcode and custref1 are required
149
                $data += array(
150 1
                    'amount'.$suffix      => $item->getQuantity() * $item->getPrice(),
151 1
                    'fundcode'.$suffix    => empty($itemFundcode) ? $fundcode : $itemFundcode,
152 1
                    'custref1'.$suffix    => empty($itemCustref1) ? $item->getName() : $itemCustref1,
153 1
                    'custref2'.$suffix    => empty($itemCustref2) ? '' : $itemCustref2,
154 1
                    'custref3'.$suffix    => empty($itemCustref3) ? '' : $itemCustref3,
155 1
                    'custref4'.$suffix    => empty($itemCustref4) ? '' : $itemCustref4,
156 1
                    'description'.$suffix => substr($item->getDescription(), 0, 255),
157
                );
158 1
            }
159
        }
160
161 2
        return $data;
162
    }
163
164 1
    public function sendData($data)
165
    {
166 1
        return $this->response = new PurchaseResponse($this, $data);
167
    }
168
169 2
    public function getEndpoint()
170
    {
171 2
        return $this->getTestMode() ? $this->getTestEndpoint() : $this->getLiveEndpoint();
172
    }
173
}
174