微信公眾號前端呼叫微信支付前設定配置問題
阿新 • • 發佈:2020-11-17
之前在csdn 上,為了方便修改更新現在移動到這裡,那邊的會直接刪除
關於公眾號前端吊起支付前 需先配置一定的引數
獲取方式
請求配置資訊 呼叫
private $appid='*'; private $appsecret='*'; public function wx_config($url='') { // echo date('YmdHi').rand(10000,99999);die; $jssdk = new JsSDKController($this->appid,$this->appsecret); $signPackage = $jssdk->GetSignPackage($url); return $this->reData(1,'success',$signPackage); }
這裡需要注意的是 傳遞的引數url ,在支付過程中 url不傳遞不會報錯,但是在對接微信掃一掃 分享等介面時 傳遞的是動態的url地址 前段需要將url#之前的全部傳遞 即使用 var currurl = decodeURIComponent(location.href.split('#')[0]); 將url當做引數傳遞到介面進行請求
否則會報錯
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2020/6/24 * Time: 15:04*/ namespace app\api\controller; use Think\Controller; class JsSDKController extends Controller{ private $appId; private $appSecret; public function __construct($appid,$appsecret) { $this->appId = $appid; $this->appSecret =$appsecret; } public function getSignPackage($url='') { $jsapiTicket = $this->getJsApiTicket(); if($url=='') { $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; } $timestamp = time(); $nonceStr = $this->createNonceStr(); // 這裡引數的順序要按照 key 值 ASCII 碼升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } private function getJsApiTicket() { // jsapi_ticket 應該全域性儲存與更新,以下程式碼以寫入到檔案中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->https_get($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { // access_token 應該全域性儲存與更新,以下程式碼以寫入到檔案中做示例 $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->https_get($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } private function https_get($url){ $curl=curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); $data=curl_exec($curl); if(curl_errno($curl)){ return 'ERROR'.curl_error($curl); } curl_close($curl); return $data; } }
另在public下 新增兩個可讀寫檔案
//access_token.json {"expire_time":1596792795,"access_token":"36_0YRpzQz6Rieg6fEUU8JrNchOqg-KOop-ovp8nsetBkEpdmMWzsEpgSxgXbZP90cRcQVs_T41n3ILxRSqV6fWy6pA-81EuWAgjeAPNpsFGslUou09XGyZhUro5XUNRxAi90ADDsiCSo2W7gJhMLWiAIAUTE"} //jsapi_ticket.json {"errcode":0,"errmsg":"ok","ticket":"","expires_in":7200,"expire_time":1596792796,"jsapi_ticket":"HoagFKDcsGMVCIY2vOjf9nzrOILfNLs9-BU-kjhp6N6go5YGF4XTbog47AZbTKhRDY267rjRzPUkCYlDsNJF-Q"}
注:以上程式碼直接拿公司同事楊的程式碼 我這裡為上次公眾號支付吊起做續借