Completed
Push — master ( 833afb...466e44 )
by Leith
03:05
created

PurchaseRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 3
dl 0
loc 155
ccs 0
cts 127
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getHmacKey() 0 4 1
A setHmacKey() 0 4 1
A getHmacKeyID() 0 4 1
A setHmacKeyID() 0 4 1
A getScpId() 0 4 1
A setScpId() 0 4 1
A setItems() 0 8 3
A getSiteId() 0 4 1
A setSiteId() 0 4 1
B getData() 0 78 3
A sendData() 0 6 1
A getSoapClient() 0 4 1
A getEndpoint() 0 4 2
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' => (int) round(
99
                            $item->getPrice() * pow(10, $this->getCurrencyDecimalPlaces())
100
                        ),
101
                        'reference' => substr($item->getName(), 0, 50),
102
                    ),
103
                    'quantity' => $item->getQuantity(),
104
                    'lgItemDetails' => array(
105
                        'additionalReference' => $item->getAdditionalReference(),
106
                        'fundCode' => $item->getFundCode(),
107
                        'narrative' => $item->getNarrative(),
108
                    ),
109
                    'lineId' => $itemIndex + 1
110
                );
111
            }
112
        }
113
114
        return array(
115
            'credentials' => array(
116
                'subject' => array(
117
                    'subjectType' => $subjectType,
118
                    'identifier' => $this->getScpId(),
119
                    'systemCode' => 'SCP'
120
                ),
121
                'requestIdentification' => array(
122
                    'uniqueReference' => $uniqueReference,
123
                    'timeStamp' => $timeStamp
124
                ),
125
                'signature' => array(
126
                    'algorithm' => $algorithm,
127
                    'hmacKeyID' => $this->getHmacKeyId(),
128
                    'digest' => $digest
129
                )
130
            ),
131
            'requestType' => 'payOnly',
132
            'requestId' => $this->getTransactionId(),
133
            'routing' => array(
134
                'returnUrl' => $this->getReturnUrl(),
135
                'backUrl' => $this->getCancelUrl(),
136
                'siteId' => $this->getSiteId(),
137
                'scpId' => $this->getScpId()
138
            ),
139
            'panEntryMethod' => 'ECOM',
140
            'sale' => array(
141
                'items' => $saleItems,
142
                'saleSummary' => array(
143
                    'description' => substr($this->getDescription(), 0, 100),
144
                    'amountInMinorUnits' => $this->getAmountInteger()
145
                )
146
            )
147
        );
148
    }
149
150
    public function sendData($data)
151
    {
152
        $responseData = $this->getSoapClient()->scpSimpleInvoke($data);
153
154
        return $this->response = new PurchaseResponse($this, $responseData);
155
    }
156
157
    protected function getSoapClient()
158
    {
159
        return new SoapClient($this->getEndpoint(), array());
160
    }
161
162
    public function getEndpoint()
163
    {
164
        return $this->getTestMode() ? $this->testWSDL : $this->liveWSDL;
165
    }
166
}
167