微信支付v3版本之app支付

本文详细介绍了微信支付V3版本中App支付的实现过程,包括公共参数配置、MD5加密算法、XML解析以及支付工具类的使用。在支付回调和订单查询部分,展示了如何处理支付成功的业务逻辑以及如何验证和响应微信的支付通知。
摘要由CSDN通过智能技术生成

首次开发微信支付,大部分代码都是参考

http://blog.csdn.net/wangqiuyun/article/details/51241064


1.公共参数配置

CommonsConstants{

    public static final String APP_ID  = "......";//微信开放平台审核通过的应用APPID
    
    public static final String MCH_ID = "......";//商户号
    
    public static final String PARTNER_KEY = ".......";//API的key
    key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置   

    public static final String CREATE_ORDER_URL="https://api.mch.weixin.qq.com/pay/unifiedorder"; //生成支付订单的URL
    
    public static final String QUERY_ORDER_URL="https://api.mch.weixin.qq.com/pay/orderquery"; //查询订单的URL
    
    public static final String CLOSE_ORDER_URL="https://api.mch.weixin.qq.com/pay/closeorder"; //关闭订单的URL
    
    public static final String NOTIFY_URL="http://........./PayController/payResult.do";//异步通知地址  
    
    public static final String BODY="云禽通商城商品";//描述
    
    public static final String TRADE_TYPE="APP";//支付类型

2.Md5加密算法

import java.security.MessageDigest;

public class MD5Utils {

    private static String byteArrayToHexString(byte b[]) {
          StringBuffer resultSb = new StringBuffer();
          for (int i = 0; i < b.length; i++)
             resultSb.append(byteToHexString(b[i]));

          return resultSb.toString();
       }

       private static String byteToHexString(byte b) {
          int n = b;
          if (n < 0)
             n += 256;
          int d1 = n / 16;
          int d2 = n % 16;
          return hexDigits[d1] + hexDigits[d2];
       }

       public static String MD5Encode(String origin, String charsetname) {
          String resultString = null;
          try {
             resultString = new String(origin);
             MessageDigest md = MessageDigest.getInstance("MD5");
             if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString
                      .getBytes()));
             else
                resultString = byteArrayToHexString(md.digest(resultString
                      .getBytes(charsetname)));
          } catch (Exception exception) {
          }
          return resultString;
       }

       private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}


3.xml解析

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class XMLUtil {

       /**
        * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
        * @param strxml
        * @return
        * @throws JDOMException
        * @throws IOException
        */
       public static Map doXMLParse(String strxml) throws JDOMException, IOException {
          strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

          if(null == strxml || "".equals(strxml)) {
             return null;
          }

          Map map = new HashMap();

          InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
          SAXBuilder builder = new SAXBuilder();
          Document doc = builder.build(in);
          Element root = doc.getRootElement();
          List list = root.getChildren();
          Iterator it = list.iterator();
          while(it.hasNext()) {
             Element e = (Element) it.next();
             String k = e.getName();
             String v = "";
             List children = e.getChildren();
             if(children.isEmpty()) {
                v = e.getTextNormalize();
             } else {
                v = XMLUtil.getChildrenText(children);
             }

             map.put(k, v);
          }

          //关闭流
          in.close();

          return map;
       }

       /**
        * 获取子结点的xml
        * @param children
        * @return String
        */
       public static String getChildrenText(List children) {
          StringBuffer sb = new StringBuffer();
          if(!children.isEmpty()) {
             Iterator it = children.iterator();
             while(it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if(!list.isEmpty()) {
                   sb.append(XMLUtil.getChildrenText(list));
                }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值