From c5293c8884ea34c77e0ba6637a36f0735d0db917 Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 30 Jul 2023 13:54:27 +0800 Subject: [PATCH] 1 --- .../controller/CallbackController.class.php | 4 +- webapp/controller/ShopController.class.php | 34 --- ...opBuyGoodsDirect.php => InAppPurchase.php} | 4 +- webapp/services/callback/IutAppPurchase.php | 226 ++++++++++++++++++ webapp/services/callback/OutAppPurchase.php | 226 ++++++++++++++++++ 5 files changed, 456 insertions(+), 38 deletions(-) rename webapp/services/callback/{ShopBuyGoodsDirect.php => InAppPurchase.php} (99%) create mode 100644 webapp/services/callback/IutAppPurchase.php create mode 100644 webapp/services/callback/OutAppPurchase.php diff --git a/webapp/controller/CallbackController.class.php b/webapp/controller/CallbackController.class.php index 6266d6c5..be917c15 100644 --- a/webapp/controller/CallbackController.class.php +++ b/webapp/controller/CallbackController.class.php @@ -15,8 +15,8 @@ class CallbackController extends BaseController { 'MarketBuyOrderOk' => 'MarketBuyOrderOk', 'MarketCancelOrderOk' => 'MarketCancelOrderOk', 'MarketPriceUpdateOrderOk' => 'MarketPriceUpdateOrderOk', - 'ShopBuyGoodsDirect' => 'ShopBuyGoodsDirect', - //'ShopInappPurchaseDiamonds' => 'ShopInappPurchaseDiamonds', + 'inappPurchase' => 'InAppPurchase', + 'outappPurchase' => 'OutAppPurchase', ); public function dispatch() diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 834ba6dd..9fccbffc 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -408,38 +408,4 @@ class ShopController extends BaseAuthedController { } } - private function beginFirstTupop($address = null) - { - if (!$address) { - $address = myself()->_getAddress(); - } - $conn = myself()->_getMysql(''); - - $exist = SqlHelper::selectOne( - $conn, - 't_first_topup', - array('address'), - array('address' => $address) - ); - if ($exist) { - return; - } - - // 开始首充奖励活动进程 - $chk = SqlHelper::insert( - $conn, - 't_first_topup', - array( - 'address' => $address, - 'createtime' => myself()->_getNowTime(), - 'status1' => 0, - 'status2' => 0, - 'status3' => 0, - ) - ); - if (!$chk) { - return; - } - } - } diff --git a/webapp/services/callback/ShopBuyGoodsDirect.php b/webapp/services/callback/InAppPurchase.php similarity index 99% rename from webapp/services/callback/ShopBuyGoodsDirect.php rename to webapp/services/callback/InAppPurchase.php index 673ded55..a4a5cbf3 100644 --- a/webapp/services/callback/ShopBuyGoodsDirect.php +++ b/webapp/services/callback/InAppPurchase.php @@ -16,8 +16,8 @@ use models\ShopBuyRecord; use services\LogService; -class ShopBuyGoodsDirect -{ +class InAppPurchase { + public function process() { error_log("buyGoodsDirect --- " . json_encode($_REQUEST)); diff --git a/webapp/services/callback/IutAppPurchase.php b/webapp/services/callback/IutAppPurchase.php new file mode 100644 index 00000000..b042917f --- /dev/null +++ b/webapp/services/callback/IutAppPurchase.php @@ -0,0 +1,226 @@ + $account_id, + 'id' => $id, + 'order_id' => $order_id, + 'status' => $status, + 'txhash' => $txhash, + ); + + $hash_data = http_build_query($data); + + $signature = hash_hmac('sha256', $hash_data, BUY_SERVER_PKEY); + + 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('order_id' => $order_id)); + if (!$order) { + $this->_rspErr(2, "order not found: {$order_id}"); + return; + } + + $id = $order['id']; + $goods_num = $order['goods_num']; + $o_status = $order['status']; + $address = $order['address']; + + if ($o_status != 0) { + $this->_rspErr(1, "order status error, status: {$o_status}"); + return; + } + + $buyStatus = 0; // 1: 成功, 2: 失败 + switch ($status) { + case "9": + $buyStatus = 1; + // 充值成功,开始首充奖励 + $this->beginFirstTupop($address); + break; + case "99": + case "98": + $buyStatus = 2; + break; + default: + error_log("buyGoodsDirect--- " . $order_id . " --- " . $status); + $this->_rspErr(1, "status error, status: {$status}"); + break; + } + + SqlHelper::update($conn, 't_shop_buy_order', array('order_id' => $order_id), array('status' => $buyStatus)); + + if ($buyStatus == 2) { + $this->_rspErr(2, "buyStatus error, buyStatus: {$buyStatus}"); + return; + } + + // 以下是看商品表中是否配置了充值额外奖励 + $goods = ShopGoods::get($id); + error_log("buyGoodsDirect---" . json_encode($goods)); + $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); + error_log("buyGoodsDirect---" . $item_id . "---" . $item_num . "---" . $bundle_size . "---" . $meta['name']); + 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::productDiamondCallback(['account_id' => $account_id], $event); + } + + error_log("buyGoodsDirect---" . $address . "---" . $item_id . "---" . $item_num); + + $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 beginFirstTupop($address) + { + $conn = myself()->_getMysql(''); + + $exist = SqlHelper::selectOne( + $conn, + 't_first_topup', + array('address'), + array('address' => $address) + ); + if ($exist) { + return; + } + + // 开始首充奖励活动进程 + $chk = SqlHelper::insert( + $conn, + 't_first_topup', + array( + 'address' => $address, + 'createtime' => myself()->_getNowTime(), + 'status1' => 0, + 'status2' => 0, + 'status3' => 0, + ) + ); + if (!$chk) { + return; + } + } + + 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, + )); + } +} diff --git a/webapp/services/callback/OutAppPurchase.php b/webapp/services/callback/OutAppPurchase.php new file mode 100644 index 00000000..b042917f --- /dev/null +++ b/webapp/services/callback/OutAppPurchase.php @@ -0,0 +1,226 @@ + $account_id, + 'id' => $id, + 'order_id' => $order_id, + 'status' => $status, + 'txhash' => $txhash, + ); + + $hash_data = http_build_query($data); + + $signature = hash_hmac('sha256', $hash_data, BUY_SERVER_PKEY); + + 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('order_id' => $order_id)); + if (!$order) { + $this->_rspErr(2, "order not found: {$order_id}"); + return; + } + + $id = $order['id']; + $goods_num = $order['goods_num']; + $o_status = $order['status']; + $address = $order['address']; + + if ($o_status != 0) { + $this->_rspErr(1, "order status error, status: {$o_status}"); + return; + } + + $buyStatus = 0; // 1: 成功, 2: 失败 + switch ($status) { + case "9": + $buyStatus = 1; + // 充值成功,开始首充奖励 + $this->beginFirstTupop($address); + break; + case "99": + case "98": + $buyStatus = 2; + break; + default: + error_log("buyGoodsDirect--- " . $order_id . " --- " . $status); + $this->_rspErr(1, "status error, status: {$status}"); + break; + } + + SqlHelper::update($conn, 't_shop_buy_order', array('order_id' => $order_id), array('status' => $buyStatus)); + + if ($buyStatus == 2) { + $this->_rspErr(2, "buyStatus error, buyStatus: {$buyStatus}"); + return; + } + + // 以下是看商品表中是否配置了充值额外奖励 + $goods = ShopGoods::get($id); + error_log("buyGoodsDirect---" . json_encode($goods)); + $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); + error_log("buyGoodsDirect---" . $item_id . "---" . $item_num . "---" . $bundle_size . "---" . $meta['name']); + 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::productDiamondCallback(['account_id' => $account_id], $event); + } + + error_log("buyGoodsDirect---" . $address . "---" . $item_id . "---" . $item_num); + + $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 beginFirstTupop($address) + { + $conn = myself()->_getMysql(''); + + $exist = SqlHelper::selectOne( + $conn, + 't_first_topup', + array('address'), + array('address' => $address) + ); + if ($exist) { + return; + } + + // 开始首充奖励活动进程 + $chk = SqlHelper::insert( + $conn, + 't_first_topup', + array( + 'address' => $address, + 'createtime' => myself()->_getNowTime(), + 'status1' => 0, + 'status2' => 0, + 'status3' => 0, + ) + ); + if (!$chk) { + return; + } + } + + 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, + )); + } +}