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

微信支付(java版本)_统一下单

2016-08-23 14:39 716 查看
最近工作接触到微信支付,刚开始解决微信支付很神秘,接触之后发现并没有那么神秘,就是有很多坑,在开发的时候需要注意,整理出来:

1.准备工作

首先需要登录微信支付公众平台阅读接口文档,地址:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_12&index=2 ;

其次了解微信支付商户系统和微信支付系统主要交互说明:

步骤1:用户在商户APP中选择商品,提交订单,选择微信支付。

步骤2:商户后台收到用户支付单,调用微信支付统一下单接口。参见【统一下单API】。

步骤3:统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP。参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。注意:package的值格式为Sign=WXPay

步骤4:商户APP调起微信支付。api参见本章节【app端开发步骤说明

步骤5:商户后台接收支付通知。api参见【支付结果通知API

步骤6:商户后台查询支付结果。,api参见【查询订单API

然后是微信支付交互过程



2.开始工作

不需要导入微信支付的jar包,需要哪些jar导入哪些就可以,

发送统一下单请求调用:

/**

* @方法名称:sendWxPayRequest
* @内容摘要: <发送统一下单请求>
* @param body
* @param outTradeNo
* @param totalFee
* @param spBillCreateIP
* @return 
* String
* @exception 
* @author:鹿伟伟
* @创建日期:2016年2月19日-下午2:24:05
*/
public String sendWxPayRequest(String body,String detail,String outTradeNo,int totalFee,String spBillCreateIP

    )
{
// 构造HTTP请求
HttpClient httpclient = new HttpClient();
PostMethod postMethod = new PostMethod(Configure.PAY_API);
WxPayReqData wxdata = new WxPayReqData(body,detail,outTradeNo,totalFee,spBillCreateIP);
StringBuffer requestStr = new StringBuffer(
"<xml>");
requestStr.append("<appid><![CDATA[");
requestStr.append(wxdata.getAppid());
requestStr.append("]]></appid>");
requestStr.append("<body><![CDATA[");
requestStr.append(wxdata.getBody());
requestStr.append("]]></body>");
requestStr.append("<mch_id><![CDATA[");
requestStr.append(wxdata.getMch_id());
requestStr.append("]]></mch_id>");
requestStr.append("<nonce_str><![CDATA[");
requestStr.append(wxdata.getNonce_str());
requestStr.append("]]></nonce_str>");
requestStr.append("<notify_url><![CDATA[");
requestStr.append(wxdata.getNotify_url());
requestStr.append("]]></notify_url>");
requestStr.append("<out_trade_no><![CDATA[");
requestStr.append(wxdata.getOut_trade_no());
requestStr.append("]]></out_trade_no>");
requestStr.append("<spbill_create_ip><![CDATA[");
requestStr.append(wxdata.getSpbill_create_ip());
requestStr.append("]]></spbill_create_ip>");
requestStr.append("<total_fee><![CDATA[");
requestStr.append(wxdata.getTotal_fee());
requestStr.append("]]></total_fee>");
requestStr.append("<trade_type><![CDATA[");
requestStr.append(wxdata.getTrade_type());
requestStr.append("]]></trade_type>");
requestStr.append("<sign><![CDATA[");
requestStr.append(wxdata.getSign());
requestStr.append("]]></sign>");
requestStr.append("</xml>");
// 发送请求
String strResponse = null;
try {
RequestEntity entity = new StringRequestEntity(
requestStr.toString(), "text/xml", "UTF-8");
postMethod.setRequestEntity(entity);
httpclient.executeMethod(postMethod);
strResponse = new String(postMethod.getResponseBody(), "utf-8");
Logger.getLogger(getClass()).debug(strResponse);
} catch (HttpException e) {
Logger.getLogger(getClass()).error("sendWxPayRequest", e);
} catch (IOException e) {
Logger.getLogger(getClass()).error("sendWxPayRequest", e);
} finally {
postMethod.releaseConnection();
}
return strResponse;
}

3.问题:

客户端调起微信支付接口是一直返回-1:

a.你要用验证过的keystore打包出来安装才能正常进入支付,否则不能进入微信,有可能出现-1的情况;

b.你的手机确认要有安装微信app;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微信统一下单