PtbAgent.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /**
  3. * PtbAgent.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/18 21:39
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\wallet;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\agent\AgentWallet;
  15. use huo\controller\common\Base;
  16. use huo\controller\common\CommonFunc;
  17. use huo\controller\common\HuoSession;
  18. use huo\controller\finance\Income;
  19. use huo\controller\member\Member;
  20. use huo\controller\member\MemWallet;
  21. use huo\controller\rate\PtbRate;
  22. use huo\controller\rate\Rate;
  23. use huo\model\finance\PtbAgentChargeModel;
  24. use huo\model\finance\PtbAgentOrderModel;
  25. use huo\model\finance\PtbBackModel;
  26. use huolib\constant\OrderConst;
  27. use huolib\constant\PaywayConst;
  28. use huolib\constant\SettleConst;
  29. use huolib\constant\WalletConst;
  30. use huolib\pay\Pay;
  31. use huolib\status\CommonStatus;
  32. use huolib\status\MemberStatus;
  33. use huolib\status\OrderStatus;
  34. use huolib\status\SettleStatus;
  35. use huolib\tool\StrUtils;
  36. use huolib\utils\OrderUtils;
  37. use think\Exception;
  38. class PtbAgent extends Base {
  39. /**
  40. * 给玩家代充游戏币
  41. *
  42. * @param int $agent_id
  43. * @param int $game_id
  44. * @param string $username
  45. * @param float $amount
  46. *
  47. * @return array
  48. */
  49. public function chargeMemGm($agent_id, $game_id, $username, $amount) {
  50. $_mem_id = (new Member())->getIdByName($username);
  51. if (empty($_mem_id)) {
  52. $_code = MemberStatus::UID_NOT_EXISTS;
  53. return $this->huoError($_code, MemberStatus::getMsg($_code));
  54. }
  55. $_wr_class = new WalletRequest();
  56. $_order_id = StrUtils::genOrderId($agent_id, $agent_id, $_mem_id, WalletConst::WALLET_ORDER_PREFIX_P2GM);
  57. $_wr_class->setOrderId($_order_id);
  58. $_wr_class->setAppId($game_id);
  59. $_wr_class->setMemId($_mem_id);
  60. $_wr_class->setAgentId($agent_id);
  61. /* 计算玩家可以获得的游戏币 */
  62. $_gm_cnt = $amount;
  63. $_wr_class->setGmCnt($_gm_cnt);
  64. $_wr_class->setType(WalletConst::WALLET_FROM_PROXY);
  65. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  66. /*计算等值的人民币*/
  67. $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
  68. $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rmb_rate));
  69. /*计算渠道消耗的平台币*/
  70. $_cost_ptb_cnt = $this->getCostPtbCnt($game_id, $agent_id, $amount);
  71. $_wr_class->setCostPtbCnt($_cost_ptb_cnt);
  72. $_wr_class->setRealAmount(StrUtils::formatNumber($_cost_ptb_cnt / $_ptb_rmb_rate));
  73. /* 渠道平台币消耗 */
  74. $_rs = $this->createPaoOrder($_wr_class);
  75. if (SettleStatus::NO_ERROR != $_rs) {
  76. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  77. }
  78. /* 玩家游戏币收入 */
  79. $_rs = (new MemWallet())->createGmcOrder($_wr_class);
  80. if (SettleStatus::NO_ERROR != $_rs) {
  81. // TODO: wuyonghong 2018/5/18 需要回退处理渠道平台币
  82. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  83. }
  84. /*父渠道收入*/
  85. (new Income())->incomeFroGmc($_wr_class->getOrderId());
  86. $_code = CommonStatus::NO_ERROR;
  87. return $this->huoError($_code, CommonStatus::getMsg($_code));
  88. }
  89. /**
  90. * 给玩家代充平台币
  91. *
  92. * @param int $agent_id
  93. * @param string $username
  94. * @param float $amount
  95. *
  96. * @return array
  97. */
  98. public function chargeMemPtb($agent_id, $username, $amount) {
  99. $_mem_id = (new Member())->getIdByName($username);
  100. if (empty($_mem_id)) {
  101. $_code = MemberStatus::UID_NOT_EXISTS;
  102. return $this->huoError($_code, MemberStatus::getMsg($_code));
  103. }
  104. $_wr_class = new WalletRequest();
  105. $_order_id = StrUtils::genOrderId($agent_id, $agent_id, $_mem_id, WalletConst::WALLET_ORDER_PREFIX_P2M);
  106. $_wr_class->setOrderId($_order_id);
  107. $_wr_class->setCostPtbCnt($amount);
  108. $_wr_class->setMemId($_mem_id);
  109. $_wr_class->setAgentId($agent_id);
  110. $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
  111. $_ptb_rate = (new PtbRate($agent_id))->getAgentRate();
  112. $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rmb_rate * $_ptb_rate));
  113. $_wr_class->setRealAmount(0);
  114. /* 计算玩家可以获得的平台币 */
  115. $_ptb_cnt = $amount;
  116. $_wr_class->setPtbCnt($_ptb_cnt);
  117. $_wr_class->setType(WalletConst::WALLET_FROM_GIVE);
  118. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  119. /* 渠道平台币消耗 */
  120. $_rs = $this->createPaoOrder($_wr_class);
  121. if (SettleStatus::NO_ERROR != $_rs) {
  122. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  123. }
  124. /* 玩家平台币收入 */
  125. $_rs = (new MemWallet())->createPcOrder($_wr_class);
  126. if (SettleStatus::NO_ERROR != $_rs) {
  127. // TODO: wuyonghong 2018/5/18 需要回退处理渠道平台币
  128. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  129. }
  130. $_code = CommonStatus::NO_ERROR;
  131. return $this->huoError($_code, CommonStatus::getMsg($_code));
  132. }
  133. /**
  134. * 代理平台币消耗
  135. *
  136. * @param WalletRequest $wc
  137. *
  138. * @return int
  139. */
  140. public function createPaoOrder(WalletRequest $wc) {
  141. /* 扣除渠道平台币 */
  142. $_rs = (new AgentWallet())->updatePtb(
  143. $wc->getAgentId(), $wc->getCostPtbCnt(), SettleConst::SETTLE_WALLET_DEDUCT
  144. );
  145. if (SettleStatus::NO_ERROR != $_rs) {
  146. return $_rs;
  147. }
  148. /* 插入渠道消费订单 */
  149. $_pao_data = $wc->getPtbAgentOrderData();
  150. $_rs = (new PtbAgentOrderModel())->createOrder($_pao_data);
  151. if (false == $_rs) {
  152. return SettleStatus::ORDER_CREATE_FAIL;
  153. }
  154. return SettleStatus::NO_ERROR;
  155. }
  156. /**
  157. * 代理平台币收入
  158. *
  159. * @param WalletRequest $wc
  160. *
  161. * @return int
  162. */
  163. public function createPacOrder(WalletRequest $wc) {
  164. if (OrderConst::PAY_STATUS_SUC == $wc->getStatus()) {
  165. /* 增加渠道平台币 */
  166. $_rs = (new AgentWallet())->updatePtb(
  167. $wc->getToAgentId(), $wc->getPtbCnt(), SettleConst::SETTLE_WALLET_ADD
  168. );
  169. if (SettleStatus::NO_ERROR != $_rs) {
  170. return $_rs;
  171. }
  172. }
  173. /* 插入渠道平台币收入订单 */
  174. $_pac_data = $wc->getPtbAgentChargeData();
  175. $_rs = (new PtbAgentChargeModel())->createOrder($_pac_data);
  176. if (false == $_rs) {
  177. return SettleStatus::ORDER_CREATE_FAIL;
  178. }
  179. return SettleStatus::NO_ERROR;
  180. }
  181. /**
  182. * 官方给渠道发放平台币
  183. *
  184. * @param int $cur_admin_id 当前登录的管理员id
  185. * @param $agent_id
  186. * @param $ptb_cnt
  187. * @param $remark
  188. *
  189. * @return array
  190. */
  191. public function gainAgentPtb($cur_admin_id, $agent_id, $ptb_cnt, $remark) {
  192. $_ac_class = AgentCache::ins();
  193. $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
  194. $_ae_data = $_ac_class->getAgentExtByAgentId($agent_id);
  195. if (empty($_agent_data) || empty($_ae_data)) {
  196. $_code = MemberStatus::UID_INVALID;
  197. return $this->huoError($_code, MemberStatus::getMsg($_code));
  198. }
  199. $_wr_class = new WalletRequest();
  200. $_order_id = StrUtils::genOrderId($agent_id, $agent_id, 0, WalletConst::WALLET_ORDER_PREFIX_O2A);
  201. $_wr_class->setAgentId($cur_admin_id);
  202. $_wr_class->setToAgentId($agent_id);
  203. $_wr_class->setOrderId($_order_id);
  204. $_wr_class->setType(WalletConst::WALLET_FROM_GIVE);
  205. $_wr_class->setPtbCnt($ptb_cnt);
  206. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  207. $_wr_class->setRemark($remark);
  208. $_rs = $this->createPacOrder($_wr_class);
  209. if (SettleStatus::NO_ERROR != $_rs) {
  210. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  211. }
  212. $_code = CommonStatus::NO_ERROR;
  213. return $this->huoError($_code, CommonStatus::getMsg($_code));
  214. }
  215. /**
  216. * 官方扣回渠道平台币
  217. *
  218. * @param int $cur_admin_id 当前登录的管理员id
  219. * @param $agent_id
  220. * @param $order_ptb_cnt
  221. * @param $ptb_cnt
  222. * @param $remark
  223. * @param null $order_id
  224. *
  225. * @return array
  226. */
  227. public function deductAgentPtb($cur_admin_id, $agent_id, $order_ptb_cnt, $ptb_cnt, $remark, $order_id = null) {
  228. $_ac_class = AgentCache::ins();
  229. $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
  230. $_ae_data = $_ac_class->getAgentExtByAgentId($agent_id);
  231. if (empty($_agent_data) || empty($_ae_data)) {
  232. $_code = MemberStatus::UID_INVALID;
  233. return $this->huoError($_code, MemberStatus::getMsg($_code));
  234. }
  235. $_wr_class = new WalletRequest();
  236. $_wr_class->setAgentId($cur_admin_id);
  237. $_wr_class->setToAgentId($agent_id);
  238. $_wr_class->setPtbCnt($order_ptb_cnt);
  239. $_wr_class->setBackPtbCnt($ptb_cnt);
  240. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  241. $_wr_class->setRemark($remark);
  242. if (!is_null($order_id)) {
  243. $_wr_class->setOrderId($order_id);
  244. }
  245. $_rs = $this->createPadOrder($_wr_class);
  246. if (SettleStatus::NO_ERROR != $_rs) {
  247. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  248. }
  249. $_code = CommonStatus::NO_ERROR;
  250. return $this->huoError($_code, CommonStatus::getMsg($_code));
  251. }
  252. /**
  253. * 代理平台币扣除
  254. *
  255. * @param WalletRequest $wc
  256. *
  257. * @return array|bool|int
  258. */
  259. private function createPadOrder(WalletRequest $wc) {
  260. if (OrderConst::PAY_STATUS_SUC == $wc->getStatus()) {
  261. /* 扣除渠道平台币 */
  262. $_rs = (new AgentWallet())->updatePtb(
  263. $wc->getToAgentId(), $wc->getBackPtbCnt(), SettleConst::SETTLE_WALLET_DEDUCT
  264. );
  265. if (SettleStatus::NO_ERROR != $_rs) {
  266. return $_rs;
  267. }
  268. }
  269. /* 插入扣除订单 */
  270. $_deduct_data = $wc->getPtbAgentDeductData();
  271. $_rs = (new PtbBackModel())->createOrder($_deduct_data);
  272. if (false == $_rs) {
  273. return SettleStatus::ORDER_CREATE_FAIL;
  274. }
  275. return SettleStatus::NO_ERROR;
  276. }
  277. /**
  278. * 给下级充值平台币
  279. *
  280. * @param int $agent_id
  281. * @param int $sub_agent_id
  282. * @param float $amount
  283. *
  284. * @return array
  285. */
  286. public function chargeSubAgentPtb($agent_id, $sub_agent_id, $amount) {
  287. if (empty($sub_agent_id)) {
  288. $_code = MemberStatus::UID_NOT_EXISTS;
  289. return $this->huoError($_code, MemberStatus::getMsg($_code));
  290. }
  291. $_wr_class = new WalletRequest();
  292. $_order_id = StrUtils::genOrderId($agent_id, $sub_agent_id, 0, WalletConst::WALLET_ORDER_PREFIX_P2A);
  293. $_wr_class->setOrderId($_order_id);
  294. $_wr_class->setCostPtbCnt($amount);
  295. $_wr_class->setToAgentId($sub_agent_id);
  296. $_wr_class->setAgentId($agent_id);
  297. $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
  298. $_ptb_rate = (new PtbRate($sub_agent_id))->getAgentRate();
  299. $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rmb_rate * $_ptb_rate));
  300. $_wr_class->setRealAmount(0);
  301. /* 计算下级渠道获得的平台币 */
  302. $_ptb_cnt = $_wr_class->getCostPtbCnt();
  303. $_wr_class->setPtbCnt($_ptb_cnt);
  304. $_wr_class->setType(WalletConst::WALLET_FROM_EXCHANGE);
  305. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  306. /* 渠道平台币消耗 */
  307. $_rs = $this->createPaoOrder($_wr_class);
  308. if (SettleStatus::NO_ERROR != $_rs) {
  309. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  310. }
  311. /* 玩家平台币收入 */
  312. $_rs = $this->createPacOrder($_wr_class);
  313. if (SettleStatus::NO_ERROR != $_rs) {
  314. // TODO: wuyonghong 2018/5/18 需要回退处理渠道平台币
  315. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  316. }
  317. $_code = CommonStatus::NO_ERROR;
  318. return $this->huoError($_code, CommonStatus::getMsg($_code));
  319. }
  320. /**
  321. * 预下单
  322. *
  323. * @param $agent_id
  324. * @param $amount
  325. * @param $type
  326. * @param $payway
  327. *
  328. * @return array|bool|string
  329. */
  330. public function preorder($agent_id, $amount, $type, $payway) {
  331. if (empty($agent_id)) {
  332. $_code = MemberStatus::LOGIN_IS_OUT;
  333. return $this->huoError($_code, MemberStatus::getMsg($_code));
  334. }
  335. $_chk_rs = OrderUtils::checkAmount($amount);
  336. if (OrderStatus::NO_ERROR != $_chk_rs) {
  337. return $this->huoError($_chk_rs, OrderStatus::getMsg($_chk_rs));
  338. }
  339. $_ac_class = AgentCache::ins();
  340. $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
  341. $_ae_data = $_ac_class->getAgentExtByAgentId($agent_id);
  342. if (empty($_agent_data) || empty($_ae_data)) {
  343. $_code = MemberStatus::UID_INVALID;
  344. return $this->huoError($_code, MemberStatus::getMsg($_code));
  345. }
  346. $_wr_class = new WalletRequest();
  347. $_order_id = StrUtils::genOrderId($agent_id, $agent_id, $agent_id, WalletConst::WALLET_ORDER_PREFIX_PA);
  348. $_wr_class->setOrderId($_order_id);
  349. $_wr_class->setToAgentId($agent_id);
  350. $_wr_class->setType($type);
  351. $_wr_class->setPayway($payway);
  352. $_wr_class->setRealAmount($amount);
  353. /* 平台币只能充值整数 向下取整 */
  354. $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
  355. $_ptb_rate = (new PtbRate($agent_id))->getAgentRate();
  356. $_wr_class->setDiscount($_ptb_rate);
  357. $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rate));
  358. $_wr_class->setPtbCnt($_wr_class->getAmount() * $_ptb_rmb_rate);
  359. $_rs = $this->createPacOrder($_wr_class);
  360. if (SettleStatus::NO_ERROR != $_rs) {
  361. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  362. }
  363. if (PaywayConst::PAYWAY_BALANCE == $payway) {
  364. /* 余额充值特殊流程 */
  365. /* 扣除渠道钱包 */
  366. $_aw_class = new AgentWallet();
  367. $_rs = $_aw_class->updateWallet($agent_id, $amount, SettleConst::SETTLE_WALLET_DEDUCT);
  368. if (SettleStatus::NO_ERROR != $_rs) {
  369. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  370. }
  371. /* 调用支付通知 */
  372. $_rs = (new Notify())->payNotifyAgentPtbCharge(
  373. $_order_id, $_order_id, $amount, PaywayConst::PAYWAY_BALANCE
  374. );
  375. if (false == $_rs) {
  376. $_code = OrderStatus::INNER_ERROR;
  377. return $this->huoSuccess($_code, OrderStatus::getMsg($_code));
  378. }
  379. $_code = OrderStatus::NO_ERROR;
  380. return $this->huoSuccess($_code, OrderStatus::getMsg($_code));
  381. }
  382. try {
  383. $_pay_class = Pay::ins()->get($payway);
  384. $_func = 'pcPay';
  385. $_show_url = url('agent/'.$payway.'/showUrl', ['order_id' => $_order_id], false, AGENTSITE);
  386. $_return_url = url(
  387. 'agent/'.$payway.'/return', ['order_id' => $_order_id], false, AGENTSITE
  388. );
  389. $_pay_class->setReturnUrl($_return_url);
  390. $_pay_class->setShowUrl($_show_url);
  391. $_pay_class->setOrderId($_order_id);
  392. $_product_name = config('CURRENCY_NAME').'充值'.$amount;
  393. $_pay_class->setProductName($_product_name);
  394. $_pay_class->setProductDesc($_product_name);
  395. $_pay_class->setRealAmount($amount);
  396. $_pay_class->setProductId(WalletConst::WALLET_PRODUCT_AGENT_PTB);
  397. $_pay_class->setIp(request()->ip());
  398. $_pay_class->setOpenId((new HuoSession($agent_id))->getOpenId());
  399. $_pay_class->setMemId($agent_id);
  400. // TODO: liuhongliang 2018/6/7 特殊字符是否能够支持盛付通支付?
  401. $_pay_class->setMemRealName($_agent_data['user_nicename']);
  402. $_pay_class->setMemRegTime($_agent_data['create_time']);
  403. $_pay_class->setMemMobile($_agent_data['mobile']);
  404. $_pay_rs = $_pay_class->$_func();
  405. if (empty($_pay_rs)) {
  406. $_code = OrderStatus::PAYWAY_PREORDER_ERROR;
  407. return $this->huoError($_code, SettleStatus::getMsg($_code));
  408. }
  409. $_code = OrderStatus::NO_ERROR;
  410. return $this->huoSuccess($_code, OrderStatus::getMsg($_code), $_pay_rs);
  411. } catch (Exception $e) {
  412. $_code = OrderStatus::PAYWAY_NOT_EXISTS;
  413. return $this->huoError($_code, OrderStatus::getMsg($_code));
  414. }
  415. }
  416. /**
  417. * @param $order_id
  418. *
  419. * @return bool
  420. */
  421. public function getStatus($order_id = '') {
  422. if (empty($order_id)) {
  423. return false;
  424. }
  425. $_o_model = new PtbAgentChargeModel();
  426. $_rs = $_o_model->getStatus($order_id);
  427. if (false == $_rs) {
  428. return false;
  429. }
  430. return $_rs;
  431. }
  432. /***
  433. * 计算实际花费平台币
  434. *
  435. * @param $game_id //游戏id
  436. * @param $agent_id //渠道id
  437. * @param $amount //游戏币
  438. *
  439. * @return float|int
  440. */
  441. public function getCostPtbCnt($game_id, $agent_id, $amount) {
  442. /*游戏币转换人民币*/
  443. $_gm_rmb_rate = CommonFunc::getGmRmbRate();
  444. $_money = $amount / $_gm_rmb_rate; //等值金额
  445. $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
  446. /*人民币转平台币*/
  447. $_ptb = $_money * $_ptb_rmb_rate;
  448. $_prate_class = new Rate($game_id, $agent_id);
  449. $_return = $_ptb * $_prate_class->getAgentRate();
  450. return $_return;
  451. }
  452. }