Saman   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 25
c 1
b 0
f 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 20 4
A init() 0 24 3
1
<?php
2
3
namespace Dizatech\Transaction\Drivers;
4
5
use Dizatech\SamanIpg\SamanIpg;
6
use Dizatech\Transaction\Abstracts\Driver;
7
use Dizatech\Transaction\Models\Transaction;
8
9
class Saman 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 Sadad Driver
17
        $class = new SamanIpg($this->getInformation());
18
19
        $result = $class->requestPayment(
20
            amount: $amount,
21
            order_id: $transaction->id,
22
            redirect_url: $callbackUrl
23
        );
24
25
        if (!isset($detail['auto_redirect']) || $detail['auto_redirect'] == true) {
26
            echo "<form method='post' action='https://sep.shaparak.ir/OnlinePG/OnlinePG' id='saman_redirect_form' style='display: none;'>
27
                <input type='text' name='token' value='{$result->token}'>
28
                <button type='submit'>Send</button>
29
            </form>
30
            <script>window.addEventListener('load', function () {document.getElementById('saman_redirect_form').submit()})</script>";
31
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
32
        }
33
34
        return $result;
35
    }
36
37
    public function verify($request)
38
    {
39
        $class = new SamanIpg($this->getInformation());
40
        if (isset($request['State']) && $request['State'] == 'OK') {
41
            $transaction = Transaction::whereId($request['ResNum'])->first();
42
            $result = $class->verify(amount: intval($transaction->amount), ref_number: $request['RefNum']);
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...
43
44
            if ($result->status == 'success') {
45
                $this->updateTransactionData($transaction->id, ['status' => 'successful', 'ref_no' => $result->ref_no, 'token' => $result->token]);
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
46
            } else {
47
                $this->updateTransactionData($transaction->id, ['status' => 'failed']);
48
            }
49
50
            return $result;
51
        }
52
53
        $this->updateTransactionData($request['ResNum'], ['status' => 'failed']);
54
        return (object)[
55
            'status'        => 'error',
56
            'message'       => 'پرداخت با خطا مواجه شد.',
57
        ];
58
    }
59
}
60