NagadController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use Illuminate\Http\Request;
  4. use App\Utility\NagadUtility;
  5. use App\Models\CombinedOrder;
  6. use App\Models\CustomerPackage;
  7. use App\Models\SellerPackage;
  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 NagadController{
  14. private $amount = null;
  15. private $tnx = null;
  16. private $nagadHost;
  17. private $tnx_status = false;
  18. private $merchantAdditionalInfo = [];
  19. public function __construct()
  20. {
  21. date_default_timezone_set('Asia/Dhaka');
  22. if (config('nagad.sandbox_mode') === 'sandbox') {
  23. $this->nagadHost = "http://sandbox.mynagad.com:10080/remote-payment-gateway-1.0/";
  24. }else{
  25. $this->nagadHost = "https://api.mynagad.com/";
  26. }
  27. }
  28. public function tnx($id,$status=false)
  29. {
  30. $this->tnx = $id;
  31. $this->tnx_status = $status;
  32. return $this;
  33. }
  34. public function amount($amount)
  35. {
  36. $this->amount = $amount;
  37. return $this;
  38. }
  39. public function pay()
  40. {
  41. if(Session::has('payment_type')){
  42. if(Session::get('payment_type') == 'cart_payment'){
  43. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  44. $this->amount($combined_order->grand_total);
  45. $this->tnx($combined_order->id);
  46. }
  47. if(Session::get('payment_type') == 'wallet_payment'){
  48. $this->amount(round(Session::get('payment_data')['amount']));
  49. $this->tnx(rand(000000,999999));
  50. }
  51. if(Session::get('payment_type') == 'customer_package_payment'){
  52. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  53. $this->amount(round($customer_package->amount));
  54. $this->tnx(rand(000000,999999));
  55. }
  56. if(Session::get('payment_type') == 'seller_package_payment'){
  57. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  58. $this->amount(round($seller_package->amount));
  59. $this->tnx(rand(000000,999999));
  60. }
  61. }
  62. $DateTime = Date('YmdHis');
  63. $MerchantID = config('nagad.merchant_id');
  64. //$invoice_no = 'Inv'.Date('YmdH').rand(1000, 10000);
  65. $invoice_no = $this->tnx_status ? $this->tnx :'Inv'.Date('YmdH').rand(1000, 10000);
  66. $merchantCallbackURL = config('nagad.callback_url');
  67. $SensitiveData = [
  68. 'merchantId' => $MerchantID,
  69. 'datetime' => $DateTime,
  70. 'orderId' => $invoice_no,
  71. 'challenge' => NagadUtility::generateRandomString()
  72. ];
  73. $PostData = array(
  74. 'accountNumber' => config('nagad.merchant_number'), //optional
  75. 'dateTime' => $DateTime,
  76. 'sensitiveData' => NagadUtility::EncryptDataWithPublicKey(json_encode($SensitiveData)),
  77. 'signature' => NagadUtility::SignatureGenerate(json_encode($SensitiveData))
  78. );
  79. $ur = $this->nagadHost."api/dfs/check-out/initialize/" . $MerchantID . "/" . $invoice_no;
  80. $Result_Data = NagadUtility::HttpPostMethod($ur,$PostData);
  81. if (isset($Result_Data['sensitiveData']) && isset($Result_Data['signature'])) {
  82. if ($Result_Data['sensitiveData'] != "" && $Result_Data['signature'] != "") {
  83. $PlainResponse = json_decode(NagadUtility::DecryptDataWithPrivateKey($Result_Data['sensitiveData']), true);
  84. if (isset($PlainResponse['paymentReferenceId']) && isset($PlainResponse['challenge'])) {
  85. $paymentReferenceId = $PlainResponse['paymentReferenceId'];
  86. $randomserver = $PlainResponse['challenge'];
  87. $SensitiveDataOrder = array(
  88. 'merchantId' => $MerchantID,
  89. 'orderId' => $invoice_no,
  90. 'currencyCode' => '050',
  91. 'amount' => $this->amount,
  92. 'challenge' => $randomserver
  93. );
  94. // $merchantAdditionalInfo = '{"no_of_seat": "1", "Service_Charge":"20"}';
  95. if($this->tnx !== ''){
  96. $this->merchantAdditionalInfo['tnx_id'] = $this->tnx;
  97. }
  98. // echo $merchantAdditionalInfo;
  99. // exit();
  100. $PostDataOrder = array(
  101. 'sensitiveData' => NagadUtility::EncryptDataWithPublicKey(json_encode($SensitiveDataOrder)),
  102. 'signature' => NagadUtility::SignatureGenerate(json_encode($SensitiveDataOrder)),
  103. 'merchantCallbackURL' => $merchantCallbackURL,
  104. 'additionalMerchantInfo' => (object)$this->merchantAdditionalInfo
  105. );
  106. // echo json_encode($PostDataOrder);
  107. // exit();
  108. $OrderSubmitUrl = $this->nagadHost."api/dfs/check-out/complete/" . $paymentReferenceId;
  109. $Result_Data_Order = NagadUtility::HttpPostMethod($OrderSubmitUrl, $PostDataOrder);
  110. try {
  111. if ($Result_Data_Order['status'] == "Success") {
  112. $url = ($Result_Data_Order['callBackUrl']);
  113. return redirect($url);
  114. //echo "<script>window.open('$url', '_self')</script>";
  115. }
  116. else {
  117. echo json_encode($Result_Data_Order);
  118. }
  119. } catch (\Exception $e) {
  120. dd($Result_Data_Order);
  121. }
  122. } else {
  123. echo json_encode($PlainResponse);
  124. }
  125. }
  126. }
  127. }
  128. public function verify(Request $request){
  129. $Query_String = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1]);
  130. $payment_ref_id = substr($Query_String[2], 15);
  131. $url = $this->nagadHost."api/dfs/verify/payment/" . $payment_ref_id;
  132. $json = NagadUtility::HttpGet($url);
  133. if(json_decode($json)->status == 'Success'){
  134. $payment_type = Session::get('payment_type');
  135. if ($payment_type == 'cart_payment') {
  136. return (new CheckoutController)->checkout_done(Session::get('combined_order_id'), $json);
  137. }
  138. if ($payment_type == 'wallet_payment') {
  139. return (new WalletController)->wallet_payment_done(Session::get('payment_data'), $json);
  140. }
  141. if ($payment_type == 'customer_package_payment') {
  142. return (new CustomerPackageController)->purchase_payment_done(Session::get('payment_data'), $json);
  143. }
  144. if($payment_type == 'seller_package_payment') {
  145. return (new SellerPackageController)->purchase_payment_done(Session::get('payment_data'), $json);
  146. }
  147. }
  148. flash('Payment Failed')->error();
  149. return redirect()->route('home');
  150. }
  151. }