Passed
Push — master ( 73465e...65da9e )
by mahdi
08:58
created

Toman::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
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
    protected $invoice; // Invoice.
16
17
    protected $settings; // Driver settings.
18
19
    protected $base_url;
20
21
    protected $shop_slug;
22
23
    protected $auth_code;
24
25
    protected $code;
26
27
    protected $auth_token;
28
29
    public function __construct(Invoice $invoice, $settings)
30
    {
31
        $this->invoice($invoice); // Set the invoice.
32
        $this->settings = (object) $settings; // Set settings.
33
        $this->base_url = $this->settings->base_url;
34
        $this->shop_slug = $this->settings->shop_slug;
35
        $this->auth_code = $this->settings->auth_code;
36
        $this->code = $this->shop_slug . ':' . $this->auth_code;
37
        $this->auth_token  = base64_encode($this->code);
38
    }
39
40
    // Purchase the invoice, save its transactionId and finaly return it.
41
    public function purchase()
42
    {
43
        $url = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals";
44
        $data = $this->settings->data;
45
46
        $response =  Http::withHeaders([
47
            'Authorization' => "Basic {$this->auth_token}",
48
            "Content-Type" => 'application/json'
49
        ])->post($url, $data);
50
51
        $result = json_decode($response->getBody()->getContents(), true);
52
53
        if (isset($result['trace_number'])) {
54
            $this->invoice->transactionId($result['trace_number']);
55
            return $this->invoice->getTransactionId();
56
        } else {
57
            throw new InvalidPaymentException('پرداخت با مشکل مواجه شد، لطفا با ما در ارتباط باشید');
58
        }
59
    }
60
61
    // Redirect into bank using transactionId, to complete the payment.
62
    public function pay(): RedirectionForm
63
    {
64
        $transactionId = $this->invoice->getTransactionId();
65
        $redirect_url = $this->base_url . '/deals/' . $transactionId . '/redirect';
66
67
        return $this->redirectWithForm($redirect_url, [], 'GET');
68
    }
69
70
    // Verify the payment (we must verify to ensure that user has paid the invoice).
71
    public function verify(): ReceiptInterface
72
    {
73
        $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

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