Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
asif250 committed Oct 28, 2021
1 parent f4ce838 commit ec034f0
Show file tree
Hide file tree
Showing 8 changed files with 981 additions and 0 deletions.
269 changes: 269 additions & 0 deletions src/Http/controllers/SSLCommerzController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
<?php

namespace SAM\SSLCommerz\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use SAM\SSLCommerz\Library\SslCommerz\SamSSL;

class SSLCommerzController extends Controller
{
private $returnUrl;

public function __construct()
{
$this->config = config('sslcommerz');
$this->returnUrl=$this->config['return_url'];
}

public function index($request,$type)
{
if($type=='hosted'){
return $this->hosted($request);
}
else if($type=='ajax'){
return $this->payViaAjax($request);
}else{
return redirect()->to('/');
}

}


public function hosted($request)
{


$post_data = array();
$post_data['total_amount'] = $request['total_amount']; # You cant not pay less than 10
$post_data['currency'] = "BDT";
$post_data['tran_id'] = $request['tran_id']; // tran_id must be unique
# CUSTOMER INFORMATION
$post_data['cus_name'] = $request['cus_name'];
$post_data['cus_email'] = $request['cus_email'];
$post_data['cus_add1'] = $request['cus_add1'];
$post_data['cus_add2'] = $request['cus_add2'];
$post_data['cus_city'] = $request['cus_city'];
$post_data['cus_state'] = $request['cus_state'];
$post_data['cus_postcode'] = $request['cus_postcode'];
$post_data['cus_country'] = $request['cus_country'];
$post_data['cus_phone'] = $request['cus_phone'];
$post_data['cus_fax'] = $request['cus_fax'];

# SHIPMENT INFORMATION
$post_data['ship_name'] = $request['ship_name'];
$post_data['ship_add1'] = $request['ship_add1'];
$post_data['ship_add2'] = $request['ship_add2'];
$post_data['ship_city'] = $request['ship_city'];
$post_data['ship_state'] = $request['ship_state'];
$post_data['ship_postcode'] = $request['ship_postcode'];
$post_data['ship_phone'] = $request['ship_phone'];
$post_data['ship_country'] = $request['ship_country'];

$post_data['shipping_method'] = $request['shipping_method'];
$post_data['product_name'] = $request['product_name'];
$post_data['product_category'] = $request['product_category'];
$post_data['product_profile'] = $request['product_profile'];

# OPTIONAL PARAMETERS
$post_data['value_a'] = $request['value_a'];
$post_data['value_b'] = $request['value_b'];
$post_data['value_c'] = $request['value_c'];
$post_data['value_d'] = $request['value_d'];



$sslc = new SamSSL();
# initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here )
$payment_options = $sslc->makePayment($post_data, 'hosted');

if (!is_array($payment_options)) {
print_r($payment_options);
$payment_options = array();
}

}

public function payViaAjax($request)
{


$post_data = array();
$post_data['total_amount'] = $request->total_amount; # You cant not pay less than 10
$post_data['currency'] = "BDT";
$post_data['tran_id'] = $request->tran_id; // tran_id must be unique
# CUSTOMER INFORMATION
$post_data['cus_name'] = $request->cus_name;
$post_data['cus_email'] = $request->cus_email;
$post_data['cus_add1'] = $request->cus_add1;
$post_data['cus_add2'] = $request->cus_add2;
$post_data['cus_city'] = $request->cus_city;
$post_data['cus_state'] = $request->cus_state;
$post_data['cus_postcode'] = $request->cus_postcode;
$post_data['cus_country'] = $request->cus_country;
$post_data['cus_phone'] = $request->cus_phone;
$post_data['cus_fax'] = $request->cus_fax;

# SHIPMENT INFORMATION
$post_data['ship_name'] = $request->ship_name;
$post_data['ship_add1'] = $request->ship_add1;
$post_data['ship_add2'] = $request->ship_add2;
$post_data['ship_city'] = $request->ship_city;
$post_data['ship_state'] = $request->ship_state;
$post_data['ship_postcode'] = $request->ship_postcode;
$post_data['ship_phone'] = $request->ship_phone;
$post_data['ship_country'] = $request->ship_country;

$post_data['shipping_method'] = $request->shipping_method;
$post_data['product_name'] = $request->product_name;
$post_data['product_category'] = $request->product_category;
$post_data['product_profile'] = $request->product_profile;

# OPTIONAL PARAMETERS
$post_data['value_a'] = $request->value_a;
$post_data['value_b'] = $request->value_b;
$post_data['value_c'] = $request->value_c;
$post_data['value_d'] = $request->value_d;

$sslc = new SamSSL();

$payment_options = $sslc->makePayment($post_data, 'checkout', 'json');

if (!is_array($payment_options)) {
print_r($payment_options);
$payment_options = array();
}

}

public function success(Request $request)
{

$tran_id = $request->input('tran_id');
$amount = $request->input('amount');
$currency = $request->input('currency');

$sslc = new SamSSL();

$validation = $sslc->orderValidate($request->all(), $tran_id, $amount, $currency);

if ($validation == TRUE) {


$result=$request->all();
$message='Transaction is Successful';
$status=1;
$route=$this->returnUrl;
return view('sslcommerz::index',compact('result','message','status','route'));

} else {

$result=$request->all();
$message='Validation Fail';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));



}

$result=$request->all();
$message='Something Wrong';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));




}

public function fail(Request $request)
{


$result=$request->all();
$message='Transaction is Fail';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));


}

public function cancel(Request $request)
{

$result=$request->all();
$message='Transaction is Fail';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));



}

public function ipn(Request $request)
{
#Received all the payement information from the gateway
if ($request->input('tran_id')) #Check transation id is posted or not.
{

echo "Transaction is Successful";

$tran_id = $request->input('tran_id');
$amount = $request->input('amount');
$currency = $request->input('currency');

$sslc = new SamSSL();

$validation = $sslc->orderValidate($request->all(), $tran_id, $amount, $currency);

if ($validation == TRUE) {


$result=$request->all();
$message='Transaction is Successful';
$status=1;
$route=$this->returnUrl;
return view('sslcommerz::index',compact('result','message','status','route'));

} else {

$result=$request->all();
$message='Validation Fail';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));


}


$result=$request->all();
$message='Something Wrong';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));

} else {

$result=$request->all();
$message='Something Wrong';
$status=0;
$route=$this->returnUrl;

return view('sslcommerz::index',compact('result','message','status','route'));

}
}

}
127 changes: 127 additions & 0 deletions src/Library/SslCommerz/AbstractSslCommerz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
namespace SAM\SSLCommerz\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, $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();
}
}
Loading

0 comments on commit ec034f0

Please sign in to comment.