美文网首页
微信支付

微信支付

作者: 康荨歌歌歌歌 | 来源:发表于2017-11-25 18:35 被阅读0次

    /**

    * 生成app支付订单

    *

    *@accesspublic

    *@paramstring      $total_amount

    *@paramstring      $out_trade_no

    *@returnstring

    */

    public functionbuild_order($total_amount,$out_trade_no,$body='支付')

    {

    $data=array();

    $data['appid'] =$this->user_config['appid'];

    $data['mch_id'] =$this->user_config['mch_id'];

    $data['nonce_str'] = MD5(time().rand(10000,99999));

    $data['body'] =$body;

    $data['out_trade_no'] =$out_trade_no;

    $data['total_fee'] =$total_amount;

    $data['spbill_create_ip'] = getIP();

    $data['notify_url'] =$this->user_config['notify_url'];

    $data['trade_type'] ='APP';

    ksort($data);

    $data['sign'] = getSign($data,$this->user_config['api']);

    $xml= arrayToXml($data);

    $response= postXmlCurl($xml);

    $res= xmlstr_to_array($response);

    $prepay_id=$res['prepay_id'];

    $payData= payData($prepay_id,$this->user_config);

    return$payData;

    }

    functionpostXmlCurl($xml,$url='https://api.mch.weixin.qq.com/pay/unifiedorder',$second=30)

    {

    //初始化curl

    $ch=curl_init();

    //超时时间

    curl_setopt($ch,CURLOPT_TIMEOUT,$second);

    //这里设置代理,如果有的话

    //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');

    //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);

    curl_setopt($ch,CURLOPT_URL,$url);

    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

    //设置header

    curl_setopt($ch,CURLOPT_HEADER,FALSE);

    //要求结果为字符串且输出到屏幕上

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

    //post提交方式

    curl_setopt($ch,CURLOPT_POST,TRUE);

    curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);

    //运行curl

    $data=curl_exec($ch);

    //返回结果

    if($data)

    {

    curl_close($ch);

    return$data;

    }

    else

    {

    $error=curl_errno($ch);

    echo"curl出错,错误码:$error"."
    ";

    echo"错误原因查询
    ";

    curl_close($ch);

    return false;

    }

    }

    相关文章

      网友评论

          本文标题:微信支付

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