Completed
Push — master ( de0635...69f45f )
by Leith
17:25
created

PurchaseRequest::setItems()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
cc 3
nc 2
nop 1
crap 12
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
19
    public function getHmacKey()
20
    {
21
        return $this->getParameter('hmacKey');
22
    }
23
24
    public function setHmacKey($value)
25
    {
26
        return $this->setParameter('hmacKey', $value);
27
    }
28
29
    public function getHmacKeyID()
30
    {
31
        return $this->getParameter('hmacKeyID');
32
    }
33
34
    public function setHmacKeyID($value)
35
    {
36
        return $this->setParameter('hmacKeyID', $value);
37
    }
38
39
    public function getScpId()
40
    {
41
        return $this->getParameter('scpId');
42
    }
43
44
    public function setScpId($value)
45
    {
46
        return $this->setParameter('scpId', $value);
47
    }
48
49
    public function getSiteID()
50
    {
51
        return $this->getParameter('siteID');
52
    }
53
54
    public function setSiteID($value)
55
    {
56
        return $this->setParameter('siteID', $value);
57
    }
58
59
    /**
60
     * Override {@see AbstractRequest::setItems()} to provide additional attributes
61
     */
62
    public function setItems($items)
63
    {
64
        if ($items && !$items instanceof ItemBag) {
65
            $items = new ItemBag($items);
66
        }
67
68
        return $this->setParameter('items', $items);
69
    }
70
71
    public function getData()
72
    {
73
        $this->validate('scpId', 'siteId', 'hmacKeyID', 'hmacKey', 'amount', 'currency');
74
        $timeStamp = gmdate("YmdHis");
75
        $uniqueReference = uniqid('PB');
76
        $subjectType = 'CapitaPortal';
77
        $algorithm = 'Original';
78
        $credentialsToHash = implode('!', array(
79
            $subjectType,
80
            $this->getScpId(),
81
            $uniqueReference,
82
            $timeStamp,
83
            $algorithm,
84
            $this->getHmacKeyId()
85
        ));
86
        $key = base64_decode($this->getHmacKey());
87
        $hash = hash_hmac('sha256', $credentialsToHash, $key, true);
88
        $digest = base64_encode($hash);
89
        
90
        // Create items array to return
91
        $saleItems = array();
92
        $items = $this->getItems();
93
        if ($items) {
94
            foreach($items as $itemIndex => $item) {
95
                $saleItems[] = array(
96
                    'itemSummary' => array(
97
                        'description' => substr($item->getDescription(), 0, 100),
98
                        'amountInMinorUnits' => $item->getPrice() * pow(10, $this->getCurrencyDecimalPlaces()),
99
                        'reference' => substr($item->getName(), 0, 50),
100
                    ),
101
                    'quantity' => $item->getQuantity(),
102
                    'lgItemDetails' => array(
103
                        'additionalReference' => $item->getAdditionalReference(),
104
                        'fundCode' => $item->getFundCode(),
105
                        'narrative' => $item->getNarrative(),
106
                    ),
107
                    'lineId' => $itemIndex + 1
108
                );
109
            }
110
        }
111
112
        return array(
113
            'credentials' => array(
114
                'subject' => array(
115
                    'subjectType' => $subjectType,
116
                    'identifier' => $this->getScpId(),
117
                    'systemCode' => 'SCP'
118
                ),
119
                'requestIdentification' => array(
120
                    'uniqueReference' => $uniqueReference,
121
                    'timeStamp' => $timeStamp
122
                ),
123
                'signature' => array(
124
                    'algorithm' => $algorithm,
125
                    'hmacKeyID' => $this->getHmacKeyId(),
126
                    'digest' => $digest
127
                )
128
            ),
129
            'requestType' => 'payOnly',
130
            'requestId' => $this->getTransactionId(),
131
            'routing' => array(
132
                'returnUrl' => $this->getReturnUrl(),
133
                'backUrl' => $this->getCancelUrl(),
134
                'siteId' => $this->getSiteId(),
135
                'scpId' => $this->getScpId()
136
            ),
137
            'panEntryMethod' => 'ECOM',
138
            'sale' => array(
139
                'items' => $saleItems,
140
                'saleSummary' => array(
141
                    'description' => substr($this->getDescription(), 0, 100),
142
                    'amountInMinorUnits' => $this->getAmountInteger()
143
                )
144
            )
145
        );
146
    }
147
148
    public function sendData($data)
149
    {
150
        $responseData = $this->getSoapClient()->scpSimpleInvoke($data);
151
152
        return $this->response = new PurchaseResponse($this, $responseData);
153
    }
154
155
    protected function getSoapClient() {
156
        return new SoapClient($this->getEndpoint(), array());
157
    }
158
159
    public function getEndpoint()
160
    {
161
        return $this->getTestMode() ? $this->testWSDL : $this->liveWSDL;
162
    }
163
}
164