Completed
Push — master ( 667b31...e64f5b )
by Leith
02:32
created

PurchaseRequest::setHmacKey()   A

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\CapitaPay360\Message;
4
5
use SoapClient;
6
use Omnipay\Common\Message\AbstractRequest;
7
use Omnipay\CapitaPay360\ItemBag;
8
9
/**
10
 * CapitaPay360 Purchase Request
11
 */
12
class PurchaseRequest extends AbstractRequest
13
{
14
    /** @var string */
15
    protected $liveWSDL = 'https://sbs.e-paycapita.com/scp/scpws/scpSimpleClient.wsdl';
16
    /** @var string */
17
    protected $testWSDL = 'https://sbsctest.e-paycapita.com/scp/scpws/scpSimpleClient.wsdl';
18
    /** @var mixed Soap client, added as property for unit testing and error checking */
19
    public $soapClient = null;
20
21 8
    public function getHmacKey()
22
    {
23 8
        return $this->getParameter('hmacKey');
24
    }
25
26 11
    public function setHmacKey($value)
27
    {
28 11
        return $this->setParameter('hmacKey', $value);
29
    }
30
31 8
    public function getHmacKeyID()
32
    {
33 8
        return $this->getParameter('hmacKeyID');
34
    }
35
36 11
    public function setHmacKeyID($value)
37
    {
38 11
        return $this->setParameter('hmacKeyID', $value);
39
    }
40
41 8
    public function getScpId()
42
    {
43 8
        return $this->getParameter('scpId');
44
    }
45
46 11
    public function setScpId($value)
47
    {
48 11
        return $this->setParameter('scpId', $value);
49
    }
50
51 8
    public function getSiteId()
52
    {
53 8
        return $this->getParameter('siteId');
54
    }
55
56 11
    public function setSiteId($value)
57
    {
58 11
        return $this->setParameter('siteId', $value);
59
    }
60
61
    /**
62
     * Override {@see AbstractRequest::setItems()} to provide additional attributes
63
     */
64 1
    public function setItems($items)
65
    {
66 1
        if ($items && !$items instanceof ItemBag) {
67 1
            $items = new ItemBag($items);
68
        }
69
70 1
        return $this->setParameter('items', $items);
71
    }
72
73 4
    public function getData()
74
    {
75 4
        $this->validate('scpId', 'siteId', 'hmacKeyID', 'hmacKey', 'amount', 'currency');
76 4
        $timeStamp = gmdate("YmdHis");
77 4
        $uniqueReference = uniqid('PB');
78 4
        $subjectType = 'CapitaPortal';
79 4
        $algorithm = 'Original';
80 4
        $credentialsToHash = implode('!', array(
81 4
            $subjectType,
82 4
            $this->getScpId(),
83 4
            $uniqueReference,
84 4
            $timeStamp,
85 4
            $algorithm,
86 4
            $this->getHmacKeyId()
87
        ));
88 4
        $key = base64_decode($this->getHmacKey());
89 4
        $hash = hash_hmac('sha256', $credentialsToHash, $key, true);
90 4
        $digest = base64_encode($hash);
91
        
92
        // Create items array to return
93 4
        $saleItems = array();
94 4
        $items = $this->getItems();
95 4
        if ($items) {
96 1
            foreach ($items as $itemIndex => $item) {
97 1
                $saleItems[] = array(
98
                    'itemSummary' => array(
99 1
                        'description' => substr($item->getName(), 0, 100),
100 1
                        'amountInMinorUnits' => (int) round(
101 1
                            $item->getQuantity() * $item->getPrice() * pow(10, $this->getCurrencyDecimalPlaces())
102
                        ),
103 1
                        'reference' => $item->getReference(),
104
                    ),
105 1
                    'quantity' => $item->getQuantity(),
106
                    'lgItemDetails' => array(
107 1
                        'additionalReference' => $item->getAdditionalReference(),
108 1
                        'fundCode' => $item->getFundCode(),
109 1
                        'narrative' => $item->getNarrative(),
110
                    ),
111 1
                    'lineId' => $itemIndex + 1
112
                );
113
            }
114
        }
115
116
        $data = array(
117
            'credentials' => array(
118
                'subject' => array(
119 4
                    'subjectType' => $subjectType,
120 4
                    'identifier' => $this->getScpId(),
121 4
                    'systemCode' => 'SCP'
122
                ),
123
                'requestIdentification' => array(
124 4
                    'uniqueReference' => $uniqueReference,
125 4
                    'timeStamp' => $timeStamp
126
                ),
127
                'signature' => array(
128 4
                    'algorithm' => $algorithm,
129 4
                    'hmacKeyID' => $this->getHmacKeyId(),
130 4
                    'digest' => $digest
131
                )
132
            ),
133 4
            'requestType' => 'payOnly',
134 4
            'requestId' => $this->getTransactionId(),
135
            'routing' => array(
136 4
                'returnUrl' => $this->getReturnUrl(),
137 4
                'backUrl' => $this->getCancelUrl(),
138 4
                'siteId' => $this->getSiteId(),
139 4
                'scpId' => $this->getScpId()
140
            ),
141 4
            'panEntryMethod' => 'ECOM',
142
            'sale' => array(
143 4
                'items' => $saleItems,
144
                'saleSummary' => array(
145 4
                    'description' => substr($this->getDescription(), 0, 100),
146 4
                    'amountInMinorUnits' => $this->getAmountInteger()
147
                )
148
            )
149
        );
150
151
        // add card holder details if available
152 4
        $card = $this->getCard();
153 4
        if ($card) {
154 1
            $data['billing'] = array(
155
                'cardHolderDetails' => array(
156 1
                    'cardHolderName' => $card->getName(),
157
                    'address' => array(
158 1
                        'address1' => $card->getAddress1(),
159 1
                        'address2' => $card->getAddress2(),
160 1
                        'address3' => $card->getCity(),
161 1
                        'country' => $card->getCountry(),
162 1
                        'postcode' => $card->getPostcode(),
163
                    )
164
                )
165
            );
166
        }
167
168 4
        return $data;
169
    }
170
171 1
    public function sendData($data)
172
    {
173 1
        $responseData = $this->getSoapClient()->scpSimpleInvoke($data);
174
175 1
        return $this->response = new PurchaseResponse($this, $responseData);
176
    }
177
178 2
    protected function getSoapClient()
179
    {
180 2
        return $this->soapClient === null ? new SoapClient($this->getEndpoint(), array()) : $this->soapClient;
181
    }
182
183 1
    public function getEndpoint()
184
    {
185 1
        return $this->getTestMode() ? $this->testWSDL : $this->liveWSDL;
186
    }
187
}
188