微信支付-企业付款到零钱

先贴官网文档说明文档:

https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1

 

核心代码:

注意签名部分key是商户平台api秘钥,32位

    //提现到零钱
    public function transfers()
    {
        $conf       = $this->config;
        $order_code = date('YmdHis') . mt_rand(1000, 9999);

        $param         = [
            'mch_appid'        => $conf['appid'],      //商户账号appid,小程序或公众号appid,绑定商户平台的
            'mchid'            => $conf['merchantId'], //商户号
            'nonce_str'        => substr(md5(time()), 5, 10),
            //'sign'           => '',
            'partner_trade_no' => $order_code,//商户订单号
            'openid'           => $openid,
            'check_name'       => 'NO_CHECK', //NO_CHECK:不校验真实姓名 ; FORCE_CHECK:强校验真实姓名
            // 're_user_name'  => '', //收款用户真实姓名,上面参数FORCE_CHECK必填
            'amount'           => '2', //单位为分
            'desc'             => '提现一笔',
            //'spbill_create_ip' => '',
        ];
        $param['sign'] = $this->_makeSign($param);
        $xmldata       = $this->array2xml($param);

        $option = [
            CURLOPT_SSLCERT => $conf['merchantCertFile'],
            CURLOPT_SSLKEY  => $conf['merchantPrivateKeyFile'],
//            'CURLOPT_CAINFO' => '' //api_ca
        ];
        $resxml      = \app\common\service\HttpClient::post('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $xmldata, $option);
        $rdata       = simplexml_load_string($resxml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $return_code = (string)$rdata->return_code;
        $result_code = (string)$rdata->result_code;
        $return_code = trim(strtoupper($return_code));
        $result_code = trim(strtoupper($result_code));
        print_r($rdata);
        exit;
    }

    public function _makeSign($data)
    {
        ksort($data);
        $conf = $this->config;
        $str  = '';
        foreach ($data as $k => $v) {
            $str .= $k . '=' . $v . '&';
        }
        $str .= 'key=' . $conf['api_secret'];
        return md5($str);
    }

    private function array2xml($data)
    {
        $str = '<xml>';
        foreach ($data as $k => $v) {
            $str .= '<' . $k . '>' . $v . '</' . $k . '>';
        }
        $str .= '</xml>';
        return $str;
    }

    private function xml2array($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val       = json_decode(json_encode($xmlstring), true);
        return $val;
    }