<?php
/**
 * 通用支付接口
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2019/11/28
 * Time: 3:48 PM
 */

namespace JinDouYun\Controller\Common;

use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Cache\SystemCache;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\System\MPaymentSetting;

use Util\WeiXin\Pay as wxPay;

class Pay extends BaseController
{
    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
    }

    public function micropay()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            return ResultWrapper::fail('参数为空', ErrorCode::$paramError);
        }

        $payData = [
            'out_trade_no' => getArrayItem($params, 'out_trade_no'),
            'total_fee'    => getArrayItem($params, 'total_fee'),
            'ip'           => $this->request->get_onlineip(),
            'shopName'     => getArrayItem($params, 'shopName'),
            'auth_code'    => getArrayItem($params, 'auth_code')
        ];

        foreach ($payData as $key => $value) {
            if (empty($value) && $value !== 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        // 获取当前登录企业的小程序配置
        $objSystemCache = new SystemCache();
        $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
        if (empty($miniprogramSetting)) {
            return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
        }
        $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid

        //获取支付配置
        $objMPaymentSetting = new MPaymentSetting($this->onlineUserId, $this->onlineEnterpriseId);
        $result = $objMPaymentSetting->getPayData(StatusCode::$payType['wxPay']);
        if (!$result->isSuccess() || empty($result->getData())) {
            return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor);
        }
        $payment = $result->getData();

        unset($result);
        $objwxPay = new wxPay($miniprogramSetting['appid'], $payment['mchId'], $payment['apiPartnerKey']);
        $result = $objwxPay->micropay($payData['out_trade_no'], $payData['total_fee'], $payData['ip'], $payData['shopName'], $payData['auth_code']);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

        parent::sendOutput($result->getData());
    }

    public function orderquery()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            return ResultWrapper::fail('参数为空', ErrorCode::$paramError);
        }


        // 获取当前登录企业的小程序配置
        $objSystemCache = new SystemCache();
        $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
        if (empty($miniprogramSetting)) {
            return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
        }
        $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid


        //获取支付配置
        $objMPaymentSetting = new MPaymentSetting($this->onlineUserId, $this->onlineEnterpriseId);
        $result = $objMPaymentSetting->getPayData(StatusCode::$payType['wxPay']);
        if (!$result->isSuccess() || empty($result->getData())) {
            return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor);
        }
        $payment = $result->getData();

        unset($result);
        $objwxPay = new wxPay($miniprogramSetting['appid'], $payment['mchId'], $payment['apiPartnerKey']);
        $result = $objwxPay->orderquery($params['out_trade_no']);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

        parent::sendOutput($result->getData());
    }

    // 微信付款到个人
    public function transfers()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            return ResultWrapper::fail('参数为空', ErrorCode::$paramError);
        }

        // 获取当前登录企业的小程序配置
        $objSystemCache = new SystemCache();
        $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
        if (empty($miniprogramSetting)) {
            return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
        }
        $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid

        //获取支付配置
        $objMPaymentSetting = new MPaymentSetting($this->onlineUserId, $this->onlineEnterpriseId);
        $result = $objMPaymentSetting->getPayData(StatusCode::$payType['wxPay']);
        if (!$result->isSuccess() || empty($result->getData())) {
            return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor);
        }
        $payment = $result->getData();

        unset($result);
        $objwxPay = new wxPay($miniprogramSetting['appid'], $payment['mchId'], $payment['apiPartnerKey']);
        $result = $objwxPay->transfers($params['out_trade_no'], $params['openid'], $params['amount']);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

        parent::sendOutput($result->getData());
    }

}