Test Setup Failed
Push — main ( 1f8116...4d31c9 )
by امید
11:17
created

ParsianRefund::inquiry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace HopeOfIran\ParsianRefund;
4
5
use HopeOfIran\ParsianRefund\Utils\RSAProcessor;
6
use Illuminate\Support\Facades\Http;
7
8
class ParsianRefund
9
{
10
    /**
11
     *  Refund card deposit number, if not filled, will be taken from the original transaction.
12
     *
13
     * @var string
14
     */
15
    protected $targetCardNumber;
16
    /**
17
     * Unique code that is generated by the user of the system and increases numerically by one step (1 to 900000000000000000000)
18
     *
19
     * @var int
20
     */
21
    protected $refundId = 0;
22
    /**
23
     * Bank reference number with which the transaction was successful
24
     *
25
     * @var int
26
     */
27
    protected $rrn = 0;
28
    /**
29
     * refund amount
30
     *
31
     * @var int
32
     */
33
    protected $amount = 0;
34
    /**
35
     * Refund settings
36
     *
37
     * @var array
38
     */
39
    protected $settings = [];
40
    /**
41
     * @var \phpseclib3\Crypt\RSA
42
     */
43
    private $CRYPT_RSA;
44
    /**
45
     * @var string
46
     */
47
    protected $token = '';
48
49
    /**
50
     * @param $settings
51
     *
52
     * @throws \Exception
53
     */
54
    public function __construct($settings)
55
    {
56
        $this->settings  = empty($settings) ? $this->loadDefaultConfig() : $settings;
57
        $this->CRYPT_RSA = new RSAProcessor($this->settings['certificate'], $this->settings['certificateType']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new HopeOfIran\ParsianRe...ngs['certificateType']) of type HopeOfIran\ParsianRefund\Utils\RSAProcessor is incompatible with the declared type phpseclib3\Crypt\RSA of property $CRYPT_RSA.

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...
58
    }
59
60
    /**
61
     * Retrieve default config.
62
     *
63
     * @return array
64
     */
65
    protected function loadDefaultConfig() : array
66
    {
67
        return require(static::getDefaultConfigPath());
68
    }
69
70
    /**
71
     * Retrieve Default config's path.
72
     *
73
     * @return string
74
     */
75
    public static function getDefaultConfigPath() : string
76
    {
77
        return __DIR__.'/config/parsianRefund.php';
78
    }
79
80
    /**
81
     * @param  string  $targetCardNumber
82
     *
83
     * @return $this
84
     */
85
    public function targetCardNumber(string $targetCardNumber)
86
    {
87
        $this->targetCardNumber = $targetCardNumber;
88
        return $this;
89
    }
90
91
    /**
92
     * @param  int  $amount
93
     *
94
     * @return $this
95
     */
96
    public function amount(int $amount)
97
    {
98
        $this->amount = $amount;
99
        return $this;
100
    }
101
102
    /**
103
     * @param  int  $refundId
104
     *
105
     * @return $this
106
     */
107
    public function refundId(int $refundId)
108
    {
109
        $this->refundId = $refundId;
110
        return $this;
111
    }
112
113
    /**
114
     * @param  int  $rrn
115
     *
116
     * @return $this
117
     */
118
    public function RRN(int $rrn)
119
    {
120
        $this->rrn = $rrn;
121
        return $this;
122
    }
123
124
    /**
125
     * Set custom configs
126
     * we can use this method when we want to use dynamic configs
127
     *
128
     * @param $key
129
     * @param $value  |null
0 ignored issues
show
Documentation Bug introduced by
The doc comment |null at position 0 could not be parsed: Unknown type name '|' at position 0 in |null.
Loading history...
130
     *
131
     * @return $this
132
     */
133
    public function config($key, $value = null)
134
    {
135
        $configs = [];
136
        $key     = is_array($key) ? $key : [$key => $value];
137
        foreach ($key as $k => $v) {
138
            $configs[$k] = $v;
139
        }
140
        $this->settings = array_merge((array) $this->settings, $configs);
141
        return $this;
142
    }
143
144
    /**
145
     * @return \Illuminate\Http\Client\PendingRequest
146
     */
147
    protected function httpRequest() : \Illuminate\Http\Client\PendingRequest
148
    {
149
        $response = Http::baseUrl($this->settings['apiRefundUrl'])
150
            ->withBasicAuth($this->settings['username'], $this->settings['password'])
151
            ->asForm();
152
        if ($this->settings['withoutVerifying'] === 'true') {
153
            $response->withoutVerifying();
154
        }
155
        return $response;
156
    }
157
158
    /**
159
     * @param  array  $data
160
     *
161
     * @return array
162
     */
163
    protected function getRequest(array $data)
164
    {
165
        $plaintext   = json_encode($data);
166
        $request     = base64_encode($this->CRYPT_RSA->encrypt($plaintext));
0 ignored issues
show
Bug introduced by
The method encrypt() does not exist on phpseclib3\Crypt\RSA. It seems like you code against a sub-type of phpseclib3\Crypt\RSA such as phpseclib3\Crypt\RSA\PublicKey. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
        $request     = base64_encode($this->CRYPT_RSA->/** @scrutinizer ignore-call */ encrypt($plaintext));
Loading history...
167
        $requestSign = base64_encode($this->CRYPT_RSA->sign($request));
0 ignored issues
show
Bug introduced by
The method sign() does not exist on phpseclib3\Crypt\RSA. It seems like you code against a sub-type of phpseclib3\Crypt\RSA such as phpseclib3\Crypt\RSA\PrivateKey. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
        $requestSign = base64_encode($this->CRYPT_RSA->/** @scrutinizer ignore-call */ sign($request));
Loading history...
168
        return [
169
            "Request"     => $request,
170
            "RequestSign" => $requestSign,
171
        ];
172
    }
173
174
    /**
175
     * @param  null  $finalizeCallback
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $finalizeCallback is correct as it would always require null to be passed?
Loading history...
176
     *
177
     * @return string
178
     */
179
    public function refund($finalizeCallback = null)
180
    {
181
        $fields = [
182
            "RefundId" => $this->refundId,
183
            "RRN"      => $this->rrn,
184
            "Amount"   => $this->amount,
185
        ];
186
        if ($this->targetCardNumber) {
187
            $fields['TargetCardNumber'] = $this->targetCardNumber;
188
        }
189
        $response = $this->httpRequest()->post("doRefund", $this->getRequest($fields));
190
        try {
191
            $this->token = $response->json('Data')['Token'];
192
        } catch (\Exception $exception) {
193
            logs()->error('ParsianRefund' . $exception->getMessage());
194
        }
195
196
        if ($finalizeCallback) {
0 ignored issues
show
introduced by
$finalizeCallback is of type null, thus it always evaluated to false.
Loading history...
197
            return call_user_func($finalizeCallback, $this, $response->json());
198
        }
199
        return $response;
200
    }
201
202
    /**
203
     * @param  string  $token
204
     *
205
     * @return \Illuminate\Http\Client\Response
206
     */
207
    public function approve(string $token = null)
208
    {
209
        $token = $token == null ? $this->token : $token;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $token of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
210
        return $this->httpRequest()->post("approve", $this->getRequest(["Token" => $token]));
211
    }
212
213
    /**
214
     * @param  string  $token
215
     *
216
     * @return \Illuminate\Http\Client\Response
217
     */
218
    public function cancel(string $token = null)
219
    {
220
        $token = $token == null ? $this->token : $token;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $token of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
221
        return $this->httpRequest()->post("cancel", $this->getRequest(["Token" => $token]));
222
    }
223
224
    /**
225
     * @param  string  $token
226
     *
227
     * @return \Illuminate\Http\Client\Response
228
     */
229
    public function inquiry(string $token = null)
230
    {
231
        $token = $token == null ? $this->token : $token;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $token of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
232
        return $this->httpRequest()->post("Inquiry", $this->getRequest(["Token" => $token]));
233
    }
234
}