CoinAirwallex.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * 官网的公共控制器类
  4. *
  5. */
  6. namespace app\mobile\controller;
  7. use app\common\library\AirwallexPay;
  8. use app\common\logic\PayCallback;
  9. use app\common\model\GameServer;
  10. use app\common\model\MemberCoinPay;
  11. use app\common\model\MemberGameServer;
  12. use app\common\model\Members;
  13. use app\common\model\PayType;
  14. use app\common\model\Subaccount;
  15. use app\service\GamePayService;
  16. use app\service\MemberCoinService;
  17. use app\common\model\Game;
  18. use app\common\logic\Pay as PayLogic;
  19. use think\Cache;
  20. use think\Env;
  21. use think\Exception;
  22. class CoinAirwallex extends Mobile
  23. {
  24. //v1.game_pay_type
  25. /**
  26. * 获取支付方式
  27. */
  28. public function getPayType()
  29. {
  30. $denyPayType = [
  31. 'airwallexh5',
  32. ];
  33. //wx-wap
  34. //xzzfbzf 新支付宝
  35. //old-zfb-wap 旧支付宝
  36. $payTypeModel = new PayType();
  37. $arrPayType = $payTypeModel->field('paytype,payname')
  38. ->where(['paytype' => ['in', $denyPayType]])
  39. ->select();
  40. $this->result($arrPayType, 1, '成功', 'json');
  41. }
  42. //v1.pay
  43. public function pay()
  44. {
  45. $data['type'] = input('type'); //类型 1充值平台币 2 充值游戏
  46. $data['paytype'] = input('paytype'); //支付类型
  47. $data['amount'] = input('amount'); //支付金额
  48. $data['accountType'] = input('accountType'); //账号类型1当前账号2其他账号
  49. $data['username'] = input('username');
  50. $data['gameid'] = input('gameid');
  51. $data['serverid'] = input('serverid');
  52. $data['roleid'] = input('roleid');
  53. $data['ip'] = request()->ip();
  54. $checkResult = $this->validate($data,
  55. [
  56. ['type', 'require|integer|in:1', '类型不能为空|必须为整型'],
  57. ['paytype', 'require', '支付类型不能为空'],
  58. ['amount', 'require|integer|egt:0', '支付金额不能为空|支付金额必须为整型|支付金额大于等于0的整型'],
  59. ['accountType', 'require|in:1,2', '账号类型不能为空|账号类型错误'],
  60. ['username', 'requireIf:accountType,2', '账号必填'],
  61. ['gameid', 'requireIf:type,2|integer', '游戏必填|游戏必须为整型'],
  62. ['serverid', 'requireIf:type,2', '区服必填'],
  63. ['roleid', 'requireIf:type,2', '角色必填'],
  64. ['coin_amt', 'float|egt:0', '金额必须为数字|支付金额大于等于0'],
  65. ]);
  66. if (true !== $checkResult) {
  67. $this->result('', 0, $checkResult, 'json');
  68. }
  69. if ($data['accountType'] == 2) {
  70. if (!$user = (new Members())->getUseridByName($data)) {
  71. $this->result([], 0, '账号不存在');
  72. }
  73. $data['member_id'] = $user['id'];
  74. } else {
  75. $data['member_id'] = session('front_userid');
  76. if(!session('front_userid')){
  77. $this->result([], 0, '未登录请先登录');
  78. }
  79. if (!$user = (new Members())->getUseridById($data)) {
  80. $this->result([], 0, '账号不存在');
  81. }
  82. }
  83. if(!in_array($data['paytype'],['airwallexh5'])){
  84. $this->jsonResult('', 0, '支付类型错误');
  85. }
  86. if (in_array($data['paytype'], ['coinpay','coin-old-zfb-wap','coin-kdh5']) && $data['accountType'] != 1) {
  87. $this->jsonResult('', 0, '支付方式错误');
  88. }
  89. $data['place_user_id'] = session('front_userid');
  90. if ($data['type'] == 1) {//充值平台币
  91. $result = (new MemberCoinService())->payCoin($data);
  92. } else {
  93. // $data['gameid'] = $this->input('gameid'); //游戏ID
  94. // $data['member_id'] = $this->input('userid'); //用户ID
  95. // $data['serverid'] = $this->input('serverid'); //服务器ID
  96. // $data['servername'] = $this->input('servername'); //区服名称
  97. // $data['amount'] = $this->input('amount'); //充值金额
  98. // $data['roleid'] = $this->input('roleid'); //角色ID
  99. $data['attach'] = makeOrderid('CP'); //上一级的扩展参数,多数是订单编号[麻花网络自己聚合自己时,会加个aoyou###前缀]
  100. $data['ptb_amt'] = 0; //平台币使用金额
  101. $data['imeil'] = 'pc'; //手机imeil码
  102. $data['productname'] = '游戏充值'; //消费的商品名称
  103. // $data['paytype'] = $this->input('paytype'); //支付类型
  104. // $data['rolename'] = $this->input('rolename', ''); //角色名
  105. // $data['rolelevel'] = $this->input('rolelevel', ''); //角色角色等级
  106. // $data['channel_id'] = $user['channel_id'];
  107. // $data['sub_username'] = $this->input('sub_username');
  108. if($data['accountType'] == 2 || $data['type'] == 1){
  109. $data['coin_amt'] = 0;
  110. }else{
  111. $data['coin_amt'] = input('coin_amt'); //平台币使用金额
  112. }
  113. $result = (new GamePayService())->gamePay($data,2);
  114. if ($result['code'] == 1) {
  115. $this->jsonResult($result['data'], 1, '订单生成成功', false);
  116. } else {
  117. $this->jsonResult('', 0, $result['msg']);
  118. }
  119. //全部平台币支付,并且支付成功时
  120. if ($result['code'] == 1 && (isset($result['data']['pay_status'])&& $result['data']['pay_status'] == 1)) {
  121. // 通知CP,支付成功
  122. (new PayCallback())->callBackToCp($result['data']['orderid']);
  123. (new PayLogic())->payWarning($data['member_id'], $data['channel_id'], $data['gameid'], $data['amount']);
  124. }
  125. }
  126. if ($result['code'] == 1) {
  127. $this->jsonResult($result['data'], 1, (isset($result['data']['pay_status'])&&$result['data']['pay_status'] == 1) ? '付款成功' : '订单生成成功', false);
  128. } else {
  129. $this->jsonResult('', 0, $result['msg']);
  130. }
  131. }
  132. /**
  133. * 获取可支付游戏
  134. */
  135. public function getGameList()
  136. {
  137. $this->result((new Game())->getGameListPay(), 1, '成功');
  138. }
  139. public function getServerList()
  140. {
  141. $data['gameid'] = input('gameid'); //游戏id
  142. $checkResult = $this->validate($data,
  143. [
  144. ['gameid', 'require|integer|gt:0', '游戏id不能为空|游戏id必须为整型|游戏id大于0的整型'],
  145. ]);
  146. if (true !== $checkResult) {
  147. $this->result('', 0, $checkResult, 'json');
  148. }
  149. $this->result((new GameServer())->getServerList($data), 1, '成功');
  150. }
  151. public function getRoleList()
  152. {
  153. $data['gameid'] = input('gameid'); //游戏id
  154. $data['serverid'] = input('serverid'); //游戏id
  155. $data['accountType'] = input('accountType'); //账号类型1当前账号2其他账号
  156. $data['username'] = input('username');
  157. $checkResult = $this->validate($data,
  158. [
  159. ['gameid', 'require|integer|gt:0', '游戏id不能为空|游戏id必须为整型|游戏id大于0的整型'],
  160. ['serverid', 'require|integer|gt:0', '区服id不能为空|区服id必须为整型区服id大于0的整型'],
  161. ['accountType', 'require|in:1,2', '账号类型不能为空|账号类型错误'],
  162. ['username', 'requireIf:accountType,2', '账号必填']
  163. ]);
  164. if (true !== $checkResult) {
  165. $this->result('', 0, $checkResult, 'json');
  166. }
  167. if ($data['accountType'] == 2) {
  168. if (!$user = (new Members())->getUseridByName($data)) {
  169. $this->result([], 0, '账号不存在');
  170. }
  171. $data['userid'] = $user['id'];
  172. } else {
  173. $data['userid'] = session('front_userid');
  174. }
  175. $this->result((new MemberGameServer())->getRoleList($data), 1, '成功');
  176. }
  177. public function verifyUserName()
  178. {
  179. $data['username'] = input('username'); //游戏id
  180. $checkResult = $this->validate($data,
  181. [
  182. ['username', 'require', '账号不能为空'],
  183. ]);
  184. if (true !== $checkResult) {
  185. $this->result('', 0, $checkResult, 'json');
  186. }
  187. $user = (new Members())->getUserName($data);
  188. if ($user) {
  189. $this->result($user, 1, '成功');
  190. } else {
  191. $this->result([], 0, '账号不存在');
  192. }
  193. }
  194. public function payHtml()
  195. {
  196. $data['data'] = input('data');
  197. $checkResult = $this->validate($data,
  198. [
  199. ['data', 'require', '参数不能为空'],
  200. ]);
  201. if (true !== $checkResult) {
  202. $this->result('', 0, $checkResult, 'json');
  203. }
  204. if (!$data = json_decode(opensslDecrypt($data['data']), true)) {
  205. $this->result('', 0, '订单不存在', 'json');
  206. }
  207. $this->assign('data', $data);
  208. return $this->fetch();
  209. }
  210. public function recharge()
  211. {
  212. $token = input('token');
  213. $token = str_ireplace(" ", "+", $token);
  214. if($token){
  215. //聚合渠道时,$sub_username对应的值是mg_username
  216. try {
  217. list($userid2, $username2, $gameid2,$sub_username,$token_random,$type) = explode('|', auth_code($token,"DECODE",Env::get('auth_key')));
  218. // var_dump($userid2, $username2, $gameid2,$sub_username,$token_random,$type);die();
  219. $redisTokenRandom = Cache::store('default')->get('token|'.$type.'|'.$userid2.'|'.$gameid2);
  220. $this->assign('username2', $username2);
  221. }catch (Exception $e){
  222. $username2 = '';
  223. }
  224. }else{
  225. $username2 = '';
  226. }
  227. $this->assign('username2',$username2 );
  228. return $this->fetch();
  229. }
  230. public function getCoin(){
  231. if (session('front_userid')){
  232. model('MemberCoinInfo')->releaseCoin(session('front_userid'));
  233. $userinfo = model('members')->field('id,amount')->where('id',session('front_userid'))->find();
  234. $this->jsonResult($userinfo, 1,'获取成功');
  235. }else{
  236. $this->jsonResult(['id'=>'','amount'=>'0'], 1,'获取成功');
  237. }
  238. }
  239. public function airwallex(){
  240. $pay = new AirwallexPay();
  241. $pay->getToken();
  242. // $money = input('money', 0, 'intval');
  243. $orderid = input('orderid', '');
  244. $goods = urldecode(input('goods', ''));
  245. $urltype = input('urltype',2);
  246. if($urltype == 2){
  247. $return_url = 'https://m.46yx.com/coin_airwallex/recharge.html';
  248. }elseif($urltype == 3){
  249. $return_url = "https://" . HTTP_HOST_URL . "/v1.pay_return/pay_success_airwallex.html";
  250. }
  251. $money = cache('memberCoin:real_amount:'.$orderid);
  252. if($money<0){
  253. echo 'money err'; exit();
  254. }
  255. if(!$orderid){
  256. echo 'orderid err'; exit();
  257. }
  258. $result = $pay->createOrder($money, $orderid,$return_url);
  259. $result['goods'] = $goods;
  260. $this->assign('result', $result);
  261. $this->assign('orderid', $orderid);
  262. return $this->fetch();
  263. }
  264. }