欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php微信扫码支付 php公众号支付

程序员文章站 2024-02-11 18:07:52
本文实例为大家分享了php微信扫码支付,公众号支付的具体代码,供大家参考,具体内容如下

本文实例为大家分享了php微信扫码支付,公众号支付的具体代码,供大家参考,具体内容如下

<?php
 
# 微信统一下单接口
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
 
 
$param = [
  'appid' => '公众号id',
  'mch_id' => '商户id',
  'nonce_str' =>uniqid(),
  'sign_type' => 'md5',
  'body' => 'test',
  'detail' => 'test detail',
  'out_trade_no' => date('ymd').rand(10000,99999),
  'total_fee' => 1,
  'notify_url' => 'http://www.test.top/testpay/pay.php',
  'trade_type' =>'jsapi',
];
 
 
ksort( $param );
 
$sign_str = urldecode(http_build_query( $param ));
 
 
$sign_str .= '&key=商户密钥';
 
 
//echo $sign_str;exit;
 
$sign_str = md5( $sign_str );
 
 
$param['sign'] = strtoupper( $sign_str );
 
function curlpost($url, $param = [], $is_post = 1, $timeout = 5 )
{
 
  //初始化curl
  $curl = curl_init();
 
  // 设置请求的路径
  curl_setopt($curl, curlopt_url, $url);
 
  if ($is_post == 1) {
    //设置post提交
    curl_setopt($curl, curlopt_post, 0);
  }
 
  //显示输出结果 1代表 把接口返回的结果当作一个字符串处理
  curl_setopt($curl, curlopt_returntransfer, 1);
 
  // 设置请求超时时间
  curl_setopt($curl, curlopt_timeout, $timeout);
 
  curl_setopt($curl, curlopt_ssl_verifyhost, false);
  curl_setopt($curl, curlopt_ssl_verifypeer, false);
 
 
  if ($is_post == 1) {
    //提交数据 -- 设置post提交的数据
    if (is_array($param)) {
 
      //http_build_query
      curl_setopt($curl, curlopt_postfields, http_build_query($param));
    } else {
      curl_setopt($curl, curlopt_postfields, $param);
    }
  }
 
  //执行请求
  $data = $data_str = curl_exec($curl);
  //处理错误
  if ($error = curl_error($curl)) {
    $log_data = array(
      'url' => $url,
      'param' => $param,
      'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
    );
 
    var_dump($log_data);
    exit;
  }
 
  # 关闭curl
  curl_close($curl);
 
 
  //json数据转换为数组
  $data = json_decode($data, true);
 
  if (!is_array($data)) {
    $data = $data_str;
  }
 
  #调用玩接口之后写一个日志
  $log = [
    'url' => $url,
    'param' => $param,
    'response' => $data_str
  ];
  file_put_contents(__dir__ . '/wechat.log', print_r($log, true), 8);
 
  return $data;
 
}
function arr2xml( $arr ){
 
  $xml = '<xml version="1.0" encoding="utf-8"> ';
  foreach( $arr as $key => $value ){
    if (is_numeric($value)){
      $xml.="<".$key.">".$value."</".$key.">";
    }else{
      $xml.="<".$key."><![cdata[".$value."]]></".$key.">";
    }
 
  }
  $xml .= '</xml>';
  return $xml;
}
 
$xml = arr2xml( $param );
 
$result = curlpost( $url , arr2xml($param) );
 
 
$api_arr = json_decode( json_encode(simplexml_load_string( $result , 'simplexmlelement', libxml_nocdata) ), true );
 
if( $api_arr['return_code'] == 'success' ){
 
  include __dir__ . '/phpqrcode.php';
  header('content-type:image/png');
  echo qrcode::png( $api_arr['code_url'] , false , 'h' , 6 ,2 );
}
<?php
 
#微信统一下单接口
$url='https://api.mch.weixin.qq.com/pay/unifiedorder';
 
$param=[
  #公众账号id
  'appid'=>'****',
  'mch_id'=>'***',
  'nonce_str'=>uniqid(),
  'sign_type'=>'md5',
  'body'=>'test',
  'detail'=>'detail',
  'out_trade_no'=>date('ymd').rand(10000,99999),
  'total_fee'=>1,
  'spbill_create_ip'=>$_server['server_addr'],
  'notify_url'=>'http://****/test.php',
  'trade_type'=>'native',
];
ksort($param);
 
$sign_str=urldecode(http_build_query($param));
 
$sign_str.='&key=8934e7d15453e97507ef794cf7b0519d';
 
$sign_str=md5($sign_str);
 
$param['sign']=strtoupper($sign_str);
 
//print_r($param);exit;
function curlpost($url, $param = [], $is_post = 1, $timeout = 5 )
{
  //初始化curl
  $curl = curl_init();
 
  // 设置请求的路径
  curl_setopt($curl, curlopt_url, $url);
 
  if ($is_post == 1) {
    //设置post提交
    curl_setopt($curl, curlopt_post, 0);
  }
 
  //显示输出结果 1代表 把接口返回的结果当作一个字符串处理
  curl_setopt($curl, curlopt_returntransfer, 1);
 
  // 设置请求超时时间
  curl_setopt($curl, curlopt_timeout, $timeout);
 
  curl_setopt($curl, curlopt_ssl_verifyhost, false);
  curl_setopt($curl, curlopt_ssl_verifypeer, false);
 
 
  if ($is_post == 1) {
    //提交数据 -- 设置post提交的数据
    if (is_array($param)) {
 
      //http_build_query
      curl_setopt($curl, curlopt_postfields, http_build_query($param));
    } else {
      curl_setopt($curl, curlopt_postfields, $param);
    }
  }
 
  //执行请求
  $data = $data_str = curl_exec($curl);
  //处理错误
  if ($error = curl_error($curl)) {
    $log_data = array(
      'url' => $url,
      'param' => $param,
      'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
    );
 
    var_dump($log_data);
    exit;
  }
 
  # 关闭curl
  curl_close($curl);
 
 
  //json数据转换为数组
  $data = json_decode($data, true);
 
  if (!is_array($data)) {
    $data = $data_str;
  }
 
  #调用玩接口之后写一个日志
  $log = [
    'url' => $url,
    'param' => $param,
    'response' => $data_str
  ];
  file_put_contents(__dir__ . '/wechat.log', print_r($log, true), 8);
 
  return $data;
 
}
 
function arrzxml($arr){
  $xml='<xml version="1.0" encoding="utf-8">';
  foreach($arr as $key=>$value){
    if(is_numeric($value)){
      $xml.="<".$key.">".$value."</".$key.">";
    }else{
      $xml.="<".$key."><![cdata[".$value."]]></".$key.">";
    }
 
  }
  $xml.='</xml>';
//  var_dump($xml);exit;
  return $xml;
 
}
 
$xml=arrzxml($param);
$result=curlpost($url,arrzxml($param));
//echo '<pre/>';
var_dump($result);
exit;
$api_arr=json_decode(simplexml_load_string($result,'simplexmlelement',libxml_nocdata),true);
if($result['return_code']=='success'){
  include __dir__.'/phpqrcode.php';
  header('content-type:image/png');
  echo qrcode::png($api_arr['code_url'],false,'h',6,2);
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。