PayhereController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\CheckoutController;
  5. use App\Models\User;
  6. use App\Models\Wallet;
  7. use App\Models\CombinedOrder;
  8. use App\Utility\PayhereUtility;
  9. use App\Models\CustomerPackage;
  10. use Session;
  11. use Auth;
  12. use Illuminate\Http\Request;
  13. class PayhereController extends Controller
  14. {
  15. private $security_key;
  16. public function __construct()
  17. {
  18. }
  19. public function pay(Request $request){
  20. if(Session::has('payment_type')){
  21. if(Session::get('payment_type') == 'cart_payment'){
  22. $combined_order = CombinedOrder::findOrFail($request->session()->get('combined_order_id'));
  23. $combined_order_id = $combined_order->id;
  24. $amount = $combined_order->grand_total;
  25. $first_name = json_decode($combined_order->shipping_address)->name;
  26. $last_name = 'X';
  27. $phone = json_decode($combined_order->shipping_address)->phone;
  28. $email = json_decode($combined_order->shipping_address)->email;
  29. $address = json_decode($combined_order->shipping_address)->address;
  30. $city = json_decode($combined_order->shipping_address)->city;
  31. return PayhereUtility::create_checkout_form($combined_order_id, $amount, $first_name, $last_name, $phone, $email, $address, $city);
  32. }
  33. elseif (Session::get('payment_type') == 'wallet_payment') {
  34. $order_id = rand(100000, 999999);
  35. $user_id = Auth::user()->id;
  36. $amount = $request->amount;
  37. $first_name = Auth::user()->name;
  38. $last_name = 'X';
  39. $phone = '123456789';
  40. $email = Auth::user()->email;
  41. $address = 'dummy address';
  42. $city = 'Colombo';
  43. return PayhereUtility::create_wallet_form($user_id, $order_id, $amount, $first_name, $last_name, $phone, $email, $address, $city);
  44. }
  45. elseif (Session::get('payment_type') == 'customer_package_payment') {
  46. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  47. $order_id = rand(100000, 999999);
  48. $user_id = Auth::user()->id;
  49. $package_id = $request->customer_package_id;
  50. $amount = $customer_package->amount;
  51. $first_name = Auth::user()->name;
  52. $last_name = 'X';
  53. $phone = '123456789';
  54. $email = Auth::user()->email;
  55. $address = 'dummy address';
  56. $city = 'Colombo';
  57. return PayhereUtility::create_customer_package_form($user_id, $package_id, $order_id, $amount, $first_name, $last_name, $phone, $email, $address, $city);
  58. }
  59. }
  60. }
  61. public function checkout_testing()
  62. {
  63. $order_id = rand(100000, 999999);
  64. $amount = 88.00;
  65. $first_name = 'Hasan';
  66. $last_name = 'Taluker';
  67. $phone = '2135421321';
  68. $email = 'hasan@taluker.com';
  69. $address = '22/b baker street';
  70. $city = 'Colombo';
  71. return PayhereUtility::create_checkout_form($order_id, $amount, $first_name, $last_name, $phone, $email, $address, $city);
  72. }
  73. public function wallet_testing()
  74. {
  75. $order_id = rand(100000, 999999);
  76. $user_id = Auth::user()->id;
  77. $amount = 88.00;
  78. $first_name = 'Hasan';
  79. $last_name = 'Taluker';
  80. $phone = '2135421321';
  81. $email = 'hasan@taluker.com';
  82. $address = '22/b baker street';
  83. $city = 'Colombo';
  84. return PayhereUtility::create_wallet_form($user_id, $order_id, $amount, $first_name, $last_name, $phone, $email, $address, $city);
  85. }
  86. public function customer_package_payment_testing()
  87. {
  88. $order_id = rand(100000, 999999);
  89. $user_id = Auth::user()->id;
  90. $package_id = 4;
  91. $amount = 88.00;
  92. $first_name = 'Hasan';
  93. $last_name = 'Taluker';
  94. $phone = '2135421321';
  95. $email = 'hasan@taluker.com';
  96. $address = '22/b baker street';
  97. $city = 'Colombo';
  98. return PayhereUtility::create_customer_package_form($user_id,$package_id, $order_id, $amount, $first_name, $last_name, $phone, $email, $address, $city);
  99. }
  100. //sample response
  101. /*
  102. {
  103. "merchant_id":"1215091",
  104. "order_id":"196696714",
  105. "payment_id":"320025078020",
  106. "payhere_amount":"99.00",
  107. "payhere_currency":"LKR",
  108. "status_code":"2",
  109. "md5sig":"F889DBDF7BF987529C77096E465B749B",
  110. "custom_1":"788392",
  111. "custom_2":"",
  112. "status_message":"Successfully completed the payment.",
  113. "method":"TEST",
  114. "card_holder_name":"ddd",
  115. "card_no":"************1292",
  116. "card_expiry":"1221",
  117. "recurring":"0"
  118. }
  119. */
  120. //checkout related functions ------------------------------------<starts>
  121. public static function checkout_notify()
  122. {
  123. $merchant_id = $_POST['merchant_id'];
  124. $order_id = $_POST['order_id'];
  125. $payhere_amount = $_POST['payhere_amount'];
  126. $payhere_currency = $_POST['payhere_currency'];
  127. $status_code = $_POST['status_code'];
  128. $md5sig = $_POST['md5sig'];
  129. $merchant_secret = env('PAYHERE_SECRET'); // Replace with your Merchant Secret (Can be found on your PayHere account's Settings page)
  130. $local_md5sig = strtoupper(md5($merchant_id . $order_id . $payhere_amount . $payhere_currency . $status_code . strtoupper(md5($merchant_secret))));
  131. if (($local_md5sig === $md5sig) and ($status_code == 2)) {
  132. //custom_1 will have order_id
  133. return PayhereController::checkout_success($_POST['custom_1'],$_POST);
  134. }
  135. return PayhereController::checkout_incomplete();
  136. }
  137. public static function checkout_return()
  138. {
  139. Session::put('cart', collect([]));
  140. Session::forget('payment_type');
  141. Session::forget('delivery_info');
  142. Session::forget('coupon_id');
  143. Session::forget('coupon_discount');
  144. flash(translate('Payment process completed'))->success();
  145. return redirect()->route('order_confirmed');
  146. }
  147. public static function checkout_cancel()
  148. {
  149. return PayhereController::checkout_incomplete();
  150. }
  151. public static function checkout_success($combined_order_id,$responses)
  152. {
  153. $payment_details = json_encode($responses);
  154. $checkoutController = new CheckoutController;
  155. return $checkoutController->checkout_done($combined_order_id, $payment_details);
  156. }
  157. public static function checkout_incomplete()
  158. {
  159. Session::forget('order_id');
  160. flash(translate("Incomplete"))->error();
  161. //flash(translate('Payment failed'))->error();
  162. //dd($response_text);
  163. return redirect()->route('home')->send();
  164. }
  165. //checkout related functions ------------------------------------<ends>
  166. //wallet related functions ------------------------------------<starts>
  167. public static function wallet_notify()
  168. {
  169. $merchant_id = $_POST['merchant_id'];
  170. $order_id = $_POST['order_id'];
  171. $payhere_amount = $_POST['payhere_amount'];
  172. $payhere_currency = $_POST['payhere_currency'];
  173. $status_code = $_POST['status_code'];
  174. $md5sig = $_POST['md5sig'];
  175. $merchant_secret = env('PAYHERE_SECRET'); // Replace with your Merchant Secret (Can be found on your PayHere account's Settings page)
  176. $local_md5sig = strtoupper(md5($merchant_id . $order_id . $payhere_amount . $payhere_currency . $status_code . strtoupper(md5($merchant_secret))));
  177. if (($local_md5sig === $md5sig) and ($status_code == 2)) {
  178. //custom_1 will have user_id
  179. return PayhereController::wallet_success($_POST['custom_1'],$payhere_amount,$_POST);
  180. }
  181. return PayhereController::wallet_incomplete();
  182. }
  183. public static function wallet_return()
  184. {
  185. Session::forget('payment_data');
  186. Session::forget('payment_type');
  187. flash(translate('Payment process completed'))->success();
  188. return redirect()->route('wallet.index');
  189. }
  190. public static function wallet_cancel()
  191. {
  192. return PayhereController::wallet_incomplete();
  193. }
  194. public static function wallet_success($id,$amount,$payment_details)
  195. {
  196. $user = User::find($id);
  197. $user->balance = $user->balance + $amount;
  198. $user->save();
  199. $wallet = new Wallet;
  200. $wallet->user_id = $user->id;
  201. $wallet->amount = $amount;
  202. $wallet->payment_method = 'payhere';
  203. $wallet->payment_details = json_encode($payment_details);
  204. $wallet->save();
  205. }
  206. public static function wallet_incomplete()
  207. {
  208. Session::forget('payment_data');
  209. flash(translate('Payment Incomplete'))->error();
  210. return redirect()->route('home')->send();
  211. }
  212. //wallet related functions ------------------------------------<ends>
  213. //customer package related functions ------------------------------------<starts>
  214. public static function customer_package_notify()
  215. {
  216. $merchant_id = $_POST['merchant_id'];
  217. $order_id = $_POST['order_id'];
  218. $payhere_amount = $_POST['payhere_amount'];
  219. $payhere_currency = $_POST['payhere_currency'];
  220. $status_code = $_POST['status_code'];
  221. $md5sig = $_POST['md5sig'];
  222. $merchant_secret = env('PAYHERE_SECRET'); // Replace with your Merchant Secret (Can be found on your PayHere account's Settings page)
  223. $local_md5sig = strtoupper(md5($merchant_id . $order_id . $payhere_amount . $payhere_currency . $status_code . strtoupper(md5($merchant_secret))));
  224. if (($local_md5sig === $md5sig) and ($status_code == 2)) {
  225. //custom_1 will have user_id custom_2 will have package_id
  226. return PayhereController::customer_package_success($_POST['custom_1'],$_POST['custom_2'],$_POST);
  227. }
  228. return PayhereController::customer_package_incomplete();
  229. }
  230. public static function customer_package_return()
  231. {
  232. Session::forget('payment_data');
  233. flash(translate('Payment process completed'))->success();
  234. return redirect()->route('dashboard');
  235. }
  236. public static function customer_package_cancel()
  237. {
  238. return PayhereController::customer_package_incomplete();
  239. }
  240. public static function customer_package_success($id,$customer_package_id,$payment_details)
  241. {
  242. $user = User::findOrFail($id);
  243. $user->customer_package_id = $customer_package_id;
  244. $customer_package = CustomerPackage::findOrFail($customer_package_id);
  245. $user->remaining_uploads += $customer_package->product_upload;
  246. $user->save();
  247. }
  248. public static function customer_package_incomplete()
  249. {
  250. Session::forget('payment_data');
  251. flash(translate("Payment Incomplete"))->error();
  252. return redirect()->route('home')->send();
  253. }
  254. //customer package related functions ------------------------------------<ends>
  255. }