Completed
Branch master (ec30fc)
by Kazi Mainuddin
01:42
created

PaymentVerification::sendRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 0
1
<?php
2
namespace Tzsk\Payu;
3
4
5
use Tzsk\Payu\Model\PayuPayment;
6
7
class PaymentVerification
8
{
9
    /**
10
     * Command to Pass to the PayuService.
11
     *
12
     * @var string
13
     */
14
    protected $command = 'verify_payment';
15
16
    /**
17
     * Merchant Key.
18
     *
19
     * @var string|null
20
     */
21
    protected $key = null;
22
23
    /**
24
     * Merchant Salt.
25
     *
26
     * @var string|null
27
     */
28
    protected $salt = null;
29
30
    /**
31
     * Payu Service URL.
32
     *
33
     * @var string
34
     */
35
    protected $url = null;
36
37
    /**
38
     * Original Response.
39
     *
40
     * @var array
41
     */
42
    protected $response = [];
43
44
45
    /**
46
     * PaymentVerification constructor.
47
     *
48
     * @param $txn_id
49
     */
50
    public function __construct($txn_id)
51
    {
52
        $this->txn_id = $txn_id;
0 ignored issues
show
Bug introduced by
The property txn_id 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...
53
        $this->key = config('payu.key');
54
        $this->salt = config('payu.salt');
55
56
        $env = config('payu.env');
57
        if ($env != 'test') {
58
            $env = 'info';
59
        }
60
        $this->url = "https://{$env}.payu.in/merchant/postservice?form=2";
61
    }
62
63
    /**
64
     * Request for Verification Status.
65
     *
66
     * @return $this
67
     */
68
    public function request()
69
    {
70
        $this->sendRequest();
71
72
        $this->updatePayuTransaction();
73
74
        return $this;
75
    }
76
77
    /**
78
     * Simple data accessor.
79
     *
80
     * @return array
81
     */
82
    public function simple()
83
    {
84
        return $this->getSimpleResponseData();
85
    }
86
87
    /**
88
     * Full Data Accessor.
89
     *
90
     * @return array
91
     */
92
    public function full()
93
    {
94
        return $this->getFullResponseData();
95
    }
96
97
    /**
98
     * Get the Request Params for Verification.
99
     *
100
     * @return string
101
     */
102
    public function getVerificationPostFields()
103
    {
104
        $txn_string = implode("|", $this->txn_id);
105
        $hash_str = $this->key  . '|' . $this->command . '|' . $txn_string . '|' . $this->salt ;
106
        $hash = strtolower(hash('sha512', $hash_str));
107
        $params = ['key' => $this->key , 'hash' => $hash , 'var1' => $txn_string, 'command' => $this->command];
108
109
        return http_build_query($params);
110
    }
111
112
    /**
113
     * Set the Verification Response.
114
     *
115
     * @throws \Exception
116
     */
117
    protected function sendRequest()
118
    {
119
        $post_fields = $this->getVerificationPostFields();
120
121
        $curl = curl_init();
122
        curl_setopt($curl, CURLOPT_URL, $this->url);
123
        curl_setopt($curl, CURLOPT_POST, 1);
124
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
125
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
126
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
127
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
128
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
129
        $output = curl_exec($curl);
130
131
        if(curl_errno($curl)) {
132
            throw new \Exception(curl_error($curl));
133
        }
134
        curl_close($curl);
135
136
        $this->response = json_decode($output, 1);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($output, 1) of type * is incompatible with the declared type array of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
137
    }
138
139
    /**
140
     * Update Payu Payments Table Entries.
141
     *
142
     * @return boolean
143
     */
144
    protected function updatePayuTransaction()
145
    {
146
        if (config('payu.driver') != 'database') {
147
            return false;
148
        }
149
150
        foreach ($this->txn_id as $item) {
151
            $payu_payment = PayuPayment::where('txnid', $item)->first();
152
            if ($payu_payment) {
153
                $attributes = $this->response['transaction_details'][$item];
154
                $attributes['status'] = ($this->response['transaction_details'][$item]['status'] == 'success') ?
155
                    ProcessPayment::STATUS_COMPLETED : ProcessPayment::STATUS_FAILED;
156
157
                $payu_payment->fill($attributes)->save();
158
            }
159
        }
160
161
        return true;
162
    }
163
164
    /**
165
     * Get Full Response for user.
166
     *
167
     * @return array
168
     */
169
    protected function getFullResponseData()
170
    {
171 View Code Duplication
        if (empty($this->response['status'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
172
            return $this->full_response = ['status' => false, 'message' => $this->response['msg']];
0 ignored issues
show
Bug introduced by
The property full_response does not seem to exist. Did you mean response?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
173
        }
174
175
        $data = ['status' => true, 'data' => []];
176
        foreach ($this->txn_id as $item) {
177
            $status = ($this->response['transaction_details'][$item]['status'] == 'success') ?
178
                ProcessPayment::STATUS_COMPLETED : ProcessPayment::STATUS_FAILED;
179
180
            $message = @$this->response['transaction_details'][$item]['error_Message'];
181
            if (count($this->response['transaction_details'][$item]) < 3) {
182
                $message = 'Transaction ID not found.';
183
            }
184
185
            $data['data'][$item] = ['status' => $status, 'message' => $message,
186
                'response' => $this->response['transaction_details'][$item]];
187
        }
188
189
        return $data;
190
    }
191
192
    /**
193
     * Get Simple Response for user.
194
     *
195
     * @return array
196
     */
197
    protected function getSimpleResponseData()
198
    {
199 View Code Duplication
        if (empty($this->response['status'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
200
            return ['status' => false, 'message' => $this->response['msg']];
201
        }
202
203
        $data = ['status' => true, 'data' => []];
204
        foreach ($this->txn_id as $item) {
205
            $status = ($this->response['transaction_details'][$item]['status'] == 'success') ? true : false;
206
207
            $message = @$this->response['transaction_details'][$item]['error_Message'];
208
            if (count($this->response['transaction_details'][$item]) < 3) {
209
                $message = 'Transaction ID not found.';
210
            }
211
212
            $data['data'][$item] = ['status' => $status, 'message' => $message];
213
        }
214
215
        return $data;
216
    }
217
218
}