Weixin.php 11 KB

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