Sdk.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * Order.php UTF-8
  4. *
  5. *
  6. * @date : 2018/1/19 21:17
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\pay;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\common\Base;
  15. use huo\controller\common\CommonFunc;
  16. use huo\controller\member\MemCache;
  17. use huo\controller\rate\Rate;
  18. use huo\controller\request\Channel;
  19. use huo\controller\request\Device;
  20. use huo\controller\request\Game as GameRq;
  21. use huo\controller\request\Mem;
  22. use huo\controller\request\Order;
  23. use huo\controller\request\Order as OrderRq;
  24. use huo\controller\request\Role;
  25. use huo\controller\wallet\GmCache;
  26. use huo\model\order\OrderExtModel;
  27. use huo\model\order\OrderModel;
  28. use huoAgentSwitch\controller\AgentSwitchController;
  29. use huolib\constant\CommonConst;
  30. use huolib\constant\OrderConst;
  31. use huolib\status\DeviceStatus;
  32. use huolib\status\GameStatus;
  33. use huolib\status\MemberStatus;
  34. use huolib\status\OrderStatus;
  35. use huolib\status\RoleStatus;
  36. use huolib\tool\Rand;
  37. use huolib\tool\StrUtils;
  38. use huolib\utils\DeviceUtils;
  39. use huolib\utils\GameUtils;
  40. use huolib\utils\OrderUtils;
  41. use huolib\utils\RoleUtils;
  42. use think\Log;
  43. class Sdk extends Base {
  44. protected function retSucMsg($code, $data = []) {
  45. $_msg = OrderStatus::getMsg($code);
  46. return $this->huoSuccess($code, $_msg, $data);
  47. }
  48. protected function retErrMsg($code) {
  49. $_err_msg = OrderStatus::getMsg($code);
  50. return $this->huoError($code, $_err_msg);
  51. }
  52. /**
  53. * @param OrderRq $order
  54. * @param Role $role
  55. * @param Mem $mem
  56. * @param GameRq $game_rq
  57. * @param Channel $channel
  58. * @param Device $device
  59. *
  60. * @return array|bool
  61. */
  62. public function checkParam(
  63. OrderRq $order, Role $role, Mem $mem,
  64. GameRq $game_rq, Channel $channel,
  65. Device $device
  66. ) {
  67. // TODO: wuyonghong 2018/5/24 校验参数未完成
  68. $_chk_order_rs = OrderUtils::checkSdkOrder($order->toArray());
  69. if (OrderStatus::NO_ERROR != $_chk_order_rs) {
  70. return $this->retErrMsg($_chk_order_rs);
  71. }
  72. $_chk_role_rs = RoleUtils::checkRole($role->toArray());
  73. if (RoleStatus::NO_ERROR != $_chk_role_rs) {
  74. return $this->huoError($_chk_role_rs, RoleStatus::getMsg($_chk_role_rs));
  75. }
  76. $_chk_device_rs = DeviceUtils::checkDevice($device->toArray());
  77. if (DeviceStatus::NO_ERROR != $_chk_device_rs) {
  78. return $this->huoError($_chk_device_rs, DeviceStatus::getMsg($_chk_device_rs));
  79. }
  80. $_chk_game_rs = GameUtils::checkGame($game_rq->toArray());
  81. if (GameStatus::NO_ERROR != $_chk_game_rs) {
  82. return $this->huoError($_chk_game_rs, GameStatus::getMsg($_chk_game_rs));
  83. }
  84. $_code = OrderStatus::NO_ERROR;
  85. return $this->retErrMsg($_code);
  86. }
  87. /**
  88. * 预下单
  89. *
  90. * @param OrderRq $order
  91. * @param Role $role
  92. * @param Mem $mem
  93. * @param GameRq $game_rq
  94. * @param Channel $channel
  95. * @param Device $device
  96. *
  97. * @return array|bool|string
  98. */
  99. public function preorder(OrderRq $order, Role $role, Mem $mem, GameRq $game_rq, Channel $channel, Device $device) {
  100. $_check_rs = $this->checkParam($order, $role, $mem, $game_rq, $channel, $device);
  101. if (OrderStatus::NO_ERROR != $_check_rs['code']) {
  102. return $_check_rs;
  103. }
  104. $_order_id = StrUtils::genOrderId($mem->getAgentId(), $channel->getAgentId(), $mem->getMemId());
  105. $order->setOrderId($_order_id);
  106. $_rdata = $this->buildOrder($order, $role, $mem, $game_rq, $device);
  107. if (empty($_rdata)) {
  108. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&step=2&game'.json_encode($game_rq->toArray())
  109. .'&channel'
  110. .json_encode($channel->toArray()).'&device'.json_encode($device->toArray()).'&member'
  111. .json_encode($mem->toArray());
  112. Log::write($_err_msg, Log::ERROR);
  113. return $this->retErrMsg(OrderStatus::ORDER_CREATE_FAIL);
  114. }
  115. // TODO: wuyonghong 2018/4/27 订单扩展处理
  116. $_rdata['order_id'] = $_order_id;
  117. /* 异步处理数据 */
  118. (new \huo\controller\queue\Order($game_rq->getHAppId()))->create(
  119. $device, $game_rq, $channel, $mem, $role, $order
  120. );
  121. return $this->retSucMsg(OrderStatus::NO_ERROR, $_rdata);
  122. }
  123. /**
  124. * @param OrderRq $order
  125. * @param Role $role
  126. * @param Mem $mem
  127. * @param GameRq $game_rq
  128. *
  129. * @param Device $device
  130. *
  131. * @return bool|mixed
  132. */
  133. public function buildOrder(OrderRq $order, Role $role, Mem $mem, GameRq $game_rq, Device $device) {
  134. /* 订单主表 */
  135. $_rate_class = new Rate($game_rq->getHAppId(), $mem->getAgentId());
  136. $order->setProductRealPrice(
  137. StrUtils::formatNumber($order->getProductPrice() * $_rate_class->getMemRate($mem->getMemId()))
  138. );
  139. $order->setRebateAmount(
  140. StrUtils::formatNumber($order->getProductPrice() * $_rate_class->getMemRebate($mem->getMemId()))
  141. );
  142. $_data = $order->toArray();
  143. $_data['mem_id'] = $mem->getMemId();
  144. $_data['mg_mem_id'] = $mem->getMgMemId();
  145. $_data['agent_id'] = $mem->getAgentId();
  146. $_data['app_id'] = $game_rq->getHAppId();
  147. $_data['vb_id'] = $game_rq->getVbId();
  148. $_data['rate'] = $_rate_class->getMemRate($mem->getMemId());
  149. /* 切量处理 */
  150. $_data['ip'] = $device->getIp();
  151. $_data = (new AgentSwitchController())->orderSwitch($_data);
  152. $_order_model = new OrderModel();
  153. $_pay_data = $_order_model->createOrder($_data);
  154. if (false == $_pay_data) {
  155. return false;
  156. }
  157. /* 2 订单扩展表 */
  158. $_data['pay_id'] = $_pay_data['id'];
  159. $_role_data = $role->toArray();
  160. $_device_data = $device->toArray();
  161. $_oe_data = array_merge($_data, $_role_data, $_device_data);
  162. $_oe_rs = (new OrderExtModel())->addOeLog($_oe_data);
  163. if (false == $_oe_rs) {
  164. return false;
  165. }
  166. $_pay_data['ext'] = $_oe_rs;
  167. $_rdata = SdkOrderCache::ins()->getInfoByOrderId($order->getOrderId());
  168. return $_rdata;
  169. }
  170. /**
  171. * @param $order_id
  172. *
  173. * @return bool
  174. */
  175. public function getStatus($order_id = '') {
  176. if (empty($order_id)) {
  177. return false;
  178. }
  179. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
  180. if (empty($_order_data)) {
  181. return false;
  182. }
  183. $_rdata['status'] = $_order_data['status'];
  184. $_rdata['cp_status'] = $_order_data['cp_status'];
  185. $_rdata['product_name'] = $_order_data['product_name'];
  186. $_rdata['product_id'] = $_order_data['product_id'];
  187. $_rdata['real_amount'] = $_order_data['real_amount'];
  188. $_rdata['product_cnt'] = $_order_data['product_cnt'];
  189. $_rdata['payway'] = $_order_data['payway'];
  190. return $_rdata;
  191. }
  192. /**
  193. * @param $order_id
  194. *
  195. * @param bool $is_client
  196. *
  197. * @return array|mixed
  198. */
  199. public function getPayInfo($order_id, $is_client = false) {
  200. $_order_id = $order_id;
  201. if (empty($order_id)) {
  202. return $this->retErrMsg(OrderStatus::ORDER_ID_EMPTY);
  203. }
  204. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($_order_id);
  205. if (empty($_order_data)) {
  206. return $this->retErrMsg(OrderStatus::ORDER_NOT_EXISTS);
  207. }
  208. $_me_data = MemCache::ins()->getMeInfoById($_order_data['mem_id']);
  209. if (empty($_me_data)) {
  210. $_code = MemberStatus::UID_NOT_EXISTS;
  211. return $this->huoError($_code, MemberStatus::getMsg($_code));
  212. }
  213. /* 获取玩家游戏币余额 */
  214. $_gmc_class = GmCache::ins();
  215. $_gm_cnt = $_gmc_class->getRemainByMemGame($_order_data['mem_id'], $_order_data['app_id']);
  216. if (empty($_gm_cnt)) {
  217. $_gm_cnt = 0;
  218. }
  219. if ($is_client) {
  220. $_rdata['pay_token'] = (new order())->genPayToken($_order_id);
  221. $_rdata['order_id'] = $_order_data['order_id'];
  222. $_rdata['product_name'] = $_order_data['product_name'];
  223. $_rdata['product_price'] = $_order_data['amount'];
  224. $_rdata['real_amount'] = $_order_data['real_amount'];
  225. $_rdata['ptb_cnt'] = $_me_data['ptb_cnt'];
  226. $_rdata['gm_cnt'] = $_gm_cnt;
  227. $_rdata['ptb_rate'] = CommonFunc::getPtbRmbRate();
  228. $_rdata['gm_rate'] = CommonFunc::getGmRmbRate();
  229. $_rate_class = new Rate($_order_data['app_id'], $_order_data['agent_id']);
  230. $_rdata['benefit_type'] = $_rate_class->getBenefitType();
  231. $_rdata['mem_rate'] = $_order_data['rate'];
  232. $_rdata['rebate_cnt'] = $_order_data['rebate_amount'];
  233. $_rdata['pay_type'] = $this->getClientPayWays(
  234. $_order_data['app_id'], $_rdata['product_price'], $_order_data['rate'],
  235. $_order_data['rebate_amount'], $_rdata['gm_cnt']
  236. );
  237. } else {
  238. $_rdata['order_id'] = $_order_data['order_id'];
  239. $_rdata['product_name'] = $_order_data['product_name'];
  240. $_rdata['product_price'] = $_order_data['amount'];
  241. //$_rdata['product_cnt'] = $_order_data['product_cnt']; //屏蔽数量不显示 2018/11/17 运营需求
  242. $_rdata['product_cnt'] = '';
  243. $_rdata['real_amount'] = $_order_data['real_amount'];
  244. $_rdata['ptb_cnt'] = $_me_data['ptb_cnt'];
  245. $_rdata['gm_cnt'] = isset($_me_data['gm_cnt']) ? $_me_data['gm_cnt'] : 0;
  246. $_rdata['ptb_rate'] = CommonFunc::getPtbRmbRate();
  247. $_rdata['gm_rate'] = CommonFunc::getGmRmbRate();
  248. $_rate_class = new Rate($_order_data['app_id'], $_order_data['agent_id']);
  249. $_rdata['benefit_type'] = $_rate_class->getBenefitType();
  250. $_rdata['mem_rate'] = $_order_data['rate'];
  251. $_rdata['rebate_cnt'] = $_order_data['rebate_amount'];
  252. $_rdata['pay_type'] = $this->getPayWays(
  253. $_order_data['app_id'], $_rdata['product_price'], $_rdata['ptb_cnt']
  254. );
  255. }
  256. return $this->retSucMsg(OrderStatus::NO_ERROR, $_rdata);
  257. }
  258. /**
  259. * @param string $app_id
  260. *
  261. * @param int $amount
  262. * @param int $ptb_cnt
  263. *
  264. * @return array
  265. */
  266. public function getPayWays($app_id, $amount = 0, $ptb_cnt = 0) {
  267. // TODO: wuyonghong 2018/4/28 从数据库读取
  268. if (empty($app_id)) {
  269. /* 获取支付方式 */
  270. $_rdata[0]['payway'] = 'alipay';
  271. $_rdata[0]['name'] = '支付宝';
  272. $_rdata[0]['icon'] = STATICSITE.'/h5sdk/images/arwec.png';
  273. $_rdata[0]['url'] = '支付宝';
  274. $_rdata[1]['payway'] = 'wxpay';
  275. $_rdata[1]['name'] = '微信';
  276. $_rdata[1]['icon'] = STATICSITE.'/h5sdk/images/wergcder.png';
  277. $_rdata[1]['url'] = '微信';
  278. }
  279. /* 获取支付方式 */
  280. $_rdata[0]['payway'] = 'alipay';
  281. $_rdata[0]['name'] = '支付宝';
  282. $_rdata[0]['icon'] = STATICSITE.'/h5sdk/images/arwec.png';
  283. $_rdata[0]['url'] = '支付宝';
  284. $_rdata[1]['payway'] = 'wxpay';
  285. $_rdata[1]['name'] = '微信';
  286. $_rdata[1]['icon'] = STATICSITE.'/h5sdk/images/wergcder.png';
  287. $_rdata[1]['url'] = '微信';
  288. // $_rdata[2]['payway'] = 'shengpay';
  289. // $_rdata[2]['name'] = '银行卡';
  290. // $_rdata[2]['icon'] = STATICSITE.'/h5sdk/images/upcash.png';
  291. // $_rdata[2]['url'] = '银行卡';
  292. if ($amount <= $ptb_cnt) {
  293. $_rdata[2]['payway'] = 'ptbpay';
  294. $_rdata[2]['name'] = config('CURRENCY_NAME');
  295. $_rdata[2]['icon'] = STATICSITE.'/h5sdk/images/ptbpay.png';
  296. $_rdata[2]['url'] = config('CURRENCY_NAME');
  297. }
  298. return $_rdata;
  299. }
  300. /**
  301. * @param string $app_id
  302. *
  303. * @param int $amount 支付金额
  304. * @param float $rate 折扣
  305. * @param float $rebate_cnt 返利
  306. * @param int $gm_cnt 游戏币数量
  307. *
  308. * @return array
  309. */
  310. public function getClientPayWays($app_id, $amount = 0, $rate = 1.0, $rebate_cnt = 0.0, $gm_cnt = 0) {
  311. // TODO: wuyonghong 2018/4/28 从数据库读取
  312. /* 获取支付方式 */
  313. $_rdata[0]['payway'] = 'alipay';
  314. $_rdata[0]['name'] = '支付宝';
  315. $_rdata[0]['mem_rate'] = $rate;
  316. $_rdata[0]['rebate_cnt'] = $rebate_cnt;
  317. $_rdata[0]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_zfb.png';
  318. $_rdata[0]['url'] = '支付宝';
  319. $_rdata[1]['payway'] = 'wxpay';
  320. $_rdata[1]['name'] = '微信';
  321. $_rdata[1]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_wxzf.png';
  322. $_rdata[1]['url'] = '微信';
  323. $_rdata[1]['mem_rate'] = $rate;
  324. $_rdata[1]['rebate_cnt'] = $rebate_cnt;
  325. $_rdata[2]['payway'] = 'gamepay';
  326. $_rdata[2]['name'] = config('GM_CURRENCY_NAME');
  327. if ($gm_cnt > 0) {
  328. $_rdata[2]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_youxibi.png';
  329. } else {
  330. $_rdata[2]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_youxibi_disable.png';
  331. }
  332. $_rdata[2]['url'] = config('GM_CURRENCY_NAME');
  333. $_rdata[2]['mem_rate'] = 1;
  334. $_rdata[2]['rebate_cnt'] = 0;
  335. return $_rdata;
  336. }
  337. /**
  338. * 查询微信订单
  339. *
  340. * @param $order_id
  341. */
  342. public function queryWxOrder($order_id) {
  343. // TODO: wuyonghong 2018/5/7 查询订单完成情况
  344. }
  345. }