NgeniusUtility.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Utility;
  3. use App\Http\Controllers\CheckoutController;
  4. use App\Http\Controllers\CustomerPackageController;
  5. use App\Http\Controllers\WalletController;
  6. use Session;
  7. class NgeniusUtility
  8. {
  9. public static function getUrl($key)
  10. {
  11. $mode = self::getMode();
  12. $url['sandbox']['identity'] = "https://api-gateway.sandbox.ngenius-payments.com/identity/auth/access-token";
  13. $url['sandbox']['gateway'] = "https://api-gateway.sandbox.ngenius-payments.com";
  14. // sandbox urls do not work as the identity is not retrived by sandbox identity
  15. $url['real']['identity'] = "https://identity-uat.ngenius-payments.com/auth/realms/ni/protocol/openid-connect/token";
  16. $url['real']['gateway'] = "https://api-gateway-uat.ngenius-payments.com";
  17. return $url[$mode][$key];
  18. }
  19. //sandbox or real
  20. public static function getMode()
  21. {
  22. $sandbox = false; //check from db or env
  23. // sandbox urls do not work as the identity is not retrived by sandbox identity
  24. return $sandbox ? "sandbox" : "real";
  25. }
  26. public static function getAccessToken()
  27. {
  28. $apikey = env("NGENIUS_API_KEY"); // set your service account API key (example only)
  29. $idServiceURL = self::getUrl('identity'); // set the identity service URL (example only)
  30. $tokenHeaders = array("Authorization: Basic $apikey", "Content-Type: application/x-www-form-urlencoded");
  31. $tokenResponse = self::invokeCurlRequest("POST", $idServiceURL, $tokenHeaders, http_build_query(array('grant_type' => 'client_credentials')));
  32. // $tokenHeaders = array("Authorization: Basic $apikey", "Content-Type: application/vnd.ni-payment.v2+json", "Accept: application/vnd.ni-payment.v2+json");
  33. // $tokenResponse = self::invokeCurlRequest("POST", $idServiceURL, $tokenHeaders, null);
  34. $tokenResponse = json_decode($tokenResponse);
  35. //dd($tokenResponse);
  36. $access_token = $tokenResponse->access_token;
  37. return $access_token;
  38. }
  39. public static function make_payment($callback_url, $payment_type, $amount)
  40. {
  41. $order = new \StdClass();
  42. $order->action = "SALE"; // Transaction mode ("AUTH" = authorize only, no automatic settle/capture, "SALE" = authorize + automatic settle/capture)
  43. $order->amount = new \stdClass();
  44. $order->amount->currencyCode = env('NGENIUS_CURRENCY', "AED"); // Payment currency ('AED' only for now)
  45. $order->amount->value = $amount; // Minor units (1000 = 10.00 AED)
  46. $order->language = "en"; // Payment page language ('en' or 'ar' only)
  47. $order->merchantOrderReference = time(); // Payment page language ('en' or 'ar' only)
  48. //$order->merchantAttributes->redirectUrl = "http://premizer.com/test/pp.php"; // A redirect URL to a page on your site to return the customer to
  49. //$order->merchantAttributes->redirectUrl = "http://192.168.0.111:8080/n-genius/pp.php"; // A redirect URL to a page on your site to return the customer to
  50. $order->merchantAttributes = new \stdClass();
  51. $order->merchantAttributes->redirectUrl = $callback_url;
  52. // A redirect URL to a page on your site to return the customer to
  53. //$order->merchantAttributes->redirectUrl = "http:// 192.168.0.111:8080/n-genius/pp.php"; // A redirect URL to a page on your site to return the customer to
  54. $order = json_encode($order);
  55. $outletRef = env("NGENIUS_OUTLET_ID"); // set your outlet reference/ID value here (example only)
  56. $txnServiceURL = self::getUrl('gateway') . "/transactions/outlets/$outletRef/orders"; // set the transaction service URL (example only)
  57. $access_token = self::getAccessToken();
  58. $orderCreateHeaders = array("Authorization: Bearer " . $access_token, "Content-Type: application/vnd.ni-payment.v2+json", "Accept: application/vnd.ni-payment.v2+json");
  59. $orderCreateResponse = self::invokeCurlRequest("POST", $txnServiceURL, $orderCreateHeaders, $order);
  60. $orderCreateResponse = json_decode($orderCreateResponse);
  61. //dd($callback_url,$orderCreateResponse);
  62. $paymentLink = $orderCreateResponse->_links->payment->href; // the link to the payment page for redirection (either full-page redirect or iframe)
  63. $orderReference = $orderCreateResponse->reference; // the reference to the order, which you should store in your records for future interaction with this order
  64. session()->save();
  65. /*echo "<!DOCTYPE html>
  66. <html>
  67. <script type=\"text/javascript\">
  68. window.location = \"$paymentLink\";
  69. </script>
  70. </html>";*/
  71. header("Location: " . $paymentLink); // execute redirect
  72. exit;
  73. }
  74. public static function check_callback($orderRef, $payment_type)
  75. {
  76. $outletRef = env("NGENIUS_OUTLET_ID"); // set your outlet reference/ID value here (example only)
  77. $orderCheckURL = self::getUrl('gateway') . "/transactions/outlets/$outletRef/orders/$orderRef"; // set the transaction service URL (example only)
  78. $access_token = self::getAccessToken();
  79. $headers = array("Authorization: Bearer " . $access_token);
  80. $orderStatusResponse = self::invokeCurlRequest("GET", $orderCheckURL, $headers, null);
  81. $orderStatusResponse = json_decode($orderStatusResponse);
  82. if ($orderStatusResponse->_embedded->payment[0]->state == "FAILED") {
  83. // fail or cancel or incomplete
  84. Session::forget('payment_data');
  85. flash(translate('Payment incomplete'))->error();
  86. return redirect()->route('home');
  87. } else if ($orderStatusResponse->_embedded->payment[0]->state == "CAPTURED") {
  88. // success
  89. $payment = json_encode($orderStatusResponse);
  90. //dd($payment_type, Session::get('order_id'),Session::get('payment_data'), $payment);
  91. if ($payment_type == 'cart_payment') {
  92. $checkoutController = new CheckoutController;
  93. return $checkoutController->checkout_done(session()->get('combined_order_id'), $payment);
  94. }
  95. if ($payment_type == 'wallet_payment') {
  96. $walletController = new WalletController;
  97. return $walletController->wallet_payment_done(Session::get('payment_data'), $payment);
  98. }
  99. if ($payment_type == 'customer_package_payment') {
  100. $customer_package_controller = new CustomerPackageController;
  101. return $customer_package_controller->purchase_payment_done(session()->get('payment_data'), $payment);
  102. }
  103. if ($payment_type == 'seller_package_payment') {
  104. $seller_package_controller = new \App\Http\Controllers\SellerPackageController;
  105. return $seller_package_controller->purchase_payment_done(session()->get('payment_data'), $payment);
  106. }
  107. }
  108. }
  109. public static function invokeCurlRequest($type, $url, $headers, $post)
  110. {
  111. $ch = curl_init();
  112. curl_setopt($ch, CURLOPT_URL, $url);
  113. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  115. if ($type == "POST") {
  116. curl_setopt($ch, CURLOPT_POST, 1);
  117. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  118. }
  119. $server_output = curl_exec($ch);
  120. // print_r($server_output);
  121. // exit();
  122. curl_close($ch);
  123. return $server_output;
  124. }
  125. }