<?php
namespace Mypay;
use think\facade\Config;
use think\facade\Log;
use think\facade\Cache;
class miWxpay
{
public function order($para,$openid,$cpData,$code,$gameid)
{
$config = Config::get("pay.miWxpay");
$offerId = '1450015409';
$amt = 3;
$wxPay = new PayService($cpData['wxappId'],$cpData['wxappSecret'],$config['appKey']);
$wxPay->createJsBizPackage($openid,$offerId,$amt, '11111122222222222aab',$code,$gameid);
}
}
class PayService
{
protected $appid;
protected $appsecret;
protected $appKey;
public function __construct($appid, $appsecret,$appKey)
{
$this->appid = $appid;
$this->appsecret = $appsecret;
$this->appKey = $appKey;
}
public function createJsBizPackage($openid, $offer_id, $amt, $orderid ,$code,$gameid)
{
$time = time();
$unified = [
'openid' => $openid,
'appid' => $this->appid,
'offer_id' => $offer_id,
'ts' => $time,
'zone_id' => '1' ,
'pf' => 'android',
];
$req = [
'openid' => $openid,
'appid' => $this->appid,
'offer_id' => $offer_id,
'ts' => $time,
'zone_id' => '1',
'pf' => 'android',
'amt' => $amt,
'bill_no' => $orderid,
];
$ACCESS_TOKEN = $this->getAccessToken($gameid);
$curl = "https://api.weixin.qq.com/cgi-bin/midas/pay?access_token=".$ACCESS_TOKEN;
if(!$session_key = $this->getSessionKey($code)){
return ['status' => 0, 'msg' => 'session_key get error'];
}
if( !$this->checkSessionKey($session_key,$ACCESS_TOKEN,$openid) ){
return ['status' => 0, 'msg'=>'session_key checkSessionKey error'];
}
$unified['sig'] = $this->GenerateSig($unified);
$unified['mp_sig'] = $this->GenerateMpSig($ACCESS_TOKEN,$unified,$session_key);
$req['sig'] = $unified['sig'];
$req['mp_sig'] = $unified['mp_sig'];
Log::write("PayService::createJsBizPackage()===============req".var_export($req,true),'notice');
$data = self::curlPost($curl,json_encode($req));
Log::write("PayService::createJsBizPackage()".var_export($data,true),'notice');
}
public function checkSessionKey($session_key,$access_token,$openid)
{
$SIGNATURE = hash_hmac('sha256', '' , $session_key );
$data = self::curlGet("https://api.weixin.qq.com/wxa/checksession?access_token=".$access_token."&signature=".$SIGNATURE."&openid=".$openid."&sig_method=hmac_sha256");
$data = json_decode($data,true);
if($data['errcode'] != 0){
Log::write('获取session_key->checkSessionKey::验证失败'.json_encode($data),'notice');
return false;
}
return true;
}
public function getSessionKey($code)
{
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$this->appid.'&secret='.$this->appsecret.'&js_code='.$code.'&grant_type=authorization_code';
$res = json_decode( GetHttps($url),true);
if( empty($res['session_key']) || !isset($res['session_key']) )
{
Log::write('获取session_key->GetSessionKey::状态::session_key does not exis,data::'.json_encode($res),'notice');
return false;
}
return $res['session_key'];
}
public function getAccessToken($gameid)
{
$CacheKey = "Access_token_".$gameid;
$data = Cache::get($CacheKey);
if(!$data){
$wxdata = self::curlGet('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret);
$wxdata = json_decode($wxdata,true);
if( !isset($wxdata['access_token']) ){
Log::write("PayService::getAccessToken()".var_export($wxdata['access_token'],true),'notice');
}
Cache::set($CacheKey,$wxdata['access_token'],3600);
return $wxdata['access_token'];
}
return $data;
}
public static function curlGet($url = '', $options = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public static function curlPost($url = '', $postData = '', $options = array())
{
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function GenerateSig($urlObj)
{
$str = $this->ToUrlParams($urlObj);
$stringSignTemp = $str."&org_loc=/cgi-bin/midas/pay&method=POST&secret=".$this->appsecret;
return hash_hmac('sha256', $stringSignTemp, $this->appsecret) ;
}
public function GenerateMpSig($access_token,$urlObj,$session_key)
{
$urlObj['access_token'] = $access_token;
$str = $this->ToUrlParams($urlObj);
$stringSignTemp = $str."&org_loc=/cgi-bin/midas/pay&method=POST&session_key=".$session_key;
return hash_hmac('sha256', $stringSignTemp, $session_key );
}
public function ToUrlParams($urlObj)
{
ksort($urlObj);
$buff = "";
foreach ($urlObj as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
}
你好,建议参考下面这个回答
小游戏虚拟支付报签名错误
https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=00044e0fd584b808ef8658d2156800&highline=90009