PurchaseRequest::setMerchantSession()   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\Paystation\Message;
4
5
use Omnipay\Common\Message\AbstractRequest;
6
7
/**
8
 * Paystation Purchase Request
9
 *
10
 * Documentation:
11
 * @link http://www.paystation.co.nz/cms_show_download.php?id=41
12
 */
13
class PurchaseRequest extends AbstractRequest
14
{
15
16
    protected $endpoint = 'https://www.paystation.co.nz/direct/paystation.dll';
17
18 13
    public function getPaystationId()
19
    {
20 13
        return $this->getParameter('paystationId');
21
    }
22
23 16
    public function setPaystationId($value)
24
    {
25 16
        return $this->setParameter('paystationId', $value);
26
    }
27
28 7
    public function getGatewayId()
29
    {
30 7
        return $this->getParameter('gatewayId');
31
    }
32
33 16
    public function setGatewayId($value)
34
    {
35 16
        return $this->setParameter('gatewayId', $value);
36
    }
37
38 5
    public function getMerchantSession()
39
    {
40 5
        return $this->getParameter('merchantSession');
41
    }
42
43 5
    public function setMerchantSession($value)
44
    {
45 5
        return $this->setParameter('merchantSession', $value);
46
    }
47
48 10
    public function getHmacKey()
49
    {
50 10
        return $this->getParameter('hmacKey');
51
    }
52
53 1
    public function setHmacKey($value)
54
    {
55 1
        return $this->setParameter('hmacKey', $value);
56
    }
57
58 5
    protected function getBaseData()
59
    {
60 5
        $data = array();
61 5
        $data['paystation'] = '_empty';
62 5
        $data['pstn_pi'] = $this->getPaystationId();
63 5
        $data['pstn_gi'] = $this->getGatewayId();
64 5
        $merchantSession = $this->getMerchantSession();
65 5
        if (!$merchantSession) {
66 1
            $merchantSession = uniqid();
67
        }
68 5
        $data['pstn_ms'] = $merchantSession;
69
70 5
        return $data;
71
    }
72
73 5
    public function getData()
74
    {
75 5
        $this->validate('amount', 'paystationId', 'gatewayId');
76
        //required
77 5
        $data = $this->getBaseData();
78 5
        $data['pstn_am'] = $this->getAmountInteger();
79
        //optional
80 4
        $data['pstn_cu'] = $this->getCurrency();
81 4
        $data['pstn_tm'] = $this->getTestMode() ? 'T' : null;
82 4
        $data['pstn_mc'] = $this->getCustomerDetails();
83 4
        $data['pstn_mr'] = $this->getTransactionId();
84 4
        if ($this->getHmacKey()) {
85 1
            if ($this->getReturnUrl()) {
86
                $data['pstn_du'] = urlencode($this->getReturnUrl());
87
            }
88 4
            if ($this->getNotifyUrl()) {
89
                $data['pstn_dp'] = urlencode($this->getNotifyUrl());
90
            }
91 4
        }
92
93 4
        return $data;
94 4
    }
95
96 4 View Code Duplication
    public function sendData($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $postdata = http_build_query($data);
99
        $httpResponse = $this->httpClient->request('POST', $this->getEndPoint($postdata), [], $postdata);
100
101
        return $this->response = new PurchaseResponse($this, $httpResponse->getBody()->getContents());
102
    }
103 4
104
    /**
105 4
     * Package up, and limit the customer details.
106 4
     * @return string customer details
107 4
     */
108 4
    protected function getCustomerDetails()
109 4
    {
110 4
        $card = $this->getCard();
111 4
        return substr(implode(",", array_filter(array(
112 4
            $card->getName(),
113 4
            $card->getCompany(),
114 4
            $card->getEmail(),
115 4
            $card->getPhone(),
116 4
            $card->getAddress1(),
117
            $card->getAddress2(),
118
            $card->getCity(),
119
            $card->getState(),
120
            $card->getCountry()
121
        ))), 0, 255);
122
    }
123
124 10
    /**
125
     * Get the endpoint for this request.
126 10
     * Will include hmac data in GET query, if necessary.
127 10
     * @return string endpoint url
128 1
     */
129 1
    protected function getEndPoint($postdata)
130 1
    {
131 1
        $url = $this->endpoint;
132 1
        if ($this->getHmacKey()) {
133
            $qd = array();
134
            $timestamp = time();
135 10
            $qd['pstn_HMACTimestamp'] = $timestamp;
136
            $qd['pstn_HMAC'] = $this->getHmac($timestamp, $postdata);
137
            $url .= '?'.http_build_query($qd);
138
        }
139
140
        return $url;
141
    }
142
143
    /**
144
     * Generate the hmac hash to be passed in endpoint url
145 1
     *
146
     * Code modified from
147 1
     * @link http://www.paystation.co.nz/cms_show_download.php?id=69
148 1
     * @return string hmac
149 1
     */
150 1
    protected function getHmac($timestamp, $postdata)
151
    {
152 1
        $authenticationKey = $this->getHmacKey();
153
        $hmacWebserviceName = 'paystation'; //webservice identification.
154
        $hmacBody = pack('a*', $timestamp).pack('a*', $hmacWebserviceName).pack('a*', $postdata);
155
        $hmacHash = hash_hmac('sha512', $hmacBody, $authenticationKey);
156
157
        return $hmacHash;
158
    }
159
}
160