美文网首页
禾匠V4.4.8零钱打款修改为商户打款-记录

禾匠V4.4.8零钱打款修改为商户打款-记录

作者: 晓天菌 | 来源:发表于2022-08-22 23:00 被阅读0次

代码路径
vendor/luweiss/wechat/src/Wechat/WechatPay.php
注意php 7以上的json_encode 溢出

<?php

namespace luweiss\Wechat;


class WechatPay extends WechatBase
{
    const SIGN_TYPE_MD5 = 'MD5';
    const TRADE_TYPE_JSAPI = 'JSAPI';
    const TRADE_TYPE_NATIVE = 'NATIVE';
    const TRADE_TYPE_APP = 'APP';
    const TRADE_TYPE_MWEB = 'MWEB';

    public $appId;
    public $mchId;
    public $key;
    public $certPemFile;
    public $keyPemFile;

    /**
     * WechatPay constructor.
     * @param array $config ['appId', 'mchId', 'key', 'certPemFile', 'keyPemFile']
     */
    public function __construct($config = [])
    {
        foreach ($config as $name => $value) {
            if (property_exists($this, $name)) {
                $this->$name = $value;
            }
        }
    }

    /**
     * @param array $result
     * @return array
     * @throws WechatException
     */
    protected function getClientResult($result)
    {
        if (!isset($result['return_code'])) {
            throw new WechatException(
                '返回数据格式不正确: ' . json_encode($result, JSON_UNESCAPED_UNICODE)
            );
        }
        if ($result['return_code'] !== 'SUCCESS') {
            $msg = 'returnCode: ' . $result['return_code'] . ', returnMsg: ' . $result['return_msg'];
            throw new WechatException($msg, 0, null, $result);
        }
        if (!isset($result['result_code'])) {
            throw new WechatException(
                '返回数据格式不正确: ' . json_encode($result, JSON_UNESCAPED_UNICODE)
            );
        }
        if ($result['result_code'] !== 'SUCCESS') {
            $msg = 'errCode: ' . $result['err_code'] . ', errCodeDes: ' . $result['err_code_des'];
            throw new WechatException($msg, 0, null, $result);
        }
        return $result;
    }

    /**
     * @param $api
     * @param $args
     * @return array
     * @throws WechatException
     */
    protected function send($api, $args)
    {
        $args['appid'] = !empty($args['appid']) ? $args['appid'] : $this->appId;
        $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
        $args['nonce_str'] = !empty($args['nonce_str']) ? $args['nonce_str'] : md5(uniqid());
        $args['sign'] = $this->makeSign($args);
        $xml = WechatHelper::arrayToXml($args);
        $res = $this->getClient()->setDataType(WechatHttpClient::DATA_TYPE_XML)->post($api, $xml);
        return $this->getClientResult($res);
    }

    /**
     * @param $api
     * @param $args
     * @return array
     * @throws WechatException
     */
    protected function sendWithPem($api, $args)
    {
        $args['nonce_str'] = !empty($args['nonce_str']) ? $args['nonce_str'] : md5(uniqid());
        $args['sign'] = $this->makeSign($args);
        $xml = WechatHelper::arrayToXml($args);
        $res = $this->getClient()
            ->setDataType(WechatHttpClient::DATA_TYPE_XML)
            ->setCertPemFile($this->certPemFile)
            ->setKeyPemFile($this->keyPemFile)
            ->post($api, $xml);
        return $this->getClientResult($res);
    }

    /**
     *
     * 统一下单, <a href="https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1">
     * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1</a>
     *
     * @param array $args ['body', 'out_trade_no', 'total_fee', 'notify_url', 'trade_type', 'openid']
     * @return array
     * @throws WechatException
     */
    public function unifiedOrder($args)
    {
        $args['spbill_create_ip'] = !empty($args['spbill_create_ip']) ? $args['spbill_create_ip'] : '127.0.0.1';
        $api = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        return $this->send($api, $args);
    }

    /**
     *
     * 查询订单, <a href="https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_2">
     * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_2</a>
     *
     * @param array $args ['out_trade_no']
     * @return array
     * @throws WechatException
     */
    public function orderQuery($args)
    {
        $api = 'https://api.mch.weixin.qq.com/pay/orderquery';
        return $this->send($api, $args);
    }

    /**
     *
     * 关闭订单, <a href="https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_3">
     * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_3</a>
     *
     * @param array $args
     * @return array
     * @throws WechatException
     */
    public function closeOrder($args)
    {
        $api = 'https://api.mch.weixin.qq.com/pay/closeorder';
        return $this->send($api, $args);
    }

