如何实现微信付费

//实体类
@Component
@ConfigurationProperties(prefix="qyweixin")
@PropertySource(value = {"classpath:qiyeweixin.properties"})
public class Appid {
    public  String sCorpID;
    public  String sToken;
    public  String sEncodingAESKey;
    public  String Secret;
@RequestMapping("/native")
    @ResponseBody
    public Map<String, String> natives(HttpServletRequest request) throws Exception{
        // 生成订单
        TreeMap<String, String> packageParams = new TreeMap<String, String>();
        // 企业号或公众号id  商户号
        packageParams.put("appid", we.getAppid());
        // 企业收款账号
        packageParams.put("mch_id", we.getPartner());
        // 随机字符串
        String nonce_str = TenpayUtil.getRandomStr();
        packageParams.put("nonce_str", nonce_str);
        //商品名称
        packageParams.put("body", "测试");
        // 附加数据
        packageParams.put("attach", "");
        // 订单号
        String out_trade_no = TenpayUtil.getRandomStr();
        packageParams.put("out_trade_no", out_trade_no);
 
        // 支付总金额(微信官方的支付单位是分)
        String totalFee=TenpayUtil.getMoney("0.01");
        packageParams.put("total_fee", totalFee);
        
        // 生成订单的机器IP
        String ip =     request.getRemoteAddr();
        packageParams.put("spbill_create_ip", ip);
        
        // 回调URL
        packageParams.put("notify_url", we.getNotifyurl());
 
        // 设置支付方式
        packageParams.put("trade_type", "NATIVE");
 
        // 生成数字签名
        String sign = MD5Util.createSign(packageParams, we.getAppid(),
                we.getPartnerkey());
        //out_trade_no  totalFee  ip
        jdbc.update("insert into t_weixin(id,out_trade_no,totalFee,ip,flag,create_date,create_user)values(?,?,?,?,?,now(),?)",new Object[]{UUID.randomUUID().toString().replaceAll("-",""),out_trade_no,totalFee,ip,"1","张崇俊"});
        // 拼接xml数据
        String xml = "<xml>" + "<appid>" + we.getAppid() + "</appid>"
                + "<mch_id>" + we.getPartner() + "</mch_id>" + "<nonce_str>"
                + nonce_str + "</nonce_str>" + "<sign>" + sign + "</sign>"
                + "<body><![CDATA[测试]]></body>" + "<out_trade_no>"
                + out_trade_no + "</out_trade_no>" + "<attach>" + ""
                + "</attach>" + "<total_fee>" + totalFee + "</total_fee>"
                + "<spbill_create_ip>" + ip    + "</spbill_create_ip>" 
                + "<notify_url>" + we.getNotifyurl()    + "</notify_url>" 
                + "<trade_type>" + "NATIVE"    + "</trade_type>" + "</xml>";
        //通过HttpClient对象向微信支付后台发送xml数据
        String code_url="";
        Map<String, String> maps = new TreeMap<String,String>();
        try {
            String createOrederURL="https://api.mch.weixin.qq.com/pay/unifiedorder";
            HttpClient httpClient=new HttpClient(createOrederURL);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xml);
            httpClient.post();
            String xmlStr=httpClient.getContent(); //从微信支付后台获取订单信息
            System.out.println(xmlStr);
            
            //解析xml数据得到用于支付的url地址
            Map<String, String> map =TenpayUtil.doXMLParse(xmlStr);
            maps.put("code_url", map.get("code_url"));
            
            maps.put("out_trade_no", out_trade_no);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回订单信息到native.jsp
        return maps;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值