123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace Themes\Pro\nawame\app\Services;
- use Unirest\Request;
- class MsqMarketApiService
- {
- const BASE_URL = 'https://p2u-api.msq.market';
- private function response($response, $checkCode)
- {
- if (is_array($checkCode) == 1) {
- foreach ($checkCode as $code) {
- if ($response->code == $code) {
- $data = json_encode($response->body ?? []);
- return json_decode($data, true);
- }
- }
- return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
- } else {
- if ($response->code == $checkCode) {
- $data = json_encode($response->body ?? []);
- return json_decode($data, true);
- } else {
- return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
- }
- }
- }
- public function result($request_key)
- {
- $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/result',
- [ 'Accept' => 'application/json', ],
- [ 'request_key' => $request_key, ]
- );
- return $this->response($response, [200, 201]);
- }
- public function prepare($type, $to = '', $msqxpAmount = 0, $p2upAmount = 0)
- {
- $response = Request::post(MsqMarketApiService::BASE_URL . '/wallet/prepare',
- [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
- json_encode([
- 'dapp' => [
- 'name' => 'P2U',
- 'url' => 'https://p2u.kr'
- ],
- 'type' => $type,
- 'transaction' => [
- 'tokens' => [
- // 'MSQXP' => (int)$msqxpAmount,
- 'P2UPB' => (int)$p2upAmount,
- ],
- 'to' => $to,
- ],
- ], JSON_UNESCAPED_UNICODE)
- );
- return $this->response($response, 201);
- }
- public function prepareConnection()
- {
- $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/prepare_connection',
- [ 'Accept' => 'application/json' ],
- );
- return $this->response($response, 201);
- }
- public function establishConnection($uuid, $token)
- {
- $response = Request::post(MsqMarketApiService::BASE_URL . '/wallet/establish_connection',
- [
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'Authorization' => 'Bearer ' . $token
- ],
- json_encode([ 'keyId' => $uuid, 'platform' => '' ], JSON_UNESCAPED_UNICODE)
- );
- return $this->response($response, 201);
- }
- public function getBalance($token)
- {
- $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/get_balance',
- [
- 'Accept' => 'application/json',
- 'Authorization' => 'Bearer ' . $token
- ],
- );
- return $this->response($response, 200);
- }
- }
|