Sadad   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
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 5
A init() 0 24 3
1
<?php
2
3
namespace Dizatech\Transaction\Drivers;
4
5
use Dizatech\SadadIpg\SadadIpg;
6
use Dizatech\Transaction\Abstracts\Driver;
7
8
class Sadad extends Driver
9
{
10
    public function init($amount, $orderId, $callbackUrl, $detail = [])
11
    {
12
        // Create new transaction
13
        $transaction = $this->createNewTransaction($orderId, $amount);
14
15
        // Create object from Sadad Driver
16
        $class = new SadadIpg($this->getInformation());
17
18
        $result = $class->requestPayment(
19
            amount: $amount,
20
            order_id: $transaction->id,
21
            redirect_url: $callbackUrl
22
        );
23
24
        if (!isset($detail['auto_redirect']) || $detail['auto_redirect'] == true) {
25
            echo "<form action='https://sadad.shaparak.ir/VPG/Purchase' id='sadad_redirect_form' style='display: none;'>
26
                <input type='text' name='token' value='{$result->token}'>
27
                <button type='submit'>Send</button>
28
            </form>
29
            <script>window.addEventListener('load', function () {document.getElementById('sadad_redirect_form').submit()})</script>";
30
            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...
31
        }
32
33
        return $result;
34
    }
35
36
    public function verify($request)
37
    {
38
        $class = new SadadIpg($this->getInformation());
39
40
        if (isset($request['ResCode']) && $request['ResCode'] == 0 && isset($request['token'])) {
41
            $result = $class->verify(token: $request['token']);
42
            if ($result->status == 'success') {
43
                $this->updateTransactionData($request['OrderId'], ['status' => 'successful', 'ref_no' => $result->ref_no]);
44
            } else {
45
                $result->message = 'پرداخت با خطا مواجه شد.';
46
                $this->updateTransactionData($request['OrderId'], ['status' => 'failed']);
47
            }
48
49
            return $result;
50
        }
51
52
        $this->updateTransactionData($request['OrderId'], ['status' => 'failed']);
53
        return (object)[
54
            'status'        => 'error',
55
            'message'       => 'پرداخت با خطا مواجه شد.',
56
        ];
57
    }
58
}
59