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

本文介绍了微信支付统一下单的Java实现步骤,包括准备接口文档、交互流程、统一下单请求发送以及遇到的问题。详细讲解了如何构造HTTP请求,发送统一下单请求,并提醒开发者注意keystore验证和手机上需安装微信APP。
摘要由CSDN通过智能技术生成

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

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】

然后是微信支付交互过程

3fe0d50499fec60a5685eb2dc084fb24.png

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(

"");

requestStr.append("

requestStr.append(wxdata.getAppid());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getBody());

requestStr.append("]]>");

requestStr.append("

requestStr.append(wxdata.getMch_id());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getNonce_str());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getNotify_url());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getOut_trade_no());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getSpbill_create_ip());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getTotal_fee());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getTrade_type());

requestStr.append("]]>

");

requestStr.append("

requestStr.append(wxdata.getSign());

requestStr.append("]]>

");

requestStr.append("");

// 发送请求

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值