<?php
header('Content-type:text/html; Charset=utf-8');
$mchid
= 'xxxxx';
$appid
= 'xxxxx';
$appKey
= 'xxxxx';
$apiKey
= 'xxxxx';
$wxPay
=
new
WxpayService(
$mchid
,
$appid
,
$appKey
,
$apiKey
);
$openId
=
$wxPay
->GetOpenid();
if
(!
$openId
)
exit
('获取openid失败');
$outTradeNo
= uniqid();
$payAmount
= 1;
$sendName
= '织梦猫';
$wishing
= '感谢您参加猜灯谜活动,祝您元宵节快乐!';
$act_name
='猜灯谜抢红包活动';
$result
=
$wxPay
->createJsBizPackage(
$openId
,
$payAmount
,
$outTradeNo
,
$sendName
,
$wishing
,
$act_name
);
echo
'success';
class
WxpayService
{
protected
$mchid
;
protected
$appid
;
protected
$appKey
;
protected
$apiKey
;
public
$data
= null;
public
function
__construct(
$mchid
,
$appid
,
$appKey
,
$key
)
{
$this
->mchid =
$mchid
;
$this
->appid =
$appid
;
$this
->appKey =
$appKey
;
$this
->apiKey =
$key
;
}
public
function
GetOpenid()
{
if
(!isset(
$_GET
['code'])){
$scheme
=
$_SERVER
['HTTPS']=='on' ? 'https:
$baseUrl
= urlencode(
$scheme
.
$_SERVER
['HTTP_HOST'].
$_SERVER
['PHP_SELF'].
$_SERVER
['QUERY_STRING']);
$url
=
$this
->__CreateOauthUrlForCode(
$baseUrl
);
Header("Location:
$url
");
exit
();
}
else
{
$code
=
$_GET
['code'];
$openid
=
$this
->getOpenidFromMp(
$code
);
return
$openid
;
}
}
public
function
GetOpenidFromMp(
$code
)
{
$url
=
$this
->__CreateOauthUrlForOpenid(
$code
);
$res
= self::curlGet(
$url
);
$data
= json_decode(
$res
,true);
$this
->data =
$data
;
$openid
=
$data
['openid'];
return
$openid
;
}
private
function
__CreateOauthUrlForOpenid(
$code
)
{
$urlObj
["appid"] =
$this
->appid;
$urlObj
["secret"] =
$this
->appKey;
$urlObj
["code"] =
$code
;
$urlObj
["grant_type"] = "authorization_code";
$bizString
=
$this
->ToUrlParams(
$urlObj
);
return
"https:
}
private
function
__CreateOauthUrlForCode(
$redirectUrl
)
{
$urlObj
["appid"] =
$this
->appid;
$urlObj
["redirect_uri"] = "
$redirectUrl
";
$urlObj
["response_type"] = "code";
$urlObj
["scope"] = "snsapi_base";
$urlObj
["state"] = "STATE"."#wechat_redirect";
$bizString
=
$this
->ToUrlParams(
$urlObj
);
return
"https:
}
private
function
ToUrlParams(
$urlObj
)
{
$buff
= "";
foreach
(
$urlObj
as
$k
=>
$v
)
{
if
(
$k
!= "sign")
$buff
.=
$k
. "=" .
$v
. "&";
}
$buff
= trim(
$buff
, "&");
return
$buff
;
}
public
function
createJsBizPackage(
$openid
,
$totalFee
,
$outTradeNo
,
$sendName
,
$wishing
,
$actName
)
{
$config
=
array
(
'mch_id' =>
$this
->mchid,
'appid' =>
$this
->appid,
'key' =>
$this
->apiKey,
);
$unified
=
array
(
'wxappid' =>
$config
['appid'],
'send_name' =>
$sendName
,
'mch_id' =>
$config
['mch_id'],
'nonce_str' => self::createNonceStr(),
're_openid' =>
$openid
,
'mch_billno' =>
$outTradeNo
,
'client_ip' => '127.0.0.1',
'total_amount' =>
intval
(
$totalFee
* 100),
'total_num'=>1,
'wishing'=>
$wishing
,
'act_name'=>
$actName
,
'remark'=>'remark',
'scene_id'=>'PRODUCT_2',
);
$unified
['sign'] = self::getSign(
$unified
,
$config
['key']);
$responseXml
=
$this
->curlPost('https:
$unifiedOrder
= simplexml_load_string(
$responseXml
, 'SimpleXMLElement', LIBXML_NOCDATA);
if
(
$unifiedOrder
=== false) {
die
('parse xml error');
}
if
(
$unifiedOrder
->return_code != 'SUCCESS') {
die
(
$unifiedOrder
->return_msg);
}
if
(
$unifiedOrder
->result_code != 'SUCCESS') {
die
(
$unifiedOrder
->err_code);
}
return
true;
}
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
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);
curl_setopt(
$ch
,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt(
$ch
,CURLOPT_SSLCERT,
getcwd
().'/cert/apiclient_cert.pem');
curl_setopt(
$ch
,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt(
$ch
,CURLOPT_SSLKEY,
getcwd
().'/cert/apiclient_key.pem');
$data
= curl_exec(
$ch
);
curl_close(
$ch
);
return
$data
;
}
public
static
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
;
}
public
static
function
arrayToXml(
$arr
)
{
$xml
= "<xml>";
foreach
(
$arr
as
$key
=>
$val
) {
if
(
is_numeric
(
$val
)) {
$xml
.= "<" .
$key
. ">" .
$val
. "</" .
$key
. ">";
}
else
$xml
.= "<" .
$key
. "><![CDATA[" .
$val
. "]]></" .
$key
. ">";
}
$xml
.= "</xml>";
return
$xml
;
}
public
static
function
getSign(
$params
,
$key
)
{
ksort(
$params
, SORT_STRING);
$unSignParaString
= self::formatQueryParaMap(
$params
, false);
$signStr
=
strtoupper
(md5(
$unSignParaString
. "&key=" .
$key
));
return
$signStr
;
}
protected
static
function
formatQueryParaMap(
$paraMap
,
$urlEncode
= false)
{
$buff
= "";
ksort(
$paraMap
);
foreach
(
$paraMap
as
$k
=>
$v
) {
if
(null !=
$v
&& "null" !=
$v
) {
if
(
$urlEncode
) {
$v
= urlencode(
$v
);
}
$buff
.=
$k
. "=" .
$v
. "&";
}
}
$reqPar
= '';
if
(
strlen
(
$buff
) > 0) {
$reqPar
=
substr
(
$buff
, 0,
strlen
(
$buff
) - 1);
}
return
$reqPar
;
}
}
?>