Jacek Kowalski
2023-07-01 73807e6eca56610b6aa48edf1917f8d54ce24de1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
 
namespace SIPL\UCRM\wFirma;
 
class UcrmPaymentMethods {
    protected \Ubnt\UcrmPluginSdk\Service\UcrmApi $api;
    protected static array $definition = [
        'Credit card' => '',
        'Compensation' => '',
    ];
 
    function __construct(UcrmHelper $ucrmHelper) {
        $this->api = $ucrmHelper->getApi();
        $this->update();
    }
 
    function get(string $method): string {
        if (!isset(self::$definition[$method])) {
            throw new \RuntimeException('Unsupported payment method: ' . $method);
        }
        return self::$definition[$method];
    }
 
    function update(): void {
        $methods = $this->api->get('/payment-methods');
        foreach ($methods as $method) {
            if (isset(self::$definition[$method['name']])) {
                self::$definition[$method['name']] = $method['id'];
            }
        }
        foreach (self::$definition as $name => $id) {
            if (empty($id)) {
                $result = $this->api->post(
                    '/payment-methods',
                    [
                        'name' => $name,
                    ]
                );
                self::$definition[$name] = $result['id'];
            }
        }
    }
}