OrderOut.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * OrderOut.php UTF-8
  4. *
  5. *
  6. * @date : 2018/8/10 17:27
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\controller\order;
  13. use huo\controller\common\Base;
  14. use huo\controller\game\GamePaySwitchCache;
  15. use huo\controller\pay\SdkOrderCache;
  16. use huo\controller\queue\OrderQrQueue;
  17. use huolib\constant\CacheConst;
  18. use huolib\constant\CommonConst;
  19. use huolib\constant\MpConfConst;
  20. use huolib\status\OrderStatus;
  21. use huomp\controller\game\GameMini;
  22. use huomp\controller\share\QrCodeOut;
  23. use huoMpQr\QrCode;
  24. use think\Cache;
  25. use think\Log;
  26. class OrderOut extends Base {
  27. /**
  28. * 预下单米大师支付数据
  29. *
  30. * @param string $order_id 订单ID
  31. * @param bool $is_sand 是否沙盒
  32. *
  33. * @return array
  34. */
  35. public function getMpayData($order_id, $is_sand = false) {
  36. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
  37. if (empty($_order_data)) {
  38. $_code = OrderStatus::ORDER_ID_ERROR;
  39. return $this->huoError($_code, OrderStatus::getMsg($_code));
  40. }
  41. $_env = MpConfConst::MP_MPPAY_NORMAL; //默认是正式环境
  42. if (true == $is_sand) {
  43. $_env = MpConfConst::MP_MPPAY_TEST; //沙箱走测试环境
  44. }
  45. $_mpay_conf = (new GameMini())->getMpayConf($_order_data['app_id'], $is_sand);
  46. $_rdata['mode'] = 'game';
  47. $_rdata['env'] = $_env;
  48. $_rdata['offer_id'] = $_mpay_conf['offer_id'];
  49. $_rdata['currency'] = 'CNY';
  50. $_rdata['platform'] = 'android';
  51. $_rdata['buy_quantity'] = $_order_data['real_amount'] * $_mpay_conf['mini_rate'];
  52. $_rdata['zone_id'] = '1';
  53. $_code = OrderStatus::NO_ERROR;
  54. $_order_data['product_cnt'] = $_rdata['buy_quantity'];
  55. $_rs = SdkOrderCache::ins()->updateOrder($order_id, $_order_data);
  56. if (false == $_rs) {
  57. $_code = OrderStatus::UNKNOWN_ERROR;
  58. }
  59. return $this->huoSuccess($_code, OrderStatus::getMsg($_code), $_rdata);
  60. }
  61. /**
  62. * 获取切换配置
  63. *
  64. * @param int $app_id 游戏ID
  65. *
  66. * @return bool|array
  67. */
  68. public function getSwitchAppPath($app_id) {
  69. $_switch_data = GamePaySwitchCache::ins()->getInfoByAppId($app_id);
  70. if (empty($_switch_data)) {
  71. // Log::write(
  72. // 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&app_id='.$app_id
  73. // .'&error=switch_data is null', Log::ERROR
  74. // );
  75. return false;
  76. }
  77. $_rdata['path'] = $_switch_data['pages'];
  78. $_rdata['switch_app_id'] = $_switch_data['switch_app_id'];
  79. return $_rdata;
  80. }
  81. /**
  82. * 获取玩家游戏支付二维码
  83. *
  84. * @param $mem_id
  85. * @param $app_id
  86. *
  87. * @return bool
  88. */
  89. public function genMemAppQrCodeQueue($mem_id, $app_id) {
  90. $_switch_data = $this->getSwitchAppPath($app_id);
  91. if (empty($_switch_data)) {
  92. return false;
  93. }
  94. $_path = $_switch_data['path'];
  95. $_switch_app_id = $_switch_data['switch_app_id'];
  96. $_key = CacheConst::CACHE_MEM_APP_ORDER_PREFIX.md5(json_encode(array($mem_id, $app_id, $_switch_app_id)));
  97. $_image = Cache::get($_key);
  98. if (!empty($_image)) {
  99. /* 入队列 */
  100. return true;
  101. }
  102. $_order_id = $mem_id.'&'.$app_id;
  103. $_rdata = (new QrCodeOut())->getOrderQrCode(
  104. $_order_id, $_switch_app_id, $_path
  105. );
  106. $_image = !empty($_rdata['data']['image']) ? $_rdata['data']['image'] : '';
  107. if (empty($_image)) {
  108. Log::write(
  109. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&mem_id='.$mem_id.'&app_id='.$app_id
  110. .'&rdata='.json_encode($_rdata).'获取二维码错误', Log::ERROR
  111. );
  112. return false;
  113. }
  114. Cache::set($_key, $_image, CommonConst::CONST_MONTH_SECONDS);
  115. return true;
  116. }
  117. /**
  118. * 获取订单支付二维码
  119. *
  120. * @param string $order_id 订单ID
  121. *
  122. * @return bool
  123. */
  124. public function genOrderQrQueue($order_id) {
  125. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
  126. if (empty($_order_data)) {
  127. return false;
  128. }
  129. $_app_id = $_order_data['app_id'];
  130. $_switch_data = $this->getSwitchAppPath($_app_id);
  131. if (empty($_switch_data)) {
  132. return false;
  133. }
  134. $_path = $_switch_data['path'];
  135. $_switch_app_id = $_switch_data['switch_app_id'];
  136. $_key = CacheConst::CACHE_MEM_APP_ORDER_PREFIX.$order_id;
  137. $_image = Cache::get($_key);
  138. if (!empty($_image)) {
  139. return true;
  140. }
  141. $_rdata = (new QrCodeOut())->getOrderQrCode(
  142. $order_id, $_switch_app_id, $_path
  143. );
  144. $_image = !empty($_rdata['data']['image']) ? $_rdata['data']['image'] : '';
  145. if (empty($_image)) {
  146. Log::write(
  147. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&order_id='.$order_id
  148. .'&rdata='.json_encode($_rdata).'获取二维码错误', Log::ERROR
  149. );
  150. return false;
  151. }
  152. Cache::set($_key, $_image, CommonConst::CONST_DAY_SECONDS);
  153. return true;
  154. }
  155. /**
  156. * 获取玩家游戏支付二维码
  157. *
  158. * @param int $mem_id 玩家ID
  159. * @param int $app_id 游戏ID
  160. * @param bool $must_get
  161. *
  162. * @return bool
  163. */
  164. public function getMemAppQrCode($mem_id, $app_id, $must_get = false) {
  165. $_switch_data = $this->getSwitchAppPath($app_id);
  166. if (empty($_switch_data)) {
  167. return false;
  168. }
  169. $_switch_app_id = $_switch_data['switch_app_id'];
  170. $_key = CacheConst::CACHE_MEM_APP_ORDER_PREFIX.md5(json_encode(array($mem_id, $app_id, $_switch_app_id)));
  171. $_image = Cache::get($_key);
  172. $_param['mem_id'] = $mem_id;
  173. $_param['app_id'] = $app_id;
  174. if (empty($_image) && false == $must_get) {
  175. /* 入队列 */
  176. (new OrderQrQueue($_param))->pushQueue();
  177. return '';
  178. } elseif (empty($_image) && true == $must_get) {
  179. $_rs = $this->genMemAppQrCodeQueue($mem_id, $app_id);
  180. if (false == $_rs) {
  181. Log::write(
  182. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&mem_id='.$mem_id.'&app_id='.$app_id
  183. .'获取支付二维码错误', Log::ERROR
  184. );
  185. }
  186. $_image = Cache::get($_key);
  187. }
  188. return cmf_get_image_url($_image);
  189. }
  190. }