SslcommerzController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\CombinedOrder;
  6. use App\Http\Controllers\CheckoutController;
  7. use App\Http\Controllers\WalletController;
  8. use App\Models\CustomerPackage;
  9. use App\Models\SellerPackage;
  10. use App\Http\Controllers\CustomerPackageController;
  11. use App\Http\Controllers\SellerPackageController;
  12. use App\Http\Controllers\SSLCommerz;
  13. use App\Models\User;
  14. use Session;
  15. use Auth;
  16. session_start();
  17. class SslcommerzController extends Controller
  18. {
  19. public function pay(Request $request)
  20. {
  21. # Here you have to receive all the order data to initate the payment.
  22. # Lets your oder trnsaction informations are saving in a table called "orders"
  23. # In orders table order uniq identity is "order_id","order_status" field contain status of the transaction, "grand_total" is the order amount to be paid and "currency" is for storing Site Currency which will be checked with paid currency.
  24. if(Session::has('payment_type')){
  25. if(Session::get('payment_type') == 'cart_payment'){
  26. $combined_order = CombinedOrder::findOrFail($request->session()->get('combined_order_id'));
  27. $post_data = array();
  28. $post_data['total_amount'] = $combined_order->grand_total; # You cant not pay less than 10
  29. $post_data['currency'] = "BDT";
  30. $post_data['tran_id'] = substr(md5($request->session()->get('combined_order_id')), 0, 10); // tran_id must be unique
  31. $post_data['value_a'] = $post_data['tran_id'];
  32. $post_data['value_b'] = $request->session()->get('combined_order_id');
  33. $post_data['value_c'] = $request->session()->get('payment_type');
  34. #Start to save these value in session to pick in success page.
  35. // $_SESSION['payment_values']['tran_id']=$post_data['tran_id'];
  36. // $_SESSION['payment_values']['order_id']=$request->session()->get('order_id');
  37. // $_SESSION['payment_values']['payment_type']=$request->session()->get('payment_type');
  38. #End to save these value in session to pick in success page.
  39. # CUSTOMER INFORMATION
  40. // $post_data['cus_name'] = $request->session()->get('shipping_info')['name'];
  41. // $post_data['cus_add1'] = $request->session()->get('shipping_info')['address'];
  42. // $post_data['cus_city'] = $request->session()->get('shipping_info')['city'];
  43. // $post_data['cus_postcode'] = $request->session()->get('shipping_info')['postal_code'];
  44. // $post_data['cus_country'] = $request->session()->get('shipping_info')['country'];
  45. // $post_data['cus_phone'] = $request->session()->get('shipping_info')['phone'];
  46. // $post_data['cus_email'] = $request->session()->get('shipping_info')['email'];
  47. }
  48. elseif (Session::get('payment_type') == 'wallet_payment') {
  49. $post_data = array();
  50. $post_data['total_amount'] = $request->session()->get('payment_data')['amount']; # You cant not pay less than 10
  51. $post_data['currency'] = "BDT";
  52. $post_data['tran_id'] = substr(md5(Auth::user()->id), 0, 10); // tran_id must be unique
  53. $post_data['value_a'] = $post_data['tran_id'];
  54. $post_data['value_b'] = $request->session()->get('payment_data')['amount'];
  55. $post_data['value_c'] = $request->session()->get('payment_type');
  56. $post_data['value_d'] = Auth::user()->id;
  57. #Start to save these value in session to pick in success page.
  58. // $_SESSION['payment_values']['tran_id']=$post_data['tran_id'];
  59. // $_SESSION['payment_values']['payment_data']=$request->session()->get('payment_data');
  60. // $_SESSION['payment_values']['payment_type']=$request->session()->get('payment_type');
  61. #End to save these value in session to pick in success page.
  62. }
  63. elseif (Session::get('payment_type') == 'customer_package_payment') {
  64. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  65. $post_data = array();
  66. $post_data['total_amount'] = $customer_package->amount; # You cant not pay less than 10
  67. $post_data['currency'] = "BDT";
  68. $post_data['tran_id'] = substr(md5(Auth::user()->id), 0, 10); // tran_id must be unique
  69. $post_data['value_a'] = $post_data['tran_id'];
  70. $post_data['value_b'] = $request->session()->get('payment_data')['customer_package_id'];
  71. $post_data['value_c'] = $request->session()->get('payment_type');
  72. $post_data['value_d'] = Auth::user()->id;
  73. #Start to save these value in session to pick in success page.
  74. // $_SESSION['payment_values']['tran_id']=$post_data['tran_id'];
  75. // $_SESSION['payment_values']['payment_data']=$request->session()->get('payment_data');
  76. // $_SESSION['payment_values']['payment_type']=$request->session()->get('payment_type');
  77. #End to save these value in session to pick in success page.
  78. }
  79. elseif (Session::get('payment_type') == 'seller_package_payment') {
  80. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  81. $post_data = array();
  82. $post_data['total_amount'] = $seller_package->amount; # You cant not pay less than 10
  83. $post_data['currency'] = "BDT";
  84. $post_data['tran_id'] = substr(md5(Auth::user()->id), 0, 10); // tran_id must be unique
  85. $post_data['value_a'] = $post_data['tran_id'];
  86. $post_data['value_b'] = $request->session()->get('payment_data')['seller_package_id'];
  87. $post_data['value_c'] = $request->session()->get('payment_type');
  88. $post_data['value_d'] = Auth::user()->id;
  89. #Start to save these value in session to pick in success page.
  90. // $_SESSION['payment_values']['tran_id']=$post_data['tran_id'];
  91. // $_SESSION['payment_values']['payment_data']=$request->session()->get('payment_data');
  92. // $_SESSION['payment_values']['payment_type']=$request->session()->get('payment_type');
  93. #End to save these value in session to pick in success page.
  94. }
  95. # CUSTOMER INFORMATION
  96. $user = Auth::user();
  97. $post_data['cus_name'] = $user->name;
  98. $post_data['cus_add1'] = $user->address;
  99. $post_data['cus_city'] = $user->city;
  100. $post_data['cus_postcode'] = $user->postal_code;
  101. $post_data['cus_country'] = $user->country;
  102. $post_data['cus_phone'] = $user->phone;
  103. $post_data['cus_email'] = $user->email;
  104. }
  105. $server_name=$request->root()."/";
  106. $post_data['success_url'] = $server_name . "sslcommerz/success";
  107. $post_data['fail_url'] = $server_name . "sslcommerz/fail";
  108. $post_data['cancel_url'] = $server_name . "sslcommerz/cancel";
  109. // dd($post_data);
  110. # SHIPMENT INFORMATION
  111. // $post_data['ship_name'] = 'ship_name';
  112. // $post_data['ship_add1 '] = 'Ship_add1';
  113. // $post_data['ship_add2'] = "";
  114. // $post_data['ship_city'] = "";
  115. // $post_data['ship_state'] = "";
  116. // $post_data['ship_postcode'] = "";
  117. // $post_data['ship_country'] = "Bangladesh";
  118. # OPTIONAL PARAMETERS
  119. // $post_data['value_a'] = "ref001";
  120. // $post_data['value_b'] = "ref002";
  121. // $post_data['value_c'] = "ref003";
  122. // $post_data['value_d'] = "ref004";
  123. $sslc = new SSLCommerz();
  124. # initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here )
  125. $payment_options = $sslc->initiate($post_data, false);
  126. if (!is_array($payment_options)) {
  127. print_r($payment_options);
  128. $payment_options = array();
  129. }
  130. }
  131. public function success(Request $request)
  132. {
  133. //echo "Transaction is Successful";
  134. $sslc = new SSLCommerz();
  135. #Start to received these value from session. which was saved in index function.
  136. $tran_id = $request->value_a;
  137. #End to received these value from session. which was saved in index function.
  138. $payment = json_encode($request->all());
  139. if(isset($request->value_c)){
  140. if($request->value_c == 'cart_payment'){
  141. return (new CheckoutController)->checkout_done($request->value_b, $payment);
  142. }
  143. elseif ($request->value_c == 'wallet_payment') {
  144. $data['amount'] = $request->value_b;
  145. $data['payment_method'] = 'sslcommerz';
  146. Auth::login(User::find($request->value_d));
  147. return (new WalletController)->wallet_payment_done($data, $payment);
  148. }
  149. elseif ($request->value_c == 'customer_package_payment') {
  150. $data['customer_package_id'] = $request->value_b;
  151. $data['payment_method'] = 'sslcommerz';
  152. Auth::login(User::find($request->value_d));
  153. return (new CustomerPackageController)->purchase_payment_done($data, $payment);
  154. }
  155. elseif ($request->value_c == 'seller_package_payment') {
  156. $data['seller_package_id'] = $request->value_b;
  157. $data['payment_method'] = 'sslcommerz';
  158. Auth::login(User::find($request->value_d));
  159. return (new SellerPackageController)->purchase_payment_done(json_decode($request->value_b), $payment);
  160. }
  161. }
  162. }
  163. public function fail(Request $request)
  164. {
  165. $request->session()->forget('order_id');
  166. $request->session()->forget('payment_data');
  167. flash(translate('Payment Failed'))->warning();
  168. return redirect()->route('home');
  169. }
  170. public function cancel(Request $request)
  171. {
  172. $request->session()->forget('order_id');
  173. $request->session()->forget('payment_data');
  174. flash(translate('Payment cancelled'))->error();
  175. return redirect()->route('home');
  176. }
  177. public function ipn(Request $request)
  178. {
  179. #Received all the payement information from the gateway
  180. if($request->input('tran_id')) #Check transation id is posted or not.
  181. {
  182. $tran_id = $request->input('tran_id');
  183. #Check order status in order tabel against the transaction id or order id.
  184. $combined_order = CombinedOrder::findOrFail($request->session()->get('combined_order_id'));
  185. if($order->payment_status =='Pending')
  186. {
  187. $sslc = new SSLCommerz();
  188. $validation = $sslc->orderValidate($tran_id, $order->grand_total, 'BDT', $request->all());
  189. if($validation == TRUE)
  190. {
  191. /*
  192. That means IPN worked. Here you need to update order status
  193. in order table as Processing or Complete.
  194. Here you can also sent sms or email for successfull transaction to customer
  195. */
  196. echo "Transaction is successfully Complete";
  197. }
  198. else
  199. {
  200. /*
  201. That means IPN worked, but Transation validation failed.
  202. Here you need to update order status as Failed in order table.
  203. */
  204. echo "validation Fail";
  205. }
  206. }
  207. }
  208. else
  209. {
  210. echo "Inavalid Data";
  211. }
  212. }
  213. }