Wechat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\api\model\CoinRecord;
  5. use app\api\model\Delivery;
  6. use app\api\model\DeliveryTrade;
  7. use app\api\model\Order;
  8. use app\api\model\Prizerecord;
  9. use app\api\model\RechargeOrder;
  10. use app\common\controller\Frontend;
  11. use think\Db;
  12. use EasyWeChat\Factory;
  13. /**
  14. * 支付宝支付接口
  15. * Class Alipay
  16. * @package app\api\controller
  17. * @version 1.0
  18. * @author fuyelk <fuyelk@fuyelk.com>
  19. */
  20. class Wechat extends Frontend
  21. {
  22. protected $noNeedLogin = ['notifyx', 'boxpaysuccess', 'rechargesuccess'];
  23. protected $noNeedRight = ['*'];
  24. /**
  25. * 盲盒订单支付
  26. * @throws \Exception
  27. * @author fuyelk <fuyelk@fuyelk.com>
  28. */
  29. public function boxpay($orderid = '')
  30. {
  31. if (empty($orderid)) {
  32. $this->error('订单ID不能为空');
  33. }
  34. $order = Order::where('id', $orderid)->where('user_id', $this->auth->id)->where('status', 'unpay')->find();
  35. if (empty($order)) {
  36. $this->error('订单有误', '');
  37. }
  38. $params = [
  39. // 'amount' => $order->rmb_amount,
  40. 'amount' => $order->coin_price,
  41. 'orderid' => $order->out_trade_no,
  42. 'type' => "wxpay",
  43. 'title' => $order->box_name,
  44. 'notifyurl' => $this->request->domain() . '/api/alipay/notifyx/orderfrom/buybox',
  45. 'returnurl' => $this->request->domain() . '/api/alipay/boxpaysuccess',
  46. 'method' => "wap",
  47. ];
  48. $query = input('get.');
  49. if (isset($query['orderid'])) {
  50. unset($query['orderid']);
  51. }
  52. $this->auth->redis->set($order->out_trade_no, $query, 60 * 30); // 将参数存入缓存,30分钟
  53. //echo 222;
  54. echo \addons\eepay\Service::pay($params);
  55. // echo \addons\epay\library\Service::submitOrder($params);
  56. }
  57. /**
  58. * 充值订单支付
  59. * @throws \Exception
  60. * @author fuyelk <fuyelk@fuyelk.com>
  61. */
  62. public function rechargepay($orderid = '')
  63. {
  64. if (empty($orderid)) {
  65. $this->error('订单ID不能为空');
  66. }
  67. $order = RechargeOrder::where('id', $orderid)->where('user_id', $this->auth->id)->where('status', 'unpay')->find();
  68. if (empty($order)) {
  69. $this->error('订单有误', '');
  70. }
  71. $data = [
  72. 'amount' => $order['rmb_amount'],
  73. 'orderid' => $order['out_trade_no'],
  74. 'type' => '',
  75. 'title' => '',
  76. 'notifyurl' => $this->request->domain() . '/addons/epay/api/notifyx2',
  77. 'returnurl' => $this->request->domain() . '/addons/epay/api/returnx2222222',
  78. 'method' => 'miniapp',
  79. 'body' => '充值',
  80. 'openid' => \app\common\model\User::where('id', $order['user_id'])->value('openid'),
  81. ];
  82. $res = Service::submitOrder($data);
  83. $data = [
  84. 'msg' => '微信支付',
  85. 'jsConfig' => $res
  86. ];
  87. return json_encode($data);
  88. }
  89. /**
  90. * 发货订单支付
  91. * @throws \Exception
  92. * @author fuyelk <fuyelk@fuyelk.com>
  93. */
  94. public function deliverypay($orderid = '')
  95. {
  96. if (empty($orderid)) {
  97. $this->error('订单ID不能为空');
  98. }
  99. $order = DeliveryTrade::where('id', $orderid)->where('user_id', $this->auth->id)->where('status', 'unpay')->find();
  100. if (empty($order)) {
  101. $this->error('订单有误', '');
  102. }
  103. $params = [
  104. 'amount' => $order->rmb_amount,
  105. 'orderid' => $order->out_trade_no,
  106. 'type' => "",
  107. 'title' => '',
  108. 'notifyurl' => $this->request->domain() . '/api/alipay/notifyx/orderfrom/delivery',
  109. 'returnurl' => $this->request->domain() . '/api/alipay/deliverypaysuccess',
  110. 'method' => "miniapp",
  111. 'body' => '快递费用',
  112. 'openid' => \app\common\model\User::where('id', $order['user_id'])->value('openid'),
  113. ];
  114. // $data = [
  115. // 'amount' => $order['rmb_amount'],
  116. // 'orderid' => $order['out_trade_no'],
  117. // 'type' => '',
  118. // 'title' => '',
  119. // 'notifyurl' => $this->request->domain() . '/addons/epay/api/notifyx2',
  120. // 'returnurl' => $this->request->domain() .'/addons/epay/api/returnx2222222',
  121. // 'method' => 'miniapp',
  122. // 'body' => '充值',
  123. // 'openid' => \app\common\model\User::where('id', $order['user_id'])->value('openid'),
  124. // ];
  125. $res = Service::submitOrder($params);
  126. $data = [
  127. 'msg' => '微信支付',
  128. 'jsConfig' => $res
  129. ];
  130. // $query = input('get.');
  131. // if (isset($query['orderid'])) {
  132. // unset($query['orderid']);
  133. // }
  134. // $this->auth->redis->set($order->out_trade_no, $query, 60 * 30); // 将参数存入缓存,30分钟
  135. //
  136. // // echo \addons\epay\library\Service::submitOrder($params);
  137. // echo \addons\eepay\Service::payfh($params);
  138. return json_encode($data);
  139. }
  140. /**
  141. * 盲盒、充值支付回调
  142. */
  143. public function notifyx($orderfrom = '')
  144. {
  145. // 回调只能来自于购买盲盒和充值
  146. if (!in_array($orderfrom, ['buybox', 'recharge', 'delivery'])) {
  147. dta(input(), '支付宝回调有误');
  148. echo '请求有误';
  149. return;
  150. }
  151. $pay = Service::checkNotify('alipay');
  152. if (!$pay) {
  153. dta('支付宝签名错误', __METHOD__ . ' ' . __LINE__);
  154. echo '签名错误';
  155. return;
  156. }
  157. Db::startTrans();
  158. try {
  159. $data = $pay->verify()->toArray();
  160. $usefulTemplate = array(
  161. 'invoice_amount' => '12.34',
  162. 'trade_status' => 'TRADE_SUCCESS',
  163. 'receipt_amount' => '12.34',
  164. 'buyer_pay_amount' => '12.34',
  165. 'notify_time' => '2021-07-16 14:40:39',
  166. 'out_trade_no' => '20210716144024',
  167. 'total_amount' => '12.34',
  168. 'trade_no' => '2021071622001413960501380461',
  169. );
  170. if ('TRADE_SUCCESS' != strtoupper($data['trade_status'])) {
  171. dta(array_intersect_key($data, $usefulTemplate), '支付宝支付失败');
  172. echo '支付失败';
  173. return;
  174. }
  175. // 购买盲盒订单,更新盲盒订单
  176. if ('buybox' == $orderfrom) {
  177. $order = Order::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  178. if (empty($order)) {
  179. dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
  180. echo '支付失败';
  181. return;
  182. }
  183. $order->save([
  184. 'pay_method' => 'alipay',
  185. 'pay_rmb' => $data['total_amount'],
  186. 'alipay_trade_no' => $data['trade_no'],
  187. 'pay_time' => time(),
  188. 'status' => 'unused',
  189. 'backend_read' => 0,
  190. ]);
  191. }
  192. // 充值订单
  193. if ('recharge' == $orderfrom) {
  194. $order = RechargeOrder::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  195. if (empty($order)) {
  196. dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
  197. echo '支付失败';
  198. return;
  199. }
  200. $order->save([
  201. 'pay_method' => 'alipay',
  202. 'pay_rmb' => $data['total_amount'],
  203. 'alipay_trade_no' => $data['trade_no'],
  204. 'pay_time' => time(),
  205. 'status' => 'paid',
  206. 'backend_read' => 0,
  207. ]);
  208. $user = \app\common\model\User::where('id', $order->user_id)->find();
  209. // 给账户充值前
  210. $coin_before = $user->coin;
  211. // 增加金币余额
  212. $user->setInc('coin', $order->coin_amount);
  213. // 创建金币记录
  214. CoinRecord::create([
  215. 'user_id' => $user->id,
  216. 'before' => $coin_before,
  217. 'after' => $user->coin,
  218. 'coin' => $order->coin_amount,
  219. 'order_id' => $order->id,
  220. 'type' => 'recharge', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入,refund=退款
  221. ]);
  222. }
  223. // 发货订单
  224. if ('delivery' == $orderfrom) {
  225. $trade = DeliveryTrade::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  226. if (empty($trade)) {
  227. dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
  228. echo '支付失败';
  229. return;
  230. }
  231. // 变更发货交易订单状态
  232. $trade->save([
  233. 'pay_method' => 'alipay',
  234. 'pay_rmb' => $data['total_amount'],
  235. 'alipay_trade_no' => $data['trade_no'],
  236. 'pay_time' => time(),
  237. 'status' => 'paid'
  238. ]);
  239. // 变更发货订单状态
  240. $deliveryOrder = Delivery::where('delivery_trade_id', $trade->id)->select();
  241. $prizeIds = [];
  242. foreach ($deliveryOrder as $order) {
  243. $order->save(['status' => 'undelivered']);
  244. $prizeIds[] = $order->prize_id;
  245. }
  246. // 变更奖品状态
  247. Prizerecord::whereIn('id', $prizeIds)->update(['status' => 'delivery', 'delivery_time' => time()]);
  248. }
  249. } catch (\Exception $e) {
  250. Db::rollback();
  251. dta($e->getMessage(), '支付宝回调执行出错');
  252. echo '错误';
  253. return;
  254. }
  255. Db::commit();
  256. echo $pay->success();
  257. }
  258. /**
  259. * 盲盒支付成功后用户会被重定向到这里
  260. * @author fuyelk <fuyelk@fuyelk.com>
  261. */
  262. public function boxpaysuccess()
  263. {
  264. $out_trade_no = $this->request->param('out_trade_no');
  265. $pay = Service::checkReturn('alipay');
  266. // 读取缓存里的参数
  267. $query = $this->auth->redis->get($out_trade_no);
  268. if (!$query || !is_array($query)) {
  269. $query = [];
  270. }
  271. $query['status'] = 1;
  272. if (!$pay) {
  273. $query['status'] = 0;
  274. }
  275. $query['out_trade_no'] = $out_trade_no;
  276. $params = http_build_query($query);
  277. // 将用户重定向到前端页面
  278. $this->redirect($this->request->domain() . "/h5/#/pagesA/pages/camera?" . $params);
  279. }
  280. /**
  281. * 充值支付成功后用户会被重定向到这里
  282. * @author fuyelk <fuyelk@fuyelk.com>
  283. */
  284. public function rechargesuccess()
  285. {
  286. $out_trade_no = $this->request->param('out_trade_no');
  287. $pay = Service::checkReturn('alipay');
  288. // 读取缓存里的参数
  289. $query = $this->auth->redis->get($out_trade_no);
  290. if (!$query || !is_array($query)) {
  291. $query = [];
  292. }
  293. $query['status'] = 1;
  294. if (!$pay) {
  295. $query['status'] = 0;
  296. }
  297. $params = http_build_query($query);
  298. // 将用户重定向到前端页面
  299. $this->redirect($this->request->domain() . "/h5/#/pages/me/wallet?" . $params);
  300. }
  301. /**
  302. * 发货订单支付成功后用户会被重定向到这里
  303. * @author fuyelk <fuyelk@fuyelk.com>
  304. */
  305. public function deliverypaysuccess()
  306. {
  307. $out_trade_no = $this->request->param('out_trade_no');
  308. $pay = Service::checkReturn('alipay');
  309. // 读取缓存里的参数
  310. $query = $this->auth->redis->get($out_trade_no);
  311. if (!$query || !is_array($query)) {
  312. $query = [];
  313. }
  314. $query['status'] = 1;
  315. if (!$pay) {
  316. $query['status'] = 0;
  317. }
  318. $params = http_build_query($query);
  319. // 将用户重定向到前端页面
  320. $this->redirect($this->request->domain() . "/h5/#/pages/me/order?type=0?" . $params);
  321. }
  322. }