PHP小程序后端支付代码亲测可用
小程序后端支付代码亲测可用
<?php
namespace Home\Controller;
use Think\Controller; class WechatpayController extends Controller
{
//微信支付
public function pay()
{
//参数定义
$appid='111111';
$appsecret='1111111';
$mch_id='1111111';
$notify_url='https://www.123.com/index.php/Home/Wechatpay/wechatNotify/'; //获取openid
/*if(I("post.userid"))
{
$infos=M('weixin_users')->find(I("post.userid"));
$openid = $infos->openid;
}*/
$openid='oOnZO5Q8OBzoZYj-1CW_6c1S86zA'; //支付相关设置
//$fee = I("post.total_fee");
$fee = 0.01;//举例支付0.01
$body ='test';
$nonce_str=$this->nonce_str();//随机字符串 $out_trade_no = $this->order_number();//商户订单号
$spbill_create_ip = '47.105.151.242';//服务器的ip【自己填写】;
$total_fee =$fee*100;// 微信支付单位是分,所以这里需要*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; //插入微信支付表
$pay=M('weixin_pay');
$count=$pay->where("out_trade_no='".$out_trade_no."'")->count(); $sign = $this->sign($post);//签名 if($count==0)
{
$data=array();
$data['out_trade_no']=$out_trade_no;
$data['status']=1;
$data['create_time']=time();
$data['sign']=$sign;
$data['openid']=$openid;
$data['total_fee']=$total_fee;
$data['trade_type']=$trade_type;
$data['notify_url']=$notify_url;
$pay->add($data);
} $post_xml = '<xml>
<appid>'.$appid.'</appid>
<body>'.$body.'</body>
<mch_id>'.$mch_id.'</mch_id>
<nonce_str>'.$nonce_str.'</nonce_str>
<notify_url>'.$notify_url.'</notify_url>
<openid>'.$openid.'</openid>
<out_trade_no>'.$out_trade_no.'</out_trade_no>
<spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
<total_fee>'.$total_fee.'</total_fee>
<trade_type>'.$trade_type.'</trade_type>
<sign>'.$sign.'</sign>
</xml>'; //var_dump($post_xml);
//print_r($post_xml);die;
//统一接口prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$xml = $this->http_request($url,$post_xml); $array = $this->xml($xml);//全要大写 //print_r($array);
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'] = 200;
$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 = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz'; 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要先排好顺序
private function sign($data){
$stringA = '';
foreach ($data as $key=>$value){
if(!$value) continue;
if($stringA) $stringA .= '&'.$key."=".$value;
else $stringA = $key."=".$value;
}
//echo $stringA;
$wx_key='1111111';//申请支付后有给予一个商户账号和密码,登陆后自己设置的key
$stringSignTemp = $stringA.'&key='.$wx_key;
return strtoupper(md5($stringSignTemp));
} //curl请求
public 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 wechatNotify()
{
//存储微信的回调
$xml = file_get_contents("php://input"); $arr = $this -> XmlToArr($xml); $out_trade_no = $arr['out_trade_no'];
$openid=$arr['openid'];//openid
$sign=$arr['sign'];//校验码
$total_fee=$arr['total_fee'];//订单金额
$transaction_id=$arr['transaction_id'];//微信支付号 $pay=M('weixin_pay');
$count=$pay->where(array('out_trade_no'=>$out_trade_no))->count(); $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = $out_trade_no."\n";
fwrite($myfile, $txt);
$txt = $openid."\n";
fwrite($myfile, $txt);
$txt='count:'.$count;
fwrite($myfile, $txt);
fclose($myfile); if($count==1)
{
$info=$pay->where(array('out_trade_no'=>$out_trade_no))->field('id,openid,total_fee')->find();
if($info['openid']==$openid && $total_fee==$info['total_fee']) {
//更新微信支付表
$data=array();
$data['update_time']=time();
$data['status']=2;
$data['transaction_id']=$transaction_id;
$pay->where("openid='".$openid."' and out_trade_no='".$out_trade_no."'")->save($data); } }
echo 'success';
} private function XmlToArr($xml)
{
if($xml == '') return '';
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arr;
} }
PHP小程序后端支付代码亲测可用的更多相关文章
- Taro/JS/H5/小程序:纯前端解决小程序微信支付统一下单和调起支付
这个文章不会说具体0到1的代码流程,我会着重讲几个问题的解决 准备以下依赖 "md5": "^2.2.1", "xml-js": " ...
- 亲测可用!!!golang如何在idea中保存时自动进行代码格式化
亲测可用,golang在idea中的代码自动格式化 1.ctrl+alt+s打开设置界面,选择[Plugins] -> [Install JetBrains plugin...] -> 搜 ...
- 微信小程序——微信支付
这个讲起来也就比较麻烦一点,因为需要的不仅仅是咱们代码上的技术,嘿嘿! 先整理一下思路.如果想做微信支付: 1.现有一个公司账户(非个人账户),并且实名认证过的. 2.微信号 必须开通微信支付功能. ...
- 微信小程序篇(微信小程序的支付)
微信小程序的支付和微信公众号的支付是类似的,对比起来还比公众号支付简单了一些,我们只需要调用微信的统一下单接口获取prepay_id之后我们在调用微信的支付即可. 今天我们来封装一般node的支付接口 ...
- 【微信小程序】支付过程详解
一.介绍 今天跟大家分享微信小程序集成支付. 二.分析 1.小程序支付API 地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html ...
- 微信小程序在线支付功能使用总结
最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支付,那么你会发现其实小程序里的微信支付和服务号里的开发过程如出 ...
- 微信小程序微信支付的一些坑
使用的是Node.js作为后端 统一下单: appid:这里的appid是调起微信支付的appid mch_id:商户号,需要注意的是商户号要与appid对应 nonce_str:Math.rando ...
- 微信小程序后端开发流程
微信小程序后端开发流程根据官网总结为两个步骤 1.前端调用 wx.login 返回了code,然后调用wx.getUserInfo获取到用户的昵称 头像 2.服务端根据code去微信获取openid, ...
- TP5调用小程序微信支付,回调,在待支付中再次调用微信支付
1,必须要有 $mch_id $key $appid这三个值,是需要去申请的,我是直接用公司的2,购买商品订单号用户openid统一下单名称商品价格(必须以分为单位,调起微信支付)服务器的ip地址(没 ...
随机推荐
- 修改host指定域名指向ip,Windows脚本与Linux脚本
修改host指定域名指向ip,Windows脚本与Linux脚本 一,Windows系统修改hosts文件 Windows系统下hosts文件位置:C:\Windows\System32\driver ...
- Factorization Machine算法
参考: http://stackbox.cn/2018-12-factorization-machine/ https://baijiahao.baidu.com/s?id=1641085157432 ...
- linux重要的配置文件列表
启动引导程序配置文件 LILO /etc/lilo.conf GRUB /boot/grub/menu.lst 系统启动文件核脚本 主启动控制文件 /etc/inittab SysV启动脚本的位置 / ...
- mysql常用配置注意项与sql优化
建立数据库: 建立数据库时编码字符集采用utf8 排序规则: 后缀"_cs"或者"_ci"意思是区分大小写和不区分大小写(Case Sensitive & ...
- 201871010125 王玉江《面向对象程序设计(java)》第十三周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- dp的一些练习
#include<iostream> #include<vector> using namespace std; class Backpack { public: int ma ...
- 多线程中fork与mutex
在多线程程序中fork出一个新进程,发现新的进程无法正常工作.因为:在使用fork时会将原来进程中的所有内存数据复制一份保存在子进程中.但是在拷贝的时候,但是线程是无法被拷贝的.如果在原来线程中加了锁 ...
- Git 克隆远程仓库到本地
Git 克隆远程仓库到本地 参考 $ git clone --help https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E8%8E%B7% ...
- luoguP1829 [国家集训队]Crash的数字表格 / JZPTAB(莫比乌斯反演)
题意 注:默认\(n\leqslant m\). 所求即为:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}lcm(i,j)\) 因为\(i*j=\gcd(i, ...
- 【PL/SQL 卡】
DBA给解决好了,但我不在场,要问一下