From 6ba6bd7180089886eb768f795f93472a9878ecfb Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 31 Mar 2021 18:02:54 +0000 Subject: [PATCH] add send coins transactions how uses login-server endpoint from stage2 --- .../src/Controller/AppRequestsController.php | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/community_server/src/Controller/AppRequestsController.php b/community_server/src/Controller/AppRequestsController.php index c36ddf161..f2c0dbac6 100644 --- a/community_server/src/Controller/AppRequestsController.php +++ b/community_server/src/Controller/AppRequestsController.php @@ -23,7 +23,7 @@ class AppRequestsController extends AppController $this->loadComponent('JsonRequestClient'); $this->loadComponent('JsonRpcRequestClient'); //$this->Auth->allow(['add', 'edit']); - $this->Auth->allow('index'); + $this->Auth->allow(['index', 'sendCoins']); } @@ -52,6 +52,106 @@ class AppRequestsController extends AppController return $this->returnJson(['state' => 'error', 'msg' => 'no post or get']); } + private function checkRequiredFields($data, $fields) { + foreach($fields as $field) { + if(!isset($data[$field])) { + return ['state' => 'error', 'msg' => 'missing field', 'details' => $field . ' not found']; + } + } + return true; + } + + public function sendCoins() + { + /* + * { + "session_id" : -127182, + "amount": 2000000, + "email": "max.musterman@gmail.de", + "memo":"Thank you :)", + "group": "gdd1", + "auto_sign": true + */ + $data = $this->request->input('json_decode'); + $login_request_result = $this->requestLogin(0, false); + if($login_request_result !== true) { + return $this->returnJson($login_request_result); + } + $session = $this->getRequest()->getSession(); + $required_fields = $this->checkRequiredFields($data, ['amount', 'email']); + if($required_fields !== true) { + return $this->returnJson($required_fields); + } + $amount = $data['amount']; + if(intval($amount) <= 0) { + return $this->returnJson(['state' => 'error', 'msg' => 'amount is invalid', 'details' => $amount]); + } + $email = $data['email']; + if($email == '') { + return $this->returnJson(['state' => 'error', 'msg' => 'email is empty']); + } + $memo = ''; + if(isset($data['memo'])) { + $memo = $data['memo']; + } + $auto_sign = false; + if(isset($data['auto_sign'])) { + $auto_sign = boolval($data['auto_sign']); + } + $group = ''; + if(isset($data['group'])) { + $group = $data['group']; + } + + $requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([ + 'session_id' => $session->read('session_id'), + 'transaction_type' => 'transfer', + 'memo' => $memo, + 'amount' => $amount, + 'target_group' => $group, + 'target_email' => $email, + 'auto_sign' => $auto_sign + ]), '/createTransaction'); + + if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) { + $pendingTransactionCount = $session->read('Transactions.pending'); + if($pendingTransactionCount == null) { + $pendingTransactionCount = 1; + } else { + $pendingTransactionCount++; + } + $session->write('Transactions.pending', $pendingTransactionCount); + //echo "pending: " . $pendingTransactionCount; + return $this->returnJson(['state' => 'success']); + } else { + + /* + * if request contain unknown parameter format, shouldn't happen't at all + * {"state": "error", "msg": "parameter format unknown"} + * if json parsing failed + * {"state": "error", "msg": "json exception", "details":"exception text"} + * if session_id is zero or not set + * {"state": "error", "msg": "session_id invalid"} + * if session id wasn't found on login server, if server was restartet or user logged out (also per timeout, default: 15 minutes) + * {"state": "error", "msg": "session not found"} + * if session hasn't active user, shouldn't happen't at all, login-server should be checked if happen + * {"state": "code error", "msg":"user is zero"} + * if transaction type not known + * {"state": "error", "msg":"transaction_type unknown"} + * if receiver wasn't known to Login-Server + * {"state": "not found", "msg":"receiver not found"} + * if receiver account disabled, and therefor cannto receive any coins + * {"state": "disabled", "msg":"receiver is disabled"} + * if transaction was okay and will be further proccessed + * {"state":"success"} + */ + $answear_data = $requestAnswear['data']; + return $this->returnJson($answear_data); + + } + + } + private function acquireAccessToken($session_id) {