Zarinpal::init()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
rs 9.5555
cc 5
nc 3
nop 4
1
<?php
2
3
namespace Dizatech\Transaction\Drivers;
4
5
use Dizatech\Transaction\Abstracts\Driver;
6
use Dizatech\Transaction\Models\Transaction;
7
use Dizatech\ZarinpalIpg\ZarinpalIpg;
8
9
class Zarinpal extends Driver
10
{
11
    public function init($amount, $orderId, $callbackUrl, $detail = [])
12
    {
13
        // Create new transaction
14
        $transaction = $this->createNewTransaction($orderId, $amount);
15
16
        // Create object from Parsian Driver
17
        $class = new ZarinpalIpg($this->getInformation());
18
        $result = $class->getToken($amount, $transaction->id, $callbackUrl);
19
20
        if(isset($detail['auto_redirect']) && $detail['auto_redirect'] == false && $result->status == 'success') {
21
            $result->token  = $result->token;
22
            $result->url    = 'Location: https://www.zarinpal.com/pg/StartPay/' . $result->token;
23
            return $result;
24
25
        } elseif($result->status == 'success') {
26
            $this->updateTransactionData($transaction->id, ['token' => $result->token]);
27
            header( 'Location: https://www.zarinpal.com/pg/StartPay/' . $result->token );
28
            die();
29
        }
30
31
        return $result;
32
    }
33
34
    public function verify($request)
35
    {
36
        $class = new ZarinpalIpg($this->getInformation());
37
38
        $transaction = Transaction::whereToken( $request['Authority'] )->first();
39
        $result = $class->verifyRequest( intval($transaction->amount), $transaction->token );
0 ignored issues
show
Bug introduced by
The property amount does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property amount does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property token does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property token does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
40
41
        if ($result->status == 'success') {
42
            $this->updateTransactionData($transaction->id, ['status' => 'successful', 'ref_no' => $result->ref_id]);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
43
        } else {
44
            $this->updateTransactionData($transaction->id, ['status' => 'failed']);
45
        }
46
47
        return $result;
48
    }
49
}
50