php微信jsapi支付回调,PHP微信三方平台-代公众号发起微信支付(jsAPI)

一、前期准备工作

1、微信公众号需要开通微信支付认证将获取的秘钥给三方平台

2、添加支付回调域名地址:填写三方平台域名地址即可(最多5个)

二、代码demo

1、完成支付类

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2017/9/20 0020

* Time: 17:25

*/

namespace app\models;

class Payment

{

public static function WxPayInit($body, $trade_no, $total_fee, $open_id, $notify_url, $attach = [])

{

$post_params = [

'body' => $body,

'trade_no' => $trade_no,

'total_fee' => $total_fee * 100,

'open_id' => $open_id,

'attach' => $attach,

'notify_url' => \Yii::$app->request->hostInfo . \Yii::$app->request->scriptUrl . $notify_url

];

return self::WxPay($post_params);

}

public static function WxPay($data)

{

$hywx_test = \Yii::$app->params['hywx_test'];

$pay_config = [

'APPID' => $hywx_test['app_id'],

'MCHID' => $hywx_test['mch_id'],

'MCHKEY' => $hywx_test['mch_key']

];

$attach = json_encode($data['attach']);

$input = new \WxPayUnifiedOrder();

$input->SetBody($data['body']);

$input->SetAttach($attach);

$input->SetOut_trade_no($data['trade_no']);

$input->SetTotal_fee($data['total_fee']);

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

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

$input->SetNotify_url($data['notify_url']);

$input->SetTrade_type("JSAPI");

$input->SetOpenid($data['open_id']);

$result = \WxPayApi::unifiedOrder($input, $pay_config);

if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {

$jsapi = new \WxPayJsApiPay();

$jsapi->SetAppid($result["appid"]);

$timeStamp = time();

$jsapi->SetTimeStamp("$timeStamp");

$jsapi->SetNonceStr(\WxPayApi::getNonceStr());

$jsapi->SetPackage("prepay_id=" . $result['prepay_id']);

$jsapi->SetSignType("MD5");

$jsapi->SetPaySign($jsapi->MakeSign( $hywx_test['mch_key']));

return $parameters = $jsapi->GetValues();

}

}

}

2、支付请求方法

require_once dirname(dirname(__FILE__)) . '/librarys/wx/wx_pay/lib/WxPay.Api.php';

require_once dirname(dirname(__FILE__)) . '/librarys/wx/wx_pay/lib/WxPay.Config.php';

require_once dirname(dirname(__FILE__)) . '/librarys/wx/wx_pay/lib/WxPay.NativePay.php';

public function actionTest()

{

$body = '欧式body';

$order_code = 'HY' . date('YmdHis');

$total_fee = 0.01;

$open_id = 'or-9g0kq0_p4FxCLMAWPg4ZPYX0k';//用户open_id

$notify_url = '/site/test-call-back';//回调地址

$jsApiParameters = Payment::WxPayInit($body, $order_code, $total_fee, $open_id, $notify_url);

$return_arr = [];

$return_arr['trade_no'] = $order_code;

$return_arr['jsApiParameters'] = $jsApiParameters;

return $this->renderJson($return_arr);

}

3、支付页面

/**

* Created by PhpStorm.

* User: LiDingLie

* Date: 2017/10/18 0018

* Time: 11:28

*/

use yii\helpers\Url;

$web = Url::base();

$this->title = '订单支付';

$url = \app\librarys\Tools::urlBase();

?>

确定支付

function pay() {

var baseUrl = '=$url?>';

requestJson(baseUrl + '/site/test', {}, true, function (response) {

if (response.status == 0) {

trade_no = response.data.trade_no;

var jsApiData = response.data.jsApiParameters;

// var wechat = JSON.parse(jsApiData);

callpay(jsApiData);

} else {

layer.msg(response.message);

}

});

}

function callpay(wechat) {

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

}

}

function pollCode(wechat) {

var order_code = wechat.order_code;

var data = {};

data.order_code = order_code;

}

function jsApiCall(wechat) {

WeixinJSBridge.invoke('getBrandWCPayRequest', {

'appId': wechat.appId,

'timeStamp': wechat.timeStamp,

'nonceStr': wechat.nonceStr,

'package': wechat.package,

'signType': wechat.signType,

'paySign': wechat.paySign

}, function (res) {

if (res.err_msg == 'get_brand_wcpay_request:ok') {

is_allow_pay = false;

// js轮训查询 是否支付成功

var setInt = setInterval(function () {

pollCode(wechat);

}, 1000);

} else if (res.err_msg == 'get_brand_wcpay_request:cancel') {

layer.msg('取消支付成功');

//取消订单,作废报名记录

cancelPayTicketHandler(wechat.order_code);

} else {

layer.msg(res.err_msg);

}

});

}

来源:https://www.cnblogs.com/lt-com/p/12912130.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值