PHP+tp3实现微信支付,实测亲用
1.下载微信支付的sdk,然后在WxPay.Config.php中更改自己的信息
/**
* TODO: 修改这里配置为您自己申请的商户信息
* 微信公众号信息配置
*
* APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
*
* MCHID:商户号(必须配置,开户邮件中可查看)
*
*/
public function GetAppId()
{
return '';
}
public function GetMerchantId()
{
return '';
}
/*
* KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置), 请妥善保管, 避免密钥泄露
* 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
*
* APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置), 请妥善保管, 避免密钥泄露
* 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
* @var string
*/
public function GetKey()
{
return '';
}
public function GetAppSecret()
{
return '';
}
2.控制器下单部分代码
//
$logHandler= new CLogFileHandler("./ThinkPHP/Extend/Vendor/wxsjpay/logs/".date('Y-m-d').'.log');
$log = Logssss::Init($logHandler, 15);
$config = new WxPayConfig();
$trade_no = $config->GetMerchantId().'1'.date("YmdHis");
//①、获取用户openid
try{
$tools = new \JsApiPay();
$openId = $tools->GetOpenid($_POST['pay_money']);
// 这里下单金额 由获取用户openid的时候传入
$money= $_GET['jiage'];
// //②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody("测试");
$input->SetAttach("测试");
$input->SetOut_trade_no($trade_no);
$input->SetTotal_fee($money*100);//金额的单位为分,
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("VIP");
$input->SetNotify_url("");//微信回调地址,需要在微信公众平台配置
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($config, $input);
// $this->printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);
//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();
} catch(Exception $e) {
Logssss::ERROR(json_encode($e));
}
if($order['err_code'])
{
$error=$order['err_code_des'];
echo "<script> alert('$error');location.href='/';</script>";die;
}
//传入页面参数
$this->assign('jsApiParameters',$jsApiParameters);
$this->assign('editAddress',$editAddress);
$this->assign('data',$data);
$this->display();
3.html部分代码
选中内容后调用js方法支付
<a class="weui_btn weui_btn_primary" onclick="callpay();" >立即支付</a>
js部分
<script type="text/javascript">
callpay();
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<?php echo $jsApiParameters; ?>,
function(res){
WeixinJSBridge.log(res.err_msg);
// alert(res.err_msg);
if(res.err_msg=='get_brand_wcpay_request:ok'){
//支付成功,跳转支付成功页面
window.location.href="/pay_success";
}else{
alert('支付失败,请刷新重新支付!');
window.location.href="/pay";
}
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall();
}
}
//获取共享地址
function editAddress()
{
WeixinJSBridge.invoke(
'editAddress',
<?php echo $editAddress; ?>,
function(res){
var value1 = res.proviceFirstStageName;
var value2 = res.addressCitySecondStageName;
var value3 = res.addressCountiesThirdStageName;
var value4 = res.addressDetailInfo;
var tel = res.telNumber;
// alert(value1 + value2 + value3 + value4 + ":" + tel);
}
);
}
window.onload = function(){
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', editAddress, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', editAddress);
document.attachEvent('onWeixinJSBridgeReady', editAddress);
}
}else{
editAddress();
}
};
</script>
4.业务逻辑可在回调地址中做操作
if($data['return_code'] == "SUCCESS"){
//最后返回给微信
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
}