多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 注意:本文档后期不在跟新,新文档迁移到:[http://www.openkit.cc/docs/openkit/#/](http://www.openkit.cc/docs/openkit/#/) # 首先感谢使用OpenKit工具包 ## 反馈交流QQ群:696279396 # 注意这里暂时只支持APP端支付,网页端支付将在2.0版本增加 - 首先我们需要 new 一个 WeichartModel 对象 ```java /** * appId:(必填)微信开发平台应用ID * appSecret:(必填)应用对应的凭证 * appKey:(必填)应用对应的密钥 * mchId:(必填)微信支付商户号 * body:(必填)商品描述 * partnerKey:(必填)商户号对应的密钥 * partnerId:(必填)商户id * grantType:(必填)常量固定值 * gateUrl:(必填)获取预支付id的接口url * notifyUrl:(必填)微信服务器回调通知url * spbillCreateIp:(必填)请求的 IP 地址,其实就是服务器的IP地址 */ WeichartModel weichartModel = new WeichartModel() ``` - 接下去,实例化微信支付接口 ``` WeixinPayService weixinPayService = new WeixinPayServiceImpl(); ``` - 接着,你只要直接调用下面的方法就可以了 ``` /** * 统一订单生成接口 * @param request * @param response * @param weichartModel 微信配置 * @param totalFee 价格 * @param out_trade_no 订单号 * @param notify_url 回调地址 * @return code 根据这个判断微信支付订单是否生成成功,1:失败,0:成功 * @throws Exception */ weixinPayService.getOrder(HttpServletRequest request, HttpServletResponse response, WeichartModel weichartModel,String totalFee, String out_trade_no, String notify_url); ``` - 返回值中 code 判断是订单号生成成功还是失败 1:失败,0:成功 - 回调函数编写示例 ```java /** * 接收微信支付成功通知 * @param request * @param response * @throws IOException */ @Override public Map<String, String> getnotify(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter writer = response.getWriter(); InputStream inStream = request.getInputStream(); ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); String result = new String(outSteam.toByteArray(), "utf-8"); System.out.println("微信支付通知结果:" + result); Map<String, String> map = null; try { /** * 解析微信通知返回的信息 */ map = XMLUtil.doXMLParse(result); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("=========:" + result); return map; // 若支付成功,则告知微信服务器收到通知 if (map.get("return_code").equals("SUCCESS")) { if (map.get("result_code").equals("SUCCESS")) { System.out.println("充值成功!"); // 查找 PayRecord payRecord=payRecordService.get(Long.valueOf(map.get("out_trade_no"))); System.out.println("订单号:"+Long.valueOf(map.get("out_trade_no"))); System.out.println("payRecord.getPayTime():"+payRecord.getPayTime()==null+","+payRecord.getPayTime()); //判断通知是否已处理,若已处理,则不予处理 if(payRecord.getPayTime()==null){ System.out.println("通知微信后台"); payRecord.setPayTime(new Date()); String phone=payRecord.getPhone(); AppCustomer appCustomer=appCustomerService.getByPhone(phone); float balance=appCustomer.getBalance(); balance+=Float.valueOf(map.get("total_fee"))/100; appCustomer.setBalance(balance); appCustomerService.update(appCustomer); payRecordService.update(payRecord); String notifyStr = XMLUtil.setXML("SUCCESS", ""); writer.write(notifyStr); writer.flush(); } } } ``` ## 回调说明 * 此方法用于微信支付回调统一调用工具了,将 Xml 文件替换成 Map 文件返回 * 参数说明: |字段名 | 变量名 | 是否必填 | 类型 | 示例 | 描述| |:--:|:--:|:--:|:--:|:--:|:--:| |返回状态码 | return_code | 是 | String(16) | SUCCESS | SUCCESS/LFAI此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断| |返回信息 | return_msg | 否 | String(128) | 签名失败 | 返回信息,如非空,为错误原因,签名失败,参数格式校验错误| ||||||| |应用ID | appid | 是 | String(32) |wx8888888888888888 |微信开放平台审核通过的应用APPID| |商户号 | mch_id | 是 | String(32) |1900000109 | 微信支付分配的商户号| |设备号 | device_info | 否 | String(32) |013467007045764 | 微信支付分配的终端设备号, |随机字符串 | nonce_str | 是 | String(32) |5K8264ILTKCH16CQ2502SI8ZNMTM67VS| 随机字符串,不长于32位| |签名 | sign | 是 | String(32)| C380BEC2BFD727A4B6845133519F3AD6 |签名| |错误代码 | err_code | 否 |String(32) |SYSTEMERROR | 错误返回的信息描述| |错误代码描述 |err_code_des | 否 |String(128) |系统错误| 错误返回的信息描述| |用户标识 | openid | 是 | String(128)| wxd930ea5d5a258f4f |用户在商户appid下的唯一标识| |是否关注公众账号 |is_subscribe| 否| String(1) |Y |用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效| |交易类型| trade_type |是 |String(16) |APP| APP| |付款银行 |bank_type| 是 |String(16) |CMC| 银行类型,采用字符串类型的银行标识,银行类型见| |总金额 |total_fee |是 |Int |100 |订单总金额,单位为分| |货币种类| fee_type |否 |String(8) |CNY| 货币类型,符合ISO4217标准的三位字母代码,默认人民币:CNY,其他值列表详见| |现金支付金额| cash_fee |是| Int |100 |现金支付金额订单现金支付金额| |现金支付货币类型 |cash_fee_type |否 |String(16) |CNY| 货币类型,符合ISO4217标准的三位字母代码,默认人民币:CNY| |代金券金额 |coupon_fee| 否| Int |10| 代金券或立减优惠金额<=订单总金额,订单总金额-代金券或立减优惠金额=现金支付金额| |代金券使用数量| coupon_count| 否| Int |1 |代金券或立减优惠使用数量| |代金券ID| coupon_id_$n |否| String(20)| 10000 |代金券或立减优惠ID,$n为下标,从0开始编号| |单个代金券支付金额 |coupon_fee_$n |否| Int| 100 |单个代金券或立减优惠支付金额,$n为下标,从0开始编号| |微信支付订单号| transaction_id |是 |String(32) |1217752501201407033233368018 |微信支付订单号| | 商户订单号| out_trade_no| 是| String(32) |1212321211201407033568112322| 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|*@ ,且在同一个商户号下唯一。| | 商家数据包 |attach| 否 |String(128) |123456 |商家数据包,原样返回| |支付完成时间 |time_end| 是 |String(14) |20141030133525 |支付完成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010| ## 反馈交流QQ群:696279396 ## 赞助 ![](https://box.kancloud.cn/bf6f9c0e4f5a786c20d02c56b56a0eaf_600x500.png)