记一次自己亲自走通微信支付流程的过程:
首先拿到需要的公众号Appid、AppSecret、商户id、支付秘钥
然后到微信支付的开发文档中下载 需要的SDK版本,第一次是全部使用SDK只是放到服务器上走了一遍。
然后自己动手写了一遍
前端页面:
<?php
header("Content-type:text/html;charset=utf-8");
include ‘./lib/WxPay.Api.php‘;
include ‘./wxConfig.php‘;
include ‘./wxJsapiPay.php‘;
$jsapi = new wxJsapiPay();
$preOrder = $jsapi->makeWxPrepareOrder();
var_dump($preOrder);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>微信支付</title>
<style type="text/css">
.pay-btn{
display: block;
width: 200px;
height: 32px;
color:#FFF;
text-align: center;
background-color: #09BB07;
border-radius: 0.2rem;
line-height: 32px;
margin:0 auto;
margin-top: 10%;
}
</style>
</head>
<body>
<span class="pay-btn" onclick="wxpay()">微信支付</span>
<script type="text/javascript">
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();
}
};
function wxpay() {
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();
}
}
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
‘getBrandWCPayRequest‘,
{
"appId":"<?php echo $preOrder[‘appId‘]; ?>", //公众号名称,由商户传入
"timeStamp":"<?php echo $preOrder[‘timeStamp‘]; ?>", //时间戳,自1970年以来的秒数
"nonceStr":"<?php echo $preOrder[‘nonceStr‘]; ?>", //随机串
"package":"<?php echo $preOrder[‘package‘]; ?>",
"signType":"<?php echo $preOrder[‘signType‘]; ?>", //微信签名方式:
"paySign":"<?php echo $preOrder[‘paySign‘]; ?>" //微信签名
},
function(res){
WeixinJSBridge.log(res.err_msg);
alert(res.err_code+res.err_desc+res.err_msg);
// alert(JSON.stringify(res)); //查看错误消息
}
);
}
</script>
</body>
</html>
php页面:
include_once ‘./lib/WxPay.Api.php‘;
include_once ‘./wxConfig.php‘;
class wxJsapiPay
{
/**
* 创建微信支付预订单数据
*/
public function makeWxPrepareOrder(){
// $openid = $this->GetOpenid();
$wxOrderData = new WxPayUnifiedOrder();
$wxOrderData->SetOut_trade_no(‘20180806125346‘); //设置订单号
$wxOrderData->SetTrade_type(‘JSAPI‘); //微信支付方式
$wxOrderData->SetBody(‘我的微信支付‘); //商品描述
$wxOrderData->SetFee_type(‘CNY‘); //货币种类 CNY人民币
$wxOrderData->SetTotal_fee(1); //设置订单总价
$wxOrderData->SetOpenid(‘o75920o4fPcP74pTWI8MfDyYsB_A‘); //设置用户openid
$wxOrderData->SetNotify_url(‘http://paysdk.weixin.qq.com/notify.php‘); //设置微信支付回掉地址
$wxPayConfig = new wxConfig();
$wxOrder = WxPayApi::unifiedOrder($wxPayConfig,$wxOrderData);
$jsApiParameters = $this->GetJsApiParameters($wxOrder);
$jsApiParameters = json_decode($jsApiParameters,TRUE);
return $jsApiParameters;
}
}
原文地址:http://blog.51cto.com/11016194/2336138
时间: 2024-09-30 06:25:55