mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
add send coins transactions how uses login-server endpoint from stage2
This commit is contained in:
parent
3a10d1f429
commit
6ba6bd7180
@ -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)
|
||||
{
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user