BkashController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. class BkashController extends Controller
  14. {
  15. private $base_url;
  16. public function __construct()
  17. {
  18. if(get_setting('bkash_sandbox', 1)){
  19. $this->base_url = "https://checkout.sandbox.bka.sh/v1.2.0-beta/";
  20. }
  21. else {
  22. $this->base_url = "https://checkout.pay.bka.sh/v1.2.0-beta/";
  23. }
  24. }
  25. public function pay(){
  26. $amount = 0;
  27. if(Session::has('payment_type')){
  28. if(Session::get('payment_type') == 'cart_payment'){
  29. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  30. $amount = round($combined_order->grand_total);
  31. }
  32. elseif (Session::get('payment_type') == 'wallet_payment') {
  33. $amount = round(Session::get('payment_data')['amount']);
  34. }
  35. elseif (Session::get('payment_type') == 'customer_package_payment') {
  36. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  37. $amount = round($customer_package->amount);
  38. }
  39. elseif (Session::get('payment_type') == 'seller_package_payment') {
  40. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  41. $amount = round($seller_package->amount);
  42. }
  43. }
  44. $request_data = array('app_key'=> env('BKASH_CHECKOUT_APP_KEY'), 'app_secret'=>env('BKASH_CHECKOUT_APP_SECRET'));
  45. $url = curl_init($this->base_url.'checkout/token/grant');
  46. $request_data_json=json_encode($request_data);
  47. $header = array(
  48. 'Content-Type:application/json',
  49. 'username:'.env('BKASH_CHECKOUT_USER_NAME'),
  50. 'password:'.env('BKASH_CHECKOUT_PASSWORD')
  51. );
  52. curl_setopt($url,CURLOPT_HTTPHEADER, $header);
  53. curl_setopt($url,CURLOPT_CUSTOMREQUEST, "POST");
  54. curl_setopt($url,CURLOPT_RETURNTRANSFER, true);
  55. curl_setopt($url,CURLOPT_POSTFIELDS, $request_data_json);
  56. curl_setopt($url,CURLOPT_FOLLOWLOCATION, 1);
  57. curl_setopt($url, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  58. $resultdata = curl_exec($url);
  59. curl_close($url);
  60. $token = json_decode($resultdata)->id_token;
  61. Session::put('bkash_token', $token);
  62. Session::put('payment_amount', $amount);
  63. return view('frontend.bkash.index');
  64. }
  65. public function checkout(Request $request){
  66. $auth = Session::get('bkash_token');
  67. $requestbody = array(
  68. 'amount' => Session::get('payment_amount'),
  69. 'currency' => 'BDT',
  70. 'intent' => 'sale',
  71. 'merchantInvoiceNumber' => "Inv".Session::get('combined_order_id')
  72. );
  73. $url = curl_init($this->base_url.'checkout/payment/create');
  74. $requestbodyJson = json_encode($requestbody);
  75. $header = array(
  76. 'Content-Type:application/json',
  77. 'Authorization:' . $auth,
  78. 'X-APP-Key:'.env('BKASH_CHECKOUT_APP_KEY')
  79. );
  80. curl_setopt($url, CURLOPT_HTTPHEADER, $header);
  81. curl_setopt($url, CURLOPT_CUSTOMREQUEST, "POST");
  82. curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
  83. curl_setopt($url, CURLOPT_POSTFIELDS, $requestbodyJson);
  84. curl_setopt($url, CURLOPT_FOLLOWLOCATION, 1);
  85. curl_setopt($url, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  86. $resultdata = curl_exec($url);
  87. curl_close($url);
  88. return $resultdata;
  89. }
  90. public function excecute(Request $request){
  91. $paymentID = $request->paymentID;
  92. $auth = Session::get('bkash_token');
  93. $url = curl_init($this->base_url.'checkout/payment/execute/'.$paymentID);
  94. $header = array(
  95. 'Content-Type:application/json',
  96. 'Authorization:' . $auth,
  97. 'X-APP-Key:'.env('BKASH_CHECKOUT_APP_KEY')
  98. );
  99. curl_setopt($url,CURLOPT_HTTPHEADER, $header);
  100. curl_setopt($url,CURLOPT_CUSTOMREQUEST, "POST");
  101. curl_setopt($url,CURLOPT_RETURNTRANSFER, true);
  102. curl_setopt($url,CURLOPT_FOLLOWLOCATION, 1);
  103. $resultdata = curl_exec($url);
  104. curl_close($url);
  105. return $resultdata;
  106. }
  107. public function success(Request $request){
  108. $payment_type = Session::get('payment_type');
  109. if ($payment_type == 'cart_payment') {
  110. return (new CheckoutController)->checkout_done(Session::get('combined_order_id'), $request->payment_details);
  111. }
  112. if ($payment_type == 'wallet_payment') {
  113. return (new WalletController)->wallet_payment_done(Session::get('payment_data'), $request->payment_details);
  114. }
  115. if ($payment_type == 'customer_package_payment') {
  116. return (new CustomerPackageController)->purchase_payment_done(Session::get('payment_data'), $request->payment_details);
  117. }
  118. if($payment_type == 'seller_package_payment') {
  119. return (new SellerPackageController)->purchase_payment_done(Session::get('payment_data'), $request->payment_details);
  120. }
  121. }
  122. }