Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like sn_module_payment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use sn_module_payment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | abstract class sn_module_payment extends sn_module { |
||
| 7 | public static $bonus_table = array( |
||
| 8 | 2000 => 0, |
||
| 9 | // 5000 => 0, |
||
|
|
|||
| 10 | 10000 => 0, |
||
| 11 | 20000 => 0, |
||
| 12 | |||
| 13 | 50000 => 0.02, |
||
| 14 | 100000 => 0.05, |
||
| 15 | 200000 => 0.07, |
||
| 16 | 300000 => 0.10, |
||
| 17 | 400000 => 0.15, |
||
| 18 | 500000 => 0.20, |
||
| 19 | 800000 => 0.25, |
||
| 20 | 1000000 => 0.30, |
||
| 21 | 1500000 => 0.40, |
||
| 22 | 2000000 => 0.50, |
||
| 23 | 3000000 => 0.60, |
||
| 24 | 5000000 => 0.70, |
||
| 25 | ); |
||
| 26 | |||
| 27 | public static $payment_methods = array( |
||
| 28 | PAYMENT_METHOD_BANK_CARD => array( |
||
| 29 | /* |
||
| 30 | PAYMENT_METHOD_id => array( |
||
| 31 | 'currency' => 'WMR', // Currency code 3 letter |
||
| 32 | 'image' => 'design/images/payments/emoney/webmoney.png', // Optional - image location from root. Setting image disables buttoning and name printing |
||
| 33 | 'name' => true, // Optional. Forces method name printing with 'image' set |
||
| 34 | 'button' => true, // Optional. Forces method buttoning with 'image' set |
||
| 35 | ), |
||
| 36 | */ |
||
| 37 | PAYMENT_METHOD_BANK_CARD_STANDARD => array( |
||
| 38 | 'currency' => 'RUB', |
||
| 39 | 'image' => 'design/images/payments/card/generic.png', |
||
| 40 | 'button' => true, |
||
| 41 | ), |
||
| 42 | PAYMENT_METHOD_BANK_CARD_LIQPAY => array( |
||
| 43 | 'currency' => 'UAH', |
||
| 44 | 'image' => 'design/images/payments/card/liqpay.png', |
||
| 45 | 'button' => true, |
||
| 46 | ), |
||
| 47 | PAYMENT_METHOD_BANK_CARD_EASYPAY => array( |
||
| 48 | 'currency' => 'UAH', |
||
| 49 | 'image' => 'design/images/payments/card/easypay.png', |
||
| 50 | 'button' => true, |
||
| 51 | ), |
||
| 52 | PAYMENT_METHOD_BANK_CARD_AMERICAN_EXPRESS => array( |
||
| 53 | 'currency' => 'USD', |
||
| 54 | 'image' => 'design/images/payments/card/american_express.png', |
||
| 55 | 'button' => true, |
||
| 56 | ), |
||
| 57 | PAYMENT_METHOD_BANK_CARD_JCB => array( |
||
| 58 | 'currency' => 'USD', |
||
| 59 | 'image' => 'design/images/payments/card/jcb.png', |
||
| 60 | 'button' => true, |
||
| 61 | ), |
||
| 62 | PAYMENT_METHOD_BANK_CARD_UNIONPAY => array( |
||
| 63 | 'currency' => 'USD', |
||
| 64 | 'image' => 'design/images/payments/card/unionpay.png', |
||
| 65 | 'button' => true, |
||
| 66 | ), |
||
| 67 | ), |
||
| 68 | |||
| 69 | PAYMENT_METHOD_EMONEY => array( |
||
| 70 | PAYMENT_METHOD_EMONEY_YANDEX => array( |
||
| 71 | 'currency' => 'RUB', |
||
| 72 | 'image' => 'design/images/payments/emoney/yandexmoney.png', |
||
| 73 | 'button' => true, |
||
| 74 | ), |
||
| 75 | PAYMENT_METHOD_EMONEY_QIWI => array( |
||
| 76 | 'currency' => 'RUB', |
||
| 77 | 'image' => 'design/images/payments/emoney/qiwi.png', |
||
| 78 | 'button' => true, |
||
| 79 | ), |
||
| 80 | PAYMENT_METHOD_EMONEY_PAYPAL => array( |
||
| 81 | 'currency' => 'RUB', |
||
| 82 | 'image' => 'design/images/payments/emoney/paypal.png', |
||
| 83 | 'button' => true, |
||
| 84 | ), |
||
| 85 | PAYMENT_METHOD_EMONEY_WEBMONEY_WMR => array( |
||
| 86 | 'currency' => 'WMR', |
||
| 87 | 'image' => 'design/images/payments/emoney/webmoney_wmr.gif', |
||
| 88 | 'button' => true, |
||
| 89 | ), |
||
| 90 | PAYMENT_METHOD_EMONEY_WEBMONEY_WMZ => array( |
||
| 91 | 'currency' => 'WMZ', |
||
| 92 | 'image' => 'design/images/payments/emoney/webmoney_wmz.gif', |
||
| 93 | 'button' => true, |
||
| 94 | ), |
||
| 95 | PAYMENT_METHOD_EMONEY_WEBMONEY_WMU => array( |
||
| 96 | 'currency' => 'WMU', |
||
| 97 | 'image' => 'design/images/payments/emoney/webmoney_wmu.gif', |
||
| 98 | 'button' => true, |
||
| 99 | ), |
||
| 100 | PAYMENT_METHOD_EMONEY_WEBMONEY_WME => array( |
||
| 101 | 'currency' => 'WME', |
||
| 102 | 'image' => 'design/images/payments/emoney/webmoney_wme.gif', |
||
| 103 | 'button' => true, |
||
| 104 | ), |
||
| 105 | PAYMENT_METHOD_EMONEY_WEBMONEY_WMB => array( |
||
| 106 | 'currency' => 'WMB', |
||
| 107 | 'image' => 'design/images/payments/emoney/webmoney_wmb.gif', |
||
| 108 | 'button' => true, |
||
| 109 | ), |
||
| 110 | PAYMENT_METHOD_EMONEY_TELEMONEY => array( |
||
| 111 | 'currency' => 'RUB', |
||
| 112 | 'image' => 'design/images/payments/emoney/telemoney.gif', |
||
| 113 | 'button' => true, |
||
| 114 | ), |
||
| 115 | PAYMENT_METHOD_EMONEY_ELECSNET => array( |
||
| 116 | 'currency' => 'RUB', |
||
| 117 | 'image' => 'design/images/payments/emoney/elecsnet.png', |
||
| 118 | 'button' => true, |
||
| 119 | ), |
||
| 120 | PAYMENT_METHOD_EMONEY_EASYPAY => array( |
||
| 121 | 'currency' => 'RUB', |
||
| 122 | 'image' => 'design/images/payments/emoney/easypay.png', |
||
| 123 | 'button' => true, |
||
| 124 | ), |
||
| 125 | PAYMENT_METHOD_EMONEY_RUR_W1R => array( |
||
| 126 | 'currency' => 'RUB', |
||
| 127 | 'image' => 'design/images/payments/emoney/walletone.png', |
||
| 128 | 'button' => true, |
||
| 129 | ), |
||
| 130 | PAYMENT_METHOD_EMONEY_MAILRU => array( |
||
| 131 | 'currency' => 'RUB', |
||
| 132 | 'image' => 'design/images/payments/emoney/mailru.gif', |
||
| 133 | ), |
||
| 134 | ), |
||
| 135 | |||
| 136 | PAYMENT_METHOD_MOBILE => array( |
||
| 137 | PAYMENT_METHOD_MOBILE_SMS => array( |
||
| 138 | 'currency' => 'RUB', |
||
| 139 | 'image' => 'design/images/payments/mobile/sms.png', |
||
| 140 | 'name' => true, |
||
| 141 | 'button' => true, |
||
| 142 | ), |
||
| 143 | PAYMENT_METHOD_MOBILE_PAYPAL_ZONG => array( |
||
| 144 | 'currency' => 'USD', |
||
| 145 | 'image' => 'design/images/payments/mobile/paypal_zong.png', |
||
| 146 | 'name' => true, |
||
| 147 | 'button' => true, |
||
| 148 | ), |
||
| 149 | PAYMENT_METHOD_MOBILE_XSOLLA => array( |
||
| 150 | 'currency' => 'RUB', |
||
| 151 | 'image' => 'design/images/payments/mobile/xsolla.png', |
||
| 152 | 'name' => true, |
||
| 153 | 'button' => true, |
||
| 154 | ), |
||
| 155 | |||
| 156 | |||
| 157 | PAYMENT_METHOD_MOBILE_MEGAPHONE => array( |
||
| 158 | 'currency' => 'RUB', |
||
| 159 | 'image' => 'design/images/payments/mobile/megafon.png', |
||
| 160 | 'button' => true, |
||
| 161 | ), |
||
| 162 | PAYMENT_METHOD_MOBILE_MTS => array( |
||
| 163 | 'currency' => 'RUB', |
||
| 164 | 'image' => 'design/images/payments/mobile/mts.png', |
||
| 165 | 'button' => true, |
||
| 166 | ), |
||
| 167 | PAYMENT_METHOD_MOBILE_KYIVSTAR => array( |
||
| 168 | 'currency' => 'UAH', |
||
| 169 | 'image' => 'design/images/payments/mobile/kyivstar.png', |
||
| 170 | 'button' => true, |
||
| 171 | ), |
||
| 172 | ), |
||
| 173 | |||
| 174 | PAYMENT_METHOD_BANK_INTERNET => array( |
||
| 175 | PAYMENT_METHOD_BANK_INTERNET_PRIVAT24 => array( |
||
| 176 | 'currency' => 'UAH', |
||
| 177 | 'image' => 'design/images/payments/bank_internet/privat24.png', |
||
| 178 | 'button' => true, |
||
| 179 | ), |
||
| 180 | PAYMENT_METHOD_BANK_INTERNET_BANK24 => array( |
||
| 181 | 'currency' => 'UAH', |
||
| 182 | 'image' => 'design/images/payments/bank_internet/bank24.png', |
||
| 183 | 'button' => true, |
||
| 184 | ), |
||
| 185 | PAYMENT_METHOD_BANK_INTERNET_ALFA_BANK => array( |
||
| 186 | 'currency' => 'RUB', |
||
| 187 | 'image' => 'design/images/payments/bank_internet/alfa_bank.png', |
||
| 188 | 'button' => true, |
||
| 189 | ), |
||
| 190 | PAYMENT_METHOD_BANK_INTERNET_SBERBANK => array( |
||
| 191 | 'currency' => 'RUB', |
||
| 192 | 'image' => 'design/images/payments/bank_internet/sberbank.png', |
||
| 193 | 'button' => true, |
||
| 194 | ), |
||
| 195 | PAYMENT_METHOD_BANK_INTERNET_PROSMVYAZBANK => array( |
||
| 196 | 'currency' => 'RUB', |
||
| 197 | 'image' => 'design/images/payments/bank_internet/prosmvyazbank.png', |
||
| 198 | 'button' => true, |
||
| 199 | ), |
||
| 200 | PAYMENT_METHOD_BANK_INTERNET_HANDY_BANK => array( |
||
| 201 | 'currency' => 'RUB', |
||
| 202 | 'image' => 'design/images/payments/bank_internet/handy_bank.png', |
||
| 203 | 'button' => true, |
||
| 204 | ), |
||
| 205 | PAYMENT_METHOD_BANK_INTERNET_RUSSKIY_STANDART => array( |
||
| 206 | 'currency' => 'RUB', |
||
| 207 | 'image' => 'design/images/payments/bank_internet/russkiy_standart.gif', |
||
| 208 | ), |
||
| 209 | PAYMENT_METHOD_BANK_INTERNET_VTB24 => array( |
||
| 210 | 'currency' => 'RUB', |
||
| 211 | 'image' => 'design/images/payments/bank_internet/vtb24.gif', |
||
| 212 | ), |
||
| 213 | PAYMENT_METHOD_BANK_INTERNET_OCEAN_BANK => array( |
||
| 214 | 'currency' => 'RUB', |
||
| 215 | 'image' => 'design/images/payments/bank_internet/ocean_bank.gif', |
||
| 216 | ), |
||
| 217 | PAYMENT_METHOD_BANK_INTERNET_007 => array( |
||
| 218 | 'currency' => 'RUB', |
||
| 219 | ), |
||
| 220 | PAYMENT_METHOD_BANK_INTERNET_008 => array( |
||
| 221 | 'currency' => 'RUB', |
||
| 222 | ), |
||
| 223 | PAYMENT_METHOD_BANK_INTERNET_009 => array( |
||
| 224 | 'currency' => 'RUB', |
||
| 225 | ), |
||
| 226 | PAYMENT_METHOD_BANK_INTERNET_010 => array( |
||
| 227 | 'currency' => 'RUB', |
||
| 228 | ), |
||
| 229 | PAYMENT_METHOD_BANK_INTERNET_011 => array( |
||
| 230 | 'currency' => 'RUB', |
||
| 231 | ), |
||
| 232 | PAYMENT_METHOD_BANK_INTERNET_012 => array( |
||
| 233 | 'currency' => 'RUB', |
||
| 234 | ), |
||
| 235 | PAYMENT_METHOD_BANK_INTERNET_013 => array( |
||
| 236 | 'currency' => 'RUB', |
||
| 237 | ), |
||
| 238 | PAYMENT_METHOD_BANK_INTERNET_014 => array( |
||
| 239 | 'currency' => 'RUB', |
||
| 240 | ), |
||
| 241 | PAYMENT_METHOD_BANK_INTERNET_015 => array( |
||
| 242 | 'currency' => 'RUB', |
||
| 243 | ), |
||
| 244 | PAYMENT_METHOD_BANK_INTERNET_016 => array( |
||
| 245 | 'currency' => 'RUB', |
||
| 246 | ), |
||
| 247 | PAYMENT_METHOD_BANK_INTERNET_017 => array( |
||
| 248 | 'currency' => 'RUB', |
||
| 249 | ), |
||
| 250 | PAYMENT_METHOD_BANK_INTERNET_018 => array( |
||
| 251 | 'currency' => 'RUB', |
||
| 252 | ), |
||
| 253 | PAYMENT_METHOD_BANK_INTERNET_019 => array( |
||
| 254 | 'currency' => 'RUB', |
||
| 255 | ), |
||
| 256 | PAYMENT_METHOD_BANK_INTERNET_020 => array( |
||
| 257 | 'currency' => 'RUB', |
||
| 258 | ), |
||
| 259 | PAYMENT_METHOD_BANK_INTERNET_021 => array( |
||
| 260 | 'currency' => 'RUB', |
||
| 261 | ), |
||
| 262 | ), |
||
| 263 | |||
| 264 | PAYMENT_METHOD_BANK_TRANSFER => array( |
||
| 265 | ), |
||
| 266 | |||
| 267 | PAYMENT_METHOD_TERMINAL => array( |
||
| 268 | PAYMENT_METHOD_TERMINAL_UKRAINE => array( |
||
| 269 | 'currency' => 'UAH', |
||
| 270 | 'image' => 'design/images/payments/terminal/ukraine.png', |
||
| 271 | 'button' => true, |
||
| 272 | 'name' => true, |
||
| 273 | ), |
||
| 274 | PAYMENT_METHOD_TERMINAL_IBOX => array( |
||
| 275 | 'currency' => 'UAH', |
||
| 276 | 'image' => 'design/images/payments/terminal/ibox.png', |
||
| 277 | 'button' => true, |
||
| 278 | ), |
||
| 279 | PAYMENT_METHOD_TERMINAL_EASYPAY => array( |
||
| 280 | 'currency' => 'UAH', |
||
| 281 | 'image' => 'design/images/payments/terminal/easypay.png', |
||
| 282 | 'button' => true, |
||
| 283 | ), |
||
| 284 | PAYMENT_METHOD_TERMINAL_RUSSIA => array( |
||
| 285 | 'currency' => 'RUB', |
||
| 286 | 'image' => 'design/images/payments/terminal/russia.png', |
||
| 287 | 'button' => true, |
||
| 288 | 'name' => true, |
||
| 289 | ), |
||
| 290 | PAYMENT_METHOD_TERMINAL_QIWI => array( |
||
| 291 | 'currency' => 'RUB', |
||
| 292 | 'image' => 'design/images/payments/terminal/qiwi.png', |
||
| 293 | 'button' => true, |
||
| 294 | ), |
||
| 295 | PAYMENT_METHOD_TERMINAL_ELECSNET => array( |
||
| 296 | 'currency' => 'RUB', |
||
| 297 | 'image' => 'design/images/payments/terminal/elecsnet.png', |
||
| 298 | 'button' => true, |
||
| 299 | ), |
||
| 300 | PAYMENT_METHOD_TERMINAL_TELEPAY => array( |
||
| 301 | 'currency' => 'RUB', |
||
| 302 | 'image' => 'design/images/payments/terminal/telepay.png', |
||
| 303 | 'button' => true, |
||
| 304 | ), |
||
| 305 | PAYMENT_METHOD_TERMINAL_ELEMENT => array( |
||
| 306 | 'currency' => 'RUB', |
||
| 307 | 'image' => 'design/images/payments/terminal/element.gif', |
||
| 308 | ), |
||
| 309 | PAYMENT_METHOD_TERMINAL_KASSIRANET => array( |
||
| 310 | 'currency' => 'RUB', |
||
| 311 | 'image' => 'design/images/payments/terminal/kassira_net.gif', |
||
| 312 | 'button' => true, |
||
| 313 | ), |
||
| 314 | ), |
||
| 315 | |||
| 316 | PAYMENT_METHOD_OTHER => array( |
||
| 317 | PAYMENT_METHOD_OTHER_EVROSET => array( |
||
| 318 | 'currency' => 'RUB', |
||
| 319 | 'image' => 'design/images/payments/other/evroset.gif', |
||
| 320 | ), |
||
| 321 | PAYMENT_METHOD_OTHER_SVYAZNOY => array( |
||
| 322 | 'currency' => 'RUB', |
||
| 323 | 'image' => 'design/images/payments/other/svyaznoy.gif', |
||
| 324 | ), |
||
| 325 | PAYMENT_METHOD_OTHER_ROBOKASSA_MOBILE => array( |
||
| 326 | 'currency' => 'RUB', |
||
| 327 | 'image' => 'design/images/payments/other/robokassa_mobile.gif', |
||
| 328 | 'name' => true, |
||
| 329 | ), |
||
| 330 | ), |
||
| 331 | |||
| 332 | PAYMENT_METHOD_GENERIC => array( |
||
| 333 | PAYMENT_METHOD_GENERIC_XSOLLA => array( |
||
| 334 | 'currency' => 'UAH', |
||
| 335 | 'image' => 'design/images/payments/generic/xsolla.png', |
||
| 336 | 'name' => true, |
||
| 337 | 'button' => true, |
||
| 338 | ), |
||
| 339 | |||
| 340 | PAYMENT_METHOD_GENERIC_ROBOKASSA => array( |
||
| 341 | 'currency' => 'RUB', |
||
| 342 | 'image' => 'design/images/payments/generic/robokassa.jpg', |
||
| 343 | // 'name' => true, |
||
| 344 | 'button' => true, |
||
| 345 | ), |
||
| 346 | ), |
||
| 347 | ); |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @var Account $account |
||
| 351 | */ |
||
| 352 | public $account = null; |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @var db_mysql $db |
||
| 356 | */ |
||
| 357 | public $db = null; |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @var int |
||
| 361 | */ |
||
| 362 | public $request_payment_id = 0; |
||
| 363 | /** |
||
| 364 | * Идентификатор сервера, на который производится оплата |
||
| 365 | * |
||
| 366 | * @var string $request_server_id |
||
| 367 | */ |
||
| 368 | public $request_server_id = ''; |
||
| 369 | /** |
||
| 370 | * Идентификатор платящего пользователя |
||
| 371 | * |
||
| 372 | * @var int |
||
| 373 | */ |
||
| 374 | public $request_account_id = 0; |
||
| 375 | /** |
||
| 376 | * @var int |
||
| 377 | */ |
||
| 378 | // public $request_mm_amount = 0; |
||
| 379 | /** |
||
| 380 | * @var float |
||
| 381 | */ |
||
| 382 | // public $request_money_out = 0.0; |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Внутренний идентификатор платежа |
||
| 386 | * |
||
| 387 | * @var int |
||
| 388 | */ |
||
| 389 | public $payment_id = 0; |
||
| 390 | public $payment_status = PAYMENT_STATUS_NONE; |
||
| 391 | public $payment_provider_id = ACCOUNT_PROVIDER_NONE; |
||
| 392 | public $payment_account_id = 0; |
||
| 393 | public $payment_account_name = ''; |
||
| 394 | public $payment_user_id = 0; |
||
| 395 | public $payment_user_name = ''; |
||
| 396 | public $payment_amount = 0; |
||
| 397 | public $payment_currency = ''; |
||
| 398 | public $payment_dark_matter_paid = 0; |
||
| 399 | public $payment_dark_matter_gained = 0; |
||
| 400 | public $payment_date = SN_TIME_SQL; |
||
| 401 | public $payment_comment = ''; |
||
| 402 | public $payment_module_name = ''; |
||
| 403 | |||
| 404 | public $payment_external_id = ''; |
||
| 405 | public $payment_external_date = ''; |
||
| 406 | public $payment_external_lots = 0; |
||
| 407 | public $payment_external_amount = 0; |
||
| 408 | public $payment_external_currency = ''; |
||
| 409 | |||
| 410 | public $payment_test = 0; |
||
| 411 | |||
| 412 | public $is_exists = false; |
||
| 413 | public $is_loaded = false; |
||
| 414 | |||
| 415 | protected $description_generated = array(); |
||
| 416 | |||
| 417 | protected $debug = false; |
||
| 418 | |||
| 419 | protected $payment_params = array( |
||
| 420 | // 'server_id' => 'shp_server', // Должен быть server_id |
||
| 421 | // 'account_id' => 'shp_id', // Должен быть user_id |
||
| 422 | // 'payment_id' => 'InvId', // Должен быть внутренний payment_id |
||
| 423 | // 'payment_dark_matter_gained' => 'shp_dm', // TODO - Реально - dark_matter_gained! Что бы учитывались акции! |
||
| 424 | // 'payment_external_money' => 'OutSum', // Количество денег "к оплате" от СН |
||
| 425 | // 'test' => 'shp_z_test', // Тестовый статус аккаунта |
||
| 426 | // 'payment_external_id' => '', // ИД платежа в платёжной системе |
||
| 427 | // 'payment_external_currency' => 'payment_currency', // Валюта платежа в платёжной системе |
||
| 428 | ); |
||
| 429 | |||
| 430 | protected $result_translations = array( |
||
| 431 | // Универсальный ответ на неизвестную ошибку |
||
| 432 | SN_PAYMENT_REQUEST_UNDEFINED_ERROR => SN_PAYMENT_REQUEST_UNDEFINED_ERROR, |
||
| 433 | // Утвердительный ответ |
||
| 434 | SN_PAYMENT_REQUEST_OK => SN_PAYMENT_REQUEST_OK, |
||
| 435 | ); |
||
| 436 | |||
| 437 | // public function __construct($filename = __FILE__) { |
||
| 438 | // parent::__construct($filename); |
||
| 439 | // } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Компилирует запрос к платёжной системе |
||
| 443 | * |
||
| 444 | * @param $request |
||
| 445 | * |
||
| 446 | * @throws Exception |
||
| 447 | */ |
||
| 448 | public function compile_request($request) { |
||
| 449 | global $user; |
||
| 450 | |||
| 451 | if(!(classSupernova::$auth->account instanceof Account)) { |
||
| 452 | // TODO - throw new Exception(lang['pay_msg_mm_request_amount_invalid'], SN_PAYMENT_REQUEST_ERROR_UNIT_AMOUNT); |
||
| 453 | } |
||
| 454 | $this->account = classSupernova::$auth->account; |
||
| 455 | |||
| 456 | $this->db = $this->account->db; |
||
| 457 | // pdump($this->db); |
||
| 458 | |||
| 459 | $this->payment_provider_id = core_auth::$main_provider->provider_id; |
||
| 460 | $this->payment_account_id = $this->account->account_id; |
||
| 461 | $this->payment_account_name = $this->account->account_name; |
||
| 462 | $this->payment_user_id = $user['id']; |
||
| 463 | $this->payment_user_name = $user['username']; |
||
| 464 | |||
| 465 | // TODO - минимальное количество ММ к оплате |
||
| 466 | $this->payment_dark_matter_paid = $request['metamatter']; |
||
| 467 | $this->payment_dark_matter_gained = self::bonus_calculate($this->payment_dark_matter_paid, true); |
||
| 468 | |||
| 469 | $this->payment_currency = classSupernova::$config->payment_currency_default; |
||
| 470 | $this->payment_amount = self::currency_convert($this->payment_dark_matter_paid, 'MM_', $this->payment_currency); |
||
| 471 | |||
| 472 | if(empty($this->payment_external_currency) && !empty($this->config['currency'])) { |
||
| 473 | $this->payment_external_currency = $this->config['currency']; |
||
| 474 | } |
||
| 475 | if(empty($this->payment_external_currency)) { |
||
| 476 | throw new Exception(classLocale::$lang['pay_error_internal_no_external_currency_set'], SN_PAYMENT_ERROR_INTERNAL_NO_EXTERNAL_CURRENCY_SET); |
||
| 477 | } |
||
| 478 | |||
| 479 | $this->payment_external_amount = self::currency_convert($this->payment_dark_matter_paid, 'MM_', $this->payment_external_currency); |
||
| 480 | if($this->payment_external_amount < 0.01) { |
||
| 481 | throw new Exception(classLocale::$lang['pay_msg_mm_request_amount_invalid'], SN_PAYMENT_REQUEST_ERROR_UNIT_AMOUNT); |
||
| 482 | } |
||
| 483 | |||
| 484 | $this->payment_test = !empty($this->config['test']); |
||
| 485 | |||
| 486 | $this->generate_description(); |
||
| 487 | |||
| 488 | $this->db_insert(); |
||
| 489 | if(!$this->is_exists) { |
||
| 490 | throw new Exception(classLocale::$lang['pay_msg_request_error_db_payment_create'], SN_PAYMENT_REQUEST_DB_ERROR_PAYMENT_CREATE); |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param array $options |
||
| 496 | * |
||
| 497 | * @return array |
||
| 498 | * @throws Exception |
||
| 499 | */ |
||
| 500 | protected function payment_request_process($options = array()) { |
||
| 501 | if(!$this->manifest['active']) { |
||
| 502 | throw new Exception(classLocale::$lang['pay_msg_module_disabled'], SN_MODULE_DISABLED); |
||
| 503 | } |
||
| 504 | |||
| 505 | // Если есть payment_id - загружаем под него данные |
||
| 506 | if(!empty($this->payment_params['payment_id'])) { |
||
| 507 | $this->request_payment_id = sys_get_param_id($this->payment_params['payment_id']); |
||
| 508 | if(!$this->request_payment_id) { |
||
| 509 | throw new Exception(classLocale::$lang['pay_msg_request_payment_id_invalid'], SN_PAYMENT_REQUEST_INTERNAL_ID_WRONG); |
||
| 510 | } |
||
| 511 | |||
| 512 | if(!$this->db_get_by_id($this->request_payment_id)) { |
||
| 513 | throw new Exception(classLocale::$lang['pay_msg_request_payment_id_invalid'], SN_PAYMENT_REQUEST_INTERNAL_ID_WRONG); |
||
| 514 | } |
||
| 515 | |||
| 516 | // Проверяем - был ли этот платеж обработан? |
||
| 517 | // TODO - Статусы бывают разные. Нужен спецфлаг payment_processed |
||
| 518 | if($this->payment_status != PAYMENT_STATUS_NONE) { |
||
| 519 | sn_db_transaction_rollback(); |
||
| 520 | sys_redirect(SN_ROOT_VIRTUAL . 'metamatter.php?payment_id=' . $this->payment_id); |
||
| 521 | die(); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | // Пытаемся получить из запроса ИД аккаунта |
||
| 526 | $request_account_id = !empty($this->payment_params['account_id']) ? sys_get_param_id($this->payment_params['account_id']) : 0; |
||
| 527 | // Если в запросе нет ИД аккаунта - пытаемся использовать payment_account_id |
||
| 528 | if(empty($request_account_id) && !empty($this->payment_account_id)) { |
||
| 529 | $request_account_id = $this->payment_account_id; |
||
| 530 | } |
||
| 531 | // Если теперь у нас нету ИД аккаунта ни в запросе, ни в записи таблицы - можно паниковать |
||
| 532 | if(empty($request_account_id)) { |
||
| 533 | // TODO - аккаунт |
||
| 534 | throw new Exception(classLocale::$lang['pay_msg_request_user_invalid'], $this->retranslate_error(SN_PAYMENT_REQUEST_USER_NOT_FOUND, $options)); |
||
| 535 | } |
||
| 536 | // Если нет записи в таблице - тогда берем payment_account_id из запроса |
||
| 537 | if(empty($this->payment_account_id)) { |
||
| 538 | $this->payment_account_id = $request_account_id; |
||
| 539 | } |
||
| 540 | // Если у нас отличаются ИД аккаунта в запросе и ИД аккаунта в записи - тоже можно паниковать |
||
| 541 | if($this->payment_account_id != $request_account_id) { |
||
| 542 | // TODO - Поменять сообщение об ошибке |
||
| 543 | throw new Exception(classLocale::$lang['pay_msg_request_user_invalid'], $this->retranslate_error(SN_PAYMENT_REQUEST_USER_NOT_FOUND, $options)); |
||
| 544 | } |
||
| 545 | // Проверяем существование аккаунта с данным ИД |
||
| 546 | if(!$this->account->db_get_by_id($this->payment_account_id)) { |
||
| 547 | throw new Exception(classLocale::$lang['pay_msg_request_user_invalid'] . ' ID ' . $this->payment_account_id, $this->retranslate_error(SN_PAYMENT_REQUEST_USER_NOT_FOUND, $options)); |
||
| 548 | } |
||
| 549 | |||
| 550 | // TODO Проверка на сервер_ид - как бы и не нужна, наверное? |
||
| 551 | if(!empty($this->payment_params['server_id'])) { |
||
| 552 | $this->request_server_id = sys_get_param_str($this->payment_params['server_id']); |
||
| 553 | if(SN_ROOT_VIRTUAL != $this->request_server_id) { |
||
| 554 | throw new Exception(classLocale::$lang['pay_msg_request_server_wrong'] . " {$this->request_server_id} вместо " . SN_ROOT_VIRTUAL, SN_PAYMENT_REQUEST_SERVER_WRONG); |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | // Сверка количества оплаченной ММ с учётом бонусов |
||
| 559 | View Code Duplication | if(!empty($this->payment_params['payment_dark_matter_gained'])) { |
|
| 560 | $request_mm_amount = sys_get_param_id($this->payment_params['payment_dark_matter_gained']); |
||
| 561 | if($request_mm_amount != $this->payment_dark_matter_gained && $this->is_loaded) { |
||
| 562 | throw new Exception(classLocale::$lang['pay_msg_mm_request_amount_invalid'] . " пришло {$request_mm_amount} ММ вместо {$this->payment_dark_matter_gained} ММ", SN_PAYMENT_REQUEST_MM_AMOUNT_INVALID); |
||
| 563 | } |
||
| 564 | empty($this->payment_dark_matter_gained) ? $this->payment_dark_matter_gained = $request_mm_amount : false; |
||
| 565 | } |
||
| 566 | if(empty($this->payment_dark_matter_paid)) { |
||
| 567 | // TODO - обратный расчёт из gained |
||
| 568 | } |
||
| 569 | |||
| 570 | // Проверка наличия внешнего ИД платежа |
||
| 571 | if(!empty($this->payment_params['payment_external_id'])) { |
||
| 572 | $request_payment_external_id = sys_get_param_id($this->payment_params['payment_external_id']); |
||
| 573 | if(empty($request_payment_external_id)) { |
||
| 574 | throw new exception(classLocale::$lang['pay_msg_request_payment_id_invalid'], SN_PAYMENT_REQUEST_EXTERNAL_ID_WRONG); |
||
| 575 | } elseif(!empty($this->payment_external_id) && $this->payment_external_id != $request_payment_external_id) { |
||
| 576 | // TODO - Может быть поменять сообщение |
||
| 577 | throw new exception(classLocale::$lang['pay_msg_request_payment_id_invalid'], SN_PAYMENT_REQUEST_EXTERNAL_ID_WRONG); |
||
| 578 | } |
||
| 579 | $this->payment_external_id = $request_payment_external_id; |
||
| 580 | } |
||
| 581 | // Сверка суммы, запрошенной СН к оплате |
||
| 582 | View Code Duplication | if(!empty($this->payment_params['payment_external_money'])) { |
|
| 583 | $request_money_out = sys_get_param_float($this->payment_params['payment_external_money']); |
||
| 584 | if($request_money_out != $this->payment_external_amount && $this->is_loaded) { |
||
| 585 | throw new Exception(classLocale::$lang['pay_msg_request_payment_amount_invalid'] . " пришло {$request_money_out} денег вместо {$this->payment_external_amount} денег", SN_PAYMENT_REQUEST_CURRENCY_AMOUNT_INVALID); |
||
| 586 | } |
||
| 587 | empty($this->payment_external_amount) ? $this->payment_external_amount = $request_money_out : false; |
||
| 588 | } |
||
| 589 | // Заполняем поле валюты платёжной системы |
||
| 590 | if(!empty($this->payment_params['payment_external_currency'])) { |
||
| 591 | $this->payment_external_currency = sys_get_param_str($this->payment_params['payment_external_currency']); |
||
| 592 | if(empty($this->payment_external_currency)) { |
||
| 593 | // TODO - поменять сообщение |
||
| 594 | throw new Exception(classLocale::$lang['pay_msg_request_payment_amount_invalid'] . " {$this->payment_external_currency}", SN_PAYMENT_REQUEST_CURRENCY_AMOUNT_INVALID); |
||
| 595 | } |
||
| 596 | } |
||
| 597 | if(empty($this->payment_external_currency)) { |
||
| 598 | $this->payment_external_currency = $this->config['currency']; |
||
| 599 | } |
||
| 600 | |||
| 601 | // Заполнение внутренней суммы и валюты из внешних данных |
||
| 602 | if(empty($this->payment_currency)) { |
||
| 603 | $this->payment_currency = classSupernova::$config->payment_currency_default; |
||
| 604 | } |
||
| 605 | if(empty($this->payment_amount) && !empty($this->payment_external_currency)) { |
||
| 606 | $this->payment_amount = self::currency_convert($this->payment_external_amount, $this->payment_external_currency, $this->payment_currency); |
||
| 607 | } |
||
| 608 | |||
| 609 | // TODO - Тестовый режим |
||
| 610 | if(!empty($this->payment_params['test'])) { |
||
| 611 | $this->payment_test = $this->config['test'] || sys_get_param_int($this->payment_params['test']); |
||
| 612 | } |
||
| 613 | |||
| 614 | $this->generate_description(); |
||
| 615 | |||
| 616 | // // TODO - REMOVE |
||
| 617 | // return array( |
||
| 618 | // 'payer' => $this->account, |
||
| 619 | // ); |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Точка входа для коллбэка системы платежей - вызывается из <class_name>_response.php |
||
| 624 | * |
||
| 625 | * @return array |
||
| 626 | */ |
||
| 627 | // OK 4.8 |
||
| 628 | // TODO - Здесь должно происходить разделение на resultURL, successURL, failURL |
||
| 629 | public function payment_request_response() { |
||
| 657 | |||
| 658 | |||
| 659 | // Function converts money values between currencies |
||
| 660 | /** |
||
| 661 | * Внутриигровая конвертация валют |
||
| 662 | * |
||
| 663 | * @param $value |
||
| 664 | * @param string $currency_from |
||
| 665 | * @param string $currency_to |
||
| 666 | * @param int $round |
||
| 667 | * |
||
| 668 | * @return float|int |
||
| 669 | */ |
||
| 670 | public static function currency_convert($value, $currency_from = '', $currency_to = '', $round = 2) { |
||
| 684 | |||
| 685 | // Function calculates bonused DM amount for bulk purchase and ($direct = false) vice versa |
||
| 686 | /** |
||
| 687 | * Рассчёт бонуса ММ |
||
| 688 | * |
||
| 689 | * @param $dark_matter |
||
| 690 | * @param bool|true $direct |
||
| 691 | * @param bool|false $return_bonus |
||
| 692 | * |
||
| 693 | * @return float|int |
||
| 694 | */ |
||
| 695 | public static function bonus_calculate($dark_matter, $direct = true, $return_bonus = false) { |
||
| 723 | |||
| 724 | // Дополнительная ре-трансляция адреса, если в каком-то случае платежная система ожидает нелогичный ответ |
||
| 725 | // Пример: иксолла при неправильно заданном пользователе в ордере ожидает НЕПРАВИЛЬНЫЙ_ОРДЕР, а не НЕПРАВИЛЬНЫЙ_ПОЛЬЗОВАТЕЛЬ |
||
| 726 | public function retranslate_error($error_code, $options = array()) { |
||
| 729 | |||
| 730 | |||
| 731 | public function db_insert() { |
||
| 785 | |||
| 786 | |||
| 787 | public function payment_adjust_mm_new() { |
||
| 798 | |||
| 799 | public function payment_cancel(&$payment) { |
||
| 824 | |||
| 825 | |||
| 826 | protected function db_get_by_id($payment_id_unsafe) { |
||
| 840 | |||
| 841 | protected function db_complete_payment() { |
||
| 854 | |||
| 855 | protected function payment_reset() { |
||
| 887 | |||
| 888 | protected function db_assign_payment($payment = null) { |
||
| 929 | |||
| 930 | protected function generate_description() { |
||
| 941 | |||
| 942 | } |
||
| 943 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.