微信js支付不弹窗口php,PHP微信支付問題(js調用接口)

此篇博客详细介绍了如何使用ThinkPHP框架开发微信支付功能,包括获取openid、生成预支付订单、调用微信JSAPI并设置签名,以及处理微信支付回调。通过实例展示了Controller中的关键函数和参数配置。
摘要由CSDN通过智能技术生成

控制器代碼

namespace Home\Controller;

use Think\Controller;

header('Content-type:text/html;charset=utf-8');

class ShoppingController extends Controller {

//嚴重說明:  上面的命名空間給引入類要換成自己的路徑

private $app_id = '';                                   //appid

private $mch_id = '';                                //商戶號

private $makesign = '';      //支付的簽名(在商戶平台API安全按鈕中獲取)

private $parameters = NULL;

private $notify = 'http://自己的域名/wxpay.php'; //配置回調地址(給pays中轉文件上傳到根目錄下面)

private $app_secret = '';                               //微信官方獲取

public $error = 0;

public $orderid = null;

public $openid = '';

//進行微信支付

public function wxPay() {

#獲取openid ID

if ($_SESSION['openid'] == null) {

$this->Wxcallback();

}

$reannumb = $this->randomkeys(6);  //生成隨機數 以后可以當做 訂單號

$pays = 1;                       //獲取需要支付的價格

#插入語句書寫的地方

$conf = $this->payconfig('Bm' . $reannumb, $pays * 100, '報名費用支付');

if (!$conf || $conf['return_code'] == 'FAIL')

exit("");

$this->orderid = $conf['prepay_id'];

//生成頁面調用參數

$jsApiObj["appId"] = $conf['appid'];

$timeStamp = time();

$jsApiObj["timeStamp"] = "$timeStamp";

$jsApiObj["nonceStr"] = $this->createNoncestr();

$jsApiObj["package"] = "prepay_id=" . $conf['prepay_id'];

$jsApiObj["signType"] = "MD5";

$jsApiObj["paySign"] = $this->MakeSign($jsApiObj);

$this->parameters  = json_encode($jsApiObj);

$json = json_encode($jsApiObj);

$this->assign('parameters', $json);

$this->title = "用戶訂單充值管理";

$this->display();

}

//訂單管理

#微信JS支付參數獲取#

protected function payconfig($no, $fee, $body) {

$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

$data['appid'] = $this->app_id;

$data['mch_id'] = $this->mch_id;           //商戶號

$data['device_info'] = 'WEB';

$data['body'] = $body;

$data['out_trade_no'] = $no;               //訂單號

$data['total_fee'] = $fee;                 //金額

$data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"];  //ip地址

$data['notify_url'] = $this->notify;

$data['trade_type'] = 'JSAPI';

$data['openid'] = $_SESSION['openid'];   //獲取保存用戶的openid

$data['nonce_str'] = $this->createNoncestr();

$data['sign'] = $this->MakeSign($data);

//print_r($data);

$xml = $this->ToXml($data);

$curl = curl_init(); // 啟動一個CURL會話

curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

//設置header

curl_setopt($curl, CURLOPT_HEADER, FALSE);

//要求結果為字符串且輸出到屏幕上

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($curl, CURLOPT_POST, TRUE); // 發送一個常規的Post請求

curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); // Post提交的數據包

curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設置超時限制防止死循環

$tmpInfo = curl_exec($curl); // 執行操作

curl_close($curl); // 關閉CURL會話

$arr = $this->FromXml($tmpInfo);

return $arr;

}

/**

*    作用:產生隨機字符串,不長於32位

*/

public function createNoncestr($length = 32) {

$chars = "abcdefghijklmnopqrstuvwxyz0123456789";

$str = "";

for ($i = 0; $i < $length; $i++) {

$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

}

return $str;

}

/**

*    作用:產生隨機字符串,不長於32位

*/

public function randomkeys($length) {

$pattern = '1234567890123456789012345678905678901234';

$key = null;

for ($i = 0; $i < $length; $i++) {

$key .= $pattern{mt_rand(0, 30)};    //生成php隨機數

}

return $key;

}

/**

* 將xml轉為array

* @param string $xml

* @throws WxPayException

*/

