您好,登錄后才能下訂單哦!
下載php版本demo
3.后端具體調用代碼如下:
<?php
namespace app\common\library;
class Pay
{
public function wxpay($openId,$goods,$order_sn,$total_fee,$attach='')
{
require_once APP_PATH."/common/library/php_sdk/lib/WxPay.Api.php";
require_once APP_PATH."/common/library/php_sdk/lib/WxPay.JsApiPay.php";
require_once APP_PATH."/common/library/php_sdk/example/log.php";
// $logHandler= new CLogFileHandler(APP_ROOT."/Api/wxpay/logs/".date('Y-m-d').'.log');
// $log = Log::Init($logHandler, 15);
$tools = new \JsApiPay();
if(empty($openId)) $openId = $tools->GetOpenid();
$config = new \WxPayConfig();
$input = new \WxPayUnifiedOrder();
$input->SetBody($goods); //商品名稱
$input->SetAttach($attach); //附加參數,可填可不填,填寫的話,里邊字符串不能出現空格
$input->SetOut_trade_no($order_sn); //訂單號
$input->SetTotal_fee($total_fee * 100); //支付金額,單位:分
$input->SetTime_start(date("YmdHis")); //支付發起時間
$input->SetTime_expire(date("YmdHis", time() + 600));//支付超時
$input->SetGoods_tag("test3");
//$input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/payment.php"); //支付回調驗證地址
$input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/web/payment/notify/WeixinPay/notify");
$input->SetTrade_type("JSAPI"); //支付類型
$input->SetOpenid($openId); //用戶openID
$order = \WxPayApi::unifiedOrder($config,$input); //統一下單
// dump($order);exit;
$jsApiParameters = $tools->GetJsApiParameters($order);
return $jsApiParameters;
}
}
$pay = new \app\common\library\Pay();
$res = $pay->wxpay($this->openid,'開通會員',$order['order_no'],$order['money']);
4.前端js核心代碼如下:
function jsApiCall(data,id)
{
data = JSON.parse(data);
WeixinJSBridge.invoke(
'getBrandWCPayRequest', data,
function(res){
WeixinJSBridge.log(res.err_msg);
if(res.err_msg == "get_brand_wcpay_request:ok"){
layer.msg('支付成功', {time: 2500}, function () {
location.href = "{:url('xxx/xxx')}";
});
}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
layer.msg("支付取消!", {time: 1800});
}else{
layer.msg("支付失敗!", {time: 1800});
}
}
);
}
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();
}
}
$('.see_price').click(function() {
index = layer.load(1, {shade: [0.1,'#fff'] });
var $id = $(this).attr('name');
$.ajax({
type: 'post', //可選get
url: '{:url("xxx/xxx")}', //這里是接收數據的PHP程序
data: 'id='+$id, //傳給PHP的數據,多個參數用&連接
dataType: 'Json', //服務器返回的數據類型 可選XML ,Json jsonp script html text等
success: function(msg) {
if (msg.code == '200') {
layer.close(index);
jsApiCall(msg.info);
} else {
layer.close(index);
layer.msg(msg.message, {time: 1800});
}
},
error: function() {
layer.close(index);
layer.msg("提交失敗!", {time: 1800});
}
});
});
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。