Commit 0687e53b authored by rlgy's avatar rlgy

交易确认

parent 32741b93
...@@ -11,8 +11,8 @@ namespace common\service\brower; ...@@ -11,8 +11,8 @@ namespace common\service\brower;
class Brower implements BrowerInterface class Brower implements BrowerInterface
{ {
const TRANS_OK = 1;//交易成功 const TRANS_OK = 1;//交易成功
const TRANS_FAIL = 2;//交易失败 const TRANS_FAIL = -1;//交易失败
const TRANS_PADDING = 3;//交易状态未知 const TRANS_PADDING = 0;//交易状态未知
const ERROR = 4;//接口返回错误 const ERROR = 4;//接口返回错误
public static $msg = [ public static $msg = [
self::TRANS_OK => 'SUCCEED', self::TRANS_OK => 'SUCCEED',
...@@ -26,11 +26,11 @@ class Brower implements BrowerInterface ...@@ -26,11 +26,11 @@ class Brower implements BrowerInterface
return ['code' => self::ERROR, 'msg' => 'Coin not supported!']; return ['code' => self::ERROR, 'msg' => 'Coin not supported!'];
} }
public function StatusReturn($code, $msg = '') public function StatusReturn($code, $msg = '', $data = [])
{ {
if ($msg) { if ($msg) {
return ['code' => $code, 'msg' => $msg]; return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
return ['code' => $code, 'msg' => self::$msg[$code]]; return ['code' => $code, 'msg' => self::$msg[$code], 'data' => $data];
} }
} }
\ No newline at end of file
...@@ -22,7 +22,11 @@ class BtcBrower extends Brower ...@@ -22,7 +22,11 @@ class BtcBrower extends Brower
if ($content['err_no']) { if ($content['err_no']) {
return $this->StatusReturn(self::ERROR, $content['err_msg']); return $this->StatusReturn(self::ERROR, $content['err_msg']);
} }
return $this->StatusReturn(self::TRANS_OK); if ($content['data']['confirmations'] > 0) {
return $this->StatusReturn(self::TRANS_OK, '', ['height' => $content['data']['block_height'], 'blocktime' => $content['data']['block_time']]);
} else {
return $this->StatusReturn(self::TRANS_PADDING);
}
} }
return $this->StatusReturn(self::ERROR); return $this->StatusReturn(self::ERROR);
} }
......
...@@ -26,10 +26,10 @@ class BtyBrower extends Brower ...@@ -26,10 +26,10 @@ class BtyBrower extends Brower
]; ];
$post = json_encode($post); $post = json_encode($post);
$ch->setOption(CURLOPT_POSTFIELDS, $post); $ch->setOption(CURLOPT_POSTFIELDS, $post);
$result = $ch->post($this->base_uri, false); $result = $ch->post($this->base_uri, false);// from, height
if ($result) { if ($result) {
if ($result['result']) { if ($result['result']) {
return $this->StatusReturn(self::TRANS_OK); return $this->StatusReturn(self::TRANS_OK, '', ['height' => $result['result']['height']]);
} elseif ($result['error']) { } elseif ($result['error']) {
return $this->StatusReturn(self::ERROR, $result['error']); return $this->StatusReturn(self::ERROR, $result['error']);
} }
......
...@@ -22,7 +22,7 @@ class DcrBrower extends Brower ...@@ -22,7 +22,7 @@ class DcrBrower extends Brower
$result = json_decode($content, true); $result = json_decode($content, true);
if ($result) { if ($result) {
if (isset($result['confirmations']) && $result['confirmations'] > 0) { if (isset($result['confirmations']) && $result['confirmations'] > 0) {
return $this->StatusReturn(self::TRANS_OK); return $this->StatusReturn(self::TRANS_OK, '', ['height' => $result['blockheight'], 'blocktime' => $result['blocktime']]);
} else { } else {
return $this->StatusReturn(self::TRANS_PADDING); return $this->StatusReturn(self::TRANS_PADDING);
} }
......
...@@ -12,20 +12,25 @@ use common\helpers\Curl; ...@@ -12,20 +12,25 @@ use common\helpers\Curl;
class EthBrower extends Brower class EthBrower extends Brower
{ {
private $base_uri = 'https://etherscan.io/'; private $base_uri = 'https://api.etherscan.io/api?module=transaction&action=gettxreceiptstatus&txhash=';
public function getStatus(string $txhash) public function getStatus(string $txhash)
{ {
$uri = $this->base_uri . 'tx/' . $txhash; $uri = $this->base_uri . $txhash;
$ch = new Curl(); $ch = new Curl();
$content = $ch->get($uri, true); $content = $ch->get($uri, false);
if ($content) { if ($content && is_array($content)) {
if (strpos($content, 'Success') !== false) { if (1 == $content['status']) {
return $this->StatusReturn(self::TRANS_OK); $status = $content['result']['status'];
} elseif (strpos($content, 'Fail') !== false) { if (0 == $status) {
return $this->StatusReturn(self::TRANS_FAIL); return $this->StatusReturn(self::TRANS_FAIL);
} elseif (1 == $status) {
return $this->StatusReturn(self::TRANS_OK);
} else {
return $this->StatusReturn(self::TRANS_PADDING);
}
} }
} }
return $this->StatusReturn(self::ERROR, 'txHash not exists'); return $this->StatusReturn(self::ERROR);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment