Weixin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. }
  50. }
  51. /**
  52. * @param Request $request
  53. */
  54. public function result(Request $request)
  55. {
  56. $state = trim($request->get('state'));
  57. $code = trim($request->get('code'));
  58. if (empty($state)) {
  59. exit('error');
  60. }
  61. $weixinA = new weixina;
  62. $data = $weixinA->oauth_reuslt($code);
  63. if (!empty($data['access_token'])) {
  64. $userInfo = $weixinA->userinfo($data['access_token']);
  65. $userInfo['access_token'] = $data['access_token'];
  66. $userInfo['expires_in'] = $data['expires_in'];
  67. $userInfo['time'] = time();
  68. cookie("weix_userinfo", serialize($userInfo));
  69. $url = setParam(cookie('w_url'), ['data' => json_encode($userInfo, \JSON_UNESCAPED_UNICODE)]);
  70. redirect($url)->send();
  71. } else {
  72. exit('微信授权登录失败,关闭页面重新,重新扫描!');
  73. }
  74. }
  75. public function pay(Request $request)
  76. {
  77. [$orderId, $from] = UtilService::getMore([
  78. ['order_id', '', 'empty', '参数错误'],
  79. ['from', '', 'empty', '参数错误'],
  80. ], $request, true);
  81. $order = Order::where('order_id', $orderId)->find();
  82. if (empty($order)) {
  83. return app('json')->fail('找不到订单信息');
  84. }
  85. //订单已付款
  86. if (!empty($order['is_pay'])) {
  87. return app('json')->fail('订单已经支付成功');
  88. }
  89. $config = [
  90. // 必要配置
  91. 'app_id' => 'wxa9130b2090dfb130',
  92. 'mch_id' => '1582297191',
  93. 'key' => 'fgwhdf323gewdsd83ewewhd56wrr333g', // API 密钥
  94. 'notify_url' => '',
  95. ];
  96. $app = Factory::payment($config);
  97. if ($from == 'weixin') {
  98. $result = $app->order->unify([
  99. 'body' => '支付订单',
  100. 'out_trade_no' => $order['order_id'],
  101. 'total_fee' => $order['all_price'] * 100,
  102. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  103. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  104. 'openid' => 'olkUP6fDbDSQKjidMQyeQ_0TO3XE',
  105. ]);
  106. $jssdk = $app->jssdk;
  107. $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
  108. $json['result'] = $jsConfig;
  109. $json['type'] = 'WECHAT_PAY';
  110. } else {
  111. $result = $app->order->unify([
  112. 'body' => '支付订单',
  113. 'out_trade_no' => $order['order_id'],
  114. 'total_fee' => $order['all_price'] * 100,
  115. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  116. 'trade_type' => 'MWEB' // 请对应换成你的支付方式对应的值类型
  117. ]);
  118. $jssdk = $app->jssdk;
  119. $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
  120. $json['result'] = $jsConfig;
  121. $json['type'] = 'WECHAT_H5_PAY';
  122. }
  123. return app('json')->success($json);
  124. }
  125. public function recharge(Request $request)
  126. {
  127. [$money, $from, $code] = UtilService::getMore([
  128. ['money', '', 'empty', '请选择充值金额'],
  129. ['from', '', 'empty', '参数错误'],
  130. ['code', '']
  131. ], $request, true);
  132. try {
  133. Recharge::beginTrans();
  134. $recharge = new Recharge();
  135. $d = [];
  136. $d['order_id'] = 'RE' . time() . sprintf('%04d', rand(0, 1000)) . $request->user['uid'];
  137. $d['v'] = $money;
  138. $d['status'] = '0';
  139. $d['time'] = time();
  140. $d['sassid'] = $request->site['sassid'];
  141. $d['uid'] = $request->user['uid'];
  142. $recharge->insert($d);
  143. //生成支付凭证
  144. (new PayTrade)->mkTrade([
  145. 'out_trade_no' => $d['order_id'],
  146. 'uid' => $request->user['uid'],
  147. 'tag' => 'wxpay',
  148. 'type' => 'recharge',
  149. 'money' => $money,
  150. 'content' => '会员充值' . $d['v'] . '元',
  151. 'sassid' => $request->site['sassid'],
  152. ]);
  153. Recharge::commitTrans();
  154. $config = [
  155. // 必要配置
  156. 'app_id' => 'wxa9130b2090dfb130',
  157. 'mch_id' => '1582297191',
  158. 'key' => 'fgwhdf323gewdsd83ewewhd56wrr333g', // API 密钥
  159. 'notify_url' => '',
  160. ];
  161. $app = Factory::payment($config);
  162. if ($from == 'weixin') {
  163. if ($code) {
  164. $weixinA = new weixina;
  165. $token = $weixinA->oauth_reuslt($code);
  166. $openid = $token['openid'];
  167. (new Member)->where('uid', $request->user['uid'])->save(['openid' => $openid]);
  168. } else {
  169. $openid = $request->user['openid'];
  170. }
  171. $result = $app->order->unify([
  172. 'body' => '充值余额',
  173. 'out_trade_no' => $d['order_id'],
  174. 'total_fee' => $money * 100,
  175. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  176. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  177. 'openid' => $openid,
  178. ]);
  179. $jssdk = $app->jssdk;
  180. $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
  181. $json['result'] = $jsConfig;
  182. $json['type'] = 'WECHAT_PAY';
  183. } else {
  184. $result = $app->order->unify([
  185. 'body' => '充值余额',
  186. 'out_trade_no' => $d['order_id'],
  187. 'total_fee' => $money * 100,
  188. 'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  189. 'trade_type' => 'MWEB' // 请对应换成你的支付方式对应的值类型
  190. ]);
  191. $json['result'] = $result;
  192. $json['type'] = 'WECHAT_H5_PAY';
  193. }
  194. return app('json')->success($json);
  195. } catch (DbException $db) {
  196. Recharge::rollbackTrans();
  197. return app('json')->fail("充值失败,请联系客服人员");
  198. }
  199. }
  200. /**
  201. * @throws \Exception
  202. */
  203. public function notify()
  204. {
  205. // 获取微信回调的数据
  206. $notifiedData = file_get_contents('php://input');
  207. //XML格式转换
  208. $xmlObj = simplexml_load_string($notifiedData, 'SimpleXMLElement', LIBXML_NOCDATA);
  209. $xmlObj = json_decode(json_encode($xmlObj), true);
  210. // 当支付通知返回支付成功时
  211. if ($xmlObj['return_code'] == "SUCCESS" && $xmlObj['result_code'] == "SUCCESS") {
  212. try {
  213. $data['appid'] = $xmlObj['appid'];
  214. $data['bank_type'] = $xmlObj['bank_type'];
  215. $data['cash_fee'] = $xmlObj['cash_fee'];
  216. $data['fee_type'] = $xmlObj['fee_type'];
  217. $data['is_subscribe'] = $xmlObj['is_subscribe'];
  218. $data['mch_id'] = $xmlObj['mch_id'];
  219. $data['nonce_str'] = $xmlObj['nonce_str'];
  220. $data['openid'] = $xmlObj['openid'];
  221. $data['out_trade_no'] = $xmlObj['out_trade_no'];
  222. $data['result_code'] = $xmlObj['result_code'];
  223. $data['return_code'] = $xmlObj['return_code'];
  224. $data['sign'] = $xmlObj['sign'];
  225. $data['time_end'] = $xmlObj['time_end'];
  226. $data['total_fee'] = $xmlObj['total_fee'];
  227. $data['trade_type'] = $xmlObj['trade_type'];
  228. $data['transaction_id'] = $xmlObj['transaction_id'];
  229. $res = Db::name('wx_notify')->insertGetId($data);
  230. if ($res) {
  231. $payTrade = new PayTrade();
  232. $data2 = $payTrade->where('out_trade_no', $xmlObj['out_trade_no'])->find();
  233. if (empty($data2)) {
  234. echo 'SUCCESS';
  235. exit;
  236. }
  237. if ($data2['status'] == 1) {
  238. echo 'SUCCESS';
  239. exit;
  240. }
  241. if ($data2['type'] == 'recharge') {
  242. $recharge = new Recharge();
  243. $recharge->rechargeSuccess($xmlObj['out_trade_no']);
  244. }
  245. $payTrade->where('id', $data2['id'])->save(['status' => 1, 'pay_time' => time()]);
  246. echo 'SUCCESS';
  247. exit;
  248. }
  249. } catch (Exception $e) {
  250. @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);
  251. } catch (DbException $e) {
  252. @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);
  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. }
  256. }
  257. }
  258. }