Passed
Push — master ( e279fb...d2dae0 )
by Sina
10:55
created

Poolam   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 36
c 1
b 0
f 1
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 14 4
A check_payment() 0 9 1
A init() 0 21 5
A payment_request() 0 9 1
1
<?php
2
3
namespace Sinarajabpour1998\Gateway\Drivers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Sinarajabpour1998\Gateway\Abstracts\Driver;
7
8
class Poolam extends Driver
9
{
10
    public function init($amount, $orderId, $callbackUrl, $detail = [])
11
    {
12
        // Create new transaction
13
        $transaction = $this->createNewTransaction($orderId, $amount);
14
15
        // $amount, $transaction->id, $transaction->created_at->format('Y/m/d H:i:s'), $callbackUrl
16
        $result = $this->payment_request($amount, $callbackUrl);
17
18
        if(isset($detail['auto_redirect']) && $detail['auto_redirect'] == false && $result['status'] == 1) {
19
            $result['token'] = $result['invoice_key'];
20
            $result['url'] = config('gateway.information')['poolam']['api_url'] . "pay/" . $result['token'];
21
            return $result;
22
23
        } elseif($result['status'] == 1) {
24
            $this->updateTransactionData($transaction->id, ['token' => $result['invoice_key']]);
25
            header( 'Location: ' . config('gateway.information')['poolam']['api_url'] . "pay/" . $result['invoice_key']);
26
            die();
27
28
        }
29
30
        return $result;
31
    }
32
33
    public function verify($request)
34
    {
35
        $check = $this->check_payment($request['invoice_key']);
36
        $transaction = $this->getTransaction($request['invoice_key']);
37
38
        if (!is_null($transaction)) {
39
            if ($check['status'] == 1 && $transaction->parent->user->id == Auth::user()->id) {
0 ignored issues
show
Bug introduced by
The property parent does not seem to exist on Sinarajabpour1998\Gateway\Models\Transaction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
40
                $this->updateTransactionData($transaction->id, ['status' => 'successful', 'ref_no' => $check['bank_code']]);
41
                return $result;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
42
            }
43
44
            $this->updateTransactionData($transaction->id, ['status' => 'failed', 'ref_no' => $result['errorCode'], 'description' => $result['errorDescription']]);
45
        }
46
        return $check;
47
    }
48
49
    protected function check_payment($inv_key){
50
        $ch = curl_init();
51
        curl_setopt($ch,CURLOPT_URL,config('gateway.information')['poolam']['api_url'] . 'check/'.$inv_key);
52
        curl_setopt($ch,CURLOPT_POSTFIELDS,"api_key=" . config('gateway.information')['poolam']['api_key']);
53
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
54
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
55
        $res = curl_exec($ch);
56
        curl_close($ch);
57
        return json_decode($res,1);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

57
        return json_decode(/** @scrutinizer ignore-type */ $res,1);
Loading history...
58
    }
59
60
    protected function payment_request($amount,$redirect){
61
        $ch = curl_init();
62
        curl_setopt($ch,CURLOPT_URL,config('gateway.information')['poolam']['api_url'] . 'request');
63
        curl_setopt($ch,CURLOPT_POSTFIELDS,"api_key=" . config('gateway.information')['poolam']['api_key'] . "&amount=$amount&return_url=$redirect");
64
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
65
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
66
        $res = curl_exec($ch);
67
        curl_close($ch);
68
        return json_decode($res,1);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

68
        return json_decode(/** @scrutinizer ignore-type */ $res,1);
Loading history...
69
    }
70
}
71