WebhookNotification::isCancelled()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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