public function FromXml($xml) {

//將XML轉為array

return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

}

/**

* 輸出xml字符

* @throws WxPayException

* */

public function ToXml($arr) {

$xml = "";

foreach ($arr as $key => $val) {

if (is_numeric($val)) {

$xml .= "" . $val . "" . $key . ">";

} else {

$xml .= "" . $key . ">";

}

}

$xml .= "";

return $xml;

}

/**

* 生成簽名

* @return 簽名,本函數不覆蓋sign成員變量,如要設置簽名需要調用SetSign方法賦值

*/

protected function MakeSign($arr) {

//簽名步驟一:按字典序排序參數

ksort($arr);

$string = $this->ToUrlParams($arr);

//簽名步驟二:在string后加入KEY

$string = $string . "&key=" . $this->makesign;

//簽名步驟三:MD5加密

$string = md5($string);

//簽名步驟四:所有字符轉為大寫

$result = strtoupper($string);

return $result;

}

/**

* 格式化參數格式化成url參數

*/

protected function ToUrlParams($arr) {

$buff = "";

foreach ($arr as $k => $v) {

if ($k != "sign" && $v != "" && !is_array($v)) {

$buff .= $k . "=" . $v . "&";

}

}

$buff = trim($buff, "&");

return $buff;

}

#獲取openid ID

public function Wxcallback() {

$direct = $this->get_page_url(); //當前訪問URL

//$code =Yii::app()->request->getParam('code');  //獲取code碼號

$code = $_GET['code'];  //獲取code碼號

if ($code == null) {

header("Location:" . "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->app_id . "&redirect_uri=" . urlencode($direct) . "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");

} else {

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->app_id . "&secret=" . $this->app_secret . "&code={$code}&grant_type=authorization_code";

$res = $this->request_get($url);

if ($res) {

$data = json_decode($res, true);

//Yii::app()->session["openid"] = $data['openid'];    //設置session

$_SESSION['openid'] = $data['openid'];    //設置session

//$this->redirect(array('/baoming/index'));

} else {

echo json_encode(array('status' => 0, 'msg' => '獲取openid出錯', 'v' => 4));

die();

}

}

print_r($_SESSION['openid']);

die();

}

#獲取當前訪問完整URL#

public function get_page_url($site = false) {

$url = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://';

$url .= $_SERVER['HTTP_HOST'];

if ($site)

return $this->seldir() . '/'; //訪問域名網址

$url .= isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : urlencode($_SERVER['PHP_SELF']) . '?' . urlencode($_SERVER['QUERY_STRING']);

return $url;

}

//返回訪問目錄

public function seldir() {

$baseUrl = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));

//保證為空時能返回可以使用的正常值

$baseUrl = empty($baseUrl) ? '/' : '/' . trim($baseUrl, '/');

return 'http://' . $_SERVER['HTTP_HOST'] . $baseUrl;

}

public function callback(){

$xml = file_get_contents("php://input");

$log = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

$id = $log['out_trade_no'];  //獲取訂單號

exit('SUCCESS');

}

public function request_get($url) {

$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 500);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);

curl_close($curl);

return $res;

}

}

根目錄下回調文件wxpay.php

$xml = file_get_contents("php://input");

$url ='http://域名/Home/Shopping/callback';

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //get the response as a string from curl_exec(), rather than echoing it

curl_setopt($ch, CURLOPT_URL, $url );

curl_setopt($ch, CURLOPT_POST, 1 );

curl_setopt($ch, CURLOPT_POSTFIELDS,$xml);

//取到的$info 即為拿到的script 信息

$info =     curl_exec($ch) ;

curl_close($ch);    //close the handle

echo $info; //輸出

前台代碼

或許頁面不對,但是功能是這些

//調用微信JS api 支付

//在線充值

function jsApiCall()

{

var b = {$parameters};

WeixinJSBridge.invoke(

'getBrandWCPayRequest',

{

"appId": b.appId,

"nonceStr": b.nonceStr,

"package": b.package,

"paySign": b.paySign,

"signType": b.signType,

"timeStamp": b.timeStamp

},

function (res) {

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

} else {

//                           alert(JSON.stringify(res));

}

}

);

}

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

}

}

立即支付

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值