Wechat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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() . '/addons/epay/api/notifyxde',
  109. 'returnurl' => $this->request->domain() . '/addons/epay/api/returnx2222222',
  110. 'method' => "miniapp",
  111. 'body' => '快递费用',
  112. 'openid' => \app\common\model\User::where('id', $order['user_id'])->value('openid'),
  113. ];
  114. $res = Service::submitOrder($params);
  115. $data = [
  116. 'msg' => '微信支付',
  117. 'jsConfig' => $res
  118. ];
  119. // $query = input('get.');
  120. // if (isset($query['orderid'])) {
  121. // unset($query['orderid']);
  122. // }
  123. // $this->auth->redis->set($order->out_trade_no, $query, 60 * 30); // 将参数存入缓存,30分钟
  124. //
  125. // // echo \addons\epay\library\Service::submitOrder($params);
  126. // echo \addons\eepay\Service::payfh($params);
  127. return json_encode($data);
  128. }
  129. /**
  130. * 盲盒、充值支付回调
  131. */
  132. public function notifyx($orderfrom = '')
  133. {
  134. // 回调只能来自于购买盲盒和充值
  135. if (!in_array($orderfrom, ['buybox', 'recharge', 'delivery'])) {
  136. dta(input(), '支付宝回调有误');
  137. echo '请求有误';
  138. return;
  139. }
  140. $pay = Service::checkNotify('alipay');
  141. if (!$pay) {
  142. dta('支付宝签名错误', __METHOD__ . ' ' . __LINE__);
  143. echo '签名错误';
  144. return;
  145. }
  146. Db::startTrans();
  147. try {
  148. $data = $pay->verify()->toArray();
  149. $usefulTemplate = array(
  150. 'invoice_amount' => '12.34',
  151. 'trade_status' => 'TRADE_SUCCESS',
  152. 'receipt_amount' => '12.34',
  153. 'buyer_pay_amount' => '12.34',
  154. 'notify_time' => '2021-07-16 14:40:39',
  155. 'out_trade_no' => '20210716144024',
  156. 'total_amount' => '12.34',
  157. 'trade_no' => '2021071622001413960501380461',
  158. );
  159. if ('TRADE_SUCCESS' != strtoupper($data['trade_status'])) {
  160. dta(array_intersect_key($data, $usefulTemplate), '支付宝支付失败');
  161. echo '支付失败';
  162. return;
  163. }
  164. // 购买盲盒订单,更新盲盒订单
  165. if ('buybox' == $orderfrom) {
  166. $order = Order::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  167. if (empty($order)) {
  168. dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
  169. echo '支付失败';
  170. return;
  171. }
  172. $order->save([
  173. 'pay_method' => 'alipay',
  174. 'pay_rmb' => $data['total_amount'],
  175. 'alipay_trade_no' => $data['trade_no'],
  176. 'pay_time' => time(),
  177. 'status' => 'unused',
  178. 'backend_read' => 0,
  179. ]);
  180. }
  181. // 充值订单
  182. if ('recharge' == $orderfrom) {
  183. $order = RechargeOrder::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  184. if (empty($order)) {
  185. dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
  186. echo '支付失败';
  187. return;
  188. }
  189. $order->save([
  190. 'pay_method' => 'alipay',
  191. 'pay_rmb' => $data['total_amount'],
  192. 'alipay_trade_no' => $data['trade_no'],
  193. 'pay_time' => time(),
  194. 'status' => 'paid',
  195. 'backend_read' => 0,
  196. ]);
  197. $user = \app\common\model\User::where('id', $order->user_id)->find();
  198. // 给账户充值前
  199. $coin_before = $user->coin;
  200. // 增加金币余额
  201. $user->setInc('coin', $order->coin_amount);
  202. // 创建金币记录
  203. CoinRecord::create([
  204. 'user_id' => $user->id,
  205. 'before' => $coin_before,
  206. 'after' => $user->coin,
  207. 'coin' => $order->coin_amount,
  208. 'order_id' => $order->id,
  209. 'type' => 'recharge', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入,refund=退款
  210. ]);
  211. }
  212. // 发货订单
  213. if ('delivery' == $orderfrom) {
  214. $trade = DeliveryTrade::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  215. if (empty($trade)) {
  216. dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
  217. echo '支付失败';
  218. return;
  219. }
  220. // 变更发货交易订单状态
  221. $trade->save([
  222. 'pay_method' => 'alipay',
  223. 'pay_rmb' => $data['total_amount'],
  224. 'alipay_trade_no' => $data['trade_no'],
  225. 'pay_time' => time(),
  226. 'status' => 'paid'
  227. ]);
  228. // 变更发货订单状态
  229. $deliveryOrder = Delivery::where('delivery_trade_id', $trade->id)->select();
  230. $prizeIds = [];
  231. foreach ($deliveryOrder as $order) {
  232. $order->save(['status' => 'undelivered']);
  233. $prizeIds[] = $order->prize_id;
  234. }
  235. // 变更奖品状态
  236. Prizerecord::whereIn('id', $prizeIds)->update(['status' => 'delivery', 'delivery_time' => time()]);
  237. }
  238. } catch (\Exception $e) {
  239. Db::rollback();
  240. dta($e->getMessage(), '支付宝回调执行出错');
  241. echo '错误';
  242. return;
  243. }
  244. Db::commit();
  245. echo $pay->success();
  246. }
  247. /**
  248. * 盲盒支付成功后用户会被重定向到这里
  249. * @author fuyelk <fuyelk@fuyelk.com>
  250. */
  251. public function boxpaysuccess()
  252. {
  253. $out_trade_no = $this->request->param('out_trade_no');
  254. $pay = Service::checkReturn('alipay');
  255. // 读取缓存里的参数
  256. $query = $this->auth->redis->get($out_trade_no);
  257. if (!$query || !is_array($query)) {
  258. $query = [];
  259. }
  260. $query['status'] = 1;
  261. if (!$pay) {
  262. $query['status'] = 0;
  263. }
  264. $query['out_trade_no'] = $out_trade_no;
  265. $params = http_build_query($query);
  266. // 将用户重定向到前端页面
  267. $this->redirect($this->request->domain() . "/h5/#/pagesA/pages/camera?" . $params);
  268. }
  269. /**
  270. * 充值支付成功后用户会被重定向到这里
  271. * @author fuyelk <fuyelk@fuyelk.com>
  272. */
  273. public function rechargesuccess()
  274. {
  275. $out_trade_no = $this->request->param('out_trade_no');
  276. $pay = Service::checkReturn('alipay');
  277. // 读取缓存里的参数
  278. $query = $this->auth->redis->get($out_trade_no);
  279. if (!$query || !is_array($query)) {
  280. $query = [];
  281. }
  282. $query['status'] = 1;
  283. if (!$pay) {
  284. $query['status'] = 0;
  285. }
  286. $params = http_build_query($query);
  287. // 将用户重定向到前端页面
  288. $this->redirect($this->request->domain() . "/h5/#/pages/me/wallet?" . $params);
  289. }
  290. /**
  291. * 发货订单支付成功后用户会被重定向到这里
  292. * @author fuyelk <fuyelk@fuyelk.com>
  293. */
  294. public function deliverypaysuccess()
  295. {
  296. $out_trade_no = $this->request->param('out_trade_no');
  297. $pay = Service::checkReturn('alipay');
  298. // 读取缓存里的参数
  299. $query = $this->auth->redis->get($out_trade_no);
  300. if (!$query || !is_array($query)) {
  301. $query = [];
  302. }
  303. $query['status'] = 1;
  304. if (!$pay) {
  305. $query['status'] = 0;
  306. }
  307. $params = http_build_query($query);
  308. // 将用户重定向到前端页面
  309. $this->redirect($this->request->domain() . "/h5/#/pages/me/order?type=0?" . $params);
  310. }
  311. }