AfzalSabbir /
SSLaraCommerz
| 1 | <?php |
||
| 2 | namespace AfzalSabbir\SSLaraCommerz\Library\SslCommerz; |
||
| 3 | |||
| 4 | abstract class AbstractSslCommerz implements SslCommerzInterface |
||
| 5 | { |
||
| 6 | protected $apiUrl; |
||
| 7 | protected $storeId; |
||
| 8 | protected $storePassword; |
||
| 9 | |||
| 10 | protected function setStoreId($storeID) |
||
| 11 | { |
||
| 12 | $this->storeId = $storeID; |
||
| 13 | } |
||
| 14 | |||
| 15 | protected function getStoreId() |
||
| 16 | { |
||
| 17 | return $this->storeId; |
||
| 18 | } |
||
| 19 | |||
| 20 | protected function setStorePassword($storePassword) |
||
| 21 | { |
||
| 22 | $this->storePassword = $storePassword; |
||
| 23 | } |
||
| 24 | |||
| 25 | protected function getStorePassword() |
||
| 26 | { |
||
| 27 | return $this->storePassword; |
||
| 28 | } |
||
| 29 | |||
| 30 | protected function setApiUrl($url) |
||
| 31 | { |
||
| 32 | $this->apiUrl = $url; |
||
| 33 | } |
||
| 34 | |||
| 35 | protected function getApiUrl() |
||
| 36 | { |
||
| 37 | return $this->apiUrl; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param $data |
||
| 42 | * @param array $header |
||
| 43 | * @param bool $setLocalhost |
||
| 44 | * @return bool|string |
||
| 45 | */ |
||
| 46 | public function callToApi($data, $header = [], $setLocalhost = false) |
||
| 47 | { |
||
| 48 | $curl = curl_init(); |
||
| 49 | |||
| 50 | if (!$setLocalhost) { |
||
| 51 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); |
||
| 52 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // The default value for this option is 2. It means, it has to have the same name in the certificate as is in the URL you operate against. |
||
| 53 | } else { |
||
| 54 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||
| 55 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // When the verify value is 0, the connection succeeds regardless of the names in the certificate. |
||
| 56 | } |
||
| 57 | |||
| 58 | curl_setopt($curl, CURLOPT_URL, $this->getApiUrl()); |
||
| 59 | curl_setopt($curl, CURLOPT_HEADER, 0); |
||
| 60 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); |
||
| 61 | curl_setopt($curl, CURLOPT_TIMEOUT, 60); |
||
| 62 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
| 63 | |||
| 64 | curl_setopt($curl, CURLOPT_POST, 1); |
||
| 65 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
| 66 | |||
| 67 | $response = curl_exec($curl); |
||
| 68 | $err = curl_error($curl); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 69 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 70 | $curlErrorNo = curl_errno($curl); |
||
| 71 | curl_close($curl); |
||
| 72 | |||
| 73 | if ($code == 200 & !($curlErrorNo)) { |
||
|
0 ignored issues
–
show
|
|||
| 74 | return $response; |
||
| 75 | } else { |
||
| 76 | return "FAILED TO CONNECT WITH SSLCOMMERZ API"; |
||
| 77 | //return "cURL Error #:" . $err; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $response |
||
| 83 | * @param string $type |
||
| 84 | * @param string $pattern |
||
| 85 | * @return false|mixed|string |
||
| 86 | */ |
||
| 87 | public function formatResponse($response, $type = 'checkout', $pattern = 'json') |
||
| 88 | { |
||
| 89 | $sslcz = json_decode($response, true); |
||
| 90 | |||
| 91 | if ($type != 'checkout') { |
||
| 92 | return $sslcz; |
||
| 93 | } else { |
||
| 94 | if (!empty($sslcz['GatewayPageURL'])) { |
||
| 95 | // this is important to show the popup, return or echo to send json response back |
||
| 96 | if(!empty($this->getApiUrl()) && $this->getApiUrl() == 'https://securepay.sslcommerz.com') { |
||
| 97 | $response = json_encode(['status' => 'SUCCESS', 'data' => $sslcz['GatewayPageURL'], 'logo' => $sslcz['storeLogo']]); |
||
| 98 | } else { |
||
| 99 | $response = json_encode(['status' => 'success', 'data' => $sslcz['GatewayPageURL'], 'logo' => $sslcz['storeLogo']]); |
||
| 100 | } |
||
| 101 | } else { |
||
| 102 | if (strpos($sslcz['failedreason'],'Store Credential') === false) { |
||
| 103 | $message = $sslcz['failedreason']; |
||
| 104 | } else { |
||
| 105 | $message = "Check the SSLCZ_TESTMODE and SSLCZ_STORE_PASSWORD value in your .env; DO NOT USE MERCHANT PANEL PASSWORD HERE."; |
||
| 106 | } |
||
| 107 | $response = json_encode(['status' => 'fail', 'data' => null, 'message' => $message]); |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($pattern == 'json') { |
||
| 111 | return $response; |
||
| 112 | } else { |
||
| 113 | echo $response; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $url |
||
| 120 | * @param bool $permanent |
||
| 121 | */ |
||
| 122 | public function redirect($url, $permanent = false) |
||
| 123 | { |
||
| 124 | header('Location: ' . $url, true, $permanent ? 301 : 302); |
||
| 125 | |||
| 126 | exit(); |
||
| 127 | } |
||
| 128 | } |