Browse Source

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

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

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

@ -8,9 +8,11 @@ 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 EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Service\OrderStatus as Status;
@ -43,7 +45,7 @@ class OrderController extends Controller
$order_list = Order::where($where)
->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')
->simplePaginate(15)
->toArray();
@ -62,13 +64,13 @@ class OrderController extends Controller
$v['status'] = Status::CANCEL;
$v['status_text'] = '已取消';
//TODO 加回库存,未考虑到几天/几个月后再打开订单列表页的情况,需要定时任务处理
Product::query()->find($v['product_id'])->update(['stock' => $v['num']]);
Product::query()->find($v['product_id'])->increment('stock', $v['num']);
}
}
}
//超时订单设置为已取消 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);
}
@ -155,13 +157,13 @@ class OrderController extends Controller
}
if ($formData['pay_type'] == 3) { //线下支付
return $this->success('操作成功,请及时联系客服线下支付');
return $this->success('操作成功,请及时联系客服付');
} else { //在线支付或定金支付
$config = $this->payConfig($order->title, $order->order_no, $order->status, $price);
$config = $this->payConfig($order, $price);
if (!empty($config['paySign'])) {
return $this->success($config);
} 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);
}
//TODO 需要后台处理
//TODO 需要后台处理,然后向微信发起退款申请
$order = Order::find($formData['id']);
if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAID_RETAINAGE])) {
return $this->error('当前订单状态不允许退款');
@ -194,6 +196,7 @@ class OrderController extends Controller
$order->status = Status::REFUNDING;
$order->refund_info = [
'desc' => strip_tags($formData['desc']),
'refund_no' => $this->getOrderNo(), //退款单号
'pictures' => $formData['pictures'] ?? [],
];
$order->save();
@ -223,6 +226,10 @@ class OrderController extends Controller
->with('coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at')
->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->num = $formData['num'];
return $this->success($ap);
@ -242,27 +249,26 @@ class OrderController extends Controller
if (!$order) {
return $this->error('订单不存在或已支付');
}
$order = $order->toArray();
$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 {
$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'])) {
return $this->success($config);
} 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 = 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);
$result = $app->order->unify([
'body' => $title,
'out_trade_no' => $order_no . '-' . $status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'total_fee' => round($price * 100), //支付金额单位为分
'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI',
'openid' => $openid,
]);
try {
$result = $app->order->unify([
'body' => $order->title,
'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'total_fee' => round($price * 100), //支付金额单位为分
'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI',
'openid' => $openid,
]);
} catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
return ['error' => $e->getMessage(), 'line' => $e->getLine()];
}
if (empty($result['prepay_id'])) {
return $result;
}
$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();
foreach ($coupon as $v) {
// 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']) {
$total_price -= $v['detail']['reduction'];
}
} else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折
$total_price *= $v['detail']['discount'];
}
}*/
}
return round($total_price, 2);
}

Loading…
Cancel
Save