Wechat.php 11 KB

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