Commit 0687e53b authored by rlgy's avatar rlgy

交易确认

parent 32741b93
......@@ -11,8 +11,8 @@ namespace common\service\brower;
class Brower implements BrowerInterface
{
const TRANS_OK = 1;//交易成功
const TRANS_FAIL = 2;//交易失败
const TRANS_PADDING = 3;//交易状态未知
const TRANS_FAIL = -1;//交易失败
const TRANS_PADDING = 0;//交易状态未知
const ERROR = 4;//接口返回错误
public static $msg = [
self::TRANS_OK => 'SUCCEED',
......@@ -26,11 +26,11 @@ class Brower implements BrowerInterface
return ['code' => self::ERROR, 'msg' => 'Coin not supported!'];
}
public function StatusReturn($code, $msg = '')
public function StatusReturn($code, $msg = '', $data = [])
{
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
if ($content['err_no']) {
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);
}
......
......@@ -26,10 +26,10 @@ class BtyBrower extends Brower
];
$post = json_encode($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['result']) {
return $this->StatusReturn(self::TRANS_OK);
return $this->StatusReturn(self::TRANS_OK, '', ['height' => $result['result']['height']]);
} elseif ($result['error']) {
return $this->StatusReturn(self::ERROR, $result['error']);
}
......
......@@ -22,7 +22,7 @@ class DcrBrower extends Brower
$result = json_decode($content, true);
if ($result) {
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 {
return $this->StatusReturn(self::TRANS_PADDING);
}
......
......@@ -12,20 +12,25 @@ use common\helpers\Curl;
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)
{
$uri = $this->base_uri . 'tx/' . $txhash;
$uri = $this->base_uri . $txhash;
$ch = new Curl();
$content = $ch->get($uri, true);
if ($content) {
if (strpos($content, 'Success') !== false) {
return $this->StatusReturn(self::TRANS_OK);
} elseif (strpos($content, 'Fail') !== false) {
return $this->StatusReturn(self::TRANS_FAIL);
$content = $ch->get($uri, false);
if ($content && is_array($content)) {
if (1 == $content['status']) {
$status = $content['result']['status'];
if (0 == $status) {
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