Completed
Push — master ( ec2a5c...2ca1c6 )
by Leith
28s queued 12s
created

WebhookNotification::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Omnipay\Paystation\Message;
4
5
use DOMDocument;
6
use Omnipay\Common\Exception\InvalidRequestException;
7
use Omnipay\Common\Http\ClientInterface;
8
use Omnipay\Common\Message\AbstractRequest;
9
use Omnipay\Common\Message\NotificationInterface;
10
use Omnipay\Common\Message\RequestInterface;
11
use Omnipay\Common\Message\ResponseInterface;
12
use Symfony\Component\HttpFoundation\Request as HttpRequest;
13
14
/**
15
 * Accept an incoming notification (a webhook)
16
 *
17
 * @see https://docs.paystation.co.nz/#post-response
18
 */
19
class WebhookNotification extends AbstractRequest implements NotificationInterface, ResponseInterface
20
{
21
    /**
22
     * The data contained in the response.
23
     *
24
     * @var mixed
25
     */
26
    protected $data;
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function __construct(ClientInterface $httpClient, HttpRequest $httpRequest)
32
    {
33
        parent::__construct($httpClient, $httpRequest);
34
        // fetch POST stream directly
35
        $responseDom = new DOMDocument();
36
        $parsed = $responseDom->loadXML($httpRequest->getContent(), LIBXML_NOWARNING | LIBXML_NOERROR);
37
        if ($parsed === false) {
38
            $error = libxml_get_last_error();
39
            throw new InvalidRequestException($error->message);
40
        }
41
        $this->data = simplexml_import_dom($responseDom);
42
    }
43
44
    /**
45
     * ResponseInterface implemented so that we can return self here for any legacy support that uses send()
46
     */
47
    public function sendData($data)
48
    {
49
        return $this;
50
    }
51
52
    /**
53
     * Get the authorisation code if available.
54
     *
55
     * @return null|string
56
     */
57
    public function getTransactionReference()
58
    {
59
        return isset($this->data->TransactionID) ? (string)$this->data->TransactionID : null;
60
    }
61
62
    /**
63
     * Was the transaction successful?
64
     *
65
     * @return string Transaction status, one of {@link NotificationInterface::STATUS_COMPLETED},
66
     * {@link NotificationInterface::STATUS_PENDING}, or {@link NotificationInterface::STATUS_FAILED}.
67
     */
68
    public function getTransactionStatus()
69
    {
70
        if (!isset($this->data->PaystationErrorCode)) {
71
            return NotificationInterface::STATUS_FAILED;
72
        }
73
        if ((string)$this->data->PaystationErrorCode == '0') {
74
            return NotificationInterface::STATUS_COMPLETED;
75
        }
76
        if (isset($this->data->Authentication->auth_Status)
77
            && (string)$this->data->Authentication->auth_Status == 'P'
78
        ) {
79
            return NotificationInterface::STATUS_PENDING;
80
        }
81
82
        // last resort, assume failure
83
        return NotificationInterface::STATUS_FAILED;
84
    }
85
86
    /**
87
     * Get the merchant response message if available.
88
     *
89
     * Note: POST response doesn't include a <PaystationErrorMessage> node
90
     *
91
     * @return null|string
92
     */
93
    public function getMessage()
94
    {
95
        return null;
96
    }
97
98
    public function getData()
99
    {
100
        return $this->data;
101
    }
102
103
    /**
104
     * Get the original request which generated this response
105
     *
106
     * @return RequestInterface
107
     */
108
    public function getRequest()
109
    {
110
        return $this;
111
    }
112
113
    /**
114
     * Is the response successful?
115
     *
116
     * @return boolean
117
     */
118
    public function isSuccessful()
119
    {
120
        return $this->getTransactionStatus() == NotificationInterface::STATUS_COMPLETED;
121
    }
122
123
    /**
124
     * Does the response require a redirect?
125
     *
126
     * @return boolean
127
     */
128
    public function isRedirect()
129
    {
130
        return false;
131
    }
132
133
    /**
134
     * Is the transaction cancelled by the user?
135
     *
136
     * @return boolean
137
     */
138
    public function isCancelled()
139
    {
140
        return isset($this->data->Authentication->auth_Status)
141
            && (string)$this->data->Authentication->auth_Status == 'C';
142
    }
143
144
    /**
145
     * Response code
146
     *
147
     * @return null|string A response code from the payment gateway
148
     */
149
    public function getCode()
150
    {
151
        return isset($this->data->PaystationErrorCode) ? (string)$this->data->PaystationErrorCode : null;
152
    }
153
154
    /**
155
     * Get the card type if available.
156
     *
157
     * @return null|string
158
     */
159
    public function getCardType()
160
    {
161
        return isset($this->data->Cardtype) ? (string)$this->data->Cardtype : null;
162
    }
163
}
164