微信支付java端之小程序

微信支付java端之小程序

<!-- 微信支付sdk -->
		<dependency>
			<groupId>com.github.wxpay</groupId>
			<artifactId>wxpay-sdk</artifactId>
			<version>0.0.3</version>
		</dependency>
@Slf4j
public class WeChatConfig {
    @ApiModelProperty(value = "微信支付分配的公众账号ID")
    public final static String appId="";
    @ApiModelProperty(value = "secret")
    public final static String secret="";
    @ApiModelProperty(value = "微信支付分配的商户号")
    public final static String mchId="";
    @ApiModelProperty(value = "商户私钥")
    public final static String privateKey="";
    @ApiModelProperty(value = "获取token地址")
    public final static String tokenUrl="https://api.weixin.qq.com/sns/oauth2/access_token";
    @ApiModelProperty(value = "支付地址")
    public final static String payUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";
    @ApiModelProperty(value = "支付回调地址")
    public final static String notifyUrl="";

    /**
     * @Description 根据code获取微信登录的token
     * @param code:
     * @author: CXI
     * @date: 2020/11/19
     * @return: java.util.Map<java.lang.String, java.lang.String>
     */
    public static Map<String,String> getWeChatToken(String code){
        log.info("微信登录code:"+code);
        String url=tokenUrl+"?appid="+appId+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        Map<String,String> hashMap= JSON.parseObject(HttpClient.doGet(url), HashMap.class);
        log.info("微信登录返回参数:"+hashMap);
        return hashMap;
    }
    /**
     * @Description 根据code换openId
     * @param code:
     * @author: CXI
     * @date: 2020/11/19
     * @return: java.lang.String
     */
    public static String getOpenId(String code){
        Map<String,String> map=getWeChatToken(code);
        if (map.containsKey("errcode")){
            return map.toString();
        }
        return map.get("openid");
    }

}
@Data
public class WeChatPay {
    private String signType;
    @ApiModelProperty(value = "商品描述")
    private String body;
    @ApiModelProperty(value = "商户系统内部订单号")
    private String outTradeNo;
    @ApiModelProperty(value = "订单总金额(单位为分)")
    private Integer totalFee;
    @ApiModelProperty(value = "终端IP")
    private String spbillCreateIp;
    @ApiModelProperty(value = "交易类型")
    private String tradeType;
    @ApiModelProperty(value = "用户标识")
    private String openId;

    public WeChatPay(){
        super();
    }

    public WeChatPay(String outTradeNo, Integer totalFee, String spbillCreateIp,  String openId) {
        this.signType="MD5";
        this.body = "众云-在线点餐";
        this.outTradeNo = outTradeNo;
        this.totalFee = totalFee;
        this.spbillCreateIp = spbillCreateIp;
        this.tradeType = "JSAPI";
        this.openId = openId;
    }
}
public static Map<String, String> wxOrder(WeChatPay pay) {
        try {
            //文档地址https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1
            // 拼接统一下单地址参数
            log.info("pay"+pay);
            Map<String, String> paraMap = new HashMap<>();
            paraMap.put("trade_type",pay.getTradeType());
            paraMap.put("openid", pay.getOpenId());
            paraMap.put("appid", WeChatConfig.appId); // 公众账号ID
            paraMap.put("mch_id", WeChatConfig.mchId); // 商户ID
            String nonceStr=WXPayUtil.generateNonceStr();
            paraMap.put("nonce_str",nonceStr); //随机字符串UUID
            paraMap.put("body", pay.getBody()); // 商家名称-销售商品类目、String(128)
            paraMap.put("out_trade_no", pay.getOutTradeNo());// 订单号,每次都不同
            paraMap.put("total_fee", String.valueOf(pay.getTotalFee())); // 支付金额,单位分
            paraMap.put("spbill_create_ip", pay.getSpbillCreateIp());
            paraMap.put("notify_url", WeChatConfig.notifyUrl);// 此路径是微信服务器调用支付结果通知路径随意写
            String sign = WXPayUtil.generateSignature(paraMap, WeChatConfig.privateKey);
            paraMap.put("sign", sign);
            String xml = WXPayUtil.mapToXml(paraMap);// 将所有参数(map)转xml格式
            String url = WeChatConfig.payUrl;
            log.info("微信支付下单参数:"+xml);
            //发送post请求"统一下单接口"返回预支付id:prepay_id
            String xmlStr = HttpClient.httpsRequest(url,"POST", xml);
            log.info("微信支付返回参数:"+xmlStr);
            // 以下内容是返回前端页面的json数据
            if (xmlStr.indexOf("SUCCESS") != -1) {
                Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
                Map<String, String> payMap = new HashMap<String, String>();
                payMap.put("appId", WeChatConfig.appId);
                payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
                payMap.put("nonceStr", WXPayUtil.generateNonceStr());
                payMap.put("signType", "MD5");
                payMap.put("package", "prepay_id=" + map.get("prepay_id"));
                String paySign = WXPayUtil.generateSignature(payMap, WeChatConfig.privateKey);
                payMap.put("paySign", paySign);
                log.info("微信支付返回给前端参数:"+payMap);
                return payMap;
            }else {
                return null;
            }
        } catch (Exception e) {
            log.info("微信支付下单异常");
            e.printStackTrace();
            return null;
        }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值