微信商户向用户打款

微信商户向用户打款

商户打款到用户零钱:


$amount=价格;
$re_openid=openid;
$desc=描述;
$check_name=姓名;
$APPID=appid;
$MCHID =商户号;
$SECRECT_KEY=支付密钥;
$IP=IP地址;
//支付        
$responseObj= $this->sendMoney($id,$amount,$re_openid,$desc,$check_name,$APPID,$MCHID,$SECRECT_KEY,$IP,$_W['uniacid']);
if($responseObj['return_code']=='SUCCESS' && $responseObj['result_code']=='SUCCESS'){
        $update=array('payment_no'=>$responseObj['payment_no'],'status'=>4,'paytime'=>date("Y-m-d H:i:s",time()));
        pdo_update('ims_ewei_shop_reward',$update, array('id' =>$id,'uniacid'=>$uniacid));
        $responseObj['warning']="提现成功";
        $st=1;
}else{
        $update=array('status'=>5,'paytime'=>date("Y-m-d H:i:s",time()));
        pdo_update('ims_ewei_shop_reward',$update, array('id' =>$id,'uniacid'=>$uniacid));
        $responseObj['warning']=$responseObj['return_msg'];    
        $st=0;
}
return $responseObj;

商户付款到零钱

function sendMoney($id,$amount,$re_openid,$desc='达标奖提现',$check_name='基地三七',$APPID,$MCHID,$SECRECT_KEY,$IP,$uniacid){
          $total_amount = (100) * $amount;
          $data=array(
           'mch_appid'=>$APPID,//商户账号appid
           'mchid'=> $MCHID,//商户号
           'nonce_str'=>$this->createNoncestr(),//随机字符串
           'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
           'openid'=> $re_openid,//用户openid
           'check_name'=>'NO_CHECK',//校验用户姓名选项,
           're_user_name'=> $check_name,//收款用户姓名
           'amount'=>$total_amount,//金额
           'desc'=> $desc,//企业付款描述信息
           'spbill_create_ip'=> $IP,//Ip地址
          );

          //生成签名算法
          $secrect_key=$SECRECT_KEY;///这个就是个API密码。MD5 32位。

          $data=array_filter($data);

          ksort($data);
          $str='';
          foreach($data as $k=>$v) {
           $str.=$k.'='.$v.'&';
          }
          $str.='key='.$secrect_key;
          $data['sign']=md5($str);
          //生成签名算法
          $xml=$this->arraytoxml($data);
          $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
          $res=$this->curl_post_ssl($url,$xml);
          $return=$this->xmltoarray($res);

          //返回来的结果是xml,最后转换成数组
          $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);

          return json_decode(json_encode($responseObj),true);
          // $res= $responseObj->return_code; //SUCCESS 如果返回来SUCCESS,则发生成功,处理自己的逻辑
          // echo json_encode($res);
     }

生成随机数

    function createNoncestr($length =32){
                $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789"; 
                $str ="";

                for($i=0;$i<$length;$i++){ 
                 $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); 
                } 
                return $str;
     }

数组转xml

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

xml转数组

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

发送curl_post数据

function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
                $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 $data;
                } else {
                 $error = curl_errno($ch);
                    curl_close($ch);
                 return false;
                }
}

扫描下方二维码,关注本人公众号: filefile

猜你喜欢