Systempay::getToken()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 3
b 0
f 0
nc 4
nop 2
dl 0
loc 26
rs 9.7
1
<?php
2
3
namespace Frenchykiller\LaravelSystempay\View\Components;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\GuzzleException;
7
use Illuminate\View\Component;
8
9
class Systempay extends Component
10
{
11
    protected array $props = [
12
        'amount',
13
        'site',
14
        'currency',
15
        'strong-auth',
16
        'strongAuth',
17
        'order-id',
18
        'orderId',
19
        'has-button',
20
        'hasButton',
21
        'customer',
22
        'merchant',
23
        'success',
24
        'fail',
25
    ];
26
27
    public array $request;
28
    public bool $hasButton;
29
    public string $token;
30
    public string $key;
31
    public ?string $successPost;
32
    public ?string $successGet;
33
    public ?string $failPost;
34
    public ?string $failGet;
35
    private string $site;
36
37
    /**
38
     * Get the view / contents that represent the component.
39
     *
40
     * @return \Illuminate\Contracts\View\View|\Closure|string
41
     */
42
    public function render()
43
    {
44
        return view('laravel-systempay::components.systempay');
45
    }
46
47
    /**
48
     * Create a new component instance.
49
     *
50
     * @param array  $request
51
     * @param bool   $hasButton
52
     * @param string $successPost
53
     * @param string $successGet
54
     * @param string $failPost
55
     * @param string $failGet
56
     * @param string $site
57
     *
58
     * @return void
59
     */
60
    public function __construct($request, $hasButton = true, $successPost = null, $successGet = null, $failPost = null, $failGet = null, $site = 'default')
61
    {
62
        $this->request = $request;
63
        $this->hasButton = $hasButton;
64
        $this->site = $site;
65
66
        $this->token = $this->getToken($site, [
67
            'amount'                => $request['amount'] * 100,
68
            'strongAuthentication'  => $request['strongAuth'] ?? config("systempay.{$site}.params.strongAuthentication"),
69
            'currency'              => $request['currency'] ?? config("systempay.{$site}.params.currency"),
70
            'orderId'               => $request['orderId'] ?? null,
71
            'customer'              => $request['customer'] ?? null,
72
            'merchant'              => $request['merchant'] ?? null,
73
            'transactionOptions'    => $request['transactionOptions'] ?? null,
74
            'metadata'              => $request['metadata'] ?? null,
75
        ]);
76
        $this->key = config("systempay.{$site}.site_id").':'.config("systempay.{$site}.key");
77
        $this->successPost = $successPost;
78
        $this->successGet = $successGet;
79
        $this->failPost = $failPost;
80
        $this->failGet = $failGet;
81
    }
82
83
    protected function getToken($site, $data)
84
    {
85
        $client = new Client();
86
        $headers = [
87
            'Authorization' => 'Basic'.base64_encode(config("systempay.{$site}.site_id").':'.config("systempay.{$site}.password")),
88
            'Content-Type'  => 'application/json',
89
        ];
90
91
        try {
92
            $response = $client->request('POST', config("systempay.{$site}.url").'Charge/CreatePayment', [
93
                'headers'   => $headers,
94
                'json'      => $data,
95
            ]);
96
        } catch (GuzzleException $e) {
97
            \Log::info($e->getMessage());
98
        }
99
100
        $token = json_decode($response->getBody()->getContents())->answer->formToken;
101
        if ($token) {
102
            \Log::info('Systempay formToken : ', [$token]);
103
104
            return $token;
105
        } else {
106
            \Log::error($response->getBody()->getContents());
107
108
            return null;
109
        }
110
    }
111
}
112