Completed
Push — master ( 06cc78...f70c47 )
by PROSPER
01:59
created

Paystack::genTranxRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Unicodeveloper\Paystack;
4
5
use GuzzleHttp\Client;
6
7
class Paystack {
8
9
    /**
10
     * Transaction Verification Successful
11
     */
12
    const VS = 'Verification successful';
13
14
    /**
15
     *  Invalid Transaction reference
16
     */
17
    const ITF = "Invalid transaction reference";
18
19
    /**
20
     * Issue Secret Key from your Paystack Dashboard
21
     * @var mixed
22
     */
23
    protected $secretKey;
24
25
    /**
26
     * Instance of Client
27
     * @var object
28
     */
29
    protected $client;
30
31
    /**
32
     *  Response from requests made to Paystack
33
     * @var mixed
34
     */
35
    protected $response;
36
37
    /**
38
     * Paystack API base Url
39
     * @var string
40
     */
41
    protected $baseUrl;
42
43
    /**
44
     * Authorization Url - Paystack payment page
45
     * @var string
46
     */
47
    protected $authorizationUrl;
48
49
    public function __construct()
50
    {
51
        $this->setKey();
52
        $this->setBaseUrl();
53
        $this->setRequestOptions();
54
    }
55
56
    /**
57
     * Get Base Url from Paystack config file
58
     */
59
    public function setBaseUrl()
60
    {
61
        $this->baseUrl = config('paystack.paymentUrl');
62
    }
63
64
    /**
65
     * Get secret key from Paystack config file
66
     * @return  void
67
     */
68
    public function setKey()
69
    {
70
        $this->secretKey = config('paystack.secretKey');
71
    }
72
73
    /**
74
     * Set options for making the Client request
75
     * @return  void
76
     */
77
    private function setRequestOptions()
78
    {
79
        $authBearer = 'Bearer '. $this->secretKey;
80
81
        $this->client = new Client(['base_uri' => $this->baseUrl]);
82
83
        $this->client->setDefaultOption('headers', [
84
            'Authorization' => $authBearer,
85
            'Content-Type'  => 'application/json',
86
            'Accept'        => 'application/json'
87
        ]);
88
    }
89
90
    /**
91
     * Initiate a payment request to Paystack
92
     * @return Unicodeveloper\Paystack\Paystack
93
     */
94
    public function makePaymentRequest()
95
    {
96
        $this->setResponse('/transaction/initialize');
97
98
        return $this;
99
    }
100
101
    /**
102
     * Make the client request and get the response
103
     * @param string $relativeUrl
104
     * @return Unicodeveloper\Paystack\Paystack
105
     */
106
    public function setResponse($relativeUrl)
107
    {
108
        $data = [
109
            "amount" => intval(request()->amount),
110
            "reference" => request()->reference,
111
            "email" => request()->email
112
        ];
113
114
        $this->response = $this->client->post($this->baseUrl . $relativeUrl, [
115
            'body' => json_encode($data)
116
        ]);
117
118
        return $this;
119
    }
120
121
    /**
122
     * Get the authorization url from the callback response
123
     * @return Unicodeveloper\Paystack\Paystack
124
     */
125
    public function getAuthorizationUrl()
126
    {
127
        $this->makePaymentRequest();
128
129
        $this->url = $this->response->json()["data"]["authorization_url"];
0 ignored issues
show
Bug introduced by
The property url 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...
130
131
        return $this;
132
    }
133
134
    /**
135
     * Hit Paystack Gateway to Verify that the transaction is valid
136
     * @return void
137
     */
138
    private function verifyTransactionAtGateway()
139
    {
140
        $transactionRef = request()->query('trxref');
141
142
        $relativeUrl = "/transaction/verify/{$transactionRef}";
143
144
        $this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
145
    }
146
147
    /**
148
     * True or false condition whether the transaction is verified
149
     * @return boolean
150
     */
151
    public function isTransactionVerificationValid()
152
    {
153
        $this->verifyTransactionAtGateway();
154
155
        $result = $this->response->json()["message"];
156
157
        switch($result)
158
        {
159
            case self::VS:
160
                $validate = true;
161
                break;
162
            case self:ITF:
163
                $validate = false;
164
                break;
165
            default:
166
                $validate = false;
167
                break;
168
        }
169
170
        return $validate;
171
    }
172
173
    /**
174
     * Get Payment details if the transaction was verified successfully
175
     * @throws Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException
176
     * @return json
177
     */
178
    public function getPaymentData()
179
    {
180
        if($this->isTransactionVerificationValid()) {
181
            return $this->response->json();
182
        } else {
183
            throw new PaymentVerificationFailedException("Invalid Transaction Reference");
184
        }
185
    }
186
187
    /**
188
     * Fluent method to redirect to Paystack Payment Page
189
     * @return Illuminate\Support\Redirect
190
     */
191
    public function redirectNow()
192
    {
193
        return redirect($this->url);
194
    }
195
196
    /**
197
     * Get Access code from transaction callback respose
198
     * @return string
199
     */
200
    public function getAccessCode()
201
    {
202
        return $this->response->json()["data"]["access_code"];
203
    }
204
205
    /**
206
     * Generate a Unique Transaction Reference
207
     * @return string
208
     */
209
    public function genTranxRef()
210
    {
211
        return TransRef::getHashedToken();
212
    }
213
214
}