订单支付金额为0时,自动完成支付支付
parent
478a32d9c0
commit
90b61f4fe4
|
|
@ -33,6 +33,7 @@ class Order extends Model
|
|||
*/
|
||||
public const PAY_WAY_WXPAY = 'wxpay'; // 微信支付
|
||||
public const PAY_WAY_OFFLINE = 'offline'; // 现金支付
|
||||
public const PAY_WAY_NONE = 'none'; // 无
|
||||
|
||||
/**
|
||||
* @var array
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ class OrderService
|
|||
// 订单总费用
|
||||
$totalAmount += $shippingFee;
|
||||
|
||||
$order = $user->orders()->create([
|
||||
$orderAttrs = [
|
||||
'sn' => OrderHelper::serialNumber(),
|
||||
'user_coupon_id' => $coupon?->id,
|
||||
'coupon_discount_amount' => $couponDiscountAmount,
|
||||
|
|
@ -147,16 +147,26 @@ class OrderService
|
|||
'shipping_fee' => $shippingFee,
|
||||
'products_total_amount' => $productsTotalAmount,
|
||||
'total_amount' => $totalAmount,
|
||||
'status' => $totalAmount > 0 ? Order::STATUS_PENDING : Order::STATUS_PAID,
|
||||
'note' => $note,
|
||||
// 收货地址
|
||||
'consignee_name' => $shippingAddress->consignee,
|
||||
'consignee_telephone' => $shippingAddress->telephone,
|
||||
'consignee_zone' => $shippingAddress->zone,
|
||||
'consignee_address' => $shippingAddress->address,
|
||||
]);
|
||||
];
|
||||
|
||||
$data = [];
|
||||
if ($totalAmount === 0) {
|
||||
$orderAttrs = array_merge([
|
||||
'status' => Order::STATUS_PAID,
|
||||
'pay_sn' => OrderHelper::serialNumber(),
|
||||
'pay_way' => Order::PAY_WAY_NONE,
|
||||
'pay_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
$order = $user->orders()->create($orderAttrs);
|
||||
|
||||
$orderProducts = [];
|
||||
|
||||
foreach ($mapProducts as $product) {
|
||||
$sku = $product['sku'];
|
||||
|
|
@ -164,7 +174,7 @@ class OrderService
|
|||
// 支付金额 = 商品总额 - 优惠券折扣金额- 会员折扣金额
|
||||
$totalAmount = $product['total_amount'] - $product['coupon_discount_amount'] - $product['vip_discount_amount'];
|
||||
|
||||
$data[] = [
|
||||
$orderProducts[] = [
|
||||
'user_id' => $order->user_id,
|
||||
'order_id' => $order->id,
|
||||
'spu_id' => $sku->spu_id,
|
||||
|
|
@ -185,13 +195,13 @@ class OrderService
|
|||
'updated_at' => $order->updated_at,
|
||||
];
|
||||
|
||||
// 扣商品库存
|
||||
$sku->update([
|
||||
'stock' => DB::raw("stock - {$product['quantity']}"), // 库存
|
||||
]);
|
||||
}
|
||||
|
||||
OrderProduct::insert($data);
|
||||
// 处理赠品
|
||||
OrderProduct::insert($orderProducts);
|
||||
|
||||
// 将优惠券标记为已使用
|
||||
$coupon?->markAsUse();
|
||||
|
|
|
|||
Loading…
Reference in New Issue