之前在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&timestamp=$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"}

注:以上代码直接拿公司同事杨的代码  我这里为上次公众号支付吊起做续借

 

posted on 2020-11-17 09:30  思君邪  阅读(253)  评论(0编辑  收藏  举报