完整微信小程序支付代码+支付工具

该博客展示了如何实现微信小程序支付,包括关键的Java代码片段,如`WxPayController`、`OuterOrderWxPay`实体类以及`MessageUtil`和`PayUtil`工具类,用于处理支付参数、签名和请求。
摘要由CSDN通过智能技术生成

package api.iruhua.controller;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.haierp.util.PropertiesUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.model.sale.OuterOrderWxPay;
import com.haierp.service.IOuterOrderWxPayService;
import com.haierp.util.WxPay.MessageUtil;
import com.haierp.util.WxPay.PayUtil;
import com.haierp.util.WxPay.PaymentPo;
import com.haierp.util.WxPay.UUIDHexGenerator;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(“/wx/pay”)
public class WxPayController {
private String total_fee;//总金额
private String body;//商品描述
private String detail;//商品详情
private String attach;//附加数据
private String time_start;//交易起始时间
private String time_expire;//交易结束时间
private String openid;//用户标识

@Autowired
private IOuterOrderWxPayService outerOrderWxPayService;

@RequestMapping("/unifiedorder")
@ResponseBody
public Object pay(HttpServletRequest request) throws UnsupportedEncodingException, DocumentException{
    body = new String(request.getParameter("body").getBytes("UTF-8"), "ISO-8859-1");
    String appid = PropertiesUtils.getProperties().getProperty("wx.sale.appId");//小程序ID
    String mch_id = "1505845591";//商户号
    String nonce_str = UUIDHexGenerator.generate();//随机字符串
    String today = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String code = PayUtil.createCode(8);
    String out_trade_no = mch_id+today+code;//商户订单号
    String spbill_create_ip = request.getRemoteAddr();//终端IP
    String notify_url = "http://www.weixin.qq.com/wxpay/pay.php";//通知地址
    String trade_type = "JSAPI";//交易类型  
    String openid=request.getParameter("openId");//用户标识

    //封装支付参数
    PaymentPo paymentPo = new PaymentPo();

    paymentPo.setAppid(appid);
    paymentPo.setMch_id(mch_id);
    paymentPo.setNonce_str(nonce_str);
    String newbody=new String(body.getBytes("ISO-8859-1"),"UTF-8");//以utf-8编码放入paymentPo,微信支付要求字符编码统一采用UTF-8字符编码
    paymentPo.setBody(newbody);
    paymentPo.setOut_trade_no(out_trade_no);
    paymentPo.setTotal_fee(request.getParameter("total_fee"));
    paymentPo.setSpbill_create_ip(spbill_create_ip);
    paymentPo.setNotify_url(notify_url);
    paymentPo.setTrade_type(trade_type);
    paymentPo.setOpenid(openid);

    // 把请求参数打包成Map
    Map<String, String> sParaTemp = new HashMap<String, String>();
    sParaTemp.put("appid", paymentPo.getAppid());
    sParaTemp.put("mch_id", paymentPo.getMch_id());
    sParaTemp.put("nonce_str", paymentPo.getNonce_str());
    sParaTemp.put("body",  paymentPo.getBody());
    sParaTemp.put("out_trade_no", paymentPo.getOut_trade_no());
    sParaTemp.put("total_fee",paymentPo.getTotal_fee());
    sParaTemp.put("spbill_create_ip", paymentPo.getSpbill_create_ip());
    sParaTemp.put("notify_url",paymentPo.getNotify_url());
    sParaTemp.put("trade_type", paymentPo.getTrade_type());
    sParaTemp.put("openid", paymentPo.getOpenid());

    // 除去Map中的空值和签名参数
    Map<String, String> sPara = PayUtil.paraFilter(sParaTemp);
    String prestr = PayUtil.createLinkString(sPara); // 把Map所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
    String key = "&key=Iruha2017Iruha2018Iruha2019Iruha"; // 商户支付密钥
    //MD5运算生成签名
    String mysign = PayUtil.sign(prestr, key, "utf-8").toUpperCase();
    paymentPo.setSign(mysign);
    //打包要发送的xml
    String respXml = MessageUtil.messageToXML(paymentPo);
    // 打印respXml发现,得到的xml中有“__”不对,应该替换成“_”
    respXml = respXml.replace("__", "_");
    String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//统一下单API接口链接
    String param = respXml;
    String result =PayUtil.httpRequest(url, "POST", param);
    // 将解析结果存储在HashMap中
    Map<String, String> map = new HashMap<String, String>();
    InputStream in=new ByteArrayInputStream(result.getBytes());  
    // 读取输入流
    SAXReader reader = new SAXReader();
    Document document = reader.read(in);
    // 得到xml根元素
    Element root = document.getRootElement();
    // 得到根元素的所有子节点
    @SuppressWarnings("unche
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值