<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: YingZi
// +----------------------------------------------------------------------
// | Date: 2022-05-27 15:14
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace library\utils;
use WeChatPay\Builder;
use WeChatPay\Formatter;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Crypto\AesGcm;
use WeChatPay\Crypto\AesEcb;
use WeChatPay\Crypto\Hash;
use WeChatPay\Util\PemUtil;
use WeChatPay\Transformer;


class WxpayV2{
    private $config;
    private $client;
    public $errorMsg;
    /**
     * 构造函数
     * @param type $config
     */
    public function __construct($config = []){
        if(empty($config)) $config = config('wxpay');
        $this->config = $config;
        // 工厂方法构造一个实例
        $this->client = Builder::factory([
            'mchid'      => $this->config["MCHID"],
            'serial'     => 'nop',
            'privateKey' => 'any',
            'certs'      => ['any' => null],
            'secret'     => $this->config["ApiV2Key"],
            'merchant' => [
                'cert' => $this->config["ApiclientCert"],
                'key'  => $this->config["ApiclientKey"],
            ],
        ]);
    }
    public function wxmpPay($post=[]){
        $apiUrl = "v2/pay/unifiedorder";
        //sign_type默认MD5不用传
        $params = [
            'appid'            => $this->config["APPID"], // 小程序ID/微信开放平台审核通过的应用APPID
            'mch_id'           => $this->config["MCHID"], // 商户号
            'nonce_str'        => Formatter::nonce(), // 32位随机字符串
            'body'             => empty($post["body"])  ? "微信小程序支付" : $post["body"],
            'attach'           => empty($post["attach"])? "微信小程序支付" : $post["attach"],
            'out_trade_no'     => $post["out_trade_no"], // 商户订单号
            'total_fee'        => (int)(floatval($post["total"])*100), // 订单总金额,单位分
            'spbill_create_ip' => empty($post["payer_client_ip"]) ? "127.0.0.1" : $post["payer_client_ip"], // 终端ip
            'time_expire'      => date("YmdHis",time()+30*60),//交易结束时间2018-06-08T10:34:56+08:00
            'notify_url'       => $this->config["NOTIFY_URL"], // 通知地址
            'trade_type'       => "JSAPI", // 交易类型
            'openid'           => $post["openid"],
        ];
        $result = $this->clientHttp("POST", $apiUrl, $params);
        if(empty($result)){
            if(empty($this->errorMsg)){
                $this->errorMsg = "支付错误001";
            }
            return false;
        }
        $resuleAr = Transformer::toArray($result);
        if(empty($resuleAr)){
            if(empty($this->errorMsg)){
                $this->errorMsg = "支付错误002";
            }
            return false;
        }
        if(empty($resuleAr["prepay_id"])){
            if(empty($this->errorMsg)){
                $this->errorMsg = "支付错误003";
            }
            return false;
        }
        //组装支付参数
        $payInfo=array();
        $payInfo['appId']     = $this->config["APPID"];
        $payInfo['timeStamp'] = time();
        $payInfo['nonceStr']  = Formatter::nonce();//生成随机数
        $payInfo["package"]   = "prepay_id=".$resuleAr["prepay_id"];
        $payInfo['signType']  = 'MD5';
        $payInfo['paySign']   = $this->v2makeSign($payInfo);
        return $payInfo;
    }
    
     public function v2makeSign($values) {
        //签名步骤一：按字典序排序参数
        ksort($values);
        $string = $this->ToUrlParams($values);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $this->config["ApiV2Key"];
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    /**
     * 参数数组转换为url参数
     * @param array $urlObj
     */
    private function ToUrlParams($urlObj) {
        $buff = "";
        foreach ($urlObj as $k => $v) {
            $buff .= $k . "=" . $v . "&";
        }
        $buff = trim($buff, "&");
        return $buff;
    }
    
    /**
     * 订单查询
     * @param type $out_trade_no
     */
    public function searchOrderQuery($out_trade_no){
        $apiUrl = "v2/pay/orderquery";
        $result = $this->clientHttp("POST", $apiUrl, [
            'appid'            => $this->config["APPID"], // 小程序ID/微信开放平台审核通过的应用APPID
            'mch_id'           => $this->config["MCHID"], // 商户号
            "out_trade_no"     =>$out_trade_no,
            'nonce_str'        => Formatter::nonce(), // 32位随机字符串
        ]);
        if(empty($result)){
            if(empty($this->errorMsg)){
                $this->errorMsg = "支付错误001";
            }
            return false;
        }
        return Transformer::toArray($result);
    }
    
    
    /**
     * 关闭订单
     * @param type $out_trade_no 商户订单号
     */
    public function closeOrder($out_trade_no){
        $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}/close";
        $result = $this->clientHttp("GET", $apiUrl,[
            "out_trade_no"=>$out_trade_no,
            "query"=>["mchid"=>$this->config["MCHID"]],
        ]);
        return $result;
    }
    
    /**
     * 回调验签
     * @param type $inBody
     * @return bool
     */
    public function notifyCheckSign($inBody){
        $apiv2Key = $this->config["ApiV2Key"];// 在商户平台上设置的APIv2密钥
        $inBodyArray = Transformer::toArray($inBody);
        $signType = empty($inBodyArray["sign_type"]) ? Hash::ALGO_MD5 : $inBodyArray["sign_type"];
        $sign = $inBodyArray["sign"];
        $calculated = Hash::sign(
            $signType,// 如没获取到`sign_type`，假定默认为`MD5`
            Formatter::queryStringLike(Formatter::ksort($inBodyArray)),
            $apiv2Key
        );
        $signatureStatus = Hash::equals($calculated, $sign);
        if ($signatureStatus) {
            // 如需要解密的
            $inBodyReqInfoArray = null;
            if(!empty($inBodyArray['req_info'])){
                $reqInfo = $inBodyArray['req_info'];
                $inBodyReqInfoXml = AesEcb::decrypt($reqInfo, Hash::md5($apiv2Key));
                $inBodyReqInfoArray = Transformer::toArray($inBodyReqInfoXml);
            }
            return [
                "data"=>$inBodyArray,//消息体
                "info"=>$inBodyReqInfoArray//解密数据
            ];
        }
        return $signatureStatus;
    }
    

    /**
     * http提交[同步请求]
     * @param type $type
     * @param type $url 示例:v3/pay/transactions/native
     * @param type $json
     * @return boolean
     */
    private function clientHttp($type='POST',$url='',$json=[]){
        try {
            $resp=null;
            if($type=="POST"){
                $resp = $this->client->chain($url)->post(['xml' => $json]);
                if(empty($json)){
                    $resp = $this->client->chain($url)->post();
                }else{
                    $resp = $this->client->chain($url)->post(['xml' => $json]);
                }
            }
            if($type=="GET"){
                if(empty($json)){
                    $resp = $this->client->chain($url)->get();
                }else{
                    $resp = $this->client->chain($url)->get($json);
                }
            }
            if(empty($resp)){
                $this->errorMsg="提交方式错误";
                return false;
            }
            $statusCode =  $resp->getStatusCode();
            if ($statusCode == 200) { //处理成功
                return $resp->getBody()->getContents();
            } else if ($statusCode == 204) { //处理成功，无返回Body
                $this->errorMsg = "处理成功，无返回Body";
                return false;
            }else{
                $this->errorMsg = "未知错误";
                return false;
            }
        } catch (\Exception $e) {
//            var_dump($e->getMessage());
            // 进行错误处理
            $this->errorMsg =  $e->getMessage();
            if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
                $r = $e->getResponse();
                $this->errorMsg=$r->getStatusCode()."".$r->getReasonPhrase().$r->getBody();
            }
            return false;
        }
    }
}
 

