Browse Source

1、完善订单相关逻辑;2、payConfig增加id和order_no输出;3、优惠券价格计算暂时注释;

dev
李可松 4 years ago
parent
commit
7aac6077f5
  1. 50
      app/Http/Controllers/Api/OrderController.php

50
app/Http/Controllers/Api/OrderController.php

@ -8,9 +8,11 @@ use App\Models\AgentProduct;
use App\Models\Coupon; use App\Models\Coupon;
use App\Models\Product; use App\Models\Product;
use App\Models\User; use App\Models\User;
use App\Models\UserMoneyLog;
use App\Models\Order; use App\Models\Order;
use EasyWeChat\Factory; use EasyWeChat\Factory;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use App\Service\OrderStatus as Status; use App\Service\OrderStatus as Status;
@ -43,7 +45,7 @@ class OrderController extends Controller
$order_list = Order::where($where) $order_list = Order::where($where)
->with('product:id,title,pictures') ->with('product:id,title,pictures')
->select('id', 'product_id', 'price', 'num', 'status', 'created_at')
->select('id', 'agent_product_id', 'product_id', 'price', 'num', 'status', 'created_at')
->orderBy('id', 'DESC') ->orderBy('id', 'DESC')
->simplePaginate(15) ->simplePaginate(15)
->toArray(); ->toArray();
@ -62,13 +64,13 @@ class OrderController extends Controller
$v['status'] = Status::CANCEL; $v['status'] = Status::CANCEL;
$v['status_text'] = '已取消'; $v['status_text'] = '已取消';
//TODO 加回库存,未考虑到几天/几个月后再打开订单列表页的情况,需要定时任务处理 //TODO 加回库存,未考虑到几天/几个月后再打开订单列表页的情况,需要定时任务处理
Product::query()->find($v['product_id'])->update(['stock' => $v['num']]);
Product::query()->find($v['product_id'])->increment('stock', $v['num']);
} }
} }
} }
//超时订单设置为已取消 TODO 测试阶段暂时注释 //超时订单设置为已取消 TODO 测试阶段暂时注释
// Order::query()->whereIn('id', $timeout_ids)->update(['status' => Status::CANCEL]);
Order::query()->whereIn('id', $timeout_ids)->update(['status' => Status::CANCEL]);
return $this->success($order_list); return $this->success($order_list);
} }
@ -155,13 +157,13 @@ class OrderController extends Controller
} }
if ($formData['pay_type'] == 3) { //线下支付 if ($formData['pay_type'] == 3) { //线下支付
return $this->success('操作成功,请及时联系客服线下支付');
return $this->success('操作成功,请及时联系客服付');
} else { //在线支付或定金支付 } else { //在线支付或定金支付
$config = $this->payConfig($order->title, $order->order_no, $order->status, $price);
$config = $this->payConfig($order, $price);
if (!empty($config['paySign'])) { if (!empty($config['paySign'])) {
return $this->success($config); return $this->success($config);
} else { } else {
return $this->error(join(',', $config));
return $this->error($config['err_code_des'] ?? join(',', $config));
} }
} }
} }
@ -186,7 +188,7 @@ class OrderController extends Controller
$v = str_replace($host, '', $v); $v = str_replace($host, '', $v);
} }
//TODO 需要后台处理
//TODO 需要后台处理,然后向微信发起退款申请
$order = Order::find($formData['id']); $order = Order::find($formData['id']);
if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAID_RETAINAGE])) { if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAID_RETAINAGE])) {
return $this->error('当前订单状态不允许退款'); return $this->error('当前订单状态不允许退款');
@ -194,6 +196,7 @@ class OrderController extends Controller
$order->status = Status::REFUNDING; $order->status = Status::REFUNDING;
$order->refund_info = [ $order->refund_info = [
'desc' => strip_tags($formData['desc']), 'desc' => strip_tags($formData['desc']),
'refund_no' => $this->getOrderNo(), //退款单号
'pictures' => $formData['pictures'] ?? [], 'pictures' => $formData['pictures'] ?? [],
]; ];
$order->save(); $order->save();
@ -223,6 +226,10 @@ class OrderController extends Controller
->with('coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at') ->with('coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at')
->find($formData['id'], ['id', 'price', 'original_price', 'product_id']); ->find($formData['id'], ['id', 'price', 'original_price', 'product_id']);
if (!$ap) {
return $this->error('产品信息不存在');
}
$ap->final_price = $this->calc($ap->price, $ap->coupon, $formData['num'], $formData['pay_type']); $ap->final_price = $this->calc($ap->price, $ap->coupon, $formData['num'], $formData['pay_type']);
$ap->num = $formData['num']; $ap->num = $formData['num'];
return $this->success($ap); return $this->success($ap);
@ -242,27 +249,26 @@ class OrderController extends Controller
if (!$order) { if (!$order) {
return $this->error('订单不存在或已支付'); return $this->error('订单不存在或已支付');
} }
$order = $order->toArray();
$coupon = Coupon::whereIn('id', $order['coupon_id'])->get(); $coupon = Coupon::whereIn('id', $order['coupon_id'])->get();
//如果已经付定金或首付款,则仅支付尾款 //如果已经付定金或首付款,则仅支付尾款
if ($order['status'] == Status::PAY_EARNEST) {
$price = $order['price'] - $order['paid_money'];
if ($order->status == Status::PAY_EARNEST) {
$price = $order->price - $order->paid_money;
} else { } else {
$price = $this->calc($order['price'], $coupon, $order['num'], $order['pay_type']);
$price = $this->calc($order->price, $coupon, $order->num, $order->pay_type);
} }
$config = $this->payConfig($order['title'], $order['order_no'], $order['status'], $price);
$config = $this->payConfig($order, $price);
if (!empty($config['paySign'])) { if (!empty($config['paySign'])) {
return $this->success($config); return $this->success($config);
} else { } else {
return $this->error(join(',', $config));
return $this->error($config['err_code_des'] ?? join(',', $config));
} }
} }
//获取支付配置信息 //获取支付配置信息
private function payConfig($title, $order_no, $status, $price)
private function payConfig($order, $price)
{ {
//用户openid //用户openid
$openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
@ -278,21 +284,25 @@ class OrderController extends Controller
]); ]);
$app = Factory::payment($config); $app = Factory::payment($config);
try {
$result = $app->order->unify([ $result = $app->order->unify([
'body' => $title,
'out_trade_no' => $order_no . '-' . $status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'body' => $order->title,
'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'total_fee' => round($price * 100), //支付金额单位为分 'total_fee' => round($price * 100), //支付金额单位为分
'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', 'trade_type' => 'JSAPI',
'openid' => $openid, 'openid' => $openid,
]); ]);
} catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
return ['error' => $e->getMessage(), 'line' => $e->getLine()];
}
if (empty($result['prepay_id'])) { if (empty($result['prepay_id'])) {
return $result; return $result;
} }
$jssdk = $app->jssdk; $jssdk = $app->jssdk;
return $jssdk->bridgeConfig($result['prepay_id'], false); // 返回数组
return $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组
} }
//订单详情 //订单详情
@ -348,13 +358,13 @@ class OrderController extends Controller
$coupon = $coupon->toArray(); $coupon = $coupon->toArray();
foreach ($coupon as $v) { foreach ($coupon as $v) {
// TODO 未判断优惠券有效期 // TODO 未判断优惠券有效期
if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减
/*if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减
if ($total_price >= $v['detail']['full']) { if ($total_price >= $v['detail']['full']) {
$total_price -= $v['detail']['reduction']; $total_price -= $v['detail']['reduction'];
} }
} else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折 } else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折
$total_price *= $v['detail']['discount']; $total_price *= $v['detail']['discount'];
}
}*/
} }
return round($total_price, 2); return round($total_price, 2);
} }

Loading…
Cancel
Save