Completed
Push — master ( 4628a0...e9e553 )
by Leith
09:52
created

WebhookNotification::isSuccessful()   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\BPOINT\Message;
4
5
use Omnipay\Common\Http\ClientInterface;
6
use Omnipay\Common\Message\AbstractRequest;
7
use Omnipay\Common\Message\NotificationInterface;
8
use Omnipay\Common\Message\RequestInterface;
9
use Omnipay\Common\Message\ResponseInterface;
10
use Symfony\Component\HttpFoundation\Request as HttpRequest;
11
12
/**
13
 * Accept an incoming notification (a webhook)
14
 *
15
 * @see https://www.bpoint.com.au/developers/v3/#!#webhooks
16
 */
17
class WebhookNotification extends AbstractRequest implements NotificationInterface, ResponseInterface
18
{
19
    /**
20
     * @inheritDoc
21
     */
22
    public function __construct(ClientInterface $httpClient, HttpRequest $httpRequest)
23
    {
24
        parent::__construct($httpClient, $httpRequest);
25
        // fetch POST stream directly
26
        $this->data = json_decode($httpRequest->getContent(), true);
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    /**
30
     * ResponseInterface implemented so that we can return self here for any legacy support that uses send()
31
     */
32
    public function sendData($data)
33
    {
34
        return $this;
35
    }
36
37
    /**
38
     * Get the authorisation code if available.
39
     *
40
     * @return null|string
41
     */
42
    public function getTransactionReference()
43
    {
44
        return isset($this->data['AuthoriseId']) ? $this->data['AuthoriseId'] : null;
45
    }
46
47
    /**
48
     * Was the transaction successful?
49
     *
50
     * @return string Transaction status, one of {@link NotificationInterface::STATUS_COMPLETED},
51
     * {@link NotificationInterface::STATUS_PENDING}, or {@link NotificationInterface::STATUS_FAILED}.
52
     */
53
    public function getTransactionStatus()
54
    {
55
        if (!isset($this->data['ResponseCode'])) {
56
            return NotificationInterface::STATUS_FAILED;
57
        }
58
        if ($this->data['ResponseCode'] == '0') {
59
            return NotificationInterface::STATUS_COMPLETED;
60
        }
61
        if ($this->data['ResponseCode'] == 'P') {
62
            return NotificationInterface::STATUS_PENDING;
63
        }
64
65
        // last resort, assume failure
66
        return NotificationInterface::STATUS_FAILED;
67
    }
68
69
    /**
70
     * Get the merchant response message if available.
71
     *
72
     * @return null|string
73
     */
74
    public function getMessage()
75
    {
76
        return isset($this->data['ResponseText']) ? $this->data['ResponseText'] : null;
77
    }
78
79
    public function getData()
80
    {
81
        return $this->data;
82
    }
83
84
    /**
85
     * Get the original request which generated this response
86
     *
87
     * @return RequestInterface
88
     */
89
    public function getRequest()
90
    {
91
        return $this;
92
    }
93
94
    /**
95
     * Is the response successful?
96
     *
97
     * @return boolean
98
     */
99
    public function isSuccessful()
100
    {
101
        return $this->getTransactionStatus() == NotificationInterface::STATUS_COMPLETED;
102
    }
103
104
    /**
105
     * Does the response require a redirect?
106
     *
107
     * @return boolean
108
     */
109
    public function isRedirect()
110
    {
111
        return false;
112
    }
113
114
    /**
115
     * Is the transaction cancelled by the user?
116
     *
117
     * @return boolean
118
     */
119
    public function isCancelled()
120
    {
121
        return isset($this->data['ResponseCode']) && $this->data['ResponseCode'] == 'C';
122
    }
123
124
    /**
125
     * Response code
126
     *
127
     * @return null|string A response code from the payment gateway
128
     */
129
    public function getCode()
130
    {
131
        return isset($this->data['ResponseCode']) ? $this->data['ResponseCode'] : null;
132
    }
133
134
    /**
135
     * Get the card type if available.
136
     *
137
     * @return null|string
138
     */
139
    public function getCardType()
140
    {
141
        return isset($this->data['CardType']) ? $this->data['CardType'] : null;
142
    }
143
}
144