| Total Complexity | 85 |
| Total Lines | 448 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Complex classes like SslCommerzNotification 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.
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 SslCommerzNotification, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class SslCommerzNotification extends AbstractSslCommerz |
||
| 5 | { |
||
| 6 | protected $data = []; |
||
| 7 | protected $config = []; |
||
| 8 | |||
| 9 | private $successUrl; |
||
| 10 | private $cancelUrl; |
||
| 11 | private $failedUrl; |
||
| 12 | private $ipnUrl; |
||
| 13 | private $error; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * SslCommerzNotification constructor. |
||
| 17 | */ |
||
| 18 | public function __construct() |
||
| 19 | { |
||
| 20 | $this->config = config('sslcommerz'); |
||
| 21 | |||
| 22 | $this->setStoreId($this->config['apiCredentials']['store_id']); |
||
| 23 | $this->setStorePassword($this->config['apiCredentials']['store_password']); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function orderValidate($post_data, $trx_id = '', $amount = 0, $currency = "BDT") |
||
| 27 | { |
||
| 28 | if ($post_data == '' && $trx_id == '' && !is_array($post_data)) { |
||
| 29 | $this->error = "Please provide valid transaction ID and post request data"; |
||
| 30 | return $this->error; |
||
| 31 | } |
||
| 32 | |||
| 33 | return $this->validate($trx_id, $amount, $currency, $post_data); |
||
| 34 | |||
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | # VALIDATE SSLCOMMERZ TRANSACTION |
||
| 39 | protected function validate($merchant_trans_id, $merchant_trans_amount, $merchant_trans_currency, $post_data) |
||
| 40 | { |
||
| 41 | # MERCHANT SYSTEM INFO |
||
| 42 | if (!empty($merchant_trans_id) && !empty($merchant_trans_amount)) { |
||
| 43 | |||
| 44 | # CALL THE FUNCTION TO CHECK THE RESULT |
||
| 45 | $post_data['store_id'] = $this->getStoreId(); |
||
| 46 | $post_data['store_pass'] = $this->getStorePassword(); |
||
| 47 | |||
| 48 | $val_id = urlencode($post_data['val_id']); |
||
| 49 | $store_id = urlencode($this->getStoreId()); |
||
| 50 | $store_passwd = urlencode($this->getStorePassword()); |
||
| 51 | $requested_url = ($this->config['apiDomain'] . $this->config['apiUrl']['order_validate'] . "?val_id=" . $val_id . "&store_id=" . $store_id . "&store_passwd=" . $store_passwd . "&v=1&format=json"); |
||
| 52 | |||
| 53 | $handle = curl_init(); |
||
| 54 | curl_setopt($handle, CURLOPT_URL, $requested_url); |
||
| 55 | curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
||
| 56 | |||
| 57 | if ($this->config['connect_from_localhost']) { |
||
| 58 | curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0); |
||
| 59 | curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0); |
||
| 60 | } else { |
||
| 61 | curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2); |
||
| 62 | curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 2); |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | $result = curl_exec($handle); |
||
| 67 | |||
| 68 | $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
||
| 69 | |||
| 70 | if ($code == 200 && !(curl_errno($handle))) { |
||
| 71 | |||
| 72 | # TO CONVERT AS ARRAY |
||
| 73 | # $result = json_decode($result, true); |
||
| 74 | # $status = $result['status']; |
||
| 75 | |||
| 76 | # TO CONVERT AS OBJECT |
||
| 77 | $result = json_decode($result); |
||
|
|
|||
| 78 | $this->sslc_data = $result; |
||
| 79 | |||
| 80 | # TRANSACTION INFO |
||
| 81 | $status = $result->status; |
||
| 82 | $tran_date = $result->tran_date; |
||
| 83 | $tran_id = $result->tran_id; |
||
| 84 | $val_id = $result->val_id; |
||
| 85 | $amount = $result->amount; |
||
| 86 | $store_amount = $result->store_amount; |
||
| 87 | $bank_tran_id = $result->bank_tran_id; |
||
| 88 | $card_type = $result->card_type; |
||
| 89 | $currency_type = $result->currency_type; |
||
| 90 | $currency_amount = $result->currency_amount; |
||
| 91 | |||
| 92 | # ISSUER INFO |
||
| 93 | $card_no = $result->card_no; |
||
| 94 | $card_issuer = $result->card_issuer; |
||
| 95 | $card_brand = $result->card_brand; |
||
| 96 | $card_issuer_country = $result->card_issuer_country; |
||
| 97 | $card_issuer_country_code = $result->card_issuer_country_code; |
||
| 98 | |||
| 99 | # API AUTHENTICATION |
||
| 100 | $APIConnect = $result->APIConnect; |
||
| 101 | $validated_on = $result->validated_on; |
||
| 102 | $gw_version = $result->gw_version; |
||
| 103 | |||
| 104 | # GIVE SERVICE |
||
| 105 | if ($status == "VALID" || $status == "VALIDATED") { |
||
| 106 | if ($merchant_trans_currency == "BDT") { |
||
| 107 | if (trim($merchant_trans_id) == trim($tran_id) && (abs($merchant_trans_amount - $amount) < 1) && trim($merchant_trans_currency) == trim('BDT')) { |
||
| 108 | return true; |
||
| 109 | } else { |
||
| 110 | # DATA TEMPERED |
||
| 111 | $this->error = "Data has been tempered"; |
||
| 112 | return false; |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | //echo "trim($merchant_trans_id) == trim($tran_id) && ( abs($merchant_trans_amount-$currency_amount) < 1 ) && trim($merchant_trans_currency)==trim($currency_type)"; |
||
| 116 | if (trim($merchant_trans_id) == trim($tran_id) && (abs($merchant_trans_amount - $currency_amount) < 1) && trim($merchant_trans_currency) == trim($currency_type)) { |
||
| 117 | return true; |
||
| 118 | } else { |
||
| 119 | # DATA TEMPERED |
||
| 120 | $this->error = "Data has been tempered"; |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | # FAILED TRANSACTION |
||
| 126 | $this->error = "Failed Transaction"; |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | # Failed to connect with SSLCOMMERZ |
||
| 131 | $this->error = "Faile to connect with SSLCOMMERZ"; |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | } else { |
||
| 135 | # INVALID DATA |
||
| 136 | $this->error = "Invalid data"; |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | # FUNCTION TO CHECK HASH VALUE |
||
| 142 | protected function SSLCOMMERZ_hash_verify($post_data, $store_passwd = "") |
||
| 143 | { |
||
| 144 | if (isset($post_data) && isset($post_data['verify_sign']) && isset($post_data['verify_key'])) { |
||
| 145 | # NEW ARRAY DECLARED TO TAKE VALUE OF ALL POST |
||
| 146 | $pre_define_key = explode(',', $post_data['verify_key']); |
||
| 147 | |||
| 148 | $new_data = array(); |
||
| 149 | if (!empty($pre_define_key)) { |
||
| 150 | foreach ($pre_define_key as $value) { |
||
| 151 | // if (isset($post_data[$value])) { |
||
| 152 | $new_data[$value] = ($post_data[$value]); |
||
| 153 | // } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | # ADD MD5 OF STORE PASSWORD |
||
| 157 | $new_data['store_passwd'] = md5($store_passwd); |
||
| 158 | |||
| 159 | # SORT THE KEY AS BEFORE |
||
| 160 | ksort($new_data); |
||
| 161 | |||
| 162 | $hash_string = ""; |
||
| 163 | foreach ($new_data as $key => $value) { |
||
| 164 | $hash_string .= $key . '=' . ($value) . '&'; |
||
| 165 | } |
||
| 166 | $hash_string = rtrim($hash_string, '&'); |
||
| 167 | |||
| 168 | if (md5($hash_string) == $post_data['verify_sign']) { |
||
| 169 | |||
| 170 | return true; |
||
| 171 | |||
| 172 | } else { |
||
| 173 | $this->error = "Verification signature not matched"; |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | } else { |
||
| 177 | $this->error = 'Required data mission. ex: verify_key, verify_sign'; |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param array $requestData |
||
| 184 | * @param string $type |
||
| 185 | * @param string $pattern |
||
| 186 | * @return false|mixed|string |
||
| 187 | */ |
||
| 188 | public function makePayment(array $requestData, $type = 'checkout', $pattern = 'json') |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | protected function setSuccessUrl() { |
||
| 227 | $this->successUrl = env('APP_URL') . $this->config['success_url']; |
||
| 228 | } |
||
| 229 | |||
| 230 | protected function getSuccessUrl() |
||
| 231 | { |
||
| 232 | return $this->successUrl; |
||
| 233 | } |
||
| 234 | |||
| 235 | protected function setFailedUrl() { |
||
| 236 | $this->failedUrl = env('APP_URL') . $this->config['failed_url']; |
||
| 237 | } |
||
| 238 | |||
| 239 | protected function getFailedUrl() |
||
| 240 | { |
||
| 241 | return $this->failedUrl; |
||
| 242 | } |
||
| 243 | |||
| 244 | protected function setCancelUrl() { |
||
| 245 | $this->cancelUrl = env('APP_URL') . $this->config['cancel_url']; |
||
| 246 | } |
||
| 247 | |||
| 248 | protected function getCancelUrl() |
||
| 249 | { |
||
| 250 | return $this->cancelUrl; |
||
| 251 | } |
||
| 252 | |||
| 253 | protected function setIPNUrl() { |
||
| 254 | $this->ipnUrl = env('APP_URL') . $this->config['ipn_url']; |
||
| 255 | } |
||
| 256 | |||
| 257 | protected function getIPNUrl() { |
||
| 259 | } |
||
| 260 | |||
| 261 | public function setParams($requestData) { |
||
| 262 | ## Integration Required Parameters |
||
| 263 | $this->setRequiredInfo($requestData); |
||
| 264 | |||
| 265 | ## Customer Information |
||
| 266 | $this->setCustomerInfo($requestData); |
||
| 267 | |||
| 268 | ## Shipment Information |
||
| 269 | $this->setShipmentInfo($requestData); |
||
| 270 | |||
| 271 | ## Product Information |
||
| 272 | $this->setProductInfo($requestData); |
||
| 273 | |||
| 274 | ## Customized or Additional Parameters |
||
| 275 | $this->setAdditionalInfo($requestData); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function setAuthenticationInfo() |
||
| 279 | { |
||
| 280 | $this->data['store_id'] = $this->getStoreId(); |
||
| 281 | $this->data['store_passwd'] = $this->getStorePassword(); |
||
| 282 | |||
| 283 | return $this->data; |
||
| 284 | } |
||
| 285 | |||
| 286 | public function setRequiredInfo(array $info) |
||
| 287 | { |
||
| 288 | $this->data['total_amount'] = $info['total_amount']; // decimal (10,2) Mandatory - The amount which will process by SSLCommerz. It shall be decimal value (10,2). Example : 55.40. The transaction amount must be from 10.00 BDT to 500000.00 BDT |
||
| 289 | $this->data['currency'] = $info['currency']; // string (3) Mandatory - The currency type must be mentioned. It shall be three characters. Example : BDT, USD, EUR, SGD, INR, MYR, etc. If the transaction currency is not BDT, then it will be converted to BDT based on the current convert rate. Example : 1 USD = 82.22 BDT. |
||
| 290 | $this->data['tran_id'] = $info['tran_id']; // string (30) Mandatory - Unique transaction ID to identify your order in both your end and SSLCommerz |
||
| 291 | $this->data['product_category'] = $info['product_category']; // string (50) Mandatory - Mention the product category. It is a open field. Example - clothing,shoes,watches,gift,healthcare, jewellery,top up,toys,baby care,pants,laptop,donation,etc |
||
| 292 | |||
| 293 | // Set the SUCCESS, FAIL, CANCEL Redirect URL before setting the other parameters |
||
| 294 | $this->setSuccessUrl(); |
||
| 295 | $this->setFailedUrl(); |
||
| 296 | $this->setCancelUrl(); |
||
| 297 | $this->setIPNUrl(); |
||
| 298 | |||
| 299 | $this->data['success_url'] = $this->getSuccessUrl(); // string (255) Mandatory - It is the callback URL of your website where user will redirect after successful payment (Length: 255) |
||
| 300 | $this->data['fail_url'] = $this->getFailedUrl(); // string (255) Mandatory - It is the callback URL of your website where user will redirect after any failure occure during payment (Length: 255) |
||
| 301 | $this->data['cancel_url'] = $this->getCancelUrl(); // string (255) Mandatory - It is the callback URL of your website where user will redirect if user canceled the transaction (Length: 255) |
||
| 302 | |||
| 303 | /* |
||
| 304 | * IPN is very important feature to integrate with your site(s). |
||
| 305 | * Some transaction could be pending or customer lost his/her session, in such cases back-end IPN plays a very important role to update your backend office. |
||
| 306 | * |
||
| 307 | * Type: string (255) |
||
| 308 | * Important! Not mandatory, however better to use to avoid missing any payment notification - It is the Instant Payment Notification (IPN) URL of your website where SSLCOMMERZ will send the transaction's status (Length: 255). |
||
| 309 | * The data will be communicated as SSLCOMMERZ Server to your Server. So, customer session will not work. |
||
| 310 | */ |
||
| 311 | $this->data['ipn_url'] = $this->getIPNUrl(); |
||
| 312 | |||
| 313 | /* |
||
| 314 | * Type: string (30) |
||
| 315 | * Do not Use! If you do not customize the gateway list - You can control to display the gateway list at SSLCommerz gateway selection page by providing this parameters. |
||
| 316 | * Multi Card: |
||
| 317 | brac_visa = BRAC VISA |
||
| 318 | dbbl_visa = Dutch Bangla VISA |
||
| 319 | city_visa = City Bank Visa |
||
| 320 | ebl_visa = EBL Visa |
||
| 321 | sbl_visa = Southeast Bank Visa |
||
| 322 | brac_master = BRAC MASTER |
||
| 323 | dbbl_master = MASTER Dutch-Bangla |
||
| 324 | city_master = City Master Card |
||
| 325 | ebl_master = EBL Master Card |
||
| 326 | sbl_master = Southeast Bank Master Card |
||
| 327 | city_amex = City Bank AMEX |
||
| 328 | qcash = QCash |
||
| 329 | dbbl_nexus = DBBL Nexus |
||
| 330 | bankasia = Bank Asia IB |
||
| 331 | abbank = AB Bank IB |
||
| 332 | ibbl = IBBL IB and Mobile Banking |
||
| 333 | mtbl = Mutual Trust Bank IB |
||
| 334 | bkash = Bkash Mobile Banking |
||
| 335 | dbblmobilebanking = DBBL Mobile Banking |
||
| 336 | city = City Touch IB |
||
| 337 | upay = Upay |
||
| 338 | tapnpay = Tap N Pay Gateway |
||
| 339 | * GROUP GATEWAY |
||
| 340 | internetbank = For all internet banking |
||
| 341 | mobilebank = For all mobile banking |
||
| 342 | othercard = For all cards except visa,master and amex |
||
| 343 | visacard = For all visa |
||
| 344 | mastercard = For All Master card |
||
| 345 | amexcard = For Amex Card |
||
| 346 | * */ |
||
| 347 | $this->data['multi_card_name'] = (isset($info['multi_card_name'])) ? $info['multi_card_name'] : null; |
||
| 348 | |||
| 349 | /* |
||
| 350 | * Type: string (255) |
||
| 351 | * Do not Use! If you do not control on transaction - You can provide the BIN of card to allow the transaction must be completed by this BIN. You can declare by coma ',' separate of these BIN. |
||
| 352 | * Example: 371598,371599,376947,376948,376949 |
||
| 353 | * */ |
||
| 354 | $this->data['allowed_bin'] = (isset($info['allowed_bin'])) ? $info['allowed_bin'] : null; |
||
| 355 | |||
| 356 | ## Parameters to Handle EMI Transaction ## |
||
| 357 | $this->data['emi_option'] = (isset($info['emi_option'])) ? $info['emi_option'] : null; // integer (1) Mandatory - This is mandatory if transaction is EMI enabled and Value must be 1/0. Here, 1 means customer will get EMI facility for this transaction |
||
| 358 | $this->data['emi_max_inst_option'] = (isset($info['emi_max_inst_option'])) ? $info['emi_max_inst_option'] : null; // integer (2) Max instalment Option, Here customer will get 3,6, 9 instalment at gateway page |
||
| 359 | $this->data['emi_selected_inst'] = (isset($info['emi_selected_inst'])) ? $info['emi_selected_inst'] : null; // integer (2) Customer has selected from your Site, So no instalment option will be displayed at gateway page |
||
| 360 | $this->data['emi_allow_only'] = (isset($info['emi_allow_only'])) ? $info['emi_allow_only'] : 0; |
||
| 361 | |||
| 362 | return $this->data; |
||
| 363 | } |
||
| 364 | |||
| 365 | public function setCustomerInfo(array $info) |
||
| 379 | } |
||
| 380 | |||
| 381 | public function setShipmentInfo(array $info) |
||
| 382 | { |
||
| 383 | |||
| 384 | $this->data['shipping_method'] = $info['shipping_method']; // string (50) Mandatory - Shipping method of the order. Example: YES or NO or Courier |
||
| 385 | $this->data['num_of_item'] = isset($info['num_of_item']) ? $info['num_of_item'] : 1; // integer (1) Mandatory - No of product will be shipped. Example: 1 or 2 or etc |
||
| 386 | $this->data['ship_name'] = $info['ship_name']; // string (50) Mandatory, if shipping_method is YES - Shipping Address of your order. Not mandatory but useful if provided |
||
| 387 | $this->data['ship_add1'] = $info['ship_add1']; // string (50) Mandatory, if shipping_method is YES - Additional Shipping Address of your order. Not mandatory but useful if provided |
||
| 388 | $this->data['ship_add2'] = (isset($info['ship_add2'])) ? $info['ship_add2'] : null; // string (50) Additional Shipping Address of your order. Not mandatory but useful if provided |
||
| 389 | $this->data['ship_city'] = $info['ship_city']; // string (50) Mandatory, if shipping_method is YES - Shipping city of your order. Not mandatory but useful if provided |
||
| 390 | $this->data['ship_state'] = (isset($info['ship_state'])) ? $info['ship_state'] : null; // string (50) Shipping state of your order. Not mandatory but useful if provided |
||
| 391 | $this->data['ship_postcode'] = (isset($info['ship_postcode'])) ? $info['ship_postcode'] : null; // string (50) Mandatory, if shipping_method is YES - Shipping postcode of your order. Not mandatory but useful if provided |
||
| 392 | $this->data['ship_country'] = (isset($info['ship_country'])) ? $info['ship_country'] : null; // string (50) Mandatory, if shipping_method is YES - Shipping country of your order. Not mandatory but useful if provided |
||
| 393 | |||
| 394 | return $this->data; |
||
| 395 | } |
||
| 396 | |||
| 397 | public function setProductInfo(array $info) |
||
| 442 | } |
||
| 443 | |||
| 444 | public function setAdditionalInfo(array $info) |
||
| 452 | } |
||
| 453 | } |