123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace app\api\controller;
- use addons\epay\library\Service;
- use app\api\model\CoinRecord;
- use app\api\model\Delivery;
- use app\api\model\DeliveryTrade;
- use app\api\model\Order;
- use app\api\model\Prizerecord;
- use app\api\model\RechargeOrder;
- use app\common\controller\Frontend;
- use think\Db;
- use think\exception\HttpResponseException;
- use think\Response;
- use traits\controller\Jump;
- /**
- * 微信支付接口
- * Class Wechat
- * @package app\api\controller
- * @version 1.0
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- class Wechat extends Frontend
- {
- protected $noNeedLogin = ['notifyx', 'boxpaysuccess', 'rechargesuccess', 'deliverypay'];
- protected $noNeedRight = ['*'];
- use Jump;
- /**
- * 盲盒订单支付
- * @throws \Exception
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- public function boxpay($orderid = '')
- {
- if (empty($orderid)) {
- $this->error('订单ID不能为空');
- }
- $user = $this->auth->getUser();
- if (empty($user->wechat_openid)) {
- throw new HttpResponseException(Response::create(['code' => 403, 'msg' => '请先微信登录', 'data' => null], 'json', 200));
- }
- $order = Order::where('id', $orderid)->where('user_id', $this->auth->id)->where('status', 'unpay')->find();
- if (empty($order)) {
- throw new HttpResponseException(Response::create(['code' => 0, 'msg' => '订单有误', 'data' => null], 'json', 200));
- }
- $params = [
- // 'amount' => $order->rmb_amount,
- 'amount' => 0.01, // TODO 微信支付金额固定为1分钱
- 'openid' => $user->wechat_openid,
- 'orderid' => $order->out_trade_no,
- 'type' => "wechat",
- 'title' => $order->box_name,
- 'notifyurl' => $this->request->domain() . '/api/wechat/notifyx/orderfrom/buybox',
- 'returnurl' => $this->request->domain() . '/api/wechat/boxpaysuccess',
- 'method' => "mp",
- ];
- $query = input('get.');
- if (isset($query['orderid'])) {
- unset($query['orderid']);
- }
- $this->auth->redis->set($order->out_trade_no, $query, 60 * 30); // 将参数存入缓存,30分钟
- try {
- $payinfo = \addons\epay\library\Service::submitOrder($params);
- } catch (\Exception $e) {
- throw new HttpResponseException(Response::create(['code' => 0, 'msg' => $e->getMessage(), 'data' => null], 'json', 200));
- }
- throw new HttpResponseException(Response::create(['code' => 1, 'msg' => '获取支付信息成功', 'data' => $payinfo], 'json', 200));
- }
- /**
- * 充值订单支付
- * @throws \Exception
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- public function rechargepay($orderid = '')
- {
- if (empty($orderid)) {
- $this->error('订单ID不能为空');
- }
- $user = $this->auth->getUser();
- if (empty($user->wechat_openid)) {
- throw new HttpResponseException(Response::create(['code' => 403, 'msg' => '请先微信登录', 'data' => null], 'json', 200));
- }
- $order = RechargeOrder::where('id', $orderid)->where('user_id', $this->auth->id)->where('status', 'unpay')->find();
- if (empty($order)) {
- throw new HttpResponseException(Response::create(['code' => 0, 'msg' => '订单有误', 'data' => null], 'json', 200));
- }
- $params = [
- // 'amount' => $order->rmb_amount,
- 'amount' => 0.01, // TODO 微信支付金额固定为1分钱
- 'openid' => $user->wechat_openid,
- 'orderid' => $order->out_trade_no,
- 'type' => "wechat",
- 'title' => '充值',
- 'notifyurl' => $this->request->domain() . '/api/wechat/notifyx/orderfrom/recharge',
- 'returnurl' => $this->request->domain() . '/api/wechat/rechargesuccess',
- 'method' => "mp",
- ];
- $query = input('get.');
- if (isset($query['orderid'])) {
- unset($query['orderid']);
- }
- $this->auth->redis->set($order->out_trade_no, $query, 60 * 30); // 将参数存入缓存,30分钟
- try {
- $payinfo = \addons\epay\library\Service::submitOrder($params);
- } catch (\Exception $e) {
- throw new HttpResponseException(Response::create(['code' => 0, 'msg' => $e->getMessage(), 'data' => null], 'json', 200));
- }
- throw new HttpResponseException(Response::create(['code' => 1, 'msg' => '获取支付信息成功', 'data' => $payinfo], 'json', 200));
- }
- /**
- * 发货订单支付
- * @throws \Exception
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- public function deliverypay($orderid = '')
- {
- if (empty($orderid)) {
- $this->error('订单ID不能为空');
- }
- $user = $this->auth->getUser();
- if (empty($user->wechat_openid)) {
- throw new HttpResponseException(Response::create(['code' => 403, 'msg' => '请先微信登录', 'data' => null], 'json', 200));
- }
- $order = DeliveryTrade::where('id', $orderid)->where('user_id', $this->auth->id)->where('status', 'unpay')->find();
- if (empty($order)) {
- throw new HttpResponseException(Response::create(['code' => 0, 'msg' => '订单有误', 'data' => null], 'json', 200));
- }
- $params = [
- // 'amount' => $order->rmb_amount,
- 'amount' => 0.01, // TODO 微信支付金额固定为1分钱
- 'openid' => $user->wechat_openid,
- 'orderid' => $order->out_trade_no,
- 'type' => "wechat",
- 'title' => '快递费用',
- 'notifyurl' => $this->request->domain() . '/api/wechat/notifyx/orderfrom/delivery',
- 'returnurl' => $this->request->domain() . '/api/wechat/deliverypaysuccess',
- 'method' => "mp",
- ];
- $query = input('get.');
- if (isset($query['orderid'])) {
- unset($query['orderid']);
- }
- $this->auth->redis->set($order->out_trade_no, $query, 60 * 30); // 将参数存入缓存,30分钟
- try {
- $payinfo = \addons\epay\library\Service::submitOrder($params);
- } catch (\Exception $e) {
- throw new HttpResponseException(Response::create(['code' => 0, 'msg' => $e->getMessage(), 'data' => null], 'json', 200));
- }
- throw new HttpResponseException(Response::create(['code' => 1, 'msg' => '获取支付信息成功', 'data' => $payinfo], 'json', 200));
- }
- /**
- * 盲盒、充值支付回调
- */
- public function notifyx($orderfrom = '')
- {
- // 回调只能来自于购买盲盒和充值
- if (!in_array($orderfrom, ['buybox', 'recharge', 'delivery'])) {
- dta(input(), '微信回调有误');
- echo '请求有误';
- return;
- }
- $pay = Service::checkNotify('wechat');
- if (!$pay) {
- dta('微信签名错误', __METHOD__ . ' ' . __LINE__);
- echo '签名错误';
- return;
- }
- Db::startTrans();
- try {
- $data = $pay->verify()->toArray();
- $usefulTemplate = array(
- 'out_trade_no' => '202107221724365792624',
- 'result_code' => 'SUCCESS',
- 'total_fee' => '100', // 单位:分
- 'transaction_id' => '4200001157202107228405177371',
- );
- if ('SUCCESS' != strtoupper($data['result_code'])) {
- dta(array_intersect_key($data, $usefulTemplate), '微信支付失败');
- echo '支付失败';
- return;
- }
- // 购买盲盒订单,更新盲盒订单
- if ('buybox' == $orderfrom) {
- $order = Order::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
- if (empty($order)) {
- dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
- echo '支付失败';
- return;
- }
- $order->save([
- 'pay_method' => 'wechat',
- 'pay_rmb' => round($data['total_fee'] / 100, 2),
- 'transaction_id' => $data['transaction_id'],
- 'pay_time' => time(),
- 'status' => 'unused',
- 'backend_read' => 0,
- ]);
- }
- // 充值订单
- if ('recharge' == $orderfrom) {
- $order = RechargeOrder::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
- if (empty($order)) {
- dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
- echo '支付失败';
- return;
- }
- $order->save([
- 'pay_method' => 'wechat',
- 'pay_rmb' => round($data['total_fee'] / 100, 2),
- 'transaction_id' => $data['transaction_id'],
- 'pay_time' => time(),
- 'status' => 'paid',
- 'backend_read' => 0,
- ]);
- $user = \app\common\model\User::where('id', $order->user_id)->find();
- // 给账户充值前
- $coin_before = $user->coin;
- // 增加金币余额
- $user->setInc('coin', $order->coin_amount);
- // 创建金币记录
- CoinRecord::create([
- 'user_id' => $user->id,
- 'before' => $coin_before,
- 'after' => $user->coin,
- 'coin' => $order->coin_amount,
- 'order_id' => $order->id,
- 'type' => 'recharge', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入,refund=退款
- ]);
- }
- // 发货订单
- if ('delivery' == $orderfrom) {
- $trade = DeliveryTrade::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
- if (empty($trade)) {
- dta(array_intersect_key($data, $usefulTemplate), '订单有误,支付失败');
- echo '支付失败';
- return;
- }
- // 变更发货交易订单状态
- $trade->save([
- 'pay_method' => 'wechat',
- 'pay_rmb' => round($data['total_fee'] / 100, 2),
- 'transaction_id' => $data['transaction_id'],
- 'pay_time' => time(),
- 'status' => 'paid'
- ]);
- // 变更发货订单状态
- $deliveryOrder = Delivery::where('delivery_trade_id', $trade->id)->select();
- $prizeIds = [];
- foreach ($deliveryOrder as $order) {
- $order->save(['status' => 'undelivered']);
- $prizeIds[] = $order->prize_id;
- }
- // 变更奖品状态
- Prizerecord::whereIn('id', $prizeIds)->update(['status' => 'delivery', 'delivery_time' => time()]);
- }
- } catch (\Exception $e) {
- Db::rollback();
- dta($e->getMessage(), '微信回调执行出错');
- echo '错误';
- return;
- }
- Db::commit();
- echo $pay->success();
- }
- /**
- * 盲盒支付成功后用户会被重定向到这里
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- public function boxpaysuccess()
- {
- $out_trade_no = $this->request->param('out_trade_no');
- $pay = Service::checkReturn('wechat');
- // 读取缓存里的参数
- $query = $this->auth->redis->get($out_trade_no);
- if (!$query || !is_array($query)) {
- $query = [];
- }
- $query['status'] = 1;
- if (!$pay) {
- $query['status'] = 0;
- }
- $query['out_trade_no'] = $out_trade_no;
- $params = http_build_query($query);
- // 将用户重定向到前端页面
- $this->redirect($this->request->domain() . "/h5/#/pagesA/pages/camera?" . $params);
- }
- /**
- * 充值支付成功后用户会被重定向到这里
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- public function rechargesuccess()
- {
- $out_trade_no = $this->request->param('out_trade_no');
- $pay = Service::checkReturn('wechat');
- // 读取缓存里的参数
- $query = $this->auth->redis->get($out_trade_no);
- if (!$query || !is_array($query)) {
- $query = [];
- }
- $query['status'] = 1;
- if (!$pay) {
- $query['status'] = 0;
- }
- $params = http_build_query($query);
- // 将用户重定向到前端页面
- $this->redirect($this->request->domain() . "/h5/#/pages/me/wallet?" . $params);
- }
- /**
- * 发货订单支付成功后用户会被重定向到这里
- * @author fuyelk <fuyelk@fuyelk.com>
- */
- public function deliverypaysuccess()
- {
- $out_trade_no = $this->request->param('out_trade_no');
- $pay = Service::checkReturn('wechat');
- // 读取缓存里的参数
- $query = $this->auth->redis->get($out_trade_no);
- if (!$query || !is_array($query)) {
- $query = [];
- }
- $query['status'] = 1;
- if (!$pay) {
- $query['status'] = 0;
- }
- $params = http_build_query($query);
- // 将用户重定向到前端页面
- $this->redirect($this->request->domain() . "/h5/#/pages/me/order?type=0?" . $params);
- }
- }
|