app微信 下订单
publicfunctionindex(){$order= ['appid'=>'123456','mch_id'=>'56789','nonce_str'=>uniqid(md5(time())),'body'=>'测试商品','out_trade_no'=>87654321,//商户唯一订单号,可包含字母序'total_fee'=>100,//订单金额,单位/分'spbill_create_ip'=>'123.12.12.123',//产生订单号的服务器IP'notify_url'=>'http://www.yoursite.com/wxpay',//接受微信异步通知地址'trade_type'=>'APP'];//用户http_build_query()将数据转成URL键值对形式$sign= http_build_query($order);//$sign = "appid=123456&mch_id=56789&nonce_str=c6079b98e6aeb4a98f687800c887f6cc58df95d72cd69&body=%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%81&out_trade_no=87654321&total_fee=100&spbill_create_ip=123.12.12.123¬ify_url=http%3A%2F%2Fwww.yoursite.com%2Fwxpay&trade_type=APP";//MD5处理,默认支持MD5$sign= md5($sign);//转大写$sign= strtoupper($sign);$order['sign'] =$sign;//转换成一维XML格式$xml='';foreach($orderas$k=>$v){$xml.='<'.$k.'>'; }$xml.='';//CURL会话$ch= curl_init();// 设置curl允许执行的最长秒数curl_setopt($ch, CURLOPT_TIMEOUT,3); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);// 获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//发送一个常规的POST请求。curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_URL,'https://api.mch.weixin.qq.com/pay/unifiedorder');//要传送的所有数据curl_setopt($ch, CURLOPT_POSTFIELDS,$order);// 执行操作$response= curl_exec($ch);//将xml格式的$response 转成数组$response= json_decode( json_encode( simplexml_load_string($response,'SimpleXMLElement', LIBXML_NOCDATA) ),true);//若预下单成功,return_code 和result_code为SUCCESS。if($response['return_code'] ==='SUCCESS'&&$response['result_code'] ==='SUCCESS') {//返回trade_type和prepay_id供前端调用echojson_encode( ['trade_type'=>$response['trade_type'],'prepay_id'=>$response['prepay_id']] ); } }