|
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
|
|
|
|
|
25
|
|
|
public function __construct(?array $order, $accessToken) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->order = $order; |
|
28
|
|
|
$this->accessToken = $accessToken; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function setCustomer(Customer $customer) |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
if (!($customer instanceof Customer)) { |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->customer = $customer; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
public function getCustomer() |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
return $this->customer->toArray(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function toJson(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return json_encode($this->getOrder()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
public function getOrder() |
|
60
|
|
|
{ |
|
61
|
|
|
$order = $this->order; |
|
62
|
|
|
$order['sourceCode'] = $this->getConfigSettings()->getEnvConfig('VIVA_SOURCE_CODE'); |
|
63
|
|
|
$order['customer'] = $this->getCustomer(); |
|
64
|
|
|
$order['paymentMethods'] = $this->paymentMethods; |
|
65
|
|
|
$order['paymentMethodFees'][] = $this->paymentMethodFees; |
|
66
|
|
|
return $order; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
public function getConfig() |
|
71
|
|
|
{ |
|
72
|
|
|
$environment = $this->accessToken->getEnvironment(); |
|
73
|
|
|
$config = new Config($environment); |
|
74
|
|
|
return $config; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getOrderCode() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->vivaOrderCode; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setOrderCode($vivaOrderCode) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->vivaOrderCode = $vivaOrderCode; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setPaymentMethods(array $paymentMethods) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->paymentMethods = $paymentMethods; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function setPaymentMethodsFees($paymentMethodFees) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->paymentMethodFees = $paymentMethodFees; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function send() |
|
98
|
|
|
{ |
|
99
|
|
|
|
|
100
|
|
|
//For debugin - Set it to config |
|
101
|
|
|
if ($this->getConfigSettings()->getEnvConfig('VIVA_DEBUG')) { |
|
102
|
|
|
$method = __METHOD__; |
|
103
|
|
|
$parentDir = dirname(dirname(dirname(__FILE__))); |
|
104
|
|
|
$fp = fopen($parentDir . '/debug_viva.txt', 'a+'); |
|
105
|
|
|
fwrite($fp, '------- ' . $method . ' -------' . PHP_EOL . $this->toJson() . PHP_EOL . '------------------------------' . PHP_EOL); |
|
106
|
|
|
fclose($fp); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$token = $this->accessToken->getToken(); |
|
110
|
|
|
$url = $this->getConfigSettings()->getEnvConfig('VIVA_API_URL'); |
|
111
|
|
|
$curl = new CurlWrapper($url . '/checkout/v2/orders'); |
|
112
|
|
|
|
|
113
|
|
|
$curl->addHeader('Content-Type: application/json'); |
|
114
|
|
|
$curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
|
115
|
|
|
$curl->addHeader('Accept: */*'); |
|
116
|
|
|
$curl->setBearer($token); |
|
117
|
|
|
|
|
118
|
|
|
$response = $curl->postRaw($this->toJson()); |
|
119
|
|
|
|
|
120
|
|
|
$response = json_decode($response, true); |
|
|
|
|
|
|
121
|
|
|
$this->response = $response; |
|
122
|
|
|
|
|
123
|
|
|
if (!isset($response['orderCode'])) { |
|
124
|
|
|
throw new \Exception('Invalid orderCode From viva response.'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->vivaOrderCode = $response['orderCode']; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* You can ovewrite the color from config by using |
|
133
|
|
|
* $order->redirectUrl(color:'red'); |
|
134
|
|
|
* |
|
135
|
|
|
* You can set custom language for viva SmartCheckout by using |
|
136
|
|
|
* $order->redirectUrl(lang:Enums\RequestLang::from('en-GB')); |
|
137
|
|
|
* |
|
138
|
|
|
* paymentMethod is the selected payment method on smart checkout |
|
139
|
|
|
*/ |
|
140
|
|
|
|
|
141
|
|
|
public function redirectUrl( |
|
142
|
|
|
?string $color = null, |
|
143
|
|
|
string|RequestLang $lang = null, |
|
144
|
|
|
int|PaymentMethods $PaymentMethods = null |
|
145
|
|
|
) { |
|
146
|
|
|
$params = ''; |
|
147
|
|
|
|
|
148
|
|
|
if (!$color && $this->getConfigSettings()->getEnvConfig('VIVA_SMARTCHECKOUT_COLOR')) { |
|
149
|
|
|
$params .= "&color=" . $this->getConfigSettings()->getEnvConfig('VIVA_SMARTCHECKOUT_COLOR'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($color) $params .= "&color=" . $color; |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
if ($lang instanceof RequestLang) { |
|
156
|
|
|
$params .= "&lang=" . $lang->value; |
|
157
|
|
|
} elseif (is_string($lang)) { |
|
158
|
|
|
$params .= "&lang=" . $lang; |
|
159
|
|
|
} else { |
|
160
|
|
|
$params .= "&lang=el-GR"; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ($lang instanceof PaymentMethods) { |
|
164
|
|
|
$params .= "&paymentMethod=" . $PaymentMethods->value; |
|
165
|
|
|
} elseif (is_int($PaymentMethods)) { |
|
166
|
|
|
$params .= "&paymentMethod=" . $PaymentMethods; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
$orderCode = $this->vivaOrderCode; |
|
172
|
|
|
$url = $this->getConfigSettings()->getEnvConfig('VIVA_URL'); |
|
173
|
|
|
return $url . '/web2?ref=' . $orderCode . $params; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|