Placeorder.php 5.9 KB
Newer Older
R
root 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
namespace fecshop\app\appfront\modules\checkout\block\onepage;
use Yii;
use fec\helpers\CModule;
use fec\helpers\CRequest;
class Placeorder{
	
	public $_check_error;
	public $_billing;
	public $_shipping_method;
	public $_payment_method;
	
	public function getLastData(){
		$post = Yii::$app->request->post();
		if(is_array($post) && !empty($post)){
16 17 18 19
			if($this->checkOrderInfoAndInit($post)){
				$this->guestCreateAndLoginAccountAndSaveAddress($post);
				$this->updateGuestCart($post);
				# 将购物车数据,生成订单。
R
root 已提交
20 21 22 23 24 25 26 27
				
			}
		}
		
		return [
		
		];
	}
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	/**
	 * 如果游客选择了创建账户,并且输入了密码,则使用address email作为账号,
	 * 进行账号的注册和登录。
	 */
	public function guestCreateAndLoginAccountAndSaveAddress($post){
		$create_account = $post['create_account']j;
		if($create_account){
			$customer_password = $post['customer_password'];
			$confirm_password  = $post['confirm_password'];
			if($customer_password  != $confirm_password){
				$this->_check_error[] = 'the passwords are inconsistent';
				return false;
			}
			$passMin = Yii::$service->customer->getRegisterPassMinLength();
			$passMax = Yii::$service->customer->getRegisterPassMaxLength();
			if($customer_password < $passMin){
				$this->_check_error[] = 'password must Greater than '.$passMin;
				return false;
			}
			if($customer_password > $passMax){
				$this->_check_error[] = 'password must less than '.$passMax;
				return false;
			}
			$param['email'] 	= $post['email'];
			$param['password'] 	= $post['customer_password'];
			$param['firstname'] = $post['first_name'];
			$param['lastname'] 	= $post['last_name'];
			if(!Yii::$service->customer->register($param)){
				return false;
			}else{
				Yii::$service->customer->Login([
					'email'		=> $post['email'],
					'password'	=> $post['customer_password']
				]);
			}
		}
		# 保存货运地址到customer address ,然后把生成的
		# address_id 写入到cart中。
		# shipping method写入到cart中
		# payment method 写入到cart中
		if(!Yii::$app->user->isGuest){
			$identity = Yii::$app->user->identity;
			$customer_id = $identity['id'];
			$one = [
				'first_name' 	=> $post['first_name'],
				'last_name' 	=> $post['last_name'],
				'email' 		=> $post['email'],
				'company' 		=> '',
				'telephone' 	=> $post['telephone'],
				'fax' 			=> '',
				'street1' 		=> $post['street1'],
				'street2' 		=> $post['street2'],
				'city' 			=> $post['city'],
				'state' 		=> $post['state'],
				'zip' 			=> $post['zip'],
				'country' 		=> $post['country'],
				'customer_id' 	=> $customer_id,
				'is_default' 	=> 1,
			];
			$address_id = Yii::$service->customer->address->save($one);
			if(!$address_id){
				$this->_check_error[] = 'new customer address save fail';
				return false;
			}
			return Yii::$service->cart->updateLoginCart($address_id,$this->_shipping_method,$this->_payment_method);
			
		}
		
		return true;
	}
	/**
	 * 如果是游客,那么保存货运地址到购物车表。
	 */
	public function updateGuestCart(){
		if(Yii::$app->user->isGuest){
			Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
		}
	}
R
root 已提交
106 107 108 109

	//$create_account = isset($billing['create_account']) ? $billing['create_account'] : '';
	//$customer_password 	= isset($billing['customer_password']) ? $billing['customer_password'] : '';
	//$confirm_password 	= isset($billing['confirm_password']) ? $billing['confirm_password'] : '';
110 111 112 113 114 115 116
	
	/**
	 * @property $post | Array
	 * @return boolean 
	 * 检查前台传递的信息是否正确。同时初始化一部分类变量
	 */
	public function checkOrderInfoAndInit($post){
R
root 已提交
117 118 119 120 121 122
		
		$address_one = '';
		$address_id = isset($post['address_id']) ? $post['address_id'] : '';
		$billing = isset($post['billing']) ? $post['billing'] : '';
		
		if($billing && is_array($billing)){
123
			# 检查address的必写字段是否都存在
R
root 已提交
124 125
			if(!Yii::$service->order->checkRequiredAddressAttr($billing)){
				$this->_check_error[] = Yii::$service->helper->errors->get();
126
				return false;
R
root 已提交
127 128 129 130 131
			}
			$this->_billing = $billing;
		}else if($address_id){
			if(Yii::$app->user->isGuest){
				$this->_check_error[] = 'address id can not use for guest';
132
				return false; # address_id 这种情况,必须是登录用户。
R
root 已提交
133 134 135 136 137 138 139 140 141 142 143
			}else{
				$customer_id = Yii::$app->user->identity->id;
				if(!$customer_id){
					$this->_check_error[] = 'customer id is empty';
					return false;
				}else{
					$address_one = Yii::$service->customer->address->getAddressByIdAndCustomerId($address_id,$customer_id);
					if(!$address_one){
						$this->_check_error[] = 'current address id is not belong to current user';
						return false;
					}else{
144
						# 从address_id中取出来的字段,查看是否满足必写的要求。
R
root 已提交
145 146
						if(!Yii::$service->order->checkRequiredAddressAttr($address_one)){
							$this->_check_error[] = Yii::$service->helper->errors->get();
147
							return false;
R
root 已提交
148 149 150 151 152 153 154 155
						}
						$this->_billing = $address_one;
					}
				}
			}	
		}
		$shipping_method= isset($billing['shipping_method']) ? $billing['shipping_method'] : '';
		$payment_method = isset($billing['payment_method']) ? $billing['payment_method'] : '';
156
		# 验证货运方式
R
root 已提交
157 158
		if(!$shipping_method){
			$this->_check_error[] = 'shipping method can not empty';
159
			return false;
R
root 已提交
160 161 162
		}else{
			if(!Yii::$service->shipping->ifIsCorrect($shipping_method)){
				$this->_check_error[] = 'shipping method is not correct';
163
				return false;
R
root 已提交
164 165
			}
		}
166
		# 验证支付方式
R
root 已提交
167 168
		if(!$payment_method){
			$this->_check_error[] = 'payment method can not empty';
169 170 171 172 173 174
			return false;
		}else{
			if(!Yii::$service->payment->ifIsCorrectStandard($payment_method)){
				$this->_check_error[] = 'payment method is not correct';
				return false;
			}
R
root 已提交
175 176 177 178 179 180 181 182 183 184
		}
		$this->_shipping_method = $shipping_method;
		$this->_payment_method = $payment_method;
		return true;
	}
	
	
	
	
}