NagadController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Http\Controllers\Api\V2;
  3. use App\Models\BusinessSetting;
  4. use App\Utility\NagadUtility;
  5. use App\Models\CombinedOrder;
  6. use App\Models\User;
  7. use App\Models\Wallet;
  8. use Illuminate\Http\Request;
  9. class NagadController
  10. {
  11. private $amount = null;
  12. private $tnx = null;
  13. private $nagadHost;
  14. private $tnx_status = false;
  15. private $merchantAdditionalInfo = [];
  16. public function __construct()
  17. {
  18. date_default_timezone_set('Asia/Dhaka');
  19. if (config('nagad.sandbox_mode') === 'sandbox') {
  20. $this->nagadHost = "http://sandbox.mynagad.com:10080/";
  21. } else {
  22. $this->nagadHost = "https://api.mynagad.com/";
  23. }
  24. }
  25. public function begin(Request $request)
  26. {
  27. $this->amount = $request->amount;
  28. $this->tnx_status = false;
  29. $payment_type = $request->payment_type;
  30. $combined_order_id = $request->combined_order_id;
  31. $amount = $request->amount;
  32. $user_id = $request->user_id;
  33. if ($request->payment_type == 'cart_payment') {
  34. $this->tnx = $request->combined_order_id;
  35. $combined_order = CombinedOrder::find($combined_order_id);
  36. $this->amount = $combined_order->grand_total;
  37. } else if ($request->payment_type == 'wallet_payment') {
  38. $this->tnx = rand(10000, 99999);
  39. }
  40. return $this->getSession($request->payment_type);
  41. }
  42. public function getSession($payment_type)
  43. {
  44. $DateTime = Date('YmdHis');
  45. $MerchantID = config('nagad.merchant_id');
  46. //$invoice_no = 'Inv'.Date('YmdH').rand(1000, 10000);
  47. $invoice_no = $this->tnx_status ? $this->tnx : 'Inv' . Date('YmdH') . rand(1000, 10000);
  48. $merchantCallbackURL = route('app.nagad.callback_url', ['payment_type' => $payment_type]);
  49. $SensitiveData = [
  50. 'merchantId' => $MerchantID,
  51. 'datetime' => $DateTime,
  52. 'orderId' => $invoice_no,
  53. 'challenge' => NagadUtility::generateRandomString()
  54. ];
  55. $PostData = array(
  56. 'accountNumber' => config('nagad.merchant_number'), //optional
  57. 'dateTime' => $DateTime,
  58. 'sensitiveData' => NagadUtility::EncryptDataWithPublicKey(json_encode($SensitiveData)),
  59. 'signature' => NagadUtility::SignatureGenerate(json_encode($SensitiveData))
  60. );
  61. $ur = $this->nagadHost . "api/dfs/check-out/initialize/" . $MerchantID . "/" . $invoice_no;
  62. $Result_Data = NagadUtility::HttpPostMethod($ur, $PostData);
  63. if (isset($Result_Data['sensitiveData']) && isset($Result_Data['signature'])) {
  64. if ($Result_Data['sensitiveData'] != "" && $Result_Data['signature'] != "") {
  65. $PlainResponse = json_decode(NagadUtility::DecryptDataWithPrivateKey($Result_Data['sensitiveData']), true);
  66. if (isset($PlainResponse['paymentReferenceId']) && isset($PlainResponse['challenge'])) {
  67. $paymentReferenceId = $PlainResponse['paymentReferenceId'];
  68. $randomserver = $PlainResponse['challenge'];
  69. $SensitiveDataOrder = array(
  70. 'merchantId' => $MerchantID,
  71. 'orderId' => $invoice_no,
  72. 'currencyCode' => '050',
  73. 'amount' => $this->amount,
  74. 'challenge' => $randomserver
  75. );
  76. // $merchantAdditionalInfo = '{"no_of_seat": "1", "Service_Charge":"20"}';
  77. if ($this->tnx !== '') {
  78. $this->merchantAdditionalInfo['tnx_id'] = $this->tnx;
  79. }
  80. // echo $merchantAdditionalInfo;
  81. // exit();
  82. $PostDataOrder = array(
  83. 'sensitiveData' => NagadUtility::EncryptDataWithPublicKey(json_encode($SensitiveDataOrder)),
  84. 'signature' => NagadUtility::SignatureGenerate(json_encode($SensitiveDataOrder)),
  85. 'merchantCallbackURL' => $merchantCallbackURL,
  86. 'additionalMerchantInfo' => (object)$this->merchantAdditionalInfo
  87. );
  88. // echo json_encode($PostDataOrder);
  89. // exit();
  90. $OrderSubmitUrl = $this->nagadHost . "api/dfs/check-out/complete/" . $paymentReferenceId;
  91. $Result_Data_Order = NagadUtility::HttpPostMethod($OrderSubmitUrl, $PostDataOrder);
  92. //dd($Result_Data_Order);
  93. if ($Result_Data_Order['status'] == "Success") {
  94. return response()->json([
  95. 'data' => $Result_Data_Order,
  96. 'result' => true,
  97. 'url' => $Result_Data_Order['callBackUrl'],
  98. 'message' => translate('Redirect Url is found')
  99. ]);
  100. } else {
  101. return response()->json([
  102. 'data' => $Result_Data_Order,
  103. 'result' => false,
  104. 'url' => '',
  105. 'message' => translate('Could not generate payment link')
  106. ]);
  107. }
  108. } else {
  109. return response()->json([
  110. 'data' => $PlainResponse,
  111. 'result' => false,
  112. 'url' => '',
  113. 'message' => translate('Payment reference id or challenge is missing')
  114. ]);
  115. }
  116. } else {
  117. return response()->json([
  118. 'data' => null,
  119. 'result' => false,
  120. 'url' => '',
  121. 'message' => translate('Sensitive data or Signature is empty')
  122. ]);
  123. }
  124. } else {
  125. return response()->json([
  126. 'data' => null,
  127. 'result' => false,
  128. 'url' => '',
  129. 'message' => translate('Sensitive data or Signature is missing')
  130. ]);
  131. }
  132. }
  133. public function verify(Request $request, $payment_type)
  134. {
  135. $Query_String = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1]);
  136. $payment_ref_id = substr($Query_String[2], 15);
  137. $url = $this->nagadHost . "api/dfs/verify/payment/" . $payment_ref_id;
  138. $json = NagadUtility::HttpGet($url);
  139. if (json_decode($json)->status == 'Success') {
  140. return response()->json([
  141. 'result' => true,
  142. 'message' => translate('Payment Processing'),
  143. 'payment_details' => $json
  144. ]);
  145. }
  146. return response()->json([
  147. 'result' => false,
  148. 'message' => translate('Payment failed !'),
  149. 'payment_details' => ''
  150. ]);
  151. }
  152. public function process(Request $request)
  153. {
  154. try {
  155. $payment_type = $request->payment_type;
  156. if ($payment_type == 'cart_payment') {
  157. checkout_done($request->combined_order_id, $request->payment_details);
  158. }
  159. if ($payment_type == 'wallet_payment') {
  160. wallet_payment_done($request->user_id, $request->amount, 'Nagad', $request->payment_details);
  161. }
  162. return response()->json(['result' => true, 'message' => translate("Payment is successful")]);
  163. } catch (\Exception $e) {
  164. return response()->json(['result' => false, 'message' => $e->getMessage()]);
  165. }
  166. }
  167. }