1.以下代码修改完自己的
2.appid
3.商户号
4.商户密钥
微信支付
//微信支付
public function index(){
//接收用户下单信息
$data = [];
$data['sorts'] = input('sorts');//套餐分类
$data['sciencename']=input('sciencename');//景区名称
$data['price']=input('price');//订单价格
$data['create_time']=time();//下单时间
$data['phone']=input('phone');//用户手机号
$data['userid']=input('userid');//用户id
$data['menuid']=input('menuid');//套餐id
$data['scienceid'] = input('scienceid');//景区ID
$data['num'] = input('num');//景区ID
if(input('type')){
$data['type']=input('type');//1为音频套餐
}
$user = Db::name('user_info')->where(array('id' => $data['userid']))->field('openid')->find();//获取当前用户openID
//发起微信支付,调用统一下单支付接口
$fee = $data['price'];//支付金额
$appid = '用到的appid';//appid.如果是公众号 就是公众号的appid
$body = $data['sorts'];
$mch_id = '用到的商户号'; //商户号
$nonce_str = $this->nonce_str();//随机字符串
$notify_url = 'https://ht.hongtuzhijian.top/api/order/notifyurl'; //回调的url
$openid = $user['openid'];
$time = time();
$year = date('Y',$time);
$rand = rand(000000000,999999999);
$out_trade_no = $year.$rand;//订单号
//$out_trade_no = $this->order_number($openid);//商户订单号
$spbill_create_ip = $_SERVER["REMOTE_ADDR"];//服务器的ip;
$total_fee = $fee*100;// 微信支付单位是分,所以这里需要*100
$trade_type = 'JSAPI';//交易类型 默认
$data['order_no']=$out_trade_no;
//echo json_encode($data);exit;
$order = Db::name('user_order')->insert($data);
//这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
$post['appid'] = $appid;
$post['body'] = $body;
$post['mch_id'] = $mch_id;
$post['nonce_str'] = $nonce_str;//随机字符串
$post['notify_url'] = $notify_url;
$post['openid'] = $openid;
$post['out_trade_no'] = $out_trade_no;
$post['spbill_create_ip'] = $spbill_create_ip;//用户终端的ip
$post['total_fee'] = $total_fee;//总金额
$post['trade_type'] = $trade_type;
$sign = $this