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