123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace App\Http\Api;
- use App\Services\OrderService\ComboOrderService;
- use App\Services\OrderService\GoodsOrderService;
- use EasyWeChat\Factory;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\Request;
- use Txj\Elastic\Facades\ES;
- class OrderController extends HttpBaseController
- {
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
- $this->isLoginJson();
- }
- /**
- * 创建订单
- */
- public function createOrder()
- {
- $requestData = Request::all();
- $product_type = Request::post('product_type'); // 1 服务商品 2 服务套餐
- if (!in_array($product_type, [1, 2])) {
- return responseMessage(2002, '非法提交,type参数错误!');
- }
- try {
- if ($product_type == 1) { // 服务商品
- $goodsOrder = new GoodsOrderService($this->userId);
- return $goodsOrder->create($requestData);
- } else { // 服务套餐
- $comboOrder = new ComboOrderService($this->userId);
- return $comboOrder->create($requestData);
- }
- } catch (\Exception $e) {
- return responseMessage(2003, '订单创建失败,请重试!');
- }
- }
- /**
- * 不停的检测,微信是否支付成功
- */
- public function checkIsWXPay()
- {
- $requestData = Request::all();
- $order_no = $requestData['order_no'] ?? '';
- if (empty($order_no)) {
- return responseMessage(2001, '参数错误!');
- }
- $materialService = $this->getOrderService($order_no);
- $orderInfo = $materialService->getOrderInfo($order_no);
- if ($orderInfo && $orderInfo['order_status'] == 1) {
- return responseMessage(2004, '支付中!');
- } elseif ($orderInfo['order_status'] == 2) {
- return responseMessage(1001, '恭喜你,支付成功!');
- }
- }
- ###################################################################################################################
- ##### 订单退款 ##########################################################################################################
- ###################################################################################################################
- /**
- * 申请退款
- */
- public function applyRefund()
- {
- $requestData = Request::all();
- $order_no = $requestData['order_no'] ?? '';
- $refund_reason = $requestData['refund_reason'] ?? ''; //退款原因
- $images = $requestData['images'] ?? ''; //退款图片
- if (empty($order_no)) {
- return responseMessage(2001, '参数错误!');
- }
- if (empty($refund_reason)) {
- return responseMessage(2003, '请输入退款原因!');
- }
- $refund_no = 'TG' . createNewOrderNo(); // 退款单号
- //判断该订单是否存在
- $orderInfo = ES::table('order_goods')->where(['order_no' => $order_no, 'user_id' => $this->userId])->first();
- if (empty($orderInfo)) {
- return responseMessage(2004, '该订单不存在,或已删除!');
- }
- if ($orderInfo['order_status'] != 2) {
- return responseMessage(2005, '该订单状态不正确,不允许退款!');
- }
- // 判断是否已经申请过了
- $is_exist = ES::table('order_goods_refund')->where(['order_id' => $orderInfo['id']])->count();
- if ($is_exist) {
- return responseMessage(2006, '你已经申请过了,请不要重复提交!');
- }
- // 保存售后信息
- // status 1 退款申请中 2 拒绝退款 3 退款完成 4 取消退款 5 退款中
- $is_success = ES::table('order_goods_refund')->insertGetId([
- 'refund_order_no' => $refund_no, // 退款单号
- 'order_id' => $orderInfo['id'],
- 'order_status' => $orderInfo['order_status'],
- 'user_id' => $this->userId,
- 'refund_reason' => $refund_reason,
- 'refund_images' => $images,
- 'refund_price' => $orderInfo['order_amount_total'], // 优惠平台之后的价格
- 'refund_status' => 1,
- ]);
- if ($is_success) {
- // 更新总订单的状态
- $res = ES::table('order_goods')->toRefresh()->updateEntityById($orderInfo['id'], ['order_status' => 7]);
- // // 消息通知
- // dispatch(new SendMessage($this->storeId, 'REFUND_APPLY', [
- // 'orderNo' => $orderInfo['order_no']
- // ], $this->userId));
- return responseMessage(1001, '操作成功', $res);
- } else {
- return responseMessage(2007, '操作失败,请重试!');
- }
- }
- /**
- * 取消退款
- */
- public function cancelRefund(): JsonResponse
- {
- $requestData = Request::all();
- $refund_mid = $requestData['mid'] ?? '';
- if (empty($refund_mid)) {
- return responseMessage(2001, '参数错误!');
- }
- $refundInfo = ES::table('order_goods_refund')->where('mid', $refund_mid)->where('user_id', $this->userId)->first();
- if (empty($refundInfo)) {
- return responseMessage(2005, '退款订单不存在,请重试!');
- }
- // status 1 退款申请中 2 拒绝退款 3 退款完成 4 取消退款
- if ($refundInfo['refund_status'] == 1) {
- $is_success = ES::table('order_goods_refund')->updateEntityById($refundInfo['id'], ['refund_status' => 4]);
- if ($is_success) {
- //更新总订单
- ES::table('order_goods')->updateEntityById($refundInfo['order_id'], ['order_status' => $refundInfo['order_status']]);
- return responseMessage(1001, '操作成功!');
- } else {
- return responseMessage(2007, '操作失败,请重试!');
- }
- } else {
- return responseMessage(2006, '非法操作!');
- }
- }
- /**
- * 通过微信接口,检测该订单的退款状态
- */
- public function checkRefundOrder()
- {
- $requestData = Request::all();
- $refund_no = $requestData['refund_no'] ?? '';
- $refund_mid = $requestData['mid'] ?? '';
- if (empty($refund_mid) || empty($refund_no)) {
- return responseMessage(2001, '非法提交!');
- }
- // 获取退款订单信息
- $refundInfo = ES::table('order_goods_refund')->where('mid', $refund_mid)->where('user_id', $this->userId)->first();
- if (empty($refundInfo)) {
- return responseMessage(2005, '退款订单不存在,请重试!');
- }
- if ($refundInfo['status'] == 3) {
- return responseMessage(2014, '退款完成');
- }
- // 获取原订单信息 order_id
- $orderInfo = ES::table('order_goods')->find($refundInfo['order_id']);
- if (empty($orderInfo)) {
- return responseMessage(2005, '该订单不存在,请重试!');
- }
- $config = config('wechat.payment.default');
- $app = Factory::payment($config);
- $pay_info = json_decode($orderInfo['pay_info'], true);
- $result = $app->refund->queryByTransactionId($pay_info['transaction_id']);
- if ($result['return_code'] == 'SUCCESS') {
- if ($result['result_code'] == 'SUCCESS') {
- // 更新退款状态和信息 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
- ES::table('order_goods_refund')->updateEntityById($refundInfo['id'], ['status' => 3]);
- //更新总订单的状态 订单状态 1未付款2已付款3已发货4已完成5交易关闭6退款申请中7卖家退款中8退款完成订单关闭9拒绝退款
- ES::table('order_goods')->updateEntityById($orderInfo['id'], ['order_status' => 6]);
- return responseMessage(1001, '已退款!');
- } else {
- return responseMessage(2011, '');
- }
- } else {
- return responseMessage(2010, '');
- }
- }
- }
|