game2006api/webapp/services/callback/ShopBuyGoodsDirect.php
songliang a33db90313 ...
2023-07-14 10:38:44 +08:00

174 lines
4.6 KiB
PHP

<?php
namespace services;
require_once("mt/ShopGoods.php");
require_once("mt/Item.php");
require_once("models/ShopBuyRecord.php");
require_once("services/LogService.php");
require_once("ShopAddItemService.php");
use phpcommon\SqlHelper;
use mt\ShopGoods;
use mt\Item;
use models\ShopBuyRecord;
use services\LogService;
class ShopBuyGoodsDirect
{
public function process()
{
error_log("buyGoodsDirect --- " . json_encode($_REQUEST));
// let repdata = {
// account_id: string
// order_id: string
// status: string
// id: string
// txhash: string
// }
// 我返回给你这些数据和一个sign字段,
// sign使用上面 repdata 按key 顺序排后, 组成key1=val1&key2=val2后, 使用hmac_sha256 hash, key是 iG4Rpsa)6U31$H#^T85$^^3
// PENDING = 0, // 初始状态
// TRANSFERING = 1, //只有国库模式才会有该状态
// TRANSFERED = 2, //只有国库模式才会有该状态
// SUCCESS = 9, // 成功的最终状态
// TRANSFER_FAIL = 98, // 转账错误
// FAIL = 99, // 也是错误
//
$account_id = getReqVal('account_id', '');
$order_id = getReqVal('order_id', '');
$status = getReqVal('status', '');
$id = getReqVal('id', '');
$txhash = getReqVal('txhash', '');
$sign = getReqVal('sign', '');
$data = array(
'account_id' => $account_id,
'id' => $id,
'order_id' => $order_id,
'status' => $status,
'txhash' => $txhash,
);
$hash_data = http_build_query($data);
$signature = hash_hmac('sha256', $hash_data, 'iG4Rpsa)6U31$H#^T85$^^3');
if ($signature != $sign) {
$this->_rspErr(1, "signature error, signature: {$signature}, sign: {$sign}");
return;
}
error_log("buyGoodsDirect-------" . $order_id . "---" . $status);
$conn = myself()->_getMysql('');
$order = SqlHelper::selectOne($conn, 't_shop_buy_order', array('address', 'id', 'item_id', 'goods_num', 'status'), array('idx' => $order_id));
$id = $order['id'];
$goods_num = $order['goods_num'];
$o_status = $order['status'];
if ($o_status != 0) {
$this->_rspErr(1, "order status error, status: {$o_status}");
return;
}
$buyStatus = 0; // 1: 成功, 2: 失败
switch ($status) {
case "9":
$buyStatus = 1;
break;
case "99":
case "98":
$buyStatus = 2;
break;
}
SqlHelper::update($conn, 't_shop_buy_order', array('idx' => $order_id), array('status' => $buyStatus));
// 以下是看商品表中是否配置了充值额外奖励
$goods = ShopGoods::get($id);
$goods_num = $order['goods_num'];
$bundle_size = $goods['bonus_num'] ? $goods['bonus_num'] : 0;
$item_num = $goods_num * $bundle_size;
$item_id = $goods['bonus'];
$meta = Item::get($item_id);
if ($meta && $item_num > 0) {
$address = $order['address'];
$account_id = $this->getAccountId($address);
if ($item_id == V_ITEM_DIAMOND) {
$event = [
'name' => LogService::RECHARGE_CEBG_BONUS,
'val' => $item_num
];
LogService::productDiamond(['account_id' => $account_id], $event);
}
$this->_addGoods($address, array(
'goods_id' => $item_id,
'goods_num' => $item_num,
'id' => $id,
));
}
$this->_rspOk();
}
private function getAccountId($address)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($address),
't_user',
array(
'address' => $address
)
);
return $row['account_id'];
}
private function _addGoods($address, $goods)
{
$itemService = new ShopAddItemService();
$item_id = $goods['goods_id'];
$goods_num = $goods['goods_num'];
$id = null;
if ($goods['id']) {
$id = $goods['id'];
}
error_log('_addGoods ' . $address . ' item_id ' . $item_id . ' goods_num ' . $goods_num . ' id ' . $id);
$itemService->addItem($address, $item_id, $goods_num);
if ($id) {
ShopBuyRecord::addWithAddress($address, $id, $goods_num);
}
}
private function _rspOk() {
echo json_encode(array(
'errcode' => 0,
'errmsg' => "callback success",
));
}
private function _rspErr($errcode, $errmsg) {
if (SERVER_ENV != _ONLINE) {
error_log(json_encode(array(
'errcode' => $errcode,
'errmsg' => $errmsg,
)));
}
echo json_encode(array(
'errcode' => $errcode,
'errmsg' => $errmsg,
));
}
}