YuanBaoPayProc.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace loyalsoft;
  3. /*
  4. * 支付日志模块
  5. * 2016.10.13 gwang 提取
  6. */
  7. /**
  8. * 支付日志类
  9. */
  10. class YuanBaoPayProc {
  11. /**
  12. * 【多平台】扣除玩家游戏币(Ps.发货逻辑最后执行此步骤)
  13. * @param UserModel $user
  14. * @param Req $req Description
  15. * @param int $amt 扣除数量
  16. * @param string $source (string)备注,扣除来源(模块)
  17. * @return int ErrCode
  18. */
  19. public static function mn_SaveUserYuanbao($req, $amt, $source = "") {
  20. $err = ErrCode::ok;
  21. if ($amt > 0) { // 防御,数量不可能小于0
  22. $user = $req->userInfo->game->baseInfo;
  23. $zoneid = $req->zoneid; // 分区Id
  24. if ($user->yuanbao < $amt) { // 余额不足
  25. CLog::err("[Cheating] mn_SaveUserYuanbao:" . $req->uid, __CLASS__);
  26. $err = ErrCode::notenought_yuanbao;
  27. } else {// 更新玩家游戏币余额
  28. $user->yuanbao -= $amt;
  29. self::$m_billno = date("Y-m-d H:i:is");
  30. }
  31. self::_mPayLog($zoneid, $req->uid, MLogType::Save, $err, #
  32. $amt, $user->yuanbao, $user->cash, $user->gift_cash, $user->charge_amt, $source);
  33. }
  34. return $err;
  35. }
  36. /**
  37. * 新版支付(引入元宝这种货币)
  38. * @param Req $req
  39. * @return type
  40. */
  41. public static function m_payByYuanbao($req) {
  42. $resp = Resp::err(ErrCode::err_method_notimplement);
  43. $user = $req->userInfo->game->baseInfo; # user引用
  44. $privateState = $req->userInfo->game->privateState;
  45. // 客户端参数解析
  46. $paytype = $req->paras[0]; // 付费类型 3.元宝, 2. 钻石, 1. 金币,
  47. $itemId = $req->paras[1]; // 道具ID
  48. $itemModel = CGameConfig::item_getItem($itemId); # 取道具的常量数据
  49. if ($itemModel == null) { //1.检测是否存在道具的常量数据
  50. $resp = Resp::err(ErrCode::err_const_no);
  51. } else {
  52. $properties = JsonUtil::decode($itemModel->properties);
  53. if (CommUtil::isPropertyExists($properties, "isActive") # 活动物品
  54. && StlUtil::arrayIn($privateState->activeList, $properties->isActive)) {
  55. $resp = Resp::err(Errcode::active_hasgetted); # 已领过
  56. } else {
  57. if ($paytype == 3) {# 元宝
  58. if ($user->yuanbao < $itemModel->cost) { # 元宝不足
  59. CLog::err("[Cheating] m_payByYuanbao:" . $req->uid, __CLASS__);
  60. $resp = Resp::err(ErrCode::notenought_yuanbao);
  61. } else { # 发货
  62. StoreProc::addSeprateItem($itemModel, $req);
  63. $amt = $itemModel->cost;
  64. $source = "商城购买-" . $itemModel->name;
  65. self::mn_SaveUserYuanbao($req, $amt, $source); // 更新玩家游戏币余额
  66. UserProc::updateUserInfo(); // 直接将$user的最新值返回给客户端
  67. $resp = Resp::ok("success");
  68. }
  69. } else { # 钻石、金币
  70. $prize = JsonUtil::decode($itemModel->prize); # 价格
  71. if (property_exists($prize, "c") && $prize->c > 0) {// 钻石购买
  72. $amt = self::_getItemCashPrize($itemModel); // 道具价格(钻石)
  73. if ($amt <= 0) {
  74. $resp = Resp::err(ErrCode::pay_price_err);
  75. } else {
  76. if ($user->cash < $amt) { // 余额不足
  77. $resp = Resp::err(ErrCode::notenough_cash_msg);
  78. } else { // 发货
  79. StoreProc::addSeprateItem($itemModel, $req); // 更新玩家游戏币余额
  80. // $user->cash -= $amt;
  81. self::mn_SaveUserCash($req, $amt, "商城购买-花钻", $itemId);
  82. UserProc::updateUserInfo(); // 直接将$user的最新值返回给客户端
  83. $resp = Resp::ok($user);
  84. }
  85. }
  86. }// 金币购买
  87. else if (CommUtil::isPropertyExists($prize, "g") && $prize->g > 0) {
  88. if ($req->userInfo->game->baseInfo->gold < $prize->g) {
  89. $resp = Resp::err(ErrCode::notenough_gold_msg);
  90. } else {// 发货
  91. StoreProc::addSeprateItem($itemModel, $req); // 更新玩家金币余额
  92. UserModel::Comsume_Gold($user, $prize->g); // 更新玩家数据信息
  93. UserProc::updateUserInfo(); // 直接将$user的最新值返回给客户端
  94. $resp = Resp::ok($user);
  95. }
  96. } else { # 未知的支付类型
  97. $resp = Resp::err(ErrCode::pay_m_type_err);
  98. }
  99. }
  100. }
  101. }
  102. return $resp;
  103. }
  104. /**
  105. * 缓存一个账单号,如果代码执行过程中出现错误,直接取消扣除游戏币
  106. * 注意,若是逻辑不是在一次Request内完成的,那么次订单号需要进行存储
  107. * @var string
  108. */
  109. static $m_billno;
  110. /**
  111. * 【多平台版】刷新充值订单(领取充值结果)
  112. * @param Req $req
  113. */
  114. public static function m_refreshChargeOrders($req) {
  115. my_Assert($req, ErrCode::err_method_notimplement);
  116. $userinfo = $req->userInfo;
  117. my_Assert($userinfo, ErrCode::user_no_err);
  118. $user = $userinfo->game->baseInfo;
  119. my_Assert($user, ErrCode::user_no_err);
  120. // 客户端参数解析
  121. $uid = $req->uid; // userID
  122. $zoneid = $req->zoneid; // 分区Id
  123. my_Assert(!empty($uid) && ($zoneid > 0), ErrCode::paras_err);
  124. $mem = gMem();
  125. my_Assert($mem, ErrCode::err_mem);
  126. $orders = $mem->get(MemKey_User::PayOrders($zoneid, $uid)); # 取挂起的订单数据
  127. $amt = 0;
  128. $num = 0;
  129. if ($orders == null) { # 订单为空
  130. $orders = ArrayInit();
  131. }
  132. foreach ($orders as $order) { # 多张订单合并
  133. $amt += $order->price;
  134. $num += $order->amt;
  135. }
  136. $user->yuanbao += $num; #
  137. $user->charge_amt += $amt; # 历史充值记录(单位分)
  138. if ($amt > 0 # 充值金额大于0,且首付标志为false才可以
  139. && isset($req->userInfo->game->privateState->firstPayGift) # 无此字段代表已经领取礼包
  140. && !$req->userInfo->game->privateState->firstPayGift) {
  141. $req->userInfo->game->privateState->firstPayGift = true;
  142. }
  143. $orders = ArrayInit(); # 清空
  144. $mem->set(MemKey_User::PayOrders($zoneid, $uid), $orders); # 清理已经处理过的订单
  145. UserProc::updateUserInfo(); # 更新玩家数据信息
  146. $resp = Resp::ok($user->yuanbao); // 直接将$user的最新值返回给客户端
  147. # todo: 这里可以再加一条订单记录
  148. self::_mPayLog($zoneid, $uid, MLogType::Inquire, 0, #
  149. $num, $user->yuanbao, $user->cash, $user->gift_cash, $user->charge_amt, "刷新订单");
  150. return $resp;
  151. }
  152. /**
  153. * 取物品的价格(钻石)
  154. * @param GoodsItemModel $itemModel
  155. */
  156. private static function _getItemCashPrize($itemModel) {
  157. $c = 0;
  158. $prize = JsonUtil::decode($itemModel->prize);
  159. if (CommUtil::isPropertyExists($prize, "c")) {
  160. $c = $prize->c;
  161. }
  162. return $c;
  163. }
  164. /**
  165. * 【移动端】增加玩家游戏币
  166. * @param UserModel $user
  167. * @param Req $req Description
  168. * @param int $amt 增加数量
  169. * @param string $source (string)备注,增加来源(模块)
  170. * @return int ErrCode
  171. */
  172. public static function m_AddUserYuanbao($req, $amt, $source = "") {
  173. $err = ErrCode::ok;
  174. if ($amt > 0) { // 防御,数量不可能小于0
  175. $user = $req->userInfo->game->baseInfo;
  176. $zoneid = $req->zoneid; // 分区Id
  177. // 更新玩家游戏币余额
  178. $user->yuanbao += $amt;
  179. self::$m_billno = date("Y-m-d H:i:is");
  180. self::_mPayLog($zoneid, $req->uid, MLogType::Present, $err, #
  181. $amt, $user->yuanbao, $user->cash, $user->gift_cash, $user->charge_amt, $source);
  182. }
  183. return $err;
  184. }
  185. // <editor-fold defaultstate="collapsed" desc="==== 移动支付 插入日志 ====">
  186. /**
  187. * Sql语句移动支付插入一条log日志。
  188. * @var string
  189. */
  190. const SQL_M_LOG = "INSERT INTO `tab_mpaylog_%s` (zoneid,type,result,amt,balance,cash,giftedcash,chargedcash,source,oid,itemid) VALUES (%d,%d,%d,%d,%d,%d,%d,%d,'%s','%s','%s')";
  191. /**
  192. * 移动支付插入一条记录
  193. * @param int $zoneid
  194. * @param string $oid
  195. * @param int $type
  196. * @param int $amt
  197. * @param int $balance
  198. * @param int $cash 钻石
  199. * @param int $giftedcash
  200. * @param int $chargedcash
  201. * @param int $source
  202. */
  203. public static function _mPayLog($zoneid, $oid, $type, $result, $amt, $balance, $cash, $giftedcash, $chargedcash, $source = "srcUnKnown", $itemid = "") {
  204. // $month = date("Ym");
  205. // $sql = sprintf(self::SQL_M_LOG, $month, $zoneid, $type, $result, $amt, $balance, $cash, $giftedcash, $chargedcash, $source, $oid, $itemid);
  206. // $paydb = CPayInit();
  207. // $paydb->query($sql);
  208. // $paydb->close();
  209. }
  210. // </editor-fold>
  211. // <editor-fold defaultstate="collapsed" desc="WEB支付日志">
  212. // 在自己的模块里面呆着吧
  213. // </editor-fold>
  214. // <editor-fold defaultstate="collapsed" desc="yyb支付日志">
  215. // </editor-fold>
  216. }