PHP微信三方平台-代公众号发起微信支付(jsAPI)
时间:2020-05-18 18:59:28
收藏:0
阅读:85
一、前期准备工作
1、微信公众号需要开通微信支付认证将获取的秘钥给三方平台
2、添加支付回调域名地址:填写三方平台域名地址即可(最多5个)
二、代码demo
1、完成支付类
<?php /** * 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、支付页面
<?php /** * 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(); ?> <a href="javascript:" onclick="pay()" class="zf_btn">确定支付</a> <script type="text/javascript" src="<?= $web ?>/js/jweixin-1.0.0.js"></script> <script type="text/javascript"> 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); } }); } </script>
评论(0)