MultiSafepayApiClient::initialise()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 2
b 1
f 0
nc 1
nop 4
dl 0
loc 13
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusMultiSafepayPlugin\ApiClient;
12
13
use MultiSafepayAPI\Client;
14
use MultiSafepayAPI\Object\Orders;
15
16
class MultiSafepayApiClient implements MultiSafepayApiClientInterface
17
{
18
    /** @var Client */
19
    private $client;
20
21
    /** @var string */
22
    private $type;
23
24
    /** @var bool */
25
    private $allowMultiCurrency;
26
27
    public function initialise(
28
        string $apiKey,
29
        string $type,
30
        bool $sandbox = true,
31
        bool $allowMultiCurrency = false
32
    ): void {
33
        $this->type = $type;
34
        $this->allowMultiCurrency = $allowMultiCurrency;
35
36
        $this->client = new Client();
37
        $this->client->setApiKey($apiKey);
38
        $this->client->setApiUrl(
39
            $sandbox ? self::API_URL_TEST : self::API_URL_LIVE
40
        );
41
    }
42
43
    public function createPayment(array $data): Orders
44
    {
45
        $this->client->orders->post($data);
46
47
        return $this->client->orders;
48
    }
49
50
    public function getOrderById(string $id): \stdClass
51
    {
52
        return $this->client->orders->get('orders', $id);
53
    }
54
55
    public function getType(): string
56
    {
57
        return $this->type;
58
    }
59
60
    public function getAllowMultiCurrency(): bool
61
    {
62
        return $this->allowMultiCurrency;
63
    }
64
65
    public function refund(
66
        string $orderId,
67
        int $amount,
68
        string $currencyCode
69
    ): void {
70
        $endpoint = sprintf('orders/%s/refunds', $orderId);
71
72
        $this->client->orders->post([
73
            'type' => 'refund',
74
            'amount' => $amount,
75
            'currency' => $currencyCode,
76
        ], $endpoint);
77
    }
78
79
    public function isPaymentActive(string $status): bool
80
    {
81
        return in_array($status, [self::STATUS_INITIALIZED, self::STATUS_COMPLETED, self::STATUS_UNCLEARED, self::STATUS_RESERVED], true);
82
    }
83
}
84