From c8aaa5d8789ae156c4cd47e91d3ed754f5e9a577 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 2 Aug 2023 14:30:26 +0800 Subject: [PATCH] 1 --- webapp/controller/BaseController.class.php | 20 ++ webapp/services/callback/InAppPurchase.php | 259 +++++++++++---------- 2 files changed, 158 insertions(+), 121 deletions(-) diff --git a/webapp/controller/BaseController.class.php b/webapp/controller/BaseController.class.php index 4ab5a416..367732de 100644 --- a/webapp/controller/BaseController.class.php +++ b/webapp/controller/BaseController.class.php @@ -178,4 +178,24 @@ class BaseController { return $result; } + public function _addLogEx($accountId, $type, $subtype, $params) + { + $fieldsKv = array( + 'account_id' => $accountId, + 'type' => $type, + 'subtype' => $subtype, + 'param1' => getXVal($params, 'param1', ''), + 'param2' => getXVal($params, 'param2', ''), + 'param3' => getXVal($params, 'param3', ''), + 'param4' => getXVal($params, 'param4', ''), + 'createtime' => myself()->_getNowTime(), + 'modifytime' => myself()->_getNowTime() + ); + SqlHelper::insert( + myself()->_getMysql($accountId), + 't_game_log', + $fieldsKv + ); + } + } diff --git a/webapp/services/callback/InAppPurchase.php b/webapp/services/callback/InAppPurchase.php index 90faadc0..edc12e02 100644 --- a/webapp/services/callback/InAppPurchase.php +++ b/webapp/services/callback/InAppPurchase.php @@ -4,7 +4,10 @@ namespace services; require_once("mt/ShopGoods.php"); require_once("mt/Item.php"); + require_once("models/ShopBuyRecord.php"); +require_once("models/InAppOrder.php"); + require_once("services/LogService.php"); require_once("ShopAddItemService.php"); @@ -12,7 +15,9 @@ use phpcommon\SqlHelper; use mt\ShopGoods; use mt\Item; + use models\ShopBuyRecord; +use models\InAppOrder; use services\LogService; @@ -37,7 +42,7 @@ class InAppPurchase { public function process() { - error_log('ShopInappPurchaseDiamonds:' . json_encode($_REQUEST)); + error_log('InappPurchase:' . json_encode($_REQUEST)); error_log('----- inappPurchaseDiamonds -----'); $data = json_decode(file_get_contents('php://input'), true); @@ -53,126 +58,7 @@ class InAppPurchase { $records = $data['records']; for ($i = 0; $i < count($records); $i++) { - $record = $records[$i]; - - $product_id = $record['productId']; - $order_id = $record['gameOrderId']; - $out_order_id = $record['orderId']; - $status = $record['status']; - - switch ($status) { - case 9: { - $status = 1; - if (empty($order_id)) { - if (empty($product_id)) { - $this->_rspErr(2, "product_id is empty"); - return; - } - - // $goods = mt\ShopGoods::getByProductId($product_id); - - return; - } - - $order = SqlHelper::selectOne($conn, 't_web2_order', array('address', 'id', 'item_id', 'goods_num', 'status'), array('order_id' => $order_id, 'status' => 0)); - error_log('process order ' . json_encode($order)); - if (!$order) { - $this->_rspErr(3, "order not found, order_id: {$order_id}"); - return; - } - - SqlHelper::update($conn, 't_web2_order', array('order_id' => $order_id), array('status' => $status, 'channel' => $channel, 'out_order_id' => $out_order_id)); - - $id = $order['id']; - $goods = ShopGoods::get($id); - // 这里命名混乱了, 购买个数,一捆个数命名冲突 - $goods_num = $order['goods_num']; - $bundle_size = $goods['goods_num']; - $item_num = $goods_num * $bundle_size; - $item_id = $goods['goods_id']; - $address = $order['address']; - if (empty($address)) { - $this->_rspErr(4, "address is empty"); - return; - } - $account_id = $this->getAccountId($address); - if (empty($account_id)) { - $this->_rspErr(5, "account_id is empty"); - return; - } - - if ($item_id == V_ITEM_DIAMOND) { - $event = [ - 'name' => LogService::RECHARGE_DIAMOND, - 'val' => $item_num - ]; - LogService::productDiamond(['account_id' => $account_id], $event); - } - - $this->_addGoods($address, array( - 'goods_id' => $item_id, - 'goods_num' => $item_num, - 'id' => $id, - )); - } - break; - case 96: - $status = 3; - if (empty($order_id)) { - if (empty($product_id)) { - $this->_rspErr(2, "product_id is empty"); - return; - } - // $goods = mt\ShopGoods::getByProductId($product_id); - - return; - } - // 退款 - $order = SqlHelper::selectOne($conn, 't_web2_order', array('address', 'id', 'item_id', 'goods_num', 'status'), array('order_id' => $order_id, 'status' => 1)); - if (!$order) { - $this->_rspErr(3, "order not found, order_id: {$order_id}"); - return; - } - SqlHelper::update($conn, 't_web2_order', array('order_id' => $order_id), array('status' => $status)); - - $id = $order['id']; - $goods = ShopGoods::get($id); - // 这里命名混乱了, 购买个数,一捆个数命名冲突 - $goods_num = $order['goods_num']; - $bundle_size = $goods['goods_num']; - $item_num = $goods_num * $bundle_size; - $item_id = $goods['goods_id']; - $address = $order['address']; - if (empty($address)) { - $this->_rspErr(4, "address is empty"); - return; - } - $account_id = $this->getAccountId($address); - if (empty($account_id)) { - $this->_rspErr(5, "account_id is empty"); - return; - } - - if ($item_id == V_ITEM_DIAMOND) { - $event = [ - 'name' => LogService::RECHARGE_DIAMOND, - 'val' => -$item_num - ]; - LogService::productDiamond(['account_id' => $account_id], $event); - } - - $this->_decGoods($address, array( - 'goods_id' => $item_id, - 'goods_num' => $item_num, - 'id' => $id, - )); - break; - default: - $status = 0; - $this->_rspErr(1, "status is not 9 or 96"); - return; - break; - } + $this->processOneOrder($records[i]); } $this->_rspOk(); @@ -198,4 +84,135 @@ class InAppPurchase { return $sign == $signature; } + private function processOneOrder($record) + { + $productId = $record['productId']; + $orderId = $record['gameOrderId']; + $spOrderId = $record['orderId']; + $status = $record['status']; + + $orderDb = InAppOrder::find($orderId); + if (!$orderDb) { + error_log('InAppPurchase: not found order ' . json_encode($_REQUEST)); + return; + } + switch ($status) { + case 9: { + $status = 1; + if (empty($order_id)) { + if (empty($product_id)) { + $this->_rspErr(2, "product_id is empty"); + return; + } + + // $goods = mt\ShopGoods::getByProductId($product_id); + + return; + } + + $order = SqlHelper::selectOne($conn, 't_web2_order', array('address', 'id', 'item_id', 'goods_num', 'status'), array('order_id' => $order_id, 'status' => 0)); + error_log('process order ' . json_encode($order)); + if (!$order) { + $this->_rspErr(3, "order not found, order_id: {$order_id}"); + return; + } + + SqlHelper::update($conn, 't_web2_order', array('order_id' => $order_id), array('status' => $status, 'channel' => $channel, 'out_order_id' => $out_order_id)); + + $id = $order['id']; + $goods = ShopGoods::get($id); + // 这里命名混乱了, 购买个数,一捆个数命名冲突 + $goods_num = $order['goods_num']; + $bundle_size = $goods['goods_num']; + $item_num = $goods_num * $bundle_size; + $item_id = $goods['goods_id']; + $address = $order['address']; + if (empty($address)) { + $this->_rspErr(4, "address is empty"); + return; + } + $account_id = $this->getAccountId($address); + if (empty($account_id)) { + $this->_rspErr(5, "account_id is empty"); + return; + } + + if ($item_id == V_ITEM_DIAMOND) { + $event = [ + 'name' => LogService::RECHARGE_DIAMOND, + 'val' => $item_num + ]; + LogService::productDiamond(['account_id' => $account_id], $event); + } + + $this->_addGoods($address, array( + 'goods_id' => $item_id, + 'goods_num' => $item_num, + 'id' => $id, + )); + } + break; + case 96: + $status = 3; + if (empty($order_id)) { + if (empty($product_id)) { + $this->_rspErr(2, "product_id is empty"); + return; + } + // $goods = mt\ShopGoods::getByProductId($product_id); + + return; + } + // 退款 + $order = SqlHelper::selectOne($conn, 't_web2_order', array('address', 'id', 'item_id', 'goods_num', 'status'), array('order_id' => $order_id, 'status' => 1)); + if (!$order) { + $this->_rspErr(3, "order not found, order_id: {$order_id}"); + return; + } + SqlHelper::update($conn, 't_web2_order', array('order_id' => $order_id), array('status' => $status)); + + $id = $order['id']; + $goods = ShopGoods::get($id); + // 这里命名混乱了, 购买个数,一捆个数命名冲突 + $goods_num = $order['goods_num']; + $bundle_size = $goods['goods_num']; + $item_num = $goods_num * $bundle_size; + $item_id = $goods['goods_id']; + $address = $order['address']; + if (empty($address)) { + $this->_rspErr(4, "address is empty"); + return; + } + $account_id = $this->getAccountId($address); + if (empty($account_id)) { + $this->_rspErr(5, "account_id is empty"); + return; + } + + if ($item_id == V_ITEM_DIAMOND) { + $event = [ + 'name' => LogService::RECHARGE_DIAMOND, + 'val' => -$item_num + ]; + LogService::productDiamond(['account_id' => $account_id], $event); + } + + $this->_decGoods($address, array( + 'goods_id' => $item_id, + 'goods_num' => $item_num, + 'id' => $id, + )); + break; + default: + { + myself()->_addLogEx($orderDb['account_id'], 'InAppPurchase', 'error_order_status', + array( + $ordreDb['order_id'], + json_encode($orderDb) + )); + } + break; + } + } + }