Issues (23)

src/Request/CreatePaymentOrder.php (2 issues)

1
<?php
2
3
namespace Stadem\VivaPayments\Request;
4
5
use Stadem\VivaPayments\Config\Config;
6
use Stadem\VivaPayments\Services\CurlWrapper;
7
use Stadem\VivaPayments\Enums\RequestLang;
8
use Stadem\VivaPayments\Enums\PaymentMethods;
9
use Stadem\VivaPayments\Traits\getConfigSettings;
10
11
12
class CreatePaymentOrder
13
{
14
15
    use getConfigSettings;
16
17
    private $customer;
18
    private $order;
19
    private $accessToken;
20
    private $vivaOrderCode = null;
21
    private $response;
22
    private $paymentMethods;
23
    private $paymentMethodFees;
24
    public Config|Array $configObject;
25
26
    public function __construct(?array $order, $accessToken,Config|array $configObject)
27
    {
28
        $this->configObject=$configObject;
29
        $this->order = $order;
30
        $this->accessToken = $accessToken;
31
    }
32
33
34
35
    public function setCustomer(Customer $customer)
36
    {
37
38
        if (!($customer instanceof Customer)) {
0 ignored issues
show
$customer is always a sub-type of Stadem\VivaPayments\Request\Customer.
Loading history...
39
40
            return false;
41
        }
42
43
        $this->customer = $customer;
44
45
        return $this;
46
    }
47
48
49
    public function getCustomer()
50
    {
51
52
        return $this->customer->toArray();
53
    }
54
55
    public function toJson(): string
56
    {
57
        return json_encode($this->getOrder());
58
    }
59
60
61
    public function getOrder()
62
    {
63
        $config = $this->configObject;
64
        $order = $this->order;
65
        $order['sourceCode']  = $config->getEnvConfig('VIVA_SOURCE_CODE');
66
        // $order['sourceCode']  = $this->getConfigSettings()->getEnvConfig('VIVA_SOURCE_CODE');
67
        $order['customer'] = $this->getCustomer();
68
        $order['paymentMethods'] = $this->paymentMethods;
69
        $order['paymentMethodFees'][] = $this->paymentMethodFees;
70
        return $order;
71
    }
72
73
74
    public function getConfig()
75
    {
76
        $environment = $this->accessToken->getEnvironment();
77
        $config = new Config($environment);
78
        return $config;
79
    }
80
81
    public function getOrderCode()
82
    {
83
        return $this->vivaOrderCode;
84
    }
85
86
    public function setOrderCode($vivaOrderCode)
87
    {
88
        $this->vivaOrderCode = $vivaOrderCode;
89
    }
90
91
    public function setPaymentMethods(array $paymentMethods)
92
    {
93
        $this->paymentMethods = $paymentMethods;
94
    }
95
96
    public function setPaymentMethodsFees($paymentMethodFees)
97
    {
98
        $this->paymentMethodFees = $paymentMethodFees;
99
    }
100
101
    public function send()
102
    {
103
         $config = $this->configObject;
104
        //For debugin - Set it to config
105
        // if ($this->getConfigSettings()->getEnvConfig('VIVA_DEBUG')) {
106
        if ($config->getEnvConfig('VIVA_DEBUG')) {
107
            $method = __METHOD__;
108
            $parentDir = dirname(dirname(dirname(__FILE__)));
109
            $fp = fopen($parentDir . '/debug_viva.txt', 'a+');
110
            fwrite($fp, '------- ' . $method . ' -------' . PHP_EOL . $this->toJson() . PHP_EOL . '------------------------------' . PHP_EOL);
111
            fclose($fp);
112
        }
113
114
        $token = $this->accessToken->getToken();
115
        $url = $config->getEnvConfig('VIVA_API_URL');
116
        // $url = $this->getConfigSettings()->getEnvConfig('VIVA_API_URL');
117
        $curl = new CurlWrapper($url . '/checkout/v2/orders');
118
119
        $curl->addHeader('Content-Type: application/json');
120
        $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1');
121
        $curl->addHeader('Accept: */*');
122
        $curl->setBearer($token);
123
124
        $response = $curl->postRaw($this->toJson());
125
126
        $response = json_decode($response, true);
0 ignored issues
show
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        $response = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
127
        $this->response = $response;
128
129
        if (!isset($response['orderCode'])) {
130
            throw new \Exception('Invalid orderCode From viva response.');
131
        }
132
133
        $this->vivaOrderCode = $response['orderCode'];
134
    }
135
136
137
    /**
138
     * You can ovewrite the color from config by using 
139
     * $order->redirectUrl(color:'red');
140
     * 
141
     * You can set custom language for viva SmartCheckout by using 
142
     * $order->redirectUrl(lang:Enums\RequestLang::from('en-GB'));
143
     * 
144
     * paymentMethod is the selected payment method on smart checkout
145
     */
146
147
    public function redirectUrl(
148
        ?string $color = null,
149
        string|RequestLang $lang = null,
150
        int|PaymentMethods $PaymentMethods = null
151
    ) {
152
        $params = '';
153
        $config = $this->configObject;
154
155
        // if (!$color && $this->getConfigSettings()->getEnvConfig('VIVA_SMARTCHECKOUT_COLOR')) {
156
        //     $params .= "&color=" . $this->getConfigSettings()->getEnvConfig('VIVA_SMARTCHECKOUT_COLOR');
157
        // }
158
        if (!$color && $config->getEnvConfig('VIVA_SMARTCHECKOUT_COLOR')) {
159
            $params .= "&color=" . $config->getEnvConfig('VIVA_SMARTCHECKOUT_COLOR');
160
        }
161
162
        if ($color) $params .= "&color=" . $color;
163
164
165
        if ($lang instanceof RequestLang) {
166
            $params .= "&lang=" . $lang->value;
167
        } elseif (is_string($lang)) {
168
            $params .= "&lang=" . $lang;
169
        } else {
170
            $params .= "&lang=el-GR";
171
        }
172
173
        if ($lang instanceof PaymentMethods) {
174
            $params .= "&paymentMethod=" . $PaymentMethods->value;
175
        } elseif (is_int($PaymentMethods)) {
176
            $params .= "&paymentMethod=" . $PaymentMethods;
177
        }
178
179
180
181
        $orderCode = $this->vivaOrderCode;
182
        $url = $config->getEnvConfig('VIVA_URL');
183
        // $url = $this->getConfigSettings()->getEnvConfig('VIVA_URL');
184
        return $url . '/web2?ref=' . $orderCode . $params;
185
    }
186
}
187