pay/webapp/classes/OrderCtrl.php
aozhiwei 603ee1b28e 1
2020-11-09 19:49:08 +08:00

293 lines
12 KiB
PHP

<?php
namespace classes;
use phpcommon;
class OrderCtrl {
const SEQ_KEY = 'gamepay:seq';
private function getRedis()
{
$redis_conf = getGlobalRedisConfig();
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
private function getMysql($DNA)
{
$mysql_conf = getMysqlConfig($DNA);
$conn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => 'paydb' . $mysql_conf['instance_id']
));
return $conn;
}
private function getCpOrderIdDNA($cp_orderid)
{
if (strlen($cp_orderid) < 2) {
return 0;
}
$hex_str = substr($cp_orderid, strlen($cp_orderid) - 2);
return hexdec($hex_str);
}
private function getStrDNA($account_id)
{
return crc32($account_id) % 256;
}
public function genOrderId($account_id)
{
$r = $this->getRedis();
$seq = $r->incrBy(self::SEQ_KEY, 1) & 0xffffff;
$r->pexpire(self::SEQ_KEY, 1000 * 10);
$hex_str = dechex($this->getStrDNA($account_id));
if (strlen($hex_str) < 2) {
$hex_str = '0' . $hex_str;
}
$today = date("YmdHis");
$orderid = $today . $var=sprintf("%09d", $seq) . $hex_str;
return $orderid;
}
public function getOrder($cp_orderid)
{
$conn = $this->getMysql($this->getCpOrderIdDNA($cp_orderid));
$row = $conn->execQueryOne('SELECT * FROM orderinfo ' .
'WHERE orderid=:orderid;',
array(
':orderid' => $cp_orderid
)
);
return $row;
}
public function getOrderBySpOrderId($account_id, $channel, $sp_orderid)
{
$conn = $this->getMysql($this->getStrDNA($account_id));
$row = $conn->execQueryOne('SELECT * FROM orderinfo ' .
'WHERE account_id=:account_id AND channel=:channel AND sp_orderid=:sp_orderid;',
array(
':account_id' => $account_id,
':channel' => $channel,
':sp_orderid' => $sp_orderid,
)
);
return $row;
}
public function getReceipt($receipt_id)
{
$conn = $this->getMysql($this->getStrDNA($receipt_id));
$row = $conn->execQueryOne('SELECT receipt_id, account_id, roleid, orderid FROM receipt ' .
'WHERE receipt_id=:receipt_id;',
array(
':receipt_id' => $receipt_id
)
);
return $row;
}
public function addReceipt($receipt_info)
{
$gameid = phpcommon\extractGameId($receipt_info['account_id']);
$conn = $this->getMysql($this->getStrDNA($receipt_info['receipt_id']));
$conn->execScript('INSERT INTO receipt( ' .
' receipt_id, orderid, account_id, roleid, server_id, channel, ' .
' gameid, itemid, ipv4, receipt_data, createtime) ' .
'VALUES (:receipt_id, :orderid, :account_id, :roleid, :server_id, :channel, ' .
' :gameid, :itemid, :ipv4, :receipt_data, :createtime);',
array(
':receipt_id' => $receipt_info['receipt_id'],
':orderid' => $receipt_info['orderid'],
':account_id' => $receipt_info['account_id'],
':roleid' => $receipt_info['roleid'],
':server_id' => $receipt_info['server_id'],
':channel' => $receipt_info['channel'],
':gameid' => $gameid,
':itemid' => $receipt_info['itemid'],
':ipv4' => phpcommon\getIPv4(),
':receipt_data' => $receipt_info['receipt_data'],
':createtime' => time(),
));
}
public function addPreOrder($pre_order_info)
{
$pre_order_info['sp_orderid'] = '';
$pre_order_info['sp_amount'] = 0;
$pre_order_info['sp_confirmtime'] = 0;
$pre_order_info['sp_pay_result'] = 0;
return $this->internalAddOrder($pre_order_info);
}
public function addComplateOrder($order_info)
{
return $this->internalAddOrder($order_info);
}
private function internalAddOrder($order_info)
{
$gameid = phpcommon\extractGameId($order_info['account_id']);
$openid = phpcommon\extractOpenId($order_info['account_id']);
$channel = phpcommon\extractChannel($order_info['account_id']);
$conn = $this->getMysql($this->getStrDNA($order_info['account_id']));
$ret = $conn->execScript('INSERT INTO orderinfo(' .
' orderid, account_id, roleid, server_id, channel, ' .
' poly_sdk_channel, unified_channel, ' .
' gameid, openid, itemid, try_count, price, ipv4, status, ' .
' confirmtime, createtime, sp_orderid, sp_amount, ' .
' sp_confirmtime, sp_pay_result ' .
' ) ' .
'VALUES (:orderid, :account_id, :roleid, :server_id, :channel, ' .
' :poly_sdk_channel, :unified_channel, ' .
' :gameid, :openid, :itemid, :try_count, :price, :ipv4, :status, ' .
' :confirmtime, :createtime, :sp_orderid, :sp_amount, ' .
' :sp_confirmtime, :sp_pay_result ' .
' );',
array(
':orderid' => $order_info['orderid'],
':account_id' => $order_info['account_id'],
':roleid' => $order_info['roleid'],
':server_id' => $order_info['server_id'],
':channel' => $channel,
':poly_sdk_channel' => $order_info['poly_sdk_channel'],
':unified_channel' => $order_info['unified_channel'],
':gameid' => $gameid,
':openid' => $openid,
':itemid' => $order_info['itemid'],
':try_count' => 0,
':price' => $order_info['price'],
':ipv4' => $order_info['ipv4'],
':status' => 0,
':confirmtime' => 0,
':createtime' => time(),
':sp_orderid' => $order_info['sp_orderid'],
':sp_amount' => $order_info['sp_amount'],
':sp_confirmtime' => $order_info['sp_confirmtime'],
':sp_pay_result' => $order_info['sp_pay_result'],
)
);
return $ret;
}
public function spPayConfirm($cp_orderid, $sp_orderid, $sp_amount)
{
$conn = $this->getMysql($this->getCpOrderIdDNA($cp_orderid));
$ret = $conn->execScript('UPDATE orderinfo SET ' .
' sp_orderid = :sp_orderid, ' .
' sp_amount = :sp_amount, ' .
' sp_confirmtime = :sp_confirmtime, ' .
' sp_pay_result = :sp_pay_result ' .
'WHERE orderid = :orderid;',
array(
':orderid' => $cp_orderid,
':sp_orderid' => $sp_orderid,
':sp_amount' => $sp_amount,
':sp_confirmtime' => time(),
':sp_pay_result' => 1,
)
);
return $ret;
}
public function getPendingOrderList($account_id)
{
$conn = $this->getMysql($this->getStrDNA($account_id));
$rows = $conn->execQuery('SELECT orderid, sp_orderid, itemid, status FROM orderinfo ' .
'WHERE account_id=:account_id AND status != 2;',
array(
':account_id' => $account_id
)
);
$data = array();
foreach ($rows as $row) {
array_push($data, array(
'cp_orderid' => $row['orderid'],
'sp_orderid' => $row['sp_orderid'],
'itemid' => $row['itemid'],
'status' => $row['status'],
));
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'order_list' => $data
));
}
public function getInternetGamesConf($gameid)
{
$json_str = $json_string = file_get_contents('../config/internet_games.json');
$jsonobj = json_decode($json_str, true);
foreach ($jsonobj as $conf) {
if ($conf['gameid'] == $gameid) {
return $conf;
}
}
return null;
}
public function deliver($account_id, $cp_orderid)
{
$conn = $this->getMysql($this->getStrDNA($account_id));
$row = $conn->execQueryOne('SELECT orderid, sp_orderid, itemid, status, sp_pay_result ' .
'FROM orderinfo ' .
'WHERE account_id=:account_id AND orderid=:orderid;',
array(
':account_id' => $account_id,
':orderid' => $cp_orderid
)
);
if (empty($row)) {
phpcommon\sendError(3, '不支持发货功能');
return;
}
$gameid = phpcommon\extractGameId($receipt_info['account_id']);
$game_conf = $this->getInternetGamesConf($gameid);
if ($game_conf) {
phpcommon\sendError(3, '不支持发货功能');
return;
}
if ($row['status'] == 1) {
phpcommon\sendError(1, '已经发货不能重复发货');
return;
}
if ($row['sp_pay_result'] != 1) {
phpcommon\sendError(2, '平台未确认');
return;
}
$ret = $conn->execScript('UPDATE orderinfo SET ' .
' status=1, ' .
' confirmtime=:confirmtime ' .
'WHERE orderid=:orderid;',
array(
':confirmtime' => time(),
':orderid' => $cp_orderid
));
if (!$ret) {
phpcommon\sendError(100, '服务器内部错误');
return;
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'itemid' => $row['itemid']
));
}
}