PluginProbe
imoje / 4.2.0
imoje v4.2.0
4.16.1 4.16.0 trunk 3.1.1 3.2.0 3.2.1 3.2.3 3.2.4 3.2.6 3.2.7 3.2.8 3.3.0 3.3.1 3.3.2 3.3.3 4.0.0 4.1.0 4.1.1 4.10.1 4.11.0 4.12.0 4.14.0 4.15.0 4.15.1 4.15.2 All 39 releases
imoje / includes / libs / payment-core / src / Paywall.php

Paywall.php in imoje 4.2.0, at includes/libs/payment-core/src/Paywall.php

240 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Imoje\Payment;
4
5 /**
6 * Class Paywall
7 *
8 * @package Imoje\Payment
9 */
10 class Paywall
11 {
12 const ROUTE_PAY = '/payment';
13
14 /**
15 * @var array
16 */
17 public static $serviceUrls = [
18 Util::ENVIRONMENT_PRODUCTION => 'https://paywall.imoje.pl',
19 Util::ENVIRONMENT_SANDBOX => 'https://sandbox.paywall.imoje.pl',
20 ];
21 /**
22 * @var string
23 */
24 private $serviceKey;
25
26 /**
27 * @var string
28 */
29 private $serviceId;
30
31 /**
32 * @var string
33 */
34 private $merchantId;
35
36 /**
37 * @var string
38 */
39 private $environment;
40
41 /**
42 * Constructor
43 *
44 * @param string $environment
45 * @param string $serviceKey
46 * @param string $serviceId
47 * @param string $merchantId
48 */
49 public function __construct($environment, $serviceKey, $serviceId, $merchantId)
50 {
51 $this->environment = $environment;
52 $this->serviceKey = trim($serviceKey);
53 $this->serviceId = trim($serviceId);
54 $this->merchantId = trim($merchantId);
55 }
56
57 /**
58 * @param int $amount
59 * @param string $currency
60 * @param string $orderId
61 * @param string $customerFirstName
62 * @param string $customerLastName
63 * @param string $orderDescription
64 * @param string $customerEmail
65 * @param string $customerPhone
66 * @param string $urlSuccess
67 * @param string $urlFailure
68 * @param string $urlReturn
69 * @param string $version
70 * @param string $cartData
71 * @param string $visibleMethod
72 * @param string $cart
73 *
74 * @return array
75 */
76 public static function prepareData(
77 $amount,
78 $currency,
79 $orderId,
80 $customerFirstName,
81 $customerLastName,
82 $orderDescription = '',
83 $customerEmail = '',
84 $customerPhone = '',
85 $urlSuccess = '',
86 $urlFailure = '',
87 $urlReturn = '',
88 $version = '',
89 $cartData = '',
90 $visibleMethod = '',
91 $invoice = '',
92 $cart = ''
93 ) {
94
95 $data = [];
96 $data['amount'] = $amount;
97 $data['currency'] = $currency;
98 $data['orderId'] = $orderId;
99 $data['customerFirstName'] = $customerFirstName;
100 $data['customerLastName'] = $customerLastName;
101
102 if($orderDescription) {
103 $data['orderDescription'] = $orderDescription;
104 }
105 if($customerPhone) {
106 $data['customerPhone'] = $customerPhone;
107 }
108 if($customerEmail) {
109 $data['customerEmail'] = $customerEmail;
110 }
111 if($urlSuccess) {
112 $data['urlSuccess'] = $urlSuccess;
113 }
114 if($urlFailure) {
115 $data['urlFailure'] = $urlFailure;
116 }
117 if($urlReturn) {
118 $data['urlReturn'] = $urlReturn;
119 }
120 if($version) {
121 $data['version'] = $version;
122 }
123 if($cartData) {
124 $data['cartData'] = $cartData;
125 }
126 if($visibleMethod) {
127 $data['visibleMethod'] = $visibleMethod;
128 }
129 if($invoice) {
130 $data['invoice'] = $invoice;
131 }
132 if($cart) {
133 $data['cart'] = $cart;
134 }
135
136 return $data;
137 }
138
139 /**
140 * Creates full form with order data.
141 *
142 * @param array $orderData
143 * @param string $submitValue
144 * @param string $submitClass
145 * @param string $submitStyle
146 *
147 * @return string
148 */
149 public function buildOrderForm(
150 $orderData,
151 $submitValue = '',
152 $submitClass = '',
153 $submitStyle = ''
154 ) {
155 return Util::createOrderForm(
156 $this->prepareOrderData($orderData),
157 $this->getTransactionCreateUrl($this->environment),
158 Util::METHOD_REQUEST_POST,
159 $submitValue,
160 $submitClass,
161 $submitStyle
162 );
163 }
164
165 /**
166 * @param array $order
167 * @param string $hashMethod
168 *
169 * @return array
170 */
171 private function prepareOrderData($order, $hashMethod = 'sha256')
172 {
173
174 $order['serviceId'] = $this->serviceId;
175 $order['merchantId'] = $this->merchantId;
176 $order['signature'] = $this->createSignature($order, $this->serviceKey, $hashMethod);
177
178 return $order;
179 }
180
181 /**
182 * @param array $orderData
183 * @param string $serviceKey
184 * @param string $hashMethod
185 *
186 * @return string|bool
187 */
188 private function createSignature($orderData, $serviceKey, $hashMethod = 'sha256')
189 {
190
191 $isHashMethodSupported = Util::getHashMethod($hashMethod);
192
193 if(!$isHashMethodSupported
194 || !is_array($orderData)) {
195 return false;
196 }
197
198 ksort($orderData);
199
200 $data = [];
201 foreach($orderData as $key => $value) {
202 $data[] = $key . '=' . $value;
203 }
204
205 return Util::hashSignature($hashMethod, implode('&', $data), $serviceKey) . ';' . $hashMethod;
206 }
207
208 /**
209 * @param string $environment
210 *
211 * @return string
212 */
213 private function getTransactionCreateUrl($environment)
214 {
215
216 $baseUrl = self::getServiceUrl($environment);
217
218 if($baseUrl) {
219 return $baseUrl . self::ROUTE_PAY;
220 }
221
222 return '';
223 }
224
225 /**
226 * @param string $environment
227 *
228 * @return string
229 */
230 public static function getServiceUrl($environment)
231 {
232
233 if(isset(self::$serviceUrls[$environment])) {
234 return self::$serviceUrls[$environment];
235 }
236
237 return '';
238 }
239 }
240