Placeorder.php 9.6 KB
Newer Older
T
Terry 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */

namespace fecshop\app\appserver\modules\Payment\block\paypal\express;

use Yii;

/**
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Placeorder
{
    /**
     * 用户的账单地址信息,通过用户传递的信息计算而来。
     */
    public $_billing;

    public $_address_id;
    /**
     * 用户的货运方式.
     */
    public $_shipping_method;
    /**
     * 用户的支付方式.
     */
    public $_payment_method;

    public function getLastData()
    {
        $post = Yii::$app->request->post();
        $token = Yii::$app->request->post('token');
        if(!$token){
            
            return [
                'code' => 401,
                'content' => 'token can not empty',
            ];
        }
        if (is_array($post) && !empty($post)) {
            $post = \Yii::$service->helper->htmlEncode($post);
            // 设置paypal快捷支付
            $post['payment_method'] = Yii::$service->payment->paypal->express_payment_method;
            // 检查前台传递的数据的完整性
            // 检查前台传递的数据的完整
            $checkInfo = $this->checkOrderInfoAndInit($post);
            if ($checkInfo !== true) {
                return $checkInfo;
            }
            
            // 如果游客用户勾选了注册账号,则注册,登录,并把地址写入到用户的address中
            $save_address_status = $this->updateAddress($post);
           
            // 更新Cart信息
            //$this->updateCart();
            // 设置checkout type
            $serviceOrder = Yii::$service->order;
            $checkout_type = $serviceOrder::CHECKOUT_TYPE_EXPRESS;
            $serviceOrder->setCheckoutType($checkout_type);
            // 将购物车数据,生成订单,生成订单后,不清空购物车,不扣除库存,在支付成功后在清空购物车。
            $innerTransaction = Yii::$app->db->beginTransaction();
            try {
                $genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing, $this->_shipping_method, $this->_payment_method, false,$token);
                if ($genarateStatus) {
                    $innerTransaction->commit();
                } else {
                    $innerTransaction->rollBack();
                }
            } catch (Exception $e) {
                $innerTransaction->rollBack();
            }
            //echo 22;
            if ($genarateStatus) {
                // 得到当前的订单信息
                $doExpressCheckoutReturn = $this->doExpressCheckoutPayment($token);
                //echo $doExpressCheckoutReturn;exit;
                //echo 333;
                if ($doExpressCheckoutReturn) {
                    $increment_id = Yii::$service->order->getSessionIncrementId();
                    $innerTransaction = Yii::$app->db->beginTransaction();
                    try {
                        // 插件这个订单是否被支付过,如果被支付过,则回滚
                        if(!Yii::$service->order->checkOrderVersion($increment_id)){    
                            $innerTransaction->rollBack();
                            return [
                                'code' => 401,
                                'content' => 'the order has been paid',
                            ];
                        }
                        $ExpressOrderPayment = Yii::$service->payment->paypal->updateExpressOrderPayment($doExpressCheckoutReturn,$token);
                        // 如果支付成功,并把信息更新到了订单数据中,则进行下面的操作。
                        //echo 444;
                        if ($ExpressOrderPayment) {
                            // 查看订单是否被多次支付,如果被多次支付,则回滚
                            
                            // 支付成功后,在清空购物车数据。而不是在生成订单的时候。
                            Yii::$service->cart->clearCartProductAndCoupon();
                            // (删除)支付成功后,扣除库存。
                            // (删除)Yii::$service->product->stock->deduct();
                            // echo 555;
                            // 发送新订单邮件

                            // 扣除库存和优惠券
                            // 在生成订单的时候已经扣除了。参看order service GenerateOrderByCart() function

                            // 得到支付跳转前的准备页面。
                            //$paypal_express = Yii::$service->payment->paypal->express_payment_method;
                            //$successRedirectUrl = Yii::$service->payment->getExpressSuccessRedirectUrl($paypal_express);
                            //Yii::$service->url->redirect($successRedirectUrl);
                            $innerTransaction->commit();
                            
                            return [
                                'code' => 200,
                                'content' => 'order generate and pay success'
                            ];
                        }else{
                            
                            $innerTransaction->rollBack();
                            return [
                                'code' => 401,
                                'content' => 'order pay by paypal fail',
                            ];
                        }
                    } catch (Exception $e) {
                        $innerTransaction->rollBack();
                        return false;
                    }
                }
                // 如果订单支付过程中失败,将订单取消掉
                /* 2017-09-12修改,认为没有必要取消订单,如果取消掉,在支付页面就无法继续下单,因此注释掉下面的代码
                if (!$doExpressCheckoutReturn || !$ExpressOrderPayment) {
                    $innerTransaction = Yii::$app->db->beginTransaction();
                    try {
                        if(Yii::$service->order->cancel()){
                            $innerTransaction->commit();
                        }else{
                            $innerTransaction->rollBack();
                        }
                    } catch (Exception $e) {
                        $innerTransaction->rollBack();
                    }
                }
                */
                //return true;
            }else{
                return[
                    'code' => 401,
                    'content' => 'generate order fail'
                ];
            }
            
        }
        //echo 'eeeeeeee';exit;
        Yii::$service->page->message->addByHelperErrors();

        return false;
    }
    /**
     * @property $token | String 
     * 通过paypal的api接口,进行支付下单
     */
    public function doExpressCheckoutPayment($token)
    {
        $methodName_ = 'DoExpressCheckoutPayment';
        $nvpStr_ = Yii::$service->payment->paypal->getExpressCheckoutPaymentNvpStr($token);
        //echo $nvpStr_;exit;
        $DoExpressCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_);
        //var_dump($DoExpressCheckoutReturn);
        //exit;
        if (strstr(strtolower($DoExpressCheckoutReturn['ACK']), 'success')) {
            return $DoExpressCheckoutReturn;
        } else {
            if ($DoExpressCheckoutReturn['ACK'] == 'Failure') {
                $message = $DoExpressCheckoutReturn['L_LONGMESSAGE0'];
                // 添加报错信息。
                //Message::error($message);
                Yii::$service->helper->errors->add($message);
            } else {
                Yii::$service->helper->errors->add('paypal express payment error.');
            }

            return false;
        }
    }

    /**
     * @property $post | Array
     * 登录用户,保存货运地址到customer address ,然后把生成的
     * address_id 写入到cart中。
     * shipping method写入到cart中
     * payment method 写入到cart中 updateCart
     */
    public function updateAddress($post)
    {
        return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method);
    }

    /**
     * 如果是游客,那么保存货运地址到购物车表。
     */
    /*
    public function updateCart(){
        if(Yii::$app->user->isGuest){
            return Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
        }else{
            return Yii::$service->cart->updateLoginCart($this->_address_id,$this->_shipping_method,$this->_payment_method);
        }
    }
    */

    /**
     * @property $post | Array
     * @return bool
     *              检查前台传递的信息是否正确。同时初始化一部分类变量
     */
    public function checkOrderInfoAndInit($post)
    {
        $address_one = '';
        $billing = isset($post['billing']) ? $post['billing'] : '';
        if (!Yii::$service->order->checkRequiredAddressAttr($billing)) {
            return [
                'code' => 401,
                'content' => 'address info error',
            ];
        }
        $this->_billing = $billing;

        $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : '';
        $payment_method = isset($post['payment_method']) ? $post['payment_method'] : '';
        // 验证货运方式
        if (!$shipping_method) {
            return [
                'code' => 401,
                'content' => 'shipping method can not empty',
            ];
        } else {
            if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) {
                
                return [
                    'code' => 401,
                    'content' => 'shipping method is not correct',
                ];
            }
        }

        $this->_shipping_method = $shipping_method;
        $this->_payment_method = $payment_method;
        Yii::$service->payment->setPaymentMethod($this->_payment_method);

        return true;
    }
}