根据需要,在小程序传参
//微信支付
public function Wx_Pay(){
$order = M('Orders')->where(array('order_id'=>I('post.order_id')))->find();//订单号
$papers = M('paperouts')->where(array('paper_id'=>I('post.paper_id')))->find();
// $request=Request::instance();
// $fee=$request->param('fee');
// $details=$request->param('details');//商品的详情,比如iPhone8,紫色
// $goods = M('paperouts')
$details=$papers['paper_name'].$papers['paper_price'];
// $fee = 0.01;//举例充值0.01
$fee = $order['order_price'];//订单金额
$appid = 'appid';//appid
$body = 'LIFE';//名称'【自己填写】'
$mch_id = '123456';//'你的商户号【自己填写】'
$nonce_str = $this->nonce_str();//随机字符串
$notify_url = 'https://自己的域名/calabck';//回调的url【自己填写】';
$openid = I('post.openid');//'用户的openid【自己填写】';
$out_trade_no = $order['order_sn'];//订单号
$spbill_create_ip = $_SERVER["REMOTE_ADDR"];//'服务器的ip【自己填写】';
$total_fee = $fee*100;//因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
$trade_type = 'JSAPI';//交易类型 默认
//这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
$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->sign($post);//签名
$post_xml = '
'.$body.'
//统一接口prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$xml = $this->http_request($url,$post_xml);
$array = $this->xml($xml);//全要大写
if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
$time = time();
$tmp='';//临时数组用于签名
$tmp['appId'] = $appid;
$tmp['nonceStr'] = $nonce_str;
$tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
$tmp['signType'] = 'MD5';
$tmp['timeStamp'] = "".$time."";
$data['state'] = 1;
$data['timeStamp'] = "".$time."";//时间戳
$data['nonceStr'] = $nonce_str;//随机字符串
$data['signType'] = 'MD5';//签名算法,暂支持 MD5
$data['package'] = 'prepay_id='.$array['PREPAY_ID'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
$data['paySign'] = $this->sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
$data['out_trade_no'] = $out_trade_no;
}else{
$data['state'] = 0;
$data['text'] = "错误";
$data['RETURN_CODE'] = $array['RETURN_CODE'];
$data['RETURN_MSG'] = $array['RETURN_MSG'];
}
echo json_encode($data);
}
//随机32位字符串
private function nonce_str(){
$result = '';
$str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz0123456789';
for ($i=0;$i<32;$i++){
$result .= $str[rand(0,48)];
}
return $result;
}
//生成订单号
private function order_number($openid){
//date('Ymd',time()).time().rand(10,99);//18位
return md5($openid.time().rand(10,99));//32位
}
//签名 $data要先排好顺序
public function sign($data)
{
$stringA = '';
foreach ($data as $key => $value) {
if (!$value) continue;
if ($stringA) $stringA .= '&' . $key . "=" . $value;
else $stringA = $key . "=" . $value;
}
$wx_key = '商户号的key';//申请支付后有给予一个商户账号和密码,登陆后自己设置key
$stringSignTemp = $stringA . '&key=' . $wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
return strtoupper(md5($stringSignTemp));
}
//curl请求啊
function http_request($url, $data = null, $headers = array())
{
$curl = curl_init();
if (count($headers) >= 1) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
//获取xml
private function xml($xml){
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
$data = "";
foreach ($index as $key=>$value) {
if($key == 'xml' || $key == 'XML') continue;
$tag = $vals[$value[0]]['tag'];
$value = $vals[$value[0]]['value'];
$data[$tag] = $value;
}
return $data;
}
//回调部分
public function calabck()
{
$xml = file_get_contents("php://input");//获取xml文件
$log = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$order_sn1['out_trade_no'] = $log['out_trade_no']; //订单号
$order_sn1['appid'] = $log['appid']; //订单号
$order_sn1['mch_id'] = $log['mch_id']; //订单号
$order_sn1['openid'] = $log['openid']; //订单号
$order_sn1['result_code'] = $log['result_code']; //订单号
$order_sn1['time_end'] = $log['time_end']; //订单号
// $data = json_encode($order_sn1); //接到get请求
// $file = 'log.txt';//要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个
// $content =date("Y/m/d h:i:sa").' 小程序code= '.$data."\r\n";
// if($f = file_put_contents($file, $content,FILE_APPEND)){// 这个函数支持版本(PHP 5)
// echo "写入成功。
";
// }
$orders = M('Orders')->where(array('order_sn'=>$order_sn1['out_trade_no']))->find();
if ($orders['order_status'] == 0) {
if ($log['result_code'] == "SUCCESS") {
//修改订单status
$success = $this->upds_order_status($order_sn1['out_trade_no']);
//调用接口
$curl = $this->curl_form($order_sn1['out_trade_no']);
}
}
//下面不可以去掉
$return = "
echo $return;
}
/*b表单提交数据*/(这是我自己需要发送请求的函数)
function curl_form($order_sn){
header("Content-Type: application/json;charset=utf-8");
$order = '查询数据';
$url = '接口地址';
//初始化
$postData='需要请求的数据';
//初始化一个curl会话
$ch = curl_init();
//设置相应的会话选项
// 1.设置表单提交地址
curl_setopt($ch,CURLOPT_URL,$url);
// 2.设置数据的提交方式
curl_setopt($ch,CURLOPT_POST,1);
// 3.设置提交的表单数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
// 4.提交成功后,返回数据为字符串
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//执行会话
$output = curl_exec($ch);
//关闭会话
curl_close($ch);
}