InstamojoController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Models\BusinessSetting;
  7. use App\Models\CustomerPackage;
  8. use App\Models\SellerPackage;
  9. use App\Http\Controllers\CustomerPackageController;
  10. use App\Http\Controllers\SellerPackageController;
  11. use App\Http\Controllers\WalletController;
  12. use App\Http\Controllers\CheckoutController;
  13. use Session;
  14. use Auth;
  15. class InstamojoController extends Controller
  16. {
  17. public function pay(){
  18. if(Session::has('payment_type')){
  19. if(BusinessSetting::where('type', 'instamojo_sandbox')->first()->value == 1){
  20. // testing_url
  21. $endPoint = 'https://test.instamojo.com/api/1.1/';
  22. }
  23. else{
  24. // live_url
  25. $endPoint = 'https://www.instamojo.com/api/1.1/';
  26. }
  27. $api = new \Instamojo\Instamojo(
  28. env('IM_API_KEY'),
  29. env('IM_AUTH_TOKEN'),
  30. $endPoint
  31. );
  32. if(Session::get('payment_type') == 'cart_payment'){
  33. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  34. if(preg_match_all('/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/im', Auth::user()->phone)) {
  35. try {
  36. $response = $api->paymentRequestCreate(array(
  37. "purpose" => ucfirst(str_replace('_', ' ', Session::get('payment_type'))),
  38. "amount" => round($combined_order->grand_total),
  39. "send_email" => false,
  40. "email" => Auth::user()->email,
  41. "phone" => Auth::user()->phone,
  42. "redirect_url" => url('instamojo/payment/pay-success')
  43. ));
  44. return redirect($response['longurl']);
  45. } catch (\Exception $e) {
  46. print('Error: ' . $e->getMessage());
  47. }
  48. } else {
  49. flash('Please add phone number to your profile')->warning();
  50. return redirect()->route('profile');
  51. }
  52. }
  53. elseif (Session::get('payment_type') == 'wallet_payment') {
  54. if (preg_match_all('/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/im', Auth::user()->phone)) {
  55. try {
  56. $response = $api->paymentRequestCreate(array(
  57. "purpose" => ucfirst(str_replace('_', ' ', Session::get('payment_type'))),
  58. "amount" => round(Session::get('payment_data')['amount']),
  59. "send_email" => false,
  60. "email" => Auth::user()->email,
  61. "phone" => Auth::user()->phone,
  62. "redirect_url" => url('instamojo/payment/pay-success')
  63. ));
  64. return redirect($response['longurl']);
  65. // dd($response);
  66. } catch (\Exception $e) {
  67. return back();
  68. }
  69. } else {
  70. flash('Please add phone number to your profile')->warning();
  71. return redirect()->route('profile');
  72. }
  73. }
  74. elseif (Session::get('payment_type') == 'customer_package_payment') {
  75. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  76. if (preg_match_all('/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/im', Auth::user()->phone)) {
  77. try {
  78. $response = $api->paymentRequestCreate(array(
  79. "purpose" => ucfirst(str_replace('_', ' ', Session::get('payment_type'))),
  80. "amount" => round($customer_package->amount),
  81. "send_email" => false,
  82. "email" => Auth::user()->email,
  83. "phone" => Auth::user()->phone,
  84. "redirect_url" => url('instamojo/payment/pay-success')
  85. ));
  86. return redirect($response['longurl']);
  87. } catch (\Exception $e) {
  88. return back();
  89. }
  90. } else {
  91. flash('Please add phone number to your profile')->warning();
  92. return redirect()->route('profile');
  93. }
  94. }
  95. elseif (Session::get('payment_type') == 'seller_package_payment') {
  96. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  97. if (preg_match_all('/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/im', Auth::user()->phone)) {
  98. try {
  99. $response = $api->paymentRequestCreate(array(
  100. "purpose" => ucfirst(str_replace('_', ' ', Session::get('payment_type'))),
  101. "amount" => round($seller_package->amount),
  102. "send_email" => false,
  103. "email" => Auth::user()->email,
  104. "phone" => Auth::user()->phone,
  105. "redirect_url" => url('instamojo/payment/pay-success')
  106. ));
  107. return redirect($response['longurl']);
  108. } catch (\Exception $e) {
  109. return back();
  110. }
  111. } else {
  112. flash('Please add phone number to your profile')->warning();
  113. return redirect()->route('profile');
  114. }
  115. }
  116. }
  117. }
  118. // success response method.
  119. public function success(Request $request){
  120. try {
  121. if(BusinessSetting::where('type', 'instamojo_sandbox')->first()->value == 1){
  122. $endPoint = 'https://test.instamojo.com/api/1.1/';
  123. }
  124. else{
  125. $endPoint = 'https://www.instamojo.com/api/1.1/';
  126. }
  127. $api = new \Instamojo\Instamojo(
  128. env('IM_API_KEY'),
  129. env('IM_AUTH_TOKEN'),
  130. $endPoint
  131. );
  132. $response = $api->paymentRequestStatus(request('payment_request_id'));
  133. if(!isset($response['payments'][0]['status']) ) {
  134. flash(translate('Payment Failed'))->error();
  135. return redirect()->route('home');
  136. } else if($response['payments'][0]['status'] != 'Credit') {
  137. flash(translate('Payment Failed'))->error();
  138. return redirect()->route('home');
  139. }
  140. }catch (\Exception $e) {
  141. flash(translate('Payment Failed'))->error();
  142. return redirect()->route('home');
  143. }
  144. $payment = json_encode($response);
  145. if(Session::has('payment_type')){
  146. if(Session::get('payment_type') == 'cart_payment'){
  147. return (new CheckoutController)->checkout_done(Session::get('combined_order_id'), $payment);
  148. }
  149. elseif (Session::get('payment_type') == 'wallet_payment') {
  150. return (new WalletController)->wallet_payment_done($request->session()->get('payment_data'), $payment);
  151. }
  152. elseif ($request->session()->get('payment_type') == 'customer_package_payment') {
  153. return (new CustomerPackageController)->purchase_payment_done($request->session()->get('payment_data'), $payment);
  154. }
  155. elseif ($request->session()->get('payment_type') == 'seller_package_payment') {
  156. return (new SellerPackageController)->purchase_payment_done($request->session()->get('payment_data'), $payment);
  157. }
  158. }
  159. }
  160. }