Notify.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * Notify.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/16 23:21
  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\AgentWallet;
  14. use huo\controller\common\Base;
  15. use huo\controller\finance\Income;
  16. use huo\controller\member\MemWallet;
  17. use huo\model\finance\GmChargeModel;
  18. use huo\model\finance\PtbAgentChargeModel;
  19. use huo\model\finance\PtbChargeModel;
  20. use huoAccountDeal\controller\AccountOrder;
  21. use huolib\constant\OrderConst;
  22. use huolib\constant\SettleConst;
  23. use huolib\constant\WalletConst;
  24. use huolib\pay\Pay;
  25. use huolib\status\OrderStatus;
  26. use think\Exception;
  27. use think\Log;
  28. class Notify extends Base {
  29. /**
  30. * @param $payway
  31. *
  32. * @return bool
  33. */
  34. public function notifyUrl($payway) {
  35. try {
  36. $_pay_class = Pay::ins()->get($payway);
  37. } catch (Exception $e) {
  38. Log::write(
  39. "func=".__FUNCTION__."&class=".__CLASS__."&payway=".$payway.'&data='.json_encode($_REQUEST),
  40. LOG::ERROR
  41. );
  42. exit('fail');
  43. }
  44. $_order_class = '\\huo\\controller\\wallet\\Notify';
  45. $_func = 'payNotify';
  46. $_pay_class->setOrderClass($_order_class);
  47. $_pay_class->setFunc($_func);
  48. return $_pay_class->notifyUrl();
  49. }
  50. /**
  51. * @param string $product_id
  52. * @param string $order_id
  53. * @param string $trade_no
  54. * @param float $amount
  55. * @param string $payway
  56. *
  57. * @return bool
  58. */
  59. public function payNotify($product_id, $order_id, $trade_no, $amount, $payway) {
  60. switch ($product_id) {
  61. case WalletConst::WALLET_PRODUCT_MEM_PTB:
  62. return $this->payNotifyPtbCharge($order_id, $trade_no, $amount, $payway);
  63. case WalletConst::WALLET_PRODUCT_AGENT_PTB:
  64. return $this->payNotifyAgentPtbCharge($order_id, $trade_no, $amount, $payway);
  65. case WalletConst::WALLET_PRODUCT_MEM_GM:
  66. return $this->payNotifyGmCharge($order_id, $trade_no, $amount, $payway);
  67. case WalletConst::WALLET_PRODUCT_ACCOUNT:
  68. return $this->payNotifyByAccount($order_id, $trade_no, $amount, $payway);
  69. default:
  70. return false;
  71. }
  72. }
  73. /**
  74. * @param string $order_id
  75. * @param string $trade_no
  76. * @param float $amount
  77. * @param string $payway
  78. *
  79. * @return bool
  80. */
  81. public function payNotifyPtbCharge($order_id, $trade_no, $amount, $payway) {
  82. $_notify_data['order_id'] = $order_id;
  83. $_notify_data['trade_no'] = $trade_no;
  84. $_notify_data['amount'] = $amount;
  85. $_notify_data['payway'] = $payway;
  86. if (empty($order_id)) {
  87. Log::write(
  88. "func=".__FUNCTION__."&class=".__CLASS__."&step1&code=".OrderStatus::ORDER_ID_EMPTY.'&notify_data.'
  89. .http_build_query($_notify_data),
  90. LOG::ERROR
  91. );
  92. return false;
  93. }
  94. if (empty($amount)) {
  95. Log::write(
  96. "func=".__FUNCTION__."&class=".__CLASS__."&step2&code=".OrderStatus::ORDER_AMOUNT_IS_ZERO
  97. .'&notify_data.'
  98. .http_build_query($_notify_data),
  99. LOG::ERROR
  100. );
  101. return false;
  102. }
  103. $_pc_model = new PtbChargeModel();
  104. $_order_data = $_pc_model->getDetail($order_id);
  105. if (false == $_order_data) {
  106. Log::write(
  107. "func=".__FUNCTION__."&class=".__CLASS__."&step3&code=".OrderStatus::ORDER_NOT_EXISTS.'&notify_data.'
  108. .http_build_query($_notify_data),
  109. LOG::ERROR
  110. );
  111. return false;
  112. }
  113. $_amount = number_format($amount, 2, '.', '');
  114. $_order_amount = number_format($_order_data['real_amount'], 2, '.', '');
  115. if (($_amount < $_order_amount)) {
  116. Log::write(
  117. "func=".__FUNCTION__."&class=".__CLASS__."&step4&code=".OrderStatus::NOTIFY_AMOUNT_ERROR.'&notify_data.'
  118. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  119. LOG::ERROR
  120. );
  121. return false;
  122. }
  123. if (OrderConst::PAY_STATUS_SUC == $_order_data['status']) {
  124. Log::write(
  125. "func=".__FUNCTION__."&class=".__CLASS__."&step5&code=".OrderStatus::ORDER_HAS_PAY.'&notify_data.'
  126. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  127. LOG::LOG
  128. );
  129. return true;
  130. }
  131. /* 更新订单支付状态 */
  132. $_order_data['status'] = OrderConst::PAY_STATUS_SUC;
  133. $_order_data['remark'] = $trade_no;
  134. $_rs = $_pc_model->updateOrder($_order_data, $_order_data['id']);
  135. if (false == $_rs) {
  136. return false;
  137. }
  138. $_wr_class = new WalletRequest();
  139. $_wr_class->setPtbChargeData($_order_data);
  140. $this->doAfterPtbCharge($_wr_class);
  141. return true;
  142. }
  143. /**
  144. * @param WalletRequest $wr
  145. */
  146. public function doAfterPtbCharge(WalletRequest $wr) {
  147. (new MemWallet())->updatePtb($wr->getMemId(), $wr->getPtbCnt(), SettleConst::SETTLE_WALLET_ADD);
  148. // TODO: wuyonghong 2018/5/16 平台币充值成功后需要做的操作
  149. (new Income())->incomeFromPmc($wr->getOrderId());
  150. }
  151. public function payNotifyAgentPtbCharge($order_id, $trade_no, $amount, $payway) {
  152. $_notify_data['order_id'] = $order_id;
  153. $_notify_data['trade_no'] = $trade_no;
  154. $_notify_data['amount'] = $amount;
  155. $_notify_data['payway'] = $payway;
  156. if (empty($order_id)) {
  157. Log::write(
  158. "func=".__FUNCTION__."&class=".__CLASS__."&step1&code=".OrderStatus::ORDER_ID_EMPTY.'&notify_data.'
  159. .http_build_query($_notify_data),
  160. LOG::ERROR
  161. );
  162. return false;
  163. }
  164. if (empty($amount)) {
  165. Log::write(
  166. "func=".__FUNCTION__."&class=".__CLASS__."&step2&code=".OrderStatus::ORDER_AMOUNT_IS_ZERO
  167. .'&notify_data.'
  168. .http_build_query($_notify_data),
  169. LOG::ERROR
  170. );
  171. return false;
  172. }
  173. $_pc_model = new PtbAgentChargeModel();
  174. $_order_data = $_pc_model->getDetail($order_id);
  175. if (false == $_order_data) {
  176. Log::write(
  177. "func=".__FUNCTION__."&class=".__CLASS__."&step3&code=".OrderStatus::ORDER_NOT_EXISTS.'&notify_data.'
  178. .http_build_query($_notify_data),
  179. LOG::ERROR
  180. );
  181. return false;
  182. }
  183. $_amount = number_format($amount, 2, '.', '');
  184. $_order_amount = number_format($_order_data['real_amount'], 2, '.', '');
  185. if (($_amount < $_order_amount)) {
  186. Log::write(
  187. "func=".__FUNCTION__."&class=".__CLASS__."&step4&code=".OrderStatus::NOTIFY_AMOUNT_ERROR.'&notify_data.'
  188. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  189. LOG::ERROR
  190. );
  191. return false;
  192. }
  193. if (OrderConst::PAY_STATUS_SUC == $_order_data['status']) {
  194. Log::write(
  195. "func=".__FUNCTION__."&class=".__CLASS__."&step5&code=".OrderStatus::ORDER_HAS_PAY.'&notify_data.'
  196. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  197. LOG::LOG
  198. );
  199. return false;
  200. }
  201. /* 更新订单支付状态 */
  202. $_order_data['status'] = OrderConst::PAY_STATUS_SUC;
  203. $_order_data['remark'] = $trade_no;
  204. $_rs = $_pc_model->updateOrder($_order_data, $_order_data['id']);
  205. if (false == $_rs) {
  206. return false;
  207. }
  208. $_wr_class = new WalletRequest();
  209. $_wr_class->setPtbAgentChargeData($_order_data);
  210. $this->doAfterAgentPtbCharge($_wr_class);
  211. return true;
  212. }
  213. /**
  214. * @param WalletRequest $wr
  215. */
  216. public function doAfterAgentPtbCharge(WalletRequest $wr) {
  217. (new AgentWallet())->updatePtb($wr->getAgentId(), $wr->getPtbCnt(), SettleConst::SETTLE_WALLET_ADD);
  218. /* 计算收益 */
  219. (new Income())->incomeFromPac($wr->getOrderId());
  220. }
  221. /**
  222. * @param string $order_id
  223. * @param string $trade_no
  224. * @param float $amount
  225. * @param string $payway
  226. *
  227. * @return bool
  228. */
  229. public function payNotifyGmCharge($order_id, $trade_no, $amount, $payway) {
  230. $_notify_data['order_id'] = $order_id;
  231. $_notify_data['trade_no'] = $trade_no;
  232. $_notify_data['amount'] = $amount;
  233. $_notify_data['payway'] = $payway;
  234. if (empty($order_id)) {
  235. Log::write(
  236. "func=".__FUNCTION__."&class=".__CLASS__."&step1&code=".OrderStatus::ORDER_ID_EMPTY.'&notify_data.'
  237. .http_build_query($_notify_data),
  238. LOG::ERROR
  239. );
  240. return false;
  241. }
  242. if (empty($amount)) {
  243. Log::write(
  244. "func=".__FUNCTION__."&class=".__CLASS__."&step2&code=".OrderStatus::ORDER_AMOUNT_IS_ZERO
  245. .'&notify_data.'
  246. .http_build_query($_notify_data),
  247. LOG::ERROR
  248. );
  249. return false;
  250. }
  251. $_pc_model = new GmChargeModel();
  252. $_order_data = $_pc_model->getDetail($order_id);
  253. if (false == $_order_data) {
  254. Log::write(
  255. "func=".__FUNCTION__."&class=".__CLASS__."&step3&code=".OrderStatus::ORDER_NOT_EXISTS.'&notify_data.'
  256. .http_build_query($_notify_data),
  257. LOG::ERROR
  258. );
  259. return false;
  260. }
  261. $_amount = number_format($amount, 2, '.', '');
  262. $_order_amount = number_format($_order_data['real_amount'], 2, '.', '');
  263. if (($_amount < $_order_amount)) {
  264. Log::write(
  265. "func=".__FUNCTION__."&class=".__CLASS__."&step4&code=".OrderStatus::NOTIFY_AMOUNT_ERROR.'&notify_data.'
  266. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  267. LOG::ERROR
  268. );
  269. return false;
  270. }
  271. if (OrderConst::PAY_STATUS_SUC == $_order_data['status']) {
  272. Log::write(
  273. "func=".__FUNCTION__."&class=".__CLASS__."&step5&code=".OrderStatus::ORDER_HAS_PAY.'&notify_data.'
  274. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  275. LOG::LOG
  276. );
  277. return true;
  278. }
  279. /* 更新订单支付状态 */
  280. $_order_data['status'] = OrderConst::PAY_STATUS_SUC;
  281. $_order_data['remark'] = $trade_no;
  282. $_rs = $_pc_model->updateOrder($_order_data, $_order_data['id']);
  283. if (false == $_rs) {
  284. return false;
  285. }
  286. $_wr_class = new WalletRequest();
  287. $_wr_class->setGmChargeData($_order_data);
  288. $this->doAfterGmCharge($_wr_class);
  289. return true;
  290. }
  291. /**
  292. * @param WalletRequest $wr
  293. */
  294. public function doAfterGmCharge(WalletRequest $wr) {
  295. //修改玩家游戏币余额
  296. (new MemWallet())->updateGm($wr->getMemId(), $wr->getAppId(), $wr->getGmCnt(), SettleConst::SETTLE_WALLET_ADD);
  297. }
  298. /**
  299. * 玩家购买小号
  300. *
  301. * @param $order_id
  302. * @param $trade_no
  303. * @param $amount
  304. * @param $payway
  305. *
  306. * @return bool
  307. */
  308. private function payNotifyByAccount($order_id, $trade_no, $amount, $payway) {
  309. return (new AccountOrder())->payNotifyByAccount($order_id, $trade_no, $amount, $payway);
  310. }
  311. }