购物车数据验证
This commit is contained in:
parent
4835250392
commit
2aeba25590
|
|
@ -42,15 +42,15 @@ class CartRepo
|
|||
$cart = Cart::query()->create([
|
||||
'customer_id' => $customerId,
|
||||
'shipping_address_id' => $defaultAddressId,
|
||||
'shipping_method_code' => $shippingMethod->code,
|
||||
'shipping_method_code' => $shippingMethod->code ?? '',
|
||||
'payment_address_id' => $defaultAddressId,
|
||||
'payment_method_code' => $paymentMethod->code
|
||||
'payment_method_code' => $paymentMethod->code ?? ''
|
||||
]);
|
||||
} else {
|
||||
if ($cart->shipping_address_id == 0) {
|
||||
if ($cart->shipping_address_id == 0 || empty(AddressRepo::find($cart->shipping_address_id))) {
|
||||
$cart->shipping_address_id = $defaultAddressId;
|
||||
}
|
||||
if ($cart->payment_address_id == 0) {
|
||||
if ($cart->payment_address_id == 0 || empty(AddressRepo::find($cart->payment_address_id))) {
|
||||
$cart->payment_address_id = $defaultAddressId;
|
||||
}
|
||||
$cart->save();
|
||||
|
|
|
|||
|
|
@ -157,4 +157,30 @@ class PluginRepo
|
|||
return $plugin && $plugin->getEnabled();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测对应配送方式是否可用
|
||||
*
|
||||
* @param $code
|
||||
* @return bool
|
||||
*/
|
||||
public static function shippingEnabled($code): bool
|
||||
{
|
||||
$shippingMethods = self::getShippingMethods();
|
||||
return $shippingMethods->where('code', $code)->count() > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测对应支付方式是否可用
|
||||
*
|
||||
* @param $code
|
||||
* @return bool
|
||||
*/
|
||||
public static function paymentEnabled($code): bool
|
||||
{
|
||||
$paymentMethods = self::getPaymentMethods();
|
||||
return $paymentMethods->where('code', $code)->count() > 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,10 +53,6 @@ class CheckoutController extends Controller
|
|||
*/
|
||||
public function confirm()
|
||||
{
|
||||
try {
|
||||
return (new CheckoutService)->confirm();
|
||||
} catch (\Exception $e) {
|
||||
return json_fail($e->getMessage());
|
||||
}
|
||||
return (new CheckoutService)->confirm();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@
|
|||
|
||||
namespace Beike\Shop\Services;
|
||||
|
||||
use Beike\Repositories\CartRepo;
|
||||
use Exception;
|
||||
use Beike\Models\CartProduct;
|
||||
use Beike\Repositories\CartRepo;
|
||||
use Beike\Shop\Http\Resources\CartDetail;
|
||||
|
||||
class CartService
|
||||
|
|
@ -116,6 +116,12 @@ class CartService
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除购物车商品
|
||||
*
|
||||
* @param $customer
|
||||
* @param $cartId
|
||||
*/
|
||||
public static function delete($customer, $cartId)
|
||||
{
|
||||
if (empty($cartId)) {
|
||||
|
|
@ -149,6 +155,6 @@ class CartService
|
|||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount),
|
||||
];
|
||||
return $data;
|
||||
return hook_filter('cart.data', $data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
namespace Beike\Shop\Services;
|
||||
|
||||
use Beike\Models\Order;
|
||||
use Beike\Models\Address;
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Repositories\CartRepo;
|
||||
use Beike\Repositories\OrderRepo;
|
||||
|
|
@ -88,6 +89,7 @@ class CheckoutService
|
|||
$customer = current_customer();
|
||||
$checkoutData = self::checkoutData();
|
||||
$checkoutData['customer'] = $customer;
|
||||
$this->validateConfirm($checkoutData);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
|
@ -102,6 +104,35 @@ class CheckoutService
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function validateConfirm($checkoutData)
|
||||
{
|
||||
$current = $checkoutData['current'];
|
||||
|
||||
$shippingAddressId = $current['shipping_address_id'];
|
||||
if (empty(Address::query()->find($shippingAddressId))) {
|
||||
throw new \Exception('配送地址无效');
|
||||
}
|
||||
|
||||
$paymentAddressId = $current['payment_address_id'];
|
||||
if (empty(Address::query()->find($paymentAddressId))) {
|
||||
throw new \Exception('账单地址无效');
|
||||
}
|
||||
|
||||
$shippingMethodCode = $current['shipping_method_code'];
|
||||
if (!PluginRepo::shippingEnabled($shippingMethodCode)) {
|
||||
throw new \Exception('配送方式不可用');
|
||||
}
|
||||
|
||||
$paymentMethodCode = $current['payment_method_code'];
|
||||
if (!PluginRepo::paymentEnabled($paymentMethodCode)) {
|
||||
throw new \Exception('支付方式不可用');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function updateShippingAddressId($shippingAddressId)
|
||||
{
|
||||
$this->cart->update(['shipping_address_id' => $shippingAddressId]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue