微信商户企业提现到个人零钱

   微信企业接口文档返回信息
https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2



public function createNoncestr($length =32)
   {

      $chars = "abcdefghijklmnopqrstuvwxyz0123456789";

      $str ="";

      for ( $i = 0; $i < $length; $i++ )  {

         $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);

      }
      return $str;
   }


   public function unicode() {
      $str = uniqid(mt_rand(),1);
      $str=sha1($str);
      return md5($str);
   }
   public function arraytoxml($data){
      $str='<xml>';
      foreach($data as $k=>$v) {
         $str.='<'.$k.'>'.$v.'</'.$k.'>';
      }
      $str.='</xml>';
      return $str;
   }
   public 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;
   }

  
    public function curl($param="",$url) {
        header('content-type:text/html;charset=utf-8');
        $path = '';

//    if (is_array($postData)) {
//       $postData = http_build_query($postData);
//    }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //第一种方法,cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
//        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
//        curl_setopt($ch,CURLOPT_SSLCERT,"./cert/apiclient_cert.pem");
//
//        //默认格式为PEM,可以注释
//        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
//        curl_setopt($ch,CURLOPT_SSLKEY,"./cert/apiclient_key.pem");
        curl_setopt($ch,CURLOPT_SSLCERT,'./apiclient_cert.pem'); //这个是证书的位置绝对路径
        curl_setopt($ch,CURLOPT_SSLKEY,'./apiclient_key.pem'); //

        //第二种方式,两个文件合成一个.pem文件
//        curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
        $data = curl_exec($ch);
//    print_r($data);die;
        if($data === false)
        {

            echo 'Curl error: ' . curl_error($ch);exit();
        }
        curl_close($ch);
        return $data;
    }



   /*
    $amount 发送的金额(分)目前发送金额不能少于1元
    $re_openid, 发送人的 openid
    $desc  //  企业付款描述信息 (必填)
    $check_name    收款用户姓名 (选填)
    */


//$ress=$this->sendMoney($batch_fee,$arrData['cash_no'],$openid,$user_id,$weicon['appid'],$weicon['secret'],$weicon['mch_id'],$weicon['paySignKey']);
   public function sendMoney($amount,$order_sn,$openid,$user_id,$appid,$secret,$mchid,$key,$desc='测试',$check_name=''){
      $amount=1;
      $total_amount = (100) * $amount;

      $data=array(
         'mch_appid'=>$appid,//商户账号appid
         'mchid'=> $mchid,//商户号
         'nonce_str'=>$order_sn,//随机字符串
         'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
         'openid'=> $openid,//用户openid
         'check_name'=>'NO_CHECK',//校验用户姓名选项,
         're_user_name'=> $check_name,//收款用户姓名
         'amount'=>$total_amount,//金额
         'desc'=> $desc,//企业付款描述信息
         'spbill_create_ip'=> $_SERVER["REMOTE_ADDR"],//Ip地址
      );
//    print_r($key);die;
      $secrect_key=$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);

//    echo phpinfo();die;

//    print_r($xml);die;

      $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
      $res= $this->curl($xml,$url);
//    $res= $this->curl_post_ssl($url,$xml);

//    print_r($res);die;

      $return=$this->xmltoarray($res);





      //返回来的结果
      // [return_code] => SUCCESS [return_msg] => Array ( ) [mch_appid] => wxd44b890e61f72c63 [mchid] => 1493475512 [nonce_str] => 616615516 [result_code] => SUCCESS [partner_trade_no] => 20186505080216815
      // [payment_no] => 1000018361251805057502564679 [payment_time] => 2018-05-15 15:29:50


      $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);



      if($return['result_code']=='success'){

          $order_sn=$return['nonce_str'];


         $set = M('cash_log')->where(array('cash_no'=>$order_sn))->setField('status',1);

         if($set){
            $res=1;

         }else{

            $res=0;
         }
      }else{

         $res=0;
      }

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

      return $res;
   }

调用 
$ress=$this->sendMoney($batch_fee,$arrData['cash_no'],$openid,$user_id,$weicon['appid'],$weicon['secret'],$weicon['mch_id'],$weicon['paySignKey']);

猜你喜欢

转载自blog.csdn.net/qq_25861247/article/details/84103492