MsqMarketApiService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Themes\Pro\nawame\app\Services;
  3. use Unirest\Request;
  4. class MsqMarketApiService
  5. {
  6. const BASE_URL = 'https://p2u-api.msq.market';
  7. private function response($response, $checkCode)
  8. {
  9. if (is_array($checkCode) == 1) {
  10. foreach ($checkCode as $code) {
  11. if ($response->code == $code) {
  12. $data = json_encode($response->body ?? []);
  13. return json_decode($data, true);
  14. }
  15. }
  16. return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
  17. } else {
  18. if ($response->code == $checkCode) {
  19. $data = json_encode($response->body ?? []);
  20. return json_decode($data, true);
  21. } else {
  22. return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
  23. }
  24. }
  25. }
  26. public function result($request_key)
  27. {
  28. $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/result',
  29. [ 'Accept' => 'application/json', ],
  30. [ 'request_key' => $request_key, ]
  31. );
  32. return $this->response($response, [200, 201]);
  33. }
  34. public function prepare($type, $to = '', $msqxpAmount = 0, $p2upAmount = 0)
  35. {
  36. $response = Request::post(MsqMarketApiService::BASE_URL . '/wallet/prepare',
  37. [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
  38. json_encode([
  39. 'dapp' => [
  40. 'name' => 'P2U',
  41. 'url' => 'https://p2u.kr'
  42. ],
  43. 'type' => $type,
  44. 'transaction' => [
  45. 'tokens' => [
  46. // 'MSQXP' => (int)$msqxpAmount,
  47. 'P2UPB' => (int)$p2upAmount,
  48. ],
  49. 'to' => $to,
  50. ],
  51. ], JSON_UNESCAPED_UNICODE)
  52. );
  53. return $this->response($response, 201);
  54. }
  55. public function prepareConnection()
  56. {
  57. $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/prepare_connection',
  58. [ 'Accept' => 'application/json' ],
  59. );
  60. return $this->response($response, 201);
  61. }
  62. public function establishConnection($uuid, $token)
  63. {
  64. $response = Request::post(MsqMarketApiService::BASE_URL . '/wallet/establish_connection',
  65. [
  66. 'Accept' => 'application/json',
  67. 'Content-Type' => 'application/json',
  68. 'Authorization' => 'Bearer ' . $token
  69. ],
  70. json_encode([ 'keyId' => $uuid, 'platform' => '' ], JSON_UNESCAPED_UNICODE)
  71. );
  72. return $this->response($response, 201);
  73. }
  74. public function getBalance($token)
  75. {
  76. $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/get_balance',
  77. [
  78. 'Accept' => 'application/json',
  79. 'Authorization' => 'Bearer ' . $token
  80. ],
  81. );
  82. return $this->response($response, 200);
  83. }
  84. }