WeChat enterprise payment to change cash withdrawal

Official document: https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1
The key parameters are as shown in the figure:
insert image description here
the certificate is as shown in the figure:
insert image description here
the certificate path should correspond to the path in the code, put In the root directory, it is best to save it with cert or other named folders (I found that it is effective to put it in a folder in practice), as shown in the figure
insert image description here
. Generate your own signature 3. Put the signature in the map collection [because the signature must also be passed, see the API] 4. Combine the current map and convert it into xml format 5. Send the request to the API of the enterprise to pay the change. Sending a request is a method of POST 6. Parsing the returned xml data === "map collection 7. Judging whether it is successful or not according to the result_code AND return_code in the map






upper code

/**
     * 企业支付(向微信发起企业支付到零钱的请求)
     * @param string $openid 用户openID
     * @param string $trade_no 单号
     * @param string $money 金额(单位分)
     * @param string $desc 描述
     * @param string $appid 协会appid
     * @return string   XML 结构的字符串
     **/
    function txFunc($openid,$trade_no,$money,$desc,$appid)
    {
    
    
        $data = array(
            'mch_appid' =>$appid,//协会appid
            'mchid' => '',//微信支付商户号
            'nonce_str' => getNonceStr(), //随机字符串
            'partner_trade_no' => $trade_no, //商户订单号,需要唯一
            'openid' => $openid,
            'check_name' => 'NO_CHECK', //OPTION_CHECK不强制校验真实姓名, FORCE_CHECK:强制 NO_CHECK:
            'amount' => $money * 100, //付款金额单位为分
            'desc' => $desc,
            'spbill_create_ip' => get_client_ip(),
            //'re_user_name' => 'jorsh', //收款人用户姓名 *选填
            //'device_info' => '1000',  //设备号 *选填
        );
        //生成签名
        $secrect_key = '';//API密码
        $data = array_filter($data);
        ksort($data);
        $str ='';
        foreach($data as $k=>$v) {
    
    
            $str.=$k.'='.$v.'&';
        }
        $str.='key='.$secrect_key;
        $data['sign'] = md5($str);

        //构造XML数据(数据包要以xml格式进行发送)
        $xmldata = arrToXml($data);
        //请求url
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        //发送post请求
        $res = curl_post_ssl($url,$xmldata);
        return $res;
    }


	/**
	 * 数组转XML
	 * @param $data
	 * @return string
	 */
	function arrToXml($data)
	{
    
    
	    $xml = "<xml>";
	    //  遍历组合
	    foreach ($data as $k=>$v){
    
    
	        $xml.='<'.$k.'>'.$v.'</'.$k.'>';
	    }
	    $xml .= '</xml>';
	    return $xml;
	}


/**
 * [curl_post_ssl 发送curl_post数据]
 * @param  [type]  $url     [发送地址]
 * @param  [type]  $xmldata [发送文件格式]
 * @param  [type]  $second [设置执行最长秒数]
 * @param  [type]  $aHeader [设置头部]
 * @return [type]           [description]
 */
function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
    
    
    $isdir = $_SERVER['DOCUMENT_ROOT'].'/cert/';//证书位置;绝对路径
//    return $isdir;
    $ch = curl_init();//初始化curl

    curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
    curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
    curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
    curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
    curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
    curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
    if (count($aHeader) >= 1) {
    
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
    }
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部数据使用HTTP协议中的"POST"操作来发送


    $data = curl_exec($ch);//执行回话

    if ($data) {
    
    
        curl_close($ch);
        return xmlToArray($data);
    } else {
    
    
        $error = curl_errno($ch);
        echo "call faild, errorCode:$error\n";
        curl_close($ch);
        return false;
    }
}

callback result
insert image description here

Guess you like

Origin blog.csdn.net/zax_96/article/details/109160184