<?php
/**
 * 支付
 *
 *
 *
 *
 * by 33hao.com 好商城V3 运营版
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class member_paymentControl extends mbMemberControl
{

    private $payment_code = 'alipay';

    public function __construct()
    {
        parent::__construct();
        $this->payment_code = isset($_GET['payment_code']) && trim($_GET['payment_code']) != '' ? trim($_GET['payment_code']) : 'alipay';
    }

    /**
     * 实物订单支付
     */
    public function payOp()
    {
        $pay_sn = $_GET['pay_sn'];
        $model_mb_payment = Model('mb_payment');
        $logic_payment = Logic('payment');
        $condition = array();
        $condition['payment_code'] = $this->payment_code;
        $mb_payment_info = $model_mb_payment->getMbPaymentOpenInfo($condition);
        if (!$mb_payment_info) {
            output_error('系统不支持选定的支付方式');
        }
        //重新计算所需支付金额
        $result = $logic_payment->getRealOrderInfo($pay_sn, $_SESSION['member_id']);
        if (!$result['state']) {
            output_error($result['msg']);
        }
        //第三方API支付
        $this->_api_pay($result['data'], $mb_payment_info);
    }

    /**
     * 虚拟订单支付
     */
    public function vr_payOp()
    {
        $order_sn = $_GET['pay_sn'];
        $model_mb_payment = Model('mb_payment');
        $logic_payment = Logic('payment');
        $condition = array();
        $condition['payment_code'] = $this->payment_code;
        $mb_payment_info = $model_mb_payment->getMbPaymentOpenInfo($condition);
        if (!$mb_payment_info) {
            output_error('系统不支持选定的支付方式');
        }
        //重新计算所需支付金额
        $result = $logic_payment->getVrOrderInfo($order_sn, $_SESSION['member_id']);

        if (!$result['state']) {
            output_error($result['msg']);
        }
        //第三方API支付
        $this->_api_pay($result['data'], $mb_payment_info);
    }

    /**
     * 第三方在线支付接口
     *
     */
    private function _api_pay($order_pay_info, $payment_info)
    {
        $inc_file = BASE_PATH . DS . 'api' . DS . 'payment' . DS . $this->payment_code . DS . $this->payment_code . '.php';
        if (!is_file($inc_file)) {
            output_error('支付接口不存在');
        }
        if ($payment_info['payment_code'] == 'wxpay') {
            $wxpayurl = BASE_SITE_URL . '/mobile/api/payment/wxpay/wxpay.php?pay_sn=' . $order_pay_info['pay_sn'] . '&pay_amount=' . $order_pay_info['api_pay_amount'];
            redirect($wxpayurl);
            //@header("Location: ".ApiUrl.'/api/paymentpaypay.php?pay_sn='.$order_pay_info['pay_sn'].'&pay_amount='.$order_pay_info['api_pay_amount']);
        } else {
            require($inc_file);
            $param = array();
            $param = $payment_info['payment_config'];
            $param['order_sn'] = $order_pay_info['pay_sn'];
            $param['order_amount'] = $order_pay_info['api_pay_amount'];
            $param['order_type'] = ($order_pay_info['order_type'] == 'real_order' ? 'r' : 'v');
            $payment_api = new $this->payment_code();
            $return = $payment_api->submit($param);
            echo $return;
        }
        exit();
    }

    /**
     * 获取可用参数列表
     */
    public function payment_listOp()
    {
        $model_mb_payment = Model('mb_payment');
        $payment_list = $model_mb_payment->getMbPaymentOpenList();
        $payment_array = array();
        if (!empty($payment_list)) {
            foreach ($payment_list as $value) {
                $payment_array[] = $value['payment_code'];
            }
        }
        output_data(array('payment_list' => $payment_array));
    }
}