    /**
     *
     * 申请退款, <a href="https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_4">
     * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_4</a>
     *
     * @param array $args
     * @return array
     * @throws WechatException
     */
    public function refund($args)
    {
        $args['appid'] = !empty($args['appid']) ? $args['appid'] : $this->appId;
        $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
        $api = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
        return $this->sendWithPem($api, $args);
    }

    /**
     *
     * 查询退款, <a href="https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_5">
     * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_5</a>
     *
     * @param array $args
     * @return array
     * @throws WechatException
     */
    public function refundQuery($args)
    {
        $api = 'https://api.mch.weixin.qq.com/pay/refundquery';
        return $this->send($api, $args);
    }

    /**
     *
     * 企业付款, <a href="https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2">
     * https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2</a>
     *
     * @param array $args ['partner_trade_no', 'openid', 'amount', 'desc']
     * @return array
     * @throws WechatException
     */
    public function transfers($args)
    {
        $arg = [];
        $arg['out_batch_no'] = $args['out_detail_no'];
        $arg['batch_name'] = date("Y-m-d H:i:s").$args['transfer_remark'];
        $arg['appid'] = !empty($args['mch_appid']) ? $args['mch_appid'] : $this->appId;
        $arg['batch_remark'] = date("Y-m-d H:i:s").$args['transfer_remark'];
        $arg['total_amount'] = intval($args['transfer_amount']);
        $arg['total_num'] = 1;
        $arg['transfer_detail_list'][]=$args;
        $api = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
        $token  = $this->getToken($arg);//获取token
        return $this->https_request($api, json_encode($arg),$token);
    }
    
    protected function sendBatches($api, $arg)
    {
        $res = $this->getClient()
            ->setDataType(WechatHttpClient::DATA_TYPE_JSON)
            ->setCertPemFile($this->certPemFile)
            ->setKeyPemFile($this->keyPemFile)
            ->post($api, $arg);
        return $this->getClientResult($res);
    }
  public  function https_request($url,$data = null,$token){
      try {
          $curl = curl_init();
          curl_setopt($curl, CURLOPT_URL, (string)$url);
          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
          if (!empty($data)) {
              curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
          }
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
          //添加请求头
          $headers = [
              'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $token,
              'Accept: application/json',
              'Content-Type: application/json; charset=utf-8',
              'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
          ];
          if (!empty($headers)) {
              curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
          }
          $output = curl_exec($curl);
          curl_close($curl);
          $data = json_decode($output,true);
          if($data['code']!=200&&isset($data['code'])){
              $data['err_code_des'] = $data['message'];
              throw new WechatException($data['message'], 0, null, $data);
          }
          return $data;
      }catch (WechatException $exception) {
          throw $exception;
      }
    }
    public function getToken($pars)
    {
        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
        $http_method = 'POST';//请求方法(GET,POST,PUT)
        $timestamp   = time();//请求时间戳
        $url_parts   = parse_url($url);//获取请求的绝对URL
        $nonce       = $timestamp.rand('10000','99999');//请求随机串
        $body        = json_encode((object)$pars);//请求报文主体
        $stream_opts = [
            "ssl" => [
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ]
        ];

        $apiclient_cert_path = $this->certPemFile;
        $apiclient_key_path  = $this->keyPemFile;

        $apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path,false, stream_context_create($stream_opts)));
        $serial_no          = $apiclient_cert_arr['serialNumberHex'];//证书序列号
        $mch_private_key    = file_get_contents($apiclient_key_path,false, stream_context_create($stream_opts));//密钥
        $merchant_id = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;//商户id
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $message = $http_method."\n".
            $canonical_url."\n".
            $timestamp."\n".
            $nonce."\n".
            $body."\n";
        openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
        $sign = base64_encode($raw_sign);//签名
        $schema = 'WECHATPAY2-SHA256-RSA2048';
        $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
            $merchant_id, $nonce, $timestamp, $serial_no, $sign);//微信返回token
        return $token;
    }

//    public function transfers($args)
//    {
//        $args['mch_appid'] = !empty($args['mch_appid']) ? $args['mch_appid'] : $this->appId;
//        $args['mchid'] = !empty($args['mchid']) ? $args['mchid'] : $this->mchId;
//        $args['spbill_create_ip'] = !empty($args['spbill_create_ip']) ? $args['spbill_create_ip'] : '127.0.0.1';
//        $args['check_name'] = !empty($args['check_name']) ? $args['check_name'] : 'NO_CHECK';
//        $api = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
//        return $this->sendWithPem($api, $args);
//    }

    /**
     *
     * 查询企业付款, <a href="https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3">
     * https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3</a>
     *
     * @param array $args ['partner_trade_no']
     * @return array
     * @throws WechatException
     */
    public function getTransferInfo($args)
    {
        $args['appid'] = !empty($args['appid']) ? $args['appid'] : $this->appId;
        $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
        $api = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo';
        return $this->sendWithPem($api, $args);
    }

    /**
     *
     * 企业付款到银行卡, <a href="https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_2">
     * https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_2</a>
     *
     * @param array $args
     * @return array
     * @throws WechatException
     */
    public function payBank($args)
    {
        $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
        $api = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank';
        return $this->sendWithPem($api, $args);
    }

    /**
     *
     * 查询企业付款到银行卡, <a href="https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_3">
     * https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_3</a>
     *
     * @param array $args
     * @return array
     * @throws WechatException
     */
    public function queryBank($args)
    {
        $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
        $api = 'https://api.mch.weixin.qq.com/mmpaysptrans/query_bank';
        return $this->sendWithPem($api, $args);
    }

    /**
     * 通过数组数据验证签名
     * @param array $array
     * @return bool
     */
    public function validateSignByArrayResult($array)
    {
        if (!isset($array['sign'])) {
            return false;
        }
        $inputSign = $array['sign'];
        $truthSign = $this->makeSign($array);
        return $inputSign === $truthSign;
    }

    /**
     * 通过XML数据验证签名
     * @param string $xml
     * @return bool
     */
    public function validateSignByXmlResult($xml)
    {
        $array = WechatHelper::xmlToArray($xml);
        return $this->validateSignByArrayResult($array);
    }

    /**
     * 数据签名
     * @param array $args
     * @param string $signType
     * @return string
     */
    public function makeSign($args, $signType = self::SIGN_TYPE_MD5)
    {
        if (isset($args['sign'])) {
            unset($args['sign']);
        }
        ksort($args);
        $string = '';
        foreach ($args as $i => $arg) {
            if ($arg === null || $arg === '') {
                continue;
            } else {
                $string .= ($i . '=' . $arg . '&');
            }
        }
        $string = $string . "key={$this->key}";
        $string = md5($string);
        $result = strtoupper($string);
        return $result;
    }
}

相关文章

  • 禾匠V4.4.8零钱打款修改为商户打款-记录

    代码路径vendor/luweiss/wechat/src/Wechat/WechatPay.php注意php 7...

  • 如何用微信公众号给指定用户发红包/打款?

    公众号直接给关注用户发红包或打款,公众号必须开通“微信支付”的功能,发放红包/打款操作是在该公众号的微信商户平台上...

  • 打款

    聋子是个非常仗义的人。外号大傻子! 他人长得瘦小枯干。164厘米的身高,在北方男人中就是一个矮个子。体重86斤,哪...

  • 打款

    又到月底,要发工资了,这才想起要给孩子打点生活费过去。想到这,闭目躺了一会,揉了揉不愿睁开的双眼,还是起了床来。 ...

  • 打款

    大公司都是年底打款。最近我手机收了几笔到款信息,都是大公司打过来的。因为我为他们提供过培训或评审服务。当然,这些钱...

  • 打款

    转眼又是月底,又该给各个账户打款了。 昨天晚上算了预算,很不幸,这个月的月度开支又增加了。 主要增加的是两项开支:...

  • 提现微信打款 NO_AUTH 产品权限验证失败 请查看您当前是否

    如果希望通过你的微信商户直接打款给用户,你必须要开通 “企业付款” 功能; 开通方法: 登录微信支付商户后台 ht...

  • 爱在心里,不在嘴上。 我们不联系,她却时刻惦记。 一条信息 她可能会想到好多种可能, 打款记录只有一条, 打款前的...

  • apple打款

    公司开发者账号,收款账户需开通对公账户,并填写对公账户英文名称。

  • 银行大额跨行打款流程与异常处理

    之前项目对接农行的银企直联,发现提交打款信息后接口中会实时返回“打款成功”和“打款失败”。于是就把系统内的支付流程...

网友评论

      本文标题:禾匠V4.4.8零钱打款修改为商户打款-记录

      本文链接:https://www.haomeiwen.com/subject/jbhngrtx.html