Passed
Pull Request — master (#208)
by
unknown
08:21
created

Toman   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createReceipt() 0 3 1
A pay() 0 7 1
A __construct() 0 9 1
A purchase() 0 17 2
A verify() 0 15 2
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Toman;
4
5
use Shetabit\Multipay\Invoice;
6
use Shetabit\Multipay\Receipt;
7
use Illuminate\Support\Facades\Http;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Http 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 Shetabit\Multipay\RedirectionForm;
9
use Shetabit\Multipay\Abstracts\Driver;
10
use Shetabit\Multipay\Contracts\ReceiptInterface;
11
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
12
13
class Toman extends Driver
14
{
15
16
    protected $invoice; // Invoice.
17
18
    protected $settings; // Driver settings.
19
20
    protected $base_url;
21
22
    protected $shop_slug;
23
24
    protected $auth_code;
25
26
    protected $code;
27
28
    protected $auth_token;
29
30
    public function __construct(Invoice $invoice, $settings)
31
    {
32
        $this->invoice($invoice); // Set the invoice.
33
        $this->settings = (object) $settings; // Set settings.
34
        $this->base_url = $this->settings->base_url;
35
        $this->shop_slug = $this->settings->shop_slug;
36
        $this->auth_code = $this->settings->auth_code;
37
        $this->code = $this->shop_slug . ':' . $this->auth_code;
38
        $this->auth_token  = base64_encode($this->code);
39
    }
40
41
    // Purchase the invoice, save its transactionId and finaly return it.
42
    public function purchase()
43
    {
44
        $url = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals";
45
        $data = $this->settings->data;
46
47
        $response =  Http::withHeaders([
48
            'Authorization' => "Basic {$this->auth_token}",
49
            "Content-Type" => 'application/json'
50
        ])->post($url, $data);
51
52
        $result = json_decode($response->getBody()->getContents(), true);
53
54
        if (isset($result['trace_number'])) {
55
            $this->invoice->transactionId($result['trace_number']);
56
            return $this->invoice->getTransactionId();
57
        } else {
58
            throw new InvalidPaymentException('پرداخت با مشکل مواجه شد، لطفا با ما در ارتباط باشید');
59
        }
60
    }
61
62
    // Redirect into bank using transactionId, to complete the payment.
63
    public function pay(): RedirectionForm
64
    {
65
66
        $transactionId = $this->invoice->getTransactionId();
67
        $redirect_url = $this->base_url . '/deals/' . $transactionId . '/redirect';
68
69
        return $this->redirectWithForm($redirect_url, [], 'GET');
70
    }
71
72
    // Verify the payment (we must verify to ensure that user has paid the invoice).
73
    public function verify(): ReceiptInterface
74
    {
75
        $inputs = request()->all();
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

75
        $inputs = /** @scrutinizer ignore-call */ request()->all();
Loading history...
76
77
        $transactionId = $this->invoice->getTransactionId();
78
        $verifyUrl = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals/" . $transactionId . "/verify";
79
80
        if ($inputs['state'] == 'funded') {
81
            $result =  Http::withHeaders([
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
82
                'Authorization' => "Basic {$this->auth_token}",
83
                "Content-Type" => 'application/json'
84
            ])->patch($verifyUrl);
85
            return $this->createReceipt($transactionId);
86
        } else {
87
            throw new InvalidPaymentException('پرداخت انجام نشد');
88
        }
89
    }
90
91
    /**
92
     * Generate the payment's receipt
93
     *
94
     * @param $referenceId
95
     *
96
     * @return Receipt
97
     */
98
    public function createReceipt($referenceId)
99
    {
100
        return new Receipt('toman', $referenceId);
101
    }
102
}
103