diff --git a/app/Http/Controllers/Api/OrderController.php b/app/Http/Controllers/Api/OrderController.php index a7ad068..f51c58d 100644 --- a/app/Http/Controllers/Api/OrderController.php +++ b/app/Http/Controllers/Api/OrderController.php @@ -3,10 +3,14 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; +use App\Models\Agent; use App\Models\AgentProduct; use App\Models\Coupon; +use App\Models\Product; +use App\Models\User; use App\Models\UserMoneyLog; use App\Models\Order; +use EasyWeChat\Factory; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; @@ -54,12 +58,14 @@ class OrderController extends Controller $timeout_ids[] = $v['id']; $v['status'] = -1; $v['status_text'] = '已取消'; + //TODO 加回库存,未考虑到几天/几个月后再打开订单列表页的情况 + Product::query()->find($v['product_id'])->update(['stock' => $v['num']]); } } } - //超时订单设置为已取消 - Order::query()->whereIn('id', $timeout_ids)->update(['status' => -1]); + //超时订单设置为已取消 TODO 测试阶段暂时注释 +// Order::query()->whereIn('id', $timeout_ids)->update(['status' => -1]); return $this->success($order_list); } @@ -157,6 +163,41 @@ class OrderController extends Controller return $this->success(); } + //申请退款 + public function refund(Request $request) + { + $formData = $request->only(['id', 'desc', 'pictures']); + + $request->validate([ + 'id' => 'required|integer', + 'desc' => 'required|string', + 'pictures' => 'nullable|array', + ], [ + '*.required' => '内容输入不完整', + 'pictures.array' => '图片必须是数组', + ]); + + //去掉图片地址前的域名 + $host = env('APP_URL'); + foreach ($formData['pictures'] as &$v) { + $v = str_replace($host, '', $v); + } + + //TODO 需要后台处理 + $order = Order::find($formData['id']); + if (!in_array($order->status, [1, 2, 3])) { + return $this->error('当前订单状态不允许退款'); + } + $order->status = 6; + $order->refund_info = [ + 'desc' => strip_tags($formData['desc']), + 'pictures' => $formData['pictures'] ?? [], + ]; + $order->save(); + + return $this->success(); + } + //获取应付金额 public function getPrice(Request $request) { @@ -174,13 +215,53 @@ class OrderController extends Controller return $this->success(['price' => $final_price]); } + //订单支付 + public function pay() + { + $id = (int)request()->input('id'); + + //订单信息 + $order = Order::find($id); + if (!$order) { + return $this->error('订单不存在'); + } + + //用户openid + $openid = User::query()->find($this->user_id)->value('openid'); + + //代理商信息 + $agent = Agent::find($this->agent_id); + + $config = config('wechat.payment.default'); + $config = array_merge($config, [ + 'app_id' => $agent->appid, + 'mch_id' => $agent->mchid, + 'key' => $agent->mchkey, + ]); + + $app = Factory::payment($config); + $result = $app->order->unify([ + 'body' => $order->title, + 'out_trade_no' => $order->order_no, + 'total_fee' => $order->price, //TODO 需要区分定金和全款 +// 'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付结果通知网址,如果不设置则会使用配置里的默认地址 + 'trade_type' => 'JSAPI', + 'openid' => $openid, + ]); + + $jssdk = $app->jssdk; + $config = $jssdk->bridgeConfig($result['prepay_id'], false); // 返回数组 + + return $this->success($config); + } + //订单详情 public function show() { $id = (int)request()->input('id'); - $fields = ['id', 'order_no', 'agent_product_id', 'num', 'price', 'title', - 'picture', 'status', 'pay_type', 'coupon_id', 'paid_at', 'created_at']; + $fields = ['id', 'order_no', 'agent_product_id', 'num', 'price', 'title', 'picture', + 'status', 'pay_type', 'coupon_id', 'paid_money', 'paid_at', 'refund_info', 'created_at']; $order = Order::query() ->where('user_id', $this->user_id) ->find($id, $fields);