AamarpayController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\CustomerPackage;
  6. use App\Models\SellerPackage;
  7. use App\Models\CombinedOrder;
  8. use App\Http\Controllers\CustomerPackageController;
  9. use App\Http\Controllers\SellerPackageController;
  10. use App\Http\Controllers\WalletController;
  11. use App\Http\Controllers\CheckoutController;
  12. use Session;
  13. use Auth;
  14. class AamarpayController extends Controller
  15. {
  16. public function pay(){
  17. if (Auth::user()->phone == null) {
  18. flash('Please add phone number to your profile')->warning();
  19. return redirect()->route('profile');
  20. }
  21. if (Auth::user()->email == null) {
  22. $email = 'customer@exmaple.com';
  23. }
  24. else{
  25. $email = Auth::user()->email;
  26. }
  27. if (get_setting('aamarpay_sandbox') == 1) {
  28. $url = 'https://sandbox.aamarpay.com/request.php'; // live url https://secure.aamarpay.com/request.php
  29. }
  30. else {
  31. $url = 'https://secure.aamarpay.com/request.php';
  32. }
  33. $amount = 0;
  34. if(Session::has('payment_type')){
  35. if(Session::get('payment_type') == 'cart_payment'){
  36. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  37. $amount = round($combined_order->grand_total);
  38. }
  39. elseif (Session::get('payment_type') == 'wallet_payment') {
  40. $amount = round(Session::get('payment_data')['amount']);
  41. }
  42. elseif (Session::get('payment_type') == 'customer_package_payment') {
  43. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  44. $amount = round($customer_package->amount);
  45. }
  46. elseif (Session::get('payment_type') == 'seller_package_payment') {
  47. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  48. $amount = round($seller_package->amount);
  49. }
  50. }
  51. $fields = array(
  52. 'store_id' => env('AAMARPAY_STORE_ID'), //store id will be aamarpay, contact integration@aamarpay.com for test/live id
  53. 'amount' => $amount, //transaction amount
  54. 'payment_type' => 'VISA', //no need to change
  55. 'currency' => 'BDT', //currenct will be USD/BDT
  56. 'tran_id' => rand(1111111,9999999), //transaction id must be unique from your end
  57. 'cus_name' => Auth::user()->name, //customer name
  58. 'cus_email' => $email, //customer email address
  59. 'cus_add1' => '', //customer address
  60. 'cus_add2' => '', //customer address
  61. 'cus_city' => '', //customer city
  62. 'cus_state' => '', //state
  63. 'cus_postcode' => '', //postcode or zipcode
  64. 'cus_country' => 'Bangladesh', //country
  65. 'cus_phone' => Auth::user()->phone, //customer phone number
  66. 'cus_fax' => 'Not¬Applicable', //fax
  67. 'ship_name' => '', //ship name
  68. 'ship_add1' => '', //ship address
  69. 'ship_add2' => '',
  70. 'ship_city' => '',
  71. 'ship_state' => '',
  72. 'ship_postcode' => '',
  73. 'ship_country' => 'Bangladesh',
  74. 'desc' => env('APP_NAME').' payment',
  75. 'success_url' => route('aamarpay.success'), //your success route
  76. 'fail_url' => route('aamarpay.fail'), //your fail route
  77. 'cancel_url' => route('cart'), //your cancel url
  78. 'opt_a' => Session::get('payment_type'), //optional paramter
  79. 'opt_b' => Session::get('combined_order_id'),
  80. 'opt_c' => json_encode(Session::get('payment_data')),
  81. 'opt_d' => '',
  82. 'signature_key' => env('AAMARPAY_SIGNATURE_KEY') //signature key will provided aamarpay, contact integration@aamarpay.com for test/live signature key
  83. );
  84. $fields_string = http_build_query($fields);
  85. $ch = curl_init();
  86. curl_setopt($ch, CURLOPT_VERBOSE, true);
  87. curl_setopt($ch, CURLOPT_URL, $url);
  88. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  89. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  90. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  91. $url_forward = str_replace('"', '', stripslashes(curl_exec($ch)));
  92. curl_close($ch);
  93. $this->redirect_to_merchant($url_forward);
  94. }
  95. function redirect_to_merchant($url) {
  96. if (get_setting('aamarpay_sandbox') == 1) {
  97. $base_url = 'https://sandbox.aamarpay.com/';
  98. }
  99. else {
  100. $base_url = 'https://secure.aamarpay.com/';
  101. }
  102. ?>
  103. <html xmlns="http://www.w3.org/1999/xhtml">
  104. <head><script type="text/javascript">
  105. function closethisasap() { document.forms["redirectpost"].submit(); }
  106. </script></head>
  107. <body onLoad="closethisasap();">
  108. <form name="redirectpost" method="post" action="<?php echo $base_url.$url; ?>"></form>
  109. </body>
  110. </html>
  111. <?php
  112. exit;
  113. }
  114. public function success(Request $request){
  115. $payment_type = $request->opt_a;
  116. if ($payment_type == 'cart_payment') {
  117. return (new CheckoutController)->checkout_done($request->opt_b, json_encode($request->all()));
  118. }
  119. if ($payment_type == 'wallet_payment') {
  120. return (new WalletController)->wallet_payment_done(json_decode($request->opt_c), json_encode($request->all()));
  121. }
  122. if ($payment_type == 'customer_package_payment') {
  123. return (new CustomerPackageController)->purchase_payment_done(json_decode($request->opt_c), json_encode($request->all()));
  124. }
  125. if($payment_type == 'seller_package_payment') {
  126. return (new SellerPackageController)->purchase_payment_done(json_decode($request->opt_c), json_encode($request->all()));
  127. }
  128. }
  129. public function fail(Request $request){
  130. flash(translate('Payment failed'))->error();
  131. return redirect()->route('cart');
  132. }
  133. }