您的位置:首页 > 移动开发 > 微信开发

微信jsapi支付,缺少参数。

2017-05-12 23:19 369 查看

如果你收到了微信官方的订单xml,解析成json发送给网页,前端显示json接收也没有问题,但是一直提示你缺少参数的话,你可以试着把最近生成的未支付的json直接复制到WeixinJSBridge.invoke()中去,去掉双引号,如果这样是可以支付的。那可以参照我下面代码的方式,手动解析json然后以键值对的方式放到WeixinJSBridge.invoke()中。原因我猜测需要的是一个对象而并非字符串,我传的不是json,是组合的字符串。下面的代码因为我需要删除一些东西和包含了测试的一些代码最主要csdn这个格式有点麻烦,所以请不要直接使用,可能存在一些错误。php学了才1个多月,水平有限见谅。

微信支付注意事项

1.变量名请依照官方文档中的变量名否则会出现很多不必要的错误,调试起来也很麻烦。特别注意官方文档中也存在着一些大小写的不同。

2.PHP,time生成的timestamp是int类型的所以必要要强转成string类型。

3.前端网页所需要的不是一个json,猜测是对象,我是采用键值对的方式实现的。

4XML的签名可以使用官方的签名校验工具进行自检。

前端网页php脚本

/*
以下文件需要自己按照自的目录加载,注意要修改官方sdk中config文件中ID,KEY等变量。
*/
ini_set('date.timezone','Asia/Shanghai');
require_once ("./lib/WxPay.Api.php");
require_once ("./WxPay.JsApiPay.php");
require_once ('./log.php');

/*
调用sdk获取openid。
获取web的ip地址。
支付类型。
支付金额可自行js设置好,后台生成也可以。我这里是写的一分钱。

*/
$tools = new JsApiPay();
$openId =$tools->GetOpenid();
$ip = $_SERVER['REMOTE_ADDR'];
$trade_type = 'JSAPI';
$total_fee = '1';


微信支付类文件

<?php

//微信申请支付的类。

class WxOrder
{

const APPID = '';
const MCHID = '';
const KEY = '';
const APPSECRET = '';

//获取随机字符

public static function getNonceStr($length = 32)
{

$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ )  {
$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;

}

//
//将得到的订单xml打包成字符串传回前端,这里绝对不可以直接返回json格式的数据。
//因为前端其实需要的并不是一个json字符串,我猜测是一个对象。总之传json字符串是同步多的。
//我是穿了一个以&为分隔符的字符串,发送到前端进行分割。

//
public static function returnOrder($xml)
{

$file = fopen('return.txt', 'w');

//fwrite($file, $xml."\n"."---------------------------");

libxml_disable_entity_loader(true);

$order = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);

$timestamp = strval(time());
//$timestamp = time();

$result = json_decode(json_encode($order),true);

//fwrite($file,implode("", $result));

//return $result['appid'];

//return "hello";

$new = array(

'appId' =>'',
'timeStamp' => $timestamp,
'nonceStr' =>$result['nonce_str'],
'package' =>"prepay_id=".$result['prepay_id'],
'signType' =>"MD5"

);

$sign = WxOrder::getSign($new);

$new['paySign'] = $sign;

//fwrite($file, json_encode($new));

return implode("&", $new);

}

//生成签名

public static function getSign($array)
{

ksort($array);
$buff = '';
foreach ($array as $key => $value) {

$new[] = $key."=".$value;

}

$buff = implode("&", $new);

$file=fopen('mdmdmd.txt','w');
fwrite($file, $buff);

fclose($file);

$str = $buff."&key=";
fwrite($file, "-------".$str);
$str = md5($str);

$result = strtoupper($str);

return $result;

}

//发送post请求给微信接口得到订单号等信息。

public static  function postXmlCurl($xml,$url,$second=30)
{

$ch = curl_init();
$file = fopen('xml.txt','a+');
fwrite($file, $xml);
curl_setopt($ch,CURLOPT_URL, $url);
//curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
//curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);

fwrite($file, $data);
fclose($file);

if($data)
{
return $data;
}

return null;

}

}

?>

前端ajax

function jsApiCall()
{

var dev_num = $('#dev_num').val();
var power_num = $('#power_num').val();
$.post("你自己后台接口",{

dev_num : dev_num,
power_num : power_num,
open_id : "<?php   echo $openId;?>",
total_fee : "<?php echo $total_fee;?>",
trade_type : "<?php echo $trade_type;?>",
ip : "<?php echo $ip;?>"

//openid :<?php //echo $openId;  ?>

},function(data)
{

var array = data.split("&");
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{
"appId":array[0],
"timeStamp":array[1],
"nonceStr":array[2],
"package":array[3],
"signType":array[4],
"paySign":array[5]
},
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_code+res.err_desc+res.err_msg);
if(res.err_msg =="get_brand_wcpay_request:ok")
{
alert("支付成功");
}else{
alert("支付失败");

}
}
);

});




php后台脚本

<?php

require_once"./WxOrder.php";

$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

$open_id = isset($_POST['open_id']) ? $_POST['open_id']:null;

//一下是一些我需要参数的。
$dev_num  = isset($_POST['dev_num']) ? $_POST['dev_num'] : null;

$power_num = isset($_POST['power_num']) ? $_POST['power_num'] :null;

$total_fee = isset($_POST['total_fee']) ? $_POST['total_fee'] :null;

$spbill_create_ip = isset($_POST['ip']) ? $_POST['ip'] :null;

$trade_type = isset($_POST['trade_type']) ? $_POST['trade_type'] :null;

$nonce_str = WxOrder::getNonceStr(32);

$out_trade_no = date('YmdHis').$dev_num.$power_num;

$body = '';
$notify_url = '';

$app_id ='';
$mch_id = '';

$order_array=  array(

'appid' => $app_id,
'mch_id'=> $mch_id,
'openid'=>$open_id,
'nonce_str' => $nonce_str,
'body' => $body,
'out_trade_no' =>$out_trade_no,
'total_fee' => $total_fee,
'spbill_create_ip' => $spbill_create_ip,
'notify_url' => $notify_url,
'trade_type' =>$trade_type

);

$file = fopen('order.txt','a+');
$sign = WxOrder::getSign($order_array);
$order_xml="
<xml>
<appid><![CDATA[%s]]></appid>
<body><![CDATA[%s]]></body>
<mch_id><![CDATA[%s]]></mch_id>
<nonce_str><![CDATA[%s]]></nonce_str>
<notify_url><![CDATA[%s]]></notify_url>
<openid><![CDATA[%s]]></openid>
<out_trade_no><![CDATA[%s]]></out_trade_no>
<spbill_create_ip><![CDATA[%s]]></spbill_create_ip>
<total_fee><![CDATA[%d]]></total_fee>
<trade_type><![CDATA[%s]]></trade_type>
<sign><![CDATA[%s]]></sign>
</xml>
";

//这里缺少参数请留意。。。。。。
$xml = sprintf($order_xml,$body,$nonce_str,$notify_url,$open_id,$out_trade_no,$spbill_create_ip,$total_fee,$trade_type,$sign);

fwrite($file, $xml);

$data = WxOrder::postXmlCurl($xml,$url,30);

fclose($file);

echo WxOrder::returnOrder($data);









                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: