CREATE TABLE `ec_orders` ( `id` bigint(20) NOT NULL, `order_no` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `user_meta_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `guest_id` bigint(20) DEFAULT NULL, `member_type_id` bigint(20) DEFAULT NULL, `coupon_id` bigint(20) DEFAULT NULL, `payment_type_meta_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `order_date` datetime DEFAULT NULL, `is_delevery` varchar(255) COLLATE utf8_unicode_ci DEFAULT 'pending', `is_paid` varchar(255) COLLATE utf8_unicode_ci DEFAULT '0', `transaction_voucher` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `create_by` bigint(20) DEFAULT NULL, `update_by` bigint(20) DEFAULT NULL, `create_date` date DEFAULT NULL, `update_date` date DEFAULT NULL, `process_date` datetime DEFAULT NULL, `process_by` bigint(20) DEFAULT NULL, `total` double DEFAULT NULL, `subtotal` float NOT NULL, `delivery_point_meta_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `total_in_kg` float DEFAULT NULL, `shipping_id` bigint(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Route::get('/pay', 'SslCommerzPaymentController@index'); /* first call */ Route::post('/pay-via-ajax', 'SslCommerzPaymentController@payViaAjax'); /* not use */ Route::post('/success', 'SslCommerzPaymentController@success'); Route::post('/fail', 'SslCommerzPaymentController@fail'); Route::post('/cancel', 'SslCommerzPaymentController@cancel'); Route::post('/ipn', 'SslCommerzPaymentController@ipn'); /* Received all the payement information from the gateway */
<?php namespace App\Http\Controllers; use DB; use Illuminate\Http\Request; use App\Library\SslCommerz\SslCommerzNotification; use Session; use App\User; use App\Models\Ecommerce\EcGuest; use App\Models\Ecommerce\EcWishlistProduct; use App\Models\Ecommerce\EcProdAmtPrice; use App\Models\Ecommerce\EcLocation; use Carbon\Carbon; use App\Models\Ecommerce\EcOrder; use App\Models\Ecommerce\EcAddrese; class SslCommerzPaymentController extends Controller { public function exampleEasyCheckout() { return view('exampleEasycheckout'); } public function exampleHostedCheckout() { return view('exampleHosted'); } public function index(Request $request) { # Here you have to receive all the order data to initate the payment. # Let's say, your oder transaction informations are saving in a table called "orders" # In "orders" table, order unique identity is "transaction_id". "status" field contain status of the transaction, "amount" is the order amount to be paid and "currency" is for storing Site Currency which will be checked with paid currency. $userIns = null; $guestIns = null; $dataList = Session::get('list'); $sucOrderArray = Session::get('sucOrderArray'); //print_r($sucOrderArray);die; $post_data = array(); $post_data['total_amount'] = @$dataList['grantTotal']; # You cant not pay less than 10 $priceUnitIns = EcOrder::select('ec_price_units.meta_name') ->join('ec_order_product_dtls','ec_order_product_dtls.order_id','=','ec_orders.id') ->join('ec_prod_amt_prices','ec_prod_amt_prices.meta_name','=','ec_order_product_dtls.prod_amt_price_meta_name') ->join('ec_price_units','ec_price_units.meta_name','=','ec_prod_amt_prices.price_unit_meta_name') ->where('ec_orders.order_no','=',$sucOrderArray['Order No']) ->first(); $post_data['currency'] = (@$priceUnitIns->meta_name =='tk')? 'BDT':"USD"; //$post_data['tran_id'] = uniqid(); // tran_id must be unique $post_data['tran_id'] = @$sucOrderArray['Order No']; if($dataList['user_meta_name']){ @$userIns = User::where('meta_name',@$dataList['user_meta_name'])->first(); }else{ @$guestIns = EcGuest::where('id',@$dataList['gustUserId'])->first(); } # CUSTOMER INFORMATION if(@$dataList['user_meta_name']){ $post_data['cus_name'] = @$userIns->first_name.' '.@$userIns->middle_name.' '.@$userIns->last_name; $post_data['cus_email'] = @$userIns->email; $post_data['cus_country'] = @EcLocation::where('id',@$userIns->userAddreses()->first()->location_country_id)->first()->title_en; $post_data['cus_phone'] = @$userIns->mobile; $post_data['cus_add1'] = @$userIns->userAddreses()->first()->address; $post_data['datetime'] = Carbon::now(); // additional }else{ $post_data['cus_name'] = @$guestIns->full_name; $post_data['cus_email'] = @$guestIns->email; $post_data['cus_country'] = "Bangladesh"; $post_data['cus_phone'] = @$guestIns->mobile; $post_data['cus_add1'] = @$guestIns->address; $post_data['datetime'] = Carbon::now(); // additional } $post_data['cus_add2'] = ""; $post_data['cus_city'] = ""; $post_data['cus_state'] = ""; $post_data['cus_postcode'] = ""; $post_data['cus_fax'] = ""; # SHIPMENT INFORMATION if(@$dataList['user_meta_name']){ $post_data['ship_name'] = "Store Test"; $post_data['ship_add1'] = @EcAddrese::where('user_meta_name',@$userIns->meta_name)->where('address_type','shipping')->first()->address; $post_data['ship_add2'] = ""; $post_data['ship_city'] = @EcLocation::where('id',@EcAddrese::where('user_meta_name',$userIns->meta_name)->where('address_type','shipping')->first()->location_upazila_id)->first()->title_en; $post_data['ship_state'] = @EcLocation::where('id',@EcAddrese::where('user_meta_name',$userIns->meta_name)->where('address_type','shipping')->first()->location_district_id)->first()->title_en; $post_data['ship_postcode'] =@EcAddrese::where('user_meta_name',@$userIns->meta_name)->where('address_type','shipping')->first()->zip; $post_data['ship_phone'] = @EcAddrese::where('user_meta_name',@$userIns->meta_name)->where('address_type','shipping')->first()->mobile; $post_data['ship_country'] = @EcLocation::where('id',@$userIns->userAddreses()->first()->location_country_id)->first()->title_en; }else{ $post_data['ship_name'] = "Store Test"; $post_data['ship_add1'] = @$guestIns->shipping_address; $post_data['ship_add2'] = ""; $post_data['ship_city'] = ""; $post_data['ship_state'] = ""; $post_data['ship_postcode'] = @$guestIns->delivery_area_zip; $post_data['ship_phone'] = @$guestIns->mobile; $post_data['ship_country'] = "Bangladesh"; } $post_data['shipping_method'] = "NO"; $post_data['product_name'] = "Computer"; $post_data['product_category'] = "Goods"; $post_data['product_profile'] = "physical-goods"; # OPTIONAL PARAMETERS $post_data['value_a'] = "ref001"; $post_data['value_b'] = "ref002"; $post_data['value_c'] = "ref003"; $post_data['value_d'] = "ref004"; #Before going to initiate the payment order status need to insert or update as Pending. $update_product = DB::table('ec_order_banks') ->where('transaction_id', $post_data['tran_id']) ->updateOrInsert([ 'name' => $post_data['cus_name'], 'email' => $post_data['cus_email'], 'phone' => $post_data['cus_phone'], 'amount' => $post_data['total_amount'], 'status' => 'Pending', 'address' => $post_data['cus_add1'], 'transaction_id' => $post_data['tran_id'], 'currency' => $post_data['currency'], 'datetime' => $post_data['datetime'] ]); //dd($update_product); $sslc = new SslCommerzNotification(); # 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(); return redirect()->route('ordermanage/orderSuc'); } } public function payViaAjax(Request $request) { # Here you have to receive all the order data to initate the payment. # Lets your oder trnsaction informations are saving in a table called "orders" # In orders table order uniq identity is "transaction_id","status" field contain status of the transaction, "amount" is the order amount to be paid and "currency" is for storing Site Currency which will be checked with paid currency. $post_data = array(); $post_data['total_amount'] = '10'; # You cant not pay less than 10 $post_data['currency'] = "BDT"; $post_data['tran_id'] = uniqid(); // tran_id must be unique # CUSTOMER INFORMATION $post_data['cus_name'] = 'Customer Name'; $post_data['cus_email'] = 'customer@mail.com'; $post_data['cus_add1'] = 'Customer Address'; $post_data['cus_add2'] = ""; $post_data['cus_city'] = ""; $post_data['cus_state'] = ""; $post_data['cus_postcode'] = ""; $post_data['cus_country'] = "Bangladesh"; $post_data['cus_phone'] = '8801XXXXXXXXX'; $post_data['cus_fax'] = ""; # SHIPMENT INFORMATION $post_data['ship_name'] = "Store Test"; $post_data['ship_add1'] = "Dhaka"; $post_data['ship_add2'] = "Dhaka"; $post_data['ship_city'] = "Dhaka"; $post_data['ship_state'] = "Dhaka"; $post_data['ship_postcode'] = "1000"; $post_data['ship_phone'] = ""; $post_data['ship_country'] = "Bangladesh"; $post_data['shipping_method'] = "NO"; $post_data['product_name'] = "Computer"; $post_data['product_category'] = "Goods"; $post_data['product_profile'] = "physical-goods"; # OPTIONAL PARAMETERS $post_data['value_a'] = "ref001"; $post_data['value_b'] = "ref002"; $post_data['value_c'] = "ref003"; $post_data['value_d'] = "ref004"; #Before going to initiate the payment order status need to update as Pending. $update_product = DB::table('orders') ->where('transaction_id', $post_data['tran_id']) ->updateOrInsert([ 'name' => $post_data['cus_name'], 'email' => $post_data['cus_email'], 'phone' => $post_data['cus_phone'], 'amount' => $post_data['total_amount'], 'status' => 'Pending', 'address' => $post_data['cus_add1'], 'transaction_id' => $post_data['tran_id'], 'currency' => $post_data['currency'] ]); $sslc = new SslCommerzNotification(); # initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here ) $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) { //echo "Transaction is Successful"; $tran_id = $request->input('tran_id'); $amount = $request->input('amount'); $currency = $request->input('currency'); @$card_type = $request->input('card_type'); @$card_no = $request->input('card_no'); $sslc = new SslCommerzNotification(); #Check order status in order tabel against the transaction id or order id. $order_detials = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->select('transaction_id', 'status', 'currency', 'amount')->first(); if ($order_detials->status == 'Pending') { $validation = $sslc->orderValidate($tran_id, $amount, $currency, $request->all()); if ($validation == TRUE) { /* That means IPN did not work or IPN URL was not set in your merchant panel. Here you need to update order status in order table as Processing or Complete. Here you can also sent sms or email for successfull transaction to customer */ $update_product = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->update(['status' => 'Processing','card_type'=>@$card_type,'card_no'=>@$card_no]); //echo "
Transaction is successfully Completed"; return redirect('ordermanage/orderSuc'); } else { /* That means IPN did not work or IPN URL was not set in your merchant panel and Transation validation failed. Here you need to update order status as Failed in order table. */ $update_product = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->update(['status' => 'Failed','card_type'=>$card_type,'card_no'=>$card_no]); echo "validation Fail"; Session::forget('list'); Session::forget('sucOrderArray'); } } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') { /* That means through IPN Order status already updated. Now you can just show the customer that transaction is completed. No need to udate database. */ //echo "Transaction is successfully Completed"; return redirect('ordermanage/orderSuc'); } else { #That means something wrong happened. You can redirect customer to your product page. echo "Invalid Transaction"; Session::forget('list'); Session::forget('sucOrderArray'); } } public function fail(Request $request) { $tran_id = $request->input('tran_id'); $cart_type = $request->input('cart_type'); $cart_no = $request->input('cart_no'); $order_detials = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->select('transaction_id', 'status', 'currency', 'amount')->first(); if ($order_detials->status == 'Pending') { $update_product = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->update(['status' => 'Failed','card_type'=>$card_type,'card_no'=>$card_no]); echo "Transaction is Falied"; Session::forget('list'); Session::forget('sucOrderArray'); } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') { echo "Transaction is already Successful"; Session::forget('list'); Session::forget('sucOrderArray'); } else { echo "Transaction is Invalid"; Session::forget('list'); Session::forget('sucOrderArray'); } } public function cancel(Request $request) { $tran_id = $request->input('tran_id'); $order_detials = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->select('transaction_id', 'status', 'currency', 'amount')->first(); if ($order_detials->status == 'Pending') { $update_product = DB::table('ec_order_banks') ->where('transaction_id', $tran_id) ->update(['status' => 'Canceled']); echo "Transaction is Cancel"; Session::forget('list'); Session::forget('sucOrderArray'); } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') { echo "Transaction is already Successful"; Session::forget('list'); Session::forget('sucOrderArray'); } else { echo "Transaction is Invalid"; Session::forget('list'); Session::forget('sucOrderArray'); } } 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. { $tran_id = $request->input('tran_id'); #Check order status in order tabel against the transaction id or order id. $order_details = DB::table('orders') ->where('transaction_id', $tran_id) ->select('transaction_id', 'status', 'currency', 'amount')->first(); if ($order_details->status == 'Pending') { $sslc = new SslCommerzNotification(); $validation = $sslc->orderValidate($tran_id, $order_details->amount, $order_details->currency, $request->all()); if ($validation == TRUE) { /* That means IPN worked. Here you need to update order status in order table as Processing or Complete. Here you can also sent sms or email for successful transaction to customer */ $update_product = DB::table('orders') ->where('transaction_id', $tran_id) ->update(['status' => 'Processing']); echo "Transaction is successfully Completed"; } else { /* That means IPN worked, but Transation validation failed. Here you need to update order status as Failed in order table. */ $update_product = DB::table('orders') ->where('transaction_id', $tran_id) ->update(['status' => 'Failed']); echo "validation Fail"; } } else if ($order_details->status == 'Processing' || $order_details->status == 'Complete') { #That means Order status already updated. No need to udate database. echo "Transaction is already successfully Completed"; } else { #That means something wrong happened. You can redirect customer to your product page. echo "Invalid Transaction"; } } else { echo "Invalid Data"; } } }
CREATE TABLE 'ec_order_banks' ( `id` int(11) NOT NULL, `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `email` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL, `phone` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `amount` double DEFAULT NULL, `address` text COLLATE utf8_unicode_ci DEFAULT NULL, `status` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, `transaction_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `datetime` datetime DEFAULT NULL, `currency` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `card_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `card_no` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
<?php
namespace App\Library\SslCommerz;
interface SslCommerzInterface
{
public function makePayment(array $data);
public function orderValidate($trxID, $amount, $currency, $requestData);
public function setParams($data);
public function setRequiredInfo(array $data);
public function setCustomerInfo(array $data);
public function setShipmentInfo(array $data);
public function setProductInfo(array $data);
public function setAdditionalInfo(array $data);
public function callToApi($data, $header = [], $setLocalhost = false);
}
<?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;
return "https://securepay.sslcommerz.com/gwprocess/v4/a_XXXXXXX.php";
}
/**
* @param $data
* @param array $header
* @param bool $setLocalhost
* @return bool|string
*/
public function callToApi($data, $header = [], $setLocalhost = false)
{
$curl = curl_init();
//dd($this->getApiUrl() );
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);
//dd($response);
$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);
//dd($sslcz);
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() != "" && $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' => "JSON Data parsing error!"]);
}
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();
}
}
<?php
namespace App\Library\SslCommerz;
class SslCommerzNotification extends AbstractSslCommerz
{
protected $data = [];
protected $config = [];
private $successUrl;
private $cancelUrl;
private $failedUrl;
private $error;
/**
* SslCommerzNotification constructor.
*/
public function __construct()
{
$this->config = config('sslcommerz');
//print_r($this->config);die;
$this->setStoreId("storeId"); /* store id provided by ssl*/
$this->setStorePassword("password") /* store password provided by ssl*/
$this->setApiUrl("url"); /* api url provided by ssl*/
$this->config['apiDomain'] = "url"; /* api url provided by ssl*/
}
public function orderValidate($trx_id = '', $amount = 0, $currency = "BDT", $post_data)
{
if ($post_data == '' && $trx_id == '' && !is_array($post_data)) {
$this->error = "Please provide valid transaction ID and post request data";
return $this->error;
}
$validation = $this->validate($trx_id, $amount, $currency, $post_data);
if ($validation) {
return true;
} else {
return false;
}
}
# VALIDATE SSLCOMMERZ TRANSACTION
protected function validate($merchant_trans_id, $merchant_trans_amount, $merchant_trans_currency, $post_data)
{
/*.....*/
}
# FUNCTION TO CHECK HASH VALUE
protected function SSLCOMMERZ_hash_varify($store_passwd = "", $post_data)
{
/*.....*/
}
public function makePayment(array $requestData, $type = 'checkout', $pattern = 'json')
{
/* .... */
}
protected function setSuccessUrl()
{
$this->successUrl = url('/') . $this->config['success_url'];
}
protected function getSuccessUrl()
{
return $this->successUrl;
}
protected function setFailedUrl()
{
$this->failedUrl = url('/') . $this->config['failed_url'];
}
protected function getFailedUrl()
{
return $this->failedUrl;
}
protected function setCancelUrl()
{
$this->cancelUrl = url('/') . $this->config['cancel_url'];
}
protected function getCancelUrl()
{
return $this->cancelUrl;
}
public function setParams($requestData)
{
/* ... */
}
public function setAuthenticationInfo()
{
$this->data['store_id'] = $this->getStoreId();
$this->data['store_passwd'] = $this->getStorePassword();
return $this->data;
}
public function setRequiredInfo(array $info)
{
return $this->data;
}
public function setCustomerInfo(array $info)
{
return $this->data;
}
public function setShipmentInfo(array $info)
{
return $this->data;
}
public function setProductInfo(array $info)
{
return $this->data;
}
public function setAdditionalInfo(array $info)
{
return $this->data;
}
}
Total : 21660
Today :14
Today Visit Country :