微信小程序支持调用微信支付,微信小程序内部只能调起预支付接口,其中预支付接口需要的参数,需要后端人员请求微信进行获得。这其中微信小程序需要开通或者绑定微信商户平台的账号,并且两者所属主体必须是同一个。
这里需要得到
$appid = "";//小程序
$secret = "";//小程序
$mch_id = "";//商户号
$body = "";//支付说明 腾讯充值
$fee = "";//价格 大于0.01
$order = "";//订单号,一个订单号只能调用一次微信支付
$ip = "";//后端服务器ip地址
$openid = "";//发起微信支付的用户openid
$key = "";//微信商户的key,可在微信商户平台生成
这些是必要参数。
接下来是根据已经有的参数和值生成签名,再把签名更换成xml格式,请求微信,得到返回值,得到的值为xml格式。需要转换为数组。最后将得到的参数发给小程序。附上小程序支付代码。在tp5 根目录extend 下面创建first目录,再新建Wechat.php文件,将代码复制到文件中即可。
<?php
namespace first;
class Wechat
{
private static $appid = "";
private static $secret = "";
private static $mch_id = "";
private static $body = "";
private static $fee = "";
private static $order = "";
private static $ip = "";
private static $openid = "";
private static $key = "";
public static function getprepay_id($openid="",$ordernum="",$body="",$fee="")
{
if ($openid=="" || $ordernum=="" || $body=="" || $fee=="")
{
return 0;
}
self::$openid = $openid;
self::$body = $body;
self::$fee = floatval($fee*100);
self::$order = $ordernum;
$return = self::weixinapp();
return json_encode($return);
}
private static function weixinapp()
{
$unifiedorder = self::unifiedorder();
$parameters = array(
"appId" => self::$appid,
"timeStamp" => ''.time().'',
"nonceStr" =>self::createNoncestr(),
"package" =>'prepay_id='.$unifiedorder['prepay_id'],
"signType" =>"MD5"
);
$parameters['paySign'] = self::getSign($parameters);
return $parameters;
}
private static function unifiedorder()
{
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$parameters = array(
"appid" => self::$appid,
"mch_id"=> self::$mch_id,
"nonce_str" =>self::createNoncestr() ,
"body" =>self::$body,
"out_trade_no"=>self::$order,
"total_fee"=>self::$fee,
"spbill_create_ip"=>self::$ip,
"notify_url" => 'http://www.weixin.qq.com/wxpay/pay.php',
"openid" => self::$openid,
"trade_type"=>"JSAPI"
);
$parameters['sign'] = self::getSign($parameters);
$xmlData =self::arrayToXml($parameters);
$return =self::xmlToArray(self::postXmlCurl($xmlData,$url,60));
return $return;
}
private static 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;
}
private static function getSign($obj)
{
foreach ($obj as $key => $val)
{
$Parameters[$key] = $val;
}
ksort($Parameters);
$String =self::formatBizQueryParaMap($Parameters,false);
$String =$String."&key=".self::$key;
$String =md5($String);
$result_ = strtoupper($String);
return $result_;
}
private static function formatBizQueryParaMap($paraMap,$urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v)
{
if ($urlencode)
{
$v = urlencode($v);
}
$buff .= $k ."=".$v."&";
}
$reqPar = "";
if (strlen($buff)>0)
{
$reqPar = substr($buff,0,strlen($buff)-1);
}
return $reqPar;
}
private static function arrayToXml($arr)
{
$xml = '<root>';
foreach ($arr as $key=>$val)
{
if (is_array($val))
{
$xml .="<".$key.">".self::arrayToXml($val)."</".$key.">";
}
else
{
$xml .="<".$key.">".$val."</".$key.">";
}
}
$xml.= "</root>";
return $xml;
}
private static function xmlToArray($xml)
{
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
private static function postXmlCurl($xml,$url,$second = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
set_time_limit(0);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return "curl出错,错误码:$error";
}
}
}