这里下载的是微信公众号支付所使用的demo
交待一下开发环境
window
thinkphp5
thinkcmf5
具体代码如下
ini_set('date.timezone','Asia/Shanghai');Loader::import('paywx.lib.WxPay',EXTEND_PATH,'.Api.php');
模型(model)中
public functionpay($subject,$out_trade_no,$total_amount){$input=new\WxPayUnifiedOrder();$input->SetBody($subject);$input->SetOut_trade_no($out_trade_no);$input->SetTotal_fee($total_amount*100);$input->SetNotify_url("回调地址");$input->SetTrade_type("MWEB");$scene_info='{"h5_info":{"type":"Wap","wap_url":"域名","wap_name":"企业名称"}}';$input->SetValues("scene_info",$scene_info);$wxOrder= \WxPayApi::unifiedOrder($input);if($wxOrder['return_code'] !='SUCCESS'||$wxOrder['result_code'] !='SUCCESS'){Log::record($wxOrder,'error');Log::record('获取预支付订单失败','error');}return$wxOrder;}
在控制器中 有些数据 改为自己代码中的数据
$wxpay=newWxPayService();$result=$wxpay->pay($order['snap_name'],$order_no,'0.01');
if($result['result_code'] =='SUCCESS'&&$result['return_code'] =='SUCCESS') {$url=$result['mweb_url']."&redirect_url=".urlencode("支付完需要返回的链接 /soft/check/12.html");$this->success('获取成功',null,['url'=>$url]);}else{$this->error($result['return_msg']);}
前端代码:
$.ajax({
type:'post',
dataType:'json',
data:{},
timeout:10000,
url:"{:url('Pay/wxPay')}",
success:function(res){
if(res.code){
window.location.href=res.data.url;
}
}
});