Passed
Push — main ( c570ad...3cec74 )
by
unknown
15:01
created

Sadad   A

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\PasargadIpg\PasargadIpg;
6
7
use Dizatech\SadadIpg\SadadIpg;
0 ignored issues
show
Bug introduced by
The type Dizatech\SadadIpg\SadadIpg was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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