Weixin.php 11 KB

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