PHP JSAPI 微信支付

直接上代碼:

PHP JSAPI 微信支付


public static $appid = 'wxdff3ac673973bde2'; //微信對應的appid

public static $mchid = '1483472062'; // 商戶號ID 還有一個妥妥惠的商戶號,appid跟這個相同,但是mchid跟這個不同

public static $key = '934C868AC83C08913F627202AE540055'; // 微信商戶支付平臺中的 商戶支付密鑰

public static $appsecret = '3127d8774a614dd50d27050c78dfdba2'; //公眾號的appsecret 不是微信支付商戶平臺的


三 微信核心類文件 WxPayJsApiPay.php 文件中的某個類修改

//require_once dirname(dirname(__FILE__))."/WxPayApi.php";

require_once "WxPayApi.php";


/**

*

* 通過跳轉獲取用戶的openid,跳轉流程如下:

* 1、設置自己需要調回的url及其其他參數,跳轉到微信服務器https://open.weixin.qq.com/connect/oauth2/authorize

* 2、微信服務處理完成之後會跳轉回用戶redirect_uri地址,此時會帶上一些參數,如:code

*

* @return 用戶的openid

*/

public function GetOpenid()

{

//通過code獲得openid

if (!isset($_GET['code'])){

// $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING';]);

$baseUrl = urlencode('http://';.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 後修改的這塊,之前的URL鏈接不對

$url = $this->__CreateOauthUrlForCode($baseUrl);

Header("Location: $url");

exit

();

} else {

//獲取code碼,以獲取openid

$code = $_GET['code'];

$openid = $this->getOpenidFromMp($code);

return $openid;

}

}


三 目錄結構

PHP JSAPI 微信支付


四 Controller的代碼

public function cart4(){

$save_data['coupon_id'] = I('coupon_id');

$order_id = I('order_id');

$order = M('Order')->where("order_id = $order_id")->find();

M('Order')->where("order_id = $order_id")->save($save_data);

$mods=D('OrderGoods');

$where['order_id']= I('request.order_id');

$subject_name=$mods->where($where)->select();

// 如果已經支付過的訂單直接到訂單詳情頁面. 不再進入支付頁面

if($order['pay_status'] == 1){

$order_detail_url = U("Mobile/User/order_detail",array('id'=>$order_id));

header("Location: $order_detail_url");

}

$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','alipay')")->select();

if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){

$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','cod')")->select();

}

$paymentList = convert_arr_key($paymentList, 'code');

foreach($paymentList as $key => $val)

{

$val['config_value'] = unserialize($val['config_value']);

if($val['config_value']['is_bank'] == 2)

{

$bankCodeList[$val['code']] = unserialize($val['bank_code']);

}

}

$bank_img = include 'Application/Home/Conf/bank.php'; // 銀行對應圖片

$payment = M('Plugin')->where("`type`='payment' and status = 1")->select();

$this->assign('subject_name',$subject_name[0]['goods_name']);

$this->assign('paymentList',$paymentList);

$this->assign('bank_img',$bank_img);

$this->assign('order',$order);

$this->assign('bankCodeList',$bankCodeList);

$this->assign('pay_date',date('Y-m-d', strtotime("+1 day")));

if($this->is_weixin()){

vendor('Weixinpay.WxPayJsApiPay');

//1、獲取openid

$tools = new \\JsApiPay();

// $openId = 'orLeuv4781xUlDJjwACO1vgGjObw';

$openId = $tools->GetOpenid();

$input = new \\WxPayUnifiedOrder();

$input->SetBody($subject_name[0]['goods_name']);

$input->SetAttach("萬惠科技");

$input->SetOut_trade_no($order['order_sn']);

// $input->SetOut_trade_no('1486224422'.date("YmdHis"));

$input->SetTotal_fee($order['order_amount']*100);

$input->SetTime_start(date("YmdHis"));

$input->SetTime_expire(date("YmdHis", time() + 600));

$input->SetGoods_tag("惠生活客戶服務");

$input->SetNotify_url("http://www.wanhuikeji.cn/Api/Notify/wxPayNotify";);

$input->SetTrade_type("JSAPI");

$input->SetOpenid($openId);//$openId

// dump($input);die;

$order = \\WxPayApi::unifiedOrder($input);

// dump($order);die;

echo '

統一下單支付單信息/<font>
';

$jsApiParameters = $tools->GetJsApiParameters($order);

$this->assign('jsApiParameters',$jsApiParameters);

}

$this->display();

}


五 html中的代碼

<include file="Public/header"/>

<link>"__STATIC__/css/flow.css?version=__STATIC_VERSION__">

<link>"__STATIC__/css/style_jm.css?version=__STATIC_VERSION__">

<link>"__STATIC__/css/manyuan1.css?version=__STATIC_VERSION__">

<header>

提交訂單

var order_id = "{$order['order_id']}",

order_sn = "{$order['order_sn']}",

order_amount = "{$order['order_amount']}";

subject_name = "{$subject_name}...";

$(document).ready(function(){

$('.c_checkbox_t').eq(0).attr('checked',true);

change_pay('alipay');

var iswx = isWeiXin();

if(iswx){

$("#MyjsButtons").hide();

$("#WxjsButtons").show();

}

});

// 切換支付方式

function change_pay(obj)

{

$(obj).find('input[name="pay_radio"]').prop('checked',true);

$(obj).siblings('li').find('input[name="pay_radio"]').prop('checked',false);

var pay_type = $('input[name="pay_radio"]:checked').val();

var u = navigator.userAgent;

if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {

var typs = 'shangpin';

var callbackButton = document.getElementById('MyjsButtons')

callbackButton.onclick = function(){

window.android_interface.whpay(pay_type,order_id,order_sn,order_amount,typs,subject_name);

}

} else if (u.indexOf('iPhone') > -1) {

//必要設置1

function

setupWebViewJavascriptBridge(callback) {

if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }

if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }

window.WVJBCallbacks = [callback];

var WVJBIframe = document.createElement('iframe');

WVJBIframe.style.display = 'none';

WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';

document.documentElement.appendChild(WVJBIframe);

setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)

}

setupWebViewJavascriptBridge(function(bridge) {

var callbackButton = document.getElementById('MyjsButtons');

callbackButton.onclick = function(){

// bridge.registerHandler('WWDengluHd', function(data, responseCallback) {

// var responseData = { 'order_id':{$order_id} }

// responseCallback(responseData,data)

// })

bridge.callHandler('registerHandler',{'pay_radio':pay_type,'order_id':order_id,'order_sn':order_sn,'order_amount':order_amount,'subject_name':subject_name},function(response){

})

}

})

}

}

function pay(){

var iswx = isWeiXin();

if(iswx){

callpay();

}else{

$('#cart4_form').submit();

}

}

function isWeiXin(){

var ua = window.navigator.userAgent.toLowerCase();

if(ua.match(/MicroMessenger/i) == 'micromessenger'){

return 1;

}else{

return 0;

}

}

function jsApiCall()

{

WeixinJSBridge.invoke(

'getBrandWCPayRequest',

{$jsApiParameters},

function(res){

//WeixinJSBridge.log(res.err_msg);

//alert(res.err_code+res.err_desc+res.err_msg);

}

);

}

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();

}

}


分享到:


相關文章: