快3网上购买

微信APP支付(IOS手机端+java后台)版

电脑杂谈  发布时间:2019-09-02 02:02:24  来源:网络整理

java微信软件下载_java微信_手机微信java

0。介绍预览

针对需要在IOS手机上接入原生微信支付场景,调用微信进行支付。如图:

1。资料准备

1.1 账号登录

java微信_手机微信java_java微信软件下载

打开,注册微信开放系统开发者账号

1.2 开发者认证

登录手机微信java,进入账户中心,进行开发者资质认证。

1.3 注册应用

java微信_java微信软件下载_手机微信java

认证完成后,进入管理中心,新建移动应用。填写应用资料,其中android版应用签名可借助扫码安装温馨提供的应用获取,详细参考微信文档。创建完成后单击查看手机微信java,申请开通微信支付。一切准备就绪!

2。Java后台开发

添加依赖

<!-- 微信支付依赖 -->
<dependency>
 <groupId>org.xmlpull</groupId>
 <artifactId>xmlpull</artifactId>
 <version>1.1.3.1 </version>
</dependency>
<dependency>
 <groupId>net.sf.json-lib</groupId>
 <artifactId>json-lib</artifactId>
 <version>2.3</version>
 <classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
 <groupId>com.thoughtworks.xstream</groupId>
 <artifactId>xstream</artifactId>
 <version>1.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ning/async-http-client -->
<dependency>
 <groupId>com.ning</groupId>
 <artifactId>async-http-client</artifactId>
 <version>1.8.13</version>
</dependency>

手机微信java_java微信软件下载_java微信

生成统一订单

@RequestMapping(value="/pay/wxpay/params",produces="application/json;charset=utf-8")
 @ResponseBody
 public String signprams(HttpServletRequest request){
  String res = "{code:404}";
  try{
  // 充值金额
   String account = request.getParameter("account");
  // 用户id
   String sid = request.getParameter("sid");
   String subject = "订单标题";
   String body = "订单描述";
 
   int acc = (int) (Double.valueOf(account) * 100);
   String appid = "您的APPID";
   String out_trade_no = "生成您的订单号";
  // 生成订单数据
   SortedMap<String, String> payMap = genOrderData(request, subject, body, acc, appid, out_trade_no);
 
   savePayLog(out_trade_no,account,sid,body,payMap.get("paySign"),nid,2);
 
   // 4.返回数据
   res = buildPayRes(payMap,out_trade_no);
  }catch (Exception e){
   e.printStackTrace();
   res = "{code:500}";
  }
  return res;
 }
 
 private SortedMap<String, String> genOrderData(HttpServletRequest request, String subject, String body,
          int acc, String appid, String out_trade_no)
   throws IOException, ExecutionException, InterruptedException, XmlPullParserException {
  SortedMap<String, String> paraMap = new TreeMap<String, String>();
  paraMap.put("appid", appid);
  paraMap.put("attach", subject);
  paraMap.put("body", body);
  paraMap.put("mch_id", "您的商户id,到商户平台查看");
  paraMap.put("nonce_str", create_nonce_str());
  paraMap.put("notify_url", "http://pay.xxxxx.com/pay/wxpay/notify.htm ");// 此路径是微信服务器调用支付结果通知路径
  paraMap.put("out_trade_no", out_trade_no);
  paraMap.put("spbill_create_ip", request.getRemoteAddr());
  paraMap.put("total_fee", acc+"");
  paraMap.put("trade_type", "APP");
  String sign = createSign(paraMap);
  paraMap.put("sign", sign);
  // 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder 
  String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  String xml = getRequestXml(paraMap);
  String xmlStr = HttpKit.post(url, xml);
 
  // 预付商品id
  String prepay_id = "";
  if (xmlStr.indexOf("SUCCESS") != -1) {
   Map<String, String> map = WXRequestUtil.doXMLParse(xmlStr);
   prepay_id = (String) map.get("prepay_id");
  }
 
  SortedMap<String, String> payMap = new TreeMap<String, String>();
  payMap.put("appid", appid);
  payMap.put("partnerid", "您的商户id,到商户平台查看");
  payMap.put("prepayid", prepay_id);
  payMap.put("package", "Sign=WXPay");
  payMap.put("noncestr", create_nonce_str());
  payMap.put("timestamp", WXRequestUtil.create_timestamp());
  String paySign = createSign(payMap);
  payMap.put("paySign", paySign);
  return payMap;
 }
 
 //请求xml组装 
 public static String getRequestXml(SortedMap<String,String> parameters){ 
  String sign = "";
   StringBuffer sb = new StringBuffer(); 
   sb.append("<xml>"); 
   Set es = parameters.entrySet(); 
   Iterator it = es.iterator(); 
   while(it.hasNext()) { 
    Map.Entry entry = (Map.Entry)it.next(); 
    String key = (String)entry.getKey(); 
    String value = (String)entry.getValue(); 
//    if ("attach".equalsIgnoreCase(key)||"body".equalsIgnoreCase(key)||"sign".equalsIgnoreCase(key)) { 
//     sb.append("<"+key+">"+value+"</"+key+">"); 
//    }
    if ("sign".equalsIgnoreCase(key)){
    sign = "<"+key+">"+value+"</"+key+">";
    }else { 
     sb.append("<"+key+">"+value+"</"+key+">"); 
    } 
   } 
   sb.append(sign); 
   sb.append("</xml>"); 
   return sb.toString(); 
  } 
 
 //生成签名 
 public String createSign(SortedMap<String,String> parameters){ 
   StringBuffer sb = new StringBuffer(); 
   Set es = parameters.entrySet(); 
   Iterator it = es.iterator(); 
   while(it.hasNext()) { 
    Map.Entry entry = (Map.Entry)it.next(); 
    String k = (String)entry.getKey(); 
    Object v = entry.getValue(); 
    if(null != v && !"".equals(v) 
      && !"sign".equals(k) && !"key".equals(k)) { 
     sb.append(k + "=" + v + "&"); 
    } 
   } 
   sb.append("key=" + WXConfig.APP_PERTNER_KEY); 
   System.out.println(sb.toString());
   String sign = MD5Utils.MD5Encode(sb.toString(),"UTF-8").toUpperCase(); 
   return sign; 
  } 
 
 
 public String create_nonce_str() {
 String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 String res = "";
 for (int i = 0; i < 32; i++) {
 Random rd = new Random();
 res += chars.charAt(rd.nextInt(chars.length() - 1));
 }
 return res;
 }


本文来自电脑杂谈,转载请注明本文网址:
快3网上购买http://www.kadakong.com/a/sanxing/article-121389-1.html

相关阅读
发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

吉林快3 千禧彩票是真的吗 英豪彩票开户 江西快3 万发彩票平台 快三网上投注 百胜彩票投注 快三投注网 冠军彩票开奖 河南快3