Pay.php 3.28 KB
<?php

use Action\WebAction;
use Shopping\PaymentModel;
use WebPlugin\Helpers;
use LibModels\Web\Home\UserData;
use WebPlugin\Cache;
/**
 * 支付相关的控制器
 * 
 * @name PayController
 * @package shopping
 * @copyright yoho.inc
 * @version 1.0 (2016-3-21 16:23:19)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class PayController extends WebAction
{

    // 存储的UID键名
    const SESSION_UID_KEY = 'payUserid';

    /**
     * 订单已完成,跳到支付
     */
    public function indexAction()
    {
        /* 判断用户是否登录 */
        $uid = $this->getUid(true);
        if (!$uid) {
            $this->go( Helpers::url('/signin.html', array('refer' => $this->server('HTTP_REFERER', SITE_MAIN))) );
        }
        // 订单号
        $orderCode = $this->get('order_code', $this->get('ordercode'));
        
        if (USE_CACHE) {
            // 将用户UID存入session中以便支付成功回调使用
            $this->setSession(self::SESSION_UID_KEY, $uid);
            Cache::set(self::SESSION_UID_KEY . $orderCode, $uid, 3600 * 10);
        }

        //支付宝快捷支付
        $alipayToken = $this->getSession('alipay_user_token');
        if (!empty($alipayToken)) {
            $this->go(Helpers::url('/pay/index/index', array('order_code' => $orderCode, 'payment_type' => '2_platform')));
        }

        $data = PaymentModel::getPayTypeData($uid, $orderCode);
        //不存在订单时
        if (isset($data['noOrder'])) {
            $this->helpJsRedirect('没有找到该订单');
        }

        $view = 'index';
        // 无需付款时
        if (isset($data['notNeedPay'])) {
            $view = 'notneedpay';
        }
        // 是货到付款时
        if (isset($data['finish'])) {
            $view = 'cashondelivery';
        }
        
        // 用户名
        $UserData = UserData::getUserInfo($uid);
        $data['userName'] = isset($UserData['code']) && $UserData['code'] === 200 ? $UserData['data']['profile_name'] : $this->_uname;

        $this->setSimpleHeader();
        $this->_view->display($view, array(
            'shoppingpay' =>$data,
            'pinyou' => array(
                'orderNum' => $data['orderNum'],
                'count' => $data['count'],
                'productList' => $data['pinyouGoods']
            ),
            'criteo' => array(// criteo统计代码有关数据
                'orderNum' => $data['orderNum'],
                'items' => $data['orderGoods']
            ),
            'payPage' => true
        ));
    }
    /**
     * 单击已完成支付按钮--发送一次支付确认
     */
    public function sendPayAction()
    {
        $result = array('code' => 400, 'message' => '操作失败!');
        if ($this->isAjax()) {
            do {
                $uid = $this->getUid(true);
                $orderCode = $this->post('orderCode', 0);
                $paymentId = $this->post('paymentId', 0);
                if (empty($orderCode) || empty($uid) || empty($paymentId)) {
                    break;
                }
                $result = PaymentModel::sendPayConfirm($orderCode, $paymentId, $uid);
            } while (false);
        }
        $this->echoJson($result);
    }
}