您的位置 首页 php

PHP JSAPI 微信支付

直接上代码:

一 微信支付封装到Vendor下

二 微信公众号及对应的微信支付商服号

public static $appid = ‘wxdff3ac673973bde2’; //微信对应的appid

public static $mchid = ‘1483472062’; // 商户号ID 还有一个妥妥惠的商户号,appid跟这个相同,但是mchid跟这个不同

public static $key = ‘934C868AC83C08913F627202AE540055’; // 微信商户支付平台中的 商户支付密钥

public static $appsecret = ‘3127d8774a614dd50d27050c78dfdba2’; //公众号的appsecret 不是微信支付商户平台的

三 微信核心类文件 WxPayJsApiPay.php 文件中的某个类修改

<?php

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

require_once “WxPayApi.php”;

/**

*

* 通过跳转获取用户的 openid ,跳转流程如下:

* 1、设置自己需要调回的url及其其他参数,跳转到微信服务器

* 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code

*

* @return 用户的openid

*/

public function GetOpenid()

{

//通过code获得openid

if (! isset ($_GET[‘code’])){

//触发微信返回code码

// $baseUrl = urlencode(‘#39;.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’].$_SERVER[‘QUERY_STRING’;]);

$baseUrl = urlencode(‘#39;;.$_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;

}

}

三 目录结构

四 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(“#34;;);

$input->SetTrade_type(“JSAPI”);

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

// dump($input);die;

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

// dump($order);die;

echo ‘<font color=”#f00″><b>统一下单支付单信息</b></font><br/>’;

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

$this->assign(‘jsApiParameters’,$jsApiParameters);

}

$this->display();

}

五 html中的代码

< include file=”Public/header”/>

<link rel=”stylesheet” href= “__STATIC__/css/flow.css?version=__STATIC_VERSION__” >

<link rel=”stylesheet” href= “__STATIC__/css/style_jm.css?version=__STATIC_VERSION__” >

<link rel=”stylesheet” href= “__STATIC__/css/manyuan1.css?version=__STATIC_VERSION__” >

<body style=”background: rgb(235, 236, 237);position:relative;”>

<header class=” red-packet-header “>

<div class=” tab_nav “>

<div class=” header “>

<div class=” h-left “><a class=” sb-back iconfont ” href=”{:U(‘Cart/Cart’)}” title=”返回”></a></div>

<div class=” h-mid “>提交订单</div>

</div>

</div>

</header>

<div style=”height:44px;”></div>

<div class=” screen-wrap fullscreen login “>

<form action=”” method=”post” name=”cart4_form” id=”cart4_form”>

<div class=” content_success ” >

<div class=” con-ct fo-con “>

<h4 class=” successtijiao “>订单已经提交成功!</h4>

<ul class=” ct-list “>

<li>请您在<span>{$pay_date}</span>前完成支付,否则订单将自动取消!</li>

<li >订单号:<em>{$order[‘order_sn’]}</em></li>

<li>支付金额:<em>¥{$order[‘order_amount’]}元</em></li>

</ul>

</div>

<section class=” order-info “>

<div class=” order-list “>

<div class=” content ptop0 “>

<div class=” panel panel-default info-box “>

<div class=” panel-body ” id=”pay_div” >

<div class=” title ” id=”zhifutitle” style=”border-bottom:1px solid #eeeeee;”> <span class=” i-icon-arrow-down i-icon-arrow-up ” id=”zhifuip”></span> <span class=” text “>支付方式</span> <a href=”javascript:%20void (0)” title=”修改商品列表” class=” link “>必选</a> <em class=” qxz ” id=”emzhifu”>请选择支付方式</em> </div>

<ul class=” nav nav-list-sidenav ” id=”zhifu68″ style=”display:block; border-bottom:none;”>

< foreach name=”paymentList” item=”v” key=”k”>

<li class=” clearfix name =”payment_name” onClick=”change_pay(this);”>

<label>

<input type=”radio” value=”{$k}” class=” c_checkbox_t ” name=”pay_radio” />

<div class=”fl shipping_title”>

<img src= “/plugins/{$v[‘type’]}/{$v[‘code’]}/{$v[‘icon’]}” width=”110″ height=”40″ />

</div>

</label>

</li>

</foreach>

</ul>

</div>

</div>

</div>

</div>

</section>

<div class=” pay-btn “>

<input type=”hidden” name=”order_id” value=”{$order[‘order_id’]}” />

<input type=”hidden” name=”order_sn” value=”{$order[‘order_sn’]}” />

<input type=”hidden” name=”order_amount” value=”{$order[‘order_amount’]}” />

<a href=”javascript:void(0);” onClick=”pay()” id=”MyjsButtons” class=”sub-btn btnRadius”>去支付</a>

<a href=”javascript:void(0);” style=”display:hide;” onClick=”pay()” id=”WxjsButtons” class=”sub-btn btnRadius”>去支付</a>

</div>

</div>

</form>

<script>

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 callback Button = 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 setupWebViewJavascript bridge (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();

}

}

</script>

<script type=”text/javascript”>

//判断是否为微信浏览器

function isWeiXin(){

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

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

return 1;

} else {

return 0;

}

}

//调用微信JS api 支付

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

}

}

</script>

</div>

</body>

</html>

六 出现的问题

notify_url 在控制器里设置了,但是一直不好用,后来才知道需要设置访问目录

文章来源:智云一二三科技

文章标题:PHP JSAPI 微信支付

文章地址:https://www.zhihuclub.com/76746.shtml

关于作者: 智云科技

热门文章

网站地图