Weixin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\BaseController;
  4. use app\model\api\Member;
  5. use app\model\api\Order;
  6. use app\model\api\PayTrade;
  7. use app\model\api\Recharge;
  8. use app\Request;
  9. use EasyWeChat\Factory;
  10. use library\lib\weixina;
  11. use library\services\UtilService;
  12. use think\db\exception\DbException;
  13. use think\db\exception\PDOException;
  14. use think\Exception;
  15. use think\facade\Db;
  16. class Weixin extends BaseController
  17. {
  18. public function test()
  19. {
  20. $user1 = Db::name("member")->where('i_uid',1655)
  21. ->where('regtime', 'between', [1614528000, 1617206400])
  22. ->where('last_con_time','<>',0)
  23. ->column('uid');
  24. $user2 = Db::name("member")->where('i_uid',1655)
  25. ->where('regtime', 'between', [1617206400, 1619798400])
  26. ->where('last_con_time','<>',0)
  27. ->column('uid');
  28. $user = array_merge($user1,$user2);
  29. $order = Db::name("order_info")->alias('s')->join('order o',"o.id = s.o_id")->where('s.uid', 'in', $user)
  30. ->where('o.pay_time', 'between', [1617206400, 1619798400])->count();
  31. dump($order);
  32. }
  33. public function getInfo(Request $request)
  34. {
  35. $code = trim($request->get('code'));
  36. $weixinA = new weixina;
  37. $token = $weixinA->oauth_reuslt($code);
  38. if (!empty($token['access_token'])) {
  39. $userInfo = $weixinA->userinfo($token['access_token']);
  40. $data['openid'] = $userInfo['openid'];
  41. $data['nickname'] = $userInfo['nickname'];
  42. $data['sex'] = $userInfo['sex'];
  43. $data['language'] = $userInfo['language'];
  44. $data['city'] = $userInfo['city'];
  45. $data['province'] = $userInfo['province'];
  46. $data['country'] = $userInfo['country'];
  47. $data['avatar'] = $userInfo['headimgurl'];
  48. (new Member)->where('uid',$request->user['uid'])->save($data);
  49. return app('json')->success([
  50. 'nickname' => $data['nickname'],
  51. 'avatar' => $data['avatar']
  52. ]);
  53. }
  54. }
  55. /**
  56. * @param Request $request
  57. */
  58. public function result(Request $request)
  59. {
  60. $state = trim($request->get('state'));
  61. $code = trim($request->get('code'));
  62. if (empty($state)) {
  63. exit('error');
  64. }
  65. $weixinA = new weixina;
  66. $data = $weixinA->oauth_reuslt($code);
  67. if (!empty($data['access_token'])) {
  68. $userInfo = $weixinA->userinfo($data['access_token']);
  69. $userInfo['access_token'] = $data['access_token'];
  70. $userInfo['expires_in'] = $data['expires_in'];
  71. $userInfo['time'] = time();
  72. cookie("weix_userinfo", serialize($userInfo));
  73. $url = setParam(cookie('w_url'), ['data' => json_encode($userInfo, \JSON_UNESCAPED_UNICODE)]);
  74. redirect($url)->send();
  75. } else {
  76. exit('微信授权登录失败,关闭页面重新,重新扫描!');
  77. }
  78. }
  79. public function pay(Request $request)
  80. {
  81. [$orderId, $from] = UtilService::getMore([
  82. ['order_id', '', 'empty', '参数错误'],
  83. ['from', '', 'empty', '参数错误'],
  84. ], $request, true);
  85. $order = Order::where('order_id', $orderId)->find();
  86. if (empty($order)) {
  87. return app('json')->fail('找不到订单信息');
  88. }
  89. //订单已付款
  90. if (!empty($order['is_pay'])) {
  91. return app('json')->fail('订单已经支付成功');
  92. }
  93. $config = [
  94. // 必要配置
  95. 'app_id' => 'wxa9130b2090dfb130',
  96. 'mch_id' => '1582297191',
  97. 'key' => 'fgwhdf323gewdsd83ewewhd56wrr333g', // API 密钥
  98. 'notify_url' => '',
  99. ];
  100. $app = Factory::payment($config);
  101. if ($from == 'weixin') {
  102. $result = $app->order->unify([
  103. 'body' => '支付订单',
  104. 'out_trade_no' => $order['order_id'],
  105. 'total_fee' => $order['all_price'] * 100,
  106. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  107. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  108. 'openid' => 'olkUP6fDbDSQKjidMQyeQ_0TO3XE',
  109. ]);
  110. $jssdk = $app->jssdk;
  111. $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
  112. $json['result'] = $jsConfig;
  113. $json['type'] = 'WECHAT_PAY';
  114. } else {
  115. $result = $app->order->unify([
  116. 'body' => '支付订单',
  117. 'out_trade_no' => $order['order_id'],
  118. 'total_fee' => $order['all_price'] * 100,
  119. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  120. 'trade_type' => 'MWEB' // 请对应换成你的支付方式对应的值类型
  121. ]);
  122. $jssdk = $app->jssdk;
  123. $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
  124. $json['result'] = $jsConfig;
  125. $json['type'] = 'WECHAT_H5_PAY';
  126. }
  127. return app('json')->success($json);
  128. }
  129. public function recharge(Request $request)
  130. {
  131. [$money, $from, $code] = UtilService::getMore([
  132. ['money', '', 'empty', '请选择充值金额'],
  133. ['from', '', 'empty', '参数错误'],
  134. ['code', '']
  135. ], $request, true);
  136. try {
  137. Recharge::beginTrans();
  138. $recharge = new Recharge();
  139. $d = [];
  140. $d['order_id'] = 'RE' . time() . sprintf('%04d', rand(0, 1000)) . $request->user['uid'];
  141. $d['v'] = $money;
  142. $d['status'] = '0';
  143. $d['time'] = time();
  144. $d['sassid'] = $request->site['sassid'];
  145. $d['uid'] = $request->user['uid'];
  146. $recharge->insert($d);
  147. //生成支付凭证
  148. (new PayTrade)->mkTrade([
  149. 'out_trade_no' => $d['order_id'],
  150. 'uid' => $request->user['uid'],
  151. 'tag' => 'wxpay',
  152. 'type' => 'recharge',
  153. 'money' => $money,
  154. 'content' => '会员充值' . $d['v'] . '元',
  155. 'sassid' => $request->site['sassid'],
  156. ]);
  157. Recharge::commitTrans();
  158. $config = [
  159. // 必要配置
  160. 'app_id' => 'wxa9130b2090dfb130',
  161. 'mch_id' => '1582297191',
  162. 'key' => 'fgwhdf323gewdsd83ewewhd56wrr333g', // API 密钥
  163. 'notify_url' => '',
  164. ];
  165. $app = Factory::payment($config);
  166. if ($from == 'weixin') {
  167. if ($code) {
  168. $weixinA = new weixina;
  169. $token = $weixinA->oauth_reuslt($code);
  170. $openid = $token['openid'];
  171. (new Member)->where('uid', $request->user['uid'])->save(['openid' => $openid]);
  172. } else {
  173. $openid = $request->user['openid'];
  174. }
  175. $result = $app->order->unify([
  176. 'body' => '充值余额',
  177. 'out_trade_no' => $d['order_id'],
  178. 'total_fee' => $money * 100,
  179. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  180. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  181. 'openid' => $openid,
  182. ]);
  183. $jssdk = $app->jssdk;
  184. $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
  185. $json['result'] = $jsConfig;
  186. $json['type'] = 'WECHAT_PAY';
  187. } else {
  188. $result = $app->order->unify([
  189. 'body' => '充值余额',
  190. 'out_trade_no' => $d['order_id'],
  191. 'total_fee' => $money * 100,
  192. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  193. 'trade_type' => 'MWEB' // 请对应换成你的支付方式对应的值类型
  194. ]);
  195. $json['result'] = $result;
  196. $json['type'] = 'WECHAT_H5_PAY';
  197. }
  198. return app('json')->success($json);
  199. } catch (DbException $db) {
  200. Recharge::rollbackTrans();
  201. return app('json')->fail("充值失败,请联系客服人员");
  202. }
  203. }
  204. /**
  205. * @throws \Exception
  206. */
  207. public function notify()
  208. {
  209. // 获取微信回调的数据
  210. $notifiedData = file_get_contents('php://input');
  211. //XML格式转换
  212. $xmlObj = simplexml_load_string($notifiedData, 'SimpleXMLElement', LIBXML_NOCDATA);
  213. $xmlObj = json_decode(json_encode($xmlObj), true);
  214. // 当支付通知返回支付成功时
  215. if ($xmlObj['return_code'] == "SUCCESS" && $xmlObj['result_code'] == "SUCCESS") {
  216. try {
  217. $data['appid'] = $xmlObj['appid'];
  218. $data['bank_type'] = $xmlObj['bank_type'];
  219. $data['cash_fee'] = $xmlObj['cash_fee'];
  220. $data['fee_type'] = $xmlObj['fee_type'];
  221. $data['is_subscribe'] = $xmlObj['is_subscribe'];
  222. $data['mch_id'] = $xmlObj['mch_id'];
  223. $data['nonce_str'] = $xmlObj['nonce_str'];
  224. $data['openid'] = $xmlObj['openid'];
  225. $data['out_trade_no'] = $xmlObj['out_trade_no'];
  226. $data['result_code'] = $xmlObj['result_code'];
  227. $data['return_code'] = $xmlObj['return_code'];
  228. $data['sign'] = $xmlObj['sign'];
  229. $data['time_end'] = $xmlObj['time_end'];
  230. $data['total_fee'] = $xmlObj['total_fee'];
  231. $data['trade_type'] = $xmlObj['trade_type'];
  232. $data['transaction_id'] = $xmlObj['transaction_id'];
  233. $res = Db::name('wx_notify')->insertGetId($data);
  234. if ($res) {
  235. $payTrade = new PayTrade();
  236. $data2 = $payTrade->where('out_trade_no', $xmlObj['out_trade_no'])->find();
  237. if (empty($data2)) {
  238. echo 'SUCCESS';
  239. exit;
  240. }
  241. if ($data2['status'] == 1) {
  242. echo 'SUCCESS';
  243. exit;
  244. }
  245. if ($data2['type'] == 'recharge') {
  246. $recharge = new Recharge();
  247. $recharge->rechargeSuccess($xmlObj['out_trade_no']);
  248. }
  249. $payTrade->where('id', $data2['id'])->save(['status' => 1, 'pay_time' => time()]);
  250. echo 'SUCCESS';
  251. exit;
  252. }
  253. } catch (Exception $e) {
  254. @file_put_contents('error.txt', '[' . date('Y-m-d') . ']Exception:' . json_encode(['Msg' => $e->getMessage(), 'File' => $e->getFile(), 'Line' => $e->getLine(), 'Trance' => $e->getTrace()]) . PHP_EOL, FILE_APPEND);
  255. } catch (DbException $e) {
  256. @file_put_contents('error.txt', '[' . date('Y-m-d') . ']DbException:' . json_encode(['Msg' => $e->getMessage(), 'File' => $e->getFile(), 'Line' => $e->getLine(), 'Trance' => $e->getTrace()]) . PHP_EOL, FILE_APPEND);
  257. } catch (\Exception $e) {
  258. @file_put_contents('error.txt', '[' . date('Y-m-d') . ']\Exception:' . json_encode(['Msg' => $e->getMessage(), 'File' => $e->getFile(), 'Line' => $e->getLine(), 'Trance' => $e->getTrace()]) . PHP_EOL, FILE_APPEND);
  259. }
  260. }
  261. }
  262. }