Service.php 4.34 KB
<?php

namespace WebPlugin\Pay\Wechatqrcode;

use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\Pay\Signature;
use WebPlugin\Pay\weixin\lib\WxPayApi;
use WebPlugin\Pay\weixin\lib\WxPayOrderQuery;
use WebPlugin\UdpLog;
use WebPlugin\Helpers;

class Service extends PayAbstract
{

    private $config;
    private $orderCode;
    private $paymentCode;
    private $appKey;
    private $privateKey;
    private $payCurl;

    public function __construct(array $paymentParams)
    {
        $this->logProjectPrefix = 'wechatpay';
        $this->config = new Config();
        $environParam = 'pay_' . APPLICATION_ENV . '_url';
        $this->payCurl = $this->config->$environParam;
        $this->appKey = $this->config->app_key;
        $this->privateKey = $this->config->private_key;
        $this->paymentCode = $paymentParams['id'];
    }

    public function getPayRequestPars(Reqparams $params)
    {
        $this->orderCode = $params->orderCode;
        $result = array(
            'pay_url' => Helpers::url('/pay/wechatqrcode'),
            'pars' => 'order_code=' . $this->orderCode . '&payment_code=' . $this->paymentCode,
            'reqType' => 'get'
        );
        return $result;
    }

    /**
     * 微信支付页
     * @param array $orderInfo
     * @return string
     * @internal param array $orderCode
     */
    public function pay(array $orderInfo)
    {
        UdpLog::info("【{$this->logProjectPrefix}-微信支付】,function:pay,参数", $orderInfo);
        //生成签名
        $this->orderCode = $orderInfo['order_code'];
        $payParams = array(
            'order_code' => $this->orderCode,
            'app_key' => $this->appKey,
            'payment_code' => $this->paymentCode,
            'private_key' => $this->privateKey
        );
        $_params = Signature::packageSort($payParams);
        $client_secret = Signature::makeSign($_params);
        $parameter = array(
            'order_code' => $this->orderCode,
            'app_key' => $this->appKey,
            //这里payment.yoho_pay表与payment.q_pay表数据需一致
            'payment_code' => $this->paymentCode,
            'client_secret' => $client_secret,
            'client' => 'web'
        );
        $pars = '';
        foreach ($parameter as $p_key => $p_val) {
            $pars .= $p_key . '=' . urlencode($p_val) . '&';
        }
        $payUrlInfo = array(
            'pay_url' => $this->payCurl,
            'pars' => trim($pars, '&')
        );

        $payUrl = $payUrlInfo['pay_url'] . '?' . $payUrlInfo['pars'];
        UdpLog::info("【{$this->logProjectPrefix}-微信支付】,order_code:{$orderInfo['order_code']},function:pay,结果", $payUrl);

        return array('pay_url' => $payUrl);
    }

    public function parseResponse(array $data)
    {
        /* 返回示例
         * http://www.yohobuy.com/pay/notice/wechatqrcodereturn?ordercode=93465435
         */
        UdpLog::info("{$this->logProjectPrefix}-微信支付】,function:parseResponse,参数", $data);

        // 组装微信支付的订单号
        $tradeNo = 'YOHOBuy_' . $data['orderCode'];
        $input = new WxPayOrderQuery();
        $input->SetOut_trade_no($tradeNo);
        $result = WxPayApi::orderQuery($input);
        UdpLog::info("【{$this->logProjectPrefix}-微信支付】,orderCode:{$data['orderCode']},function:parseResponse,组装微信支付的订单号", $result);

        $rsp = new Rspparams();
        // 支付成功
        if (isset($result['trade_state']) && $result['trade_state'] === 'SUCCESS') {
            $rsp->payResult = 200;
            $rsp->bankName = "WX";
            $rsp->orderCode = $data['orderCode'];
            $rsp->payTime = $result["time_end"];
            $rsp->totalFee = $result["total_fee"] / 100;
            $rsp->resultMsg = '支付成功';
            //添加支付订单号和交易号
            $rsp->payOrderCode = $data["orderCode"];
            $rsp->tradeNo = $result["out_trade_no"];
            $rsp->bankBillNo = $result['bank_type'];
        } else {
            $rsp->payResult = -1;
        }

        UdpLog::info("{$this->logProjectPrefix}-微信支付】,orderCode:{$data['orderCode']},function:parseResponse,处理结果", $rsp);
        return $rsp;
    }

}