Pay.php 4.77 KB
<?php

use Action\AbstractAction;
use Plugin\Helpers;
use LibModels\Wap\Home\OrderData;
use Plugin\Pay\weixin\JsApiPay;
use Plugin\Pay\weixin\lib\WxPayUnifiedOrder;
use Plugin\Pay\weixin\lib\WxPayApi;
use Plugin\Pay\weixin\lib\WxPayConfig;
use Plugin\Pay\aliwap\AliwapReqparams;
use Plugin\Pay\aliwap\AliwapService;

/**
 * 支付相关的控制器
 * 
 * @name PayController
 * @package Shopping
 * @copyright yoho.inc
 * @version 1.0 (2016-1-23 13:12:40)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class PayController extends AbstractAction
{
    /**
     * 支付宝跳转页
     * 
     * @param int order_code 订单号
     */
    public function alipayAction()
    {
        do {
            /* 判断是否有订单号参数 */
            $orderCode = $this->get('order_code');
            if (empty($orderCode)) {
                break;
            }
            
            /* 判断用户是否登录 */
            $uid = $this->getUid(true);
            if (!$uid) {
                $this->go( Helpers::url('/signin.html', array('refer' => $this->_request->server('HTTP_REFERER'))) );
                break;
            }
            
            /* 判断订单信息是否存在 */
            $orderDetail = OrderData::viewOrderData($orderCode, $uid, $this->_usession);
            if (empty($orderDetail['data'])) {
                $this->helpJsRedirect('没有找到该订单');
                break;
            }
            
            /* 判断订单是否已取消 */
            if (isset($orderDetail['data']['is_cancel']) && $orderDetail['data']['is_cancel'] === 'Y') {
                $this->helpJsRedirect('订单已经取消', 'window.location="' . Helpers::url('/home/orders/detail', array('order_code' => $orderCode)) .'";');
                break;
            }
            
            $totalFee = $orderDetail['data']['payment_amount'] * 100;
            $reqParams = new AliwapReqparams($orderCode, $totalFee, '有货订单号:' . $orderCode, '', $orderDetail['data']['create_time'], '', false);
            $aliwapService = new AliwapService();
            $payRequestPars = $aliwapService->getPayRequestPars($reqParams);
            if (empty($payRequestPars)) {
                $this->helpJsRedirect('支付系统繁忙,请稍后再试');
                break;
            }
            
            // 记录或修改支付方式 说明:由于在web站没有支付宝手机支付的选项,这里就记录支付方式就按照web端支付宝的支付方式平台号为2
            // 此处是咨询过JAVA开发任明明,按照老代码的实现方式判断,更新支付方式
            $paymentRecod = OrderData::updateOrderPayment($orderCode, 2, $uid);
            if (empty($paymentRecod) || $paymentRecod['code'] != 200) {
                $this->helpJsRedirect('系统繁忙,请稍后再试');
                break;
            }
            
            $this->go($payRequestPars['pay_url'] . $payRequestPars['pars']);
        }
        while (false);
    }
    
    /**
     * 微信支付 - 获取JSAPI参数
     * 
     * @param int order_code 订单号
     * @return json | void
     */
    public function wechatwapapiAction()
    {
        do {
            if (!$this->isAjax()) {
                break;
            }

            $uid = $this->getUid(true);
            if (!$uid) {
                break;
            }

            $orderCode = $this->get('order_code');
            if (empty($orderCode)) {
                break;
            }

            /* 判断订单信息不存在 */
            $orderDetail = OrderData::viewOrderData($orderCode, $uid, $this->_usession);
            if (empty($orderDetail['data'])) {
                break;
            }

            $totalFee = strval($orderDetail['data']['payment_amount'] * 100);
            $openId = $this->getSession('weixinOpenId');
            if (empty($openId)) {
                break;
            }

            //统一下单
            $tools = new JsApiPay();
            $input = new WxPayUnifiedOrder();
            $input->SetBody('有货订单号:' . $orderCode);
            $input->SetOut_trade_no('YOHOBuy_' . $orderCode); // 商户订单号
            $input->SetTotal_fee($totalFee);
            $input->SetTime_start(date("YmdHis", (int) $orderDetail['data']['create_time']));
            $input->SetTime_expire(date("YmdHis", (int) $orderDetail['data']['create_time'] + 7200));
            $input->SetNotify_url(WxPayConfig::NOTIFY_URL);
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($openId);
            $order = WxPayApi::unifiedOrder($input);
            $jsApiParameters = $tools->GetJsApiParameters($order);

            $this->echoJson(array('code' => 200, 'data' => array('jsApiParameters' => json_decode($jsApiParameters))));
            
        } while (false);
    }
    
    
}