前言:最近公司接了个商城项目,被分到了做微信支付,支付宝支付和stripe国际支付,在此记录一下。

正文:

    微信支付接入方式分很多种如下:

微信APP支付

 

比如h5,大部分业务几乎都在后台,而app支付一半在前端一半在后端,我这里讲的是app支付

官方说明文档地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1

想要调试支付,必须得注册成商户,需要有营业执照什么的,不像微信公众号会有测试公众号,这也是我们比较难测试的地方

时序图:

微信APP支付

从上面看出来,后端的工作只有三个:

1.生成加密的支付数据交给前端(统一下单,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1

2.编写后台回调(支付结果通知,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3

3.查询支付结果(查询订单,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_2&index=4

所以在app支付中我们后端只需要给前端准备数据,和接受回调就行了,还有一个查询订单支付结果,一共只3个接口需要编写,相对于h5少了很多东西。

在这里我展示统一下单,和回调方法:

第一步:

准备商户数据

微信APP支付

微信APP支付

微信APP支付

 

package com.otcbi.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.otcbi.coin.common.exception.ApiException;
import com.otcbi.coin.common.exception.ApiResponMessage;
import com.otcbi.common.dto.OperateResult;
import com.otcbi.pay.dto.ShopOrder;
import com.otcbi.pay.entity.WechatOrder;
import com.otcbi.pay.service.IWechatOrderService;
import com.otcbi.pay.service.WeiXinPayService;
import com.otcbi.pay.utils.MD5Util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class WeiXinPayServiceImpl implements WeiXinPayService {
    protected Logger logger = LoggerFactory.getLogger(WeiXinPayServiceImpl.class);

    // 正在处理的订单号
    public static Set<String> processOrder = new HashSet<String>();

    @Value("${wechat.appid}")
    private String appidConfig;

    //商户id
    @Value("${wechat.mchid}")
    private String mchidConfig;

    @Value("${wechat.apikey}")
    private String apiKeyCofig;

    @Value("${wechat.callback}")
    private String wechatCallBack;

    @Value("${wechat.lostTime}")
    private String lostTime;

    @Value("${wechat.unifiedorderUrl}")
    private String unifiedorderUrl;

    private static Set<String> commonIds = new HashSet<String>();

    @Autowired
    private IWechatOrderService iWechatOrderService;

    @Override
    public OperateResult<Map<String, Object>> buildCommonPay(String ip, ShopOrder shopOrder) {
        try {
            if (commonIds.add(String.valueOf(shopOrder.getUserId()))) {
                //简单描述
                String productName = shopOrder.getUserId() + "发起支付" + (shopOrder.getMoney()) + "元";
                return buildPrePayInfo(ip, shopOrder.getMoney().multiply(new BigDecimal(100)).intValue(), productName, shopOrder.getOrderId(), shopOrder.getUserId());
            } else {
                throw new ApiException(ApiResponMessage.SHOP_GOODS_ORDER_PAYING, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ApiException(ApiResponMessage.SHOP_GOODS_WEIXIN_PAY_ERROR, null);
        } finally {
            commonIds.remove(String.valueOf(shopOrder.getUserId()));
        }
    }


    /**
     * 生成支付的字符串
     *
     * @param ip
     * @param money
     * @param productName
     * @param orderId
     * @return
     */
    private OperateResult<Map<String, Object>> buildPrePayInfo(String ip, int money, String productName, String orderId, Long userId) throws Exception {
        //组装参数
        HashMap<String, String> map = genProductArgs(ip, money, productName, orderId, userId);
        String entity = map.get("xmlString");
        String id = map.get("id");
        //用于更新状态
        WechatOrder wechatOrder = iWechatOrderService.getById(id);
        try {
            HttpClient httpClient = new HttpClient();
            PostMethod postMethod = new PostMethod(unifiedorderUrl);
            postMethod.setRequestEntity(new ByteArrayRequestEntity(entity.getBytes()));
            int statusCode = httpClient.executeMethod(postMethod);
            byte[] responseByte = postMethod.getResponseBody();
            String responseBody = new String(responseByte, "UTF-8");

            if (statusCode == 200) {
                // 成功
                JSONObject obj = xml2jsonObject(responseBody);
                if (obj.containsKey("prepay_id")) {
                    //支付发起成功
                    return buildPayInfo(obj.getString("prepay_id"), wechatOrder);
                } else {
                    wechatOrder.setModifyTime(LocalDateTime.now());
                    wechatOrder.setState(2);
                    wechatOrder.setRemark(obj.toJSONString());
                    iWechatOrderService.updateById(wechatOrder);
                    //请求支付失败
                    logger.error("微信支付失败" + obj.toJSONString());
                    throw new ApiException(ApiResponMessage.SHOP_GOODS_WEIXIN_PAY_ERROR, null);
                }
            } else {
                wechatOrder.setModifyTime(LocalDateTime.now());
                wechatOrder.setState(2);
                wechatOrder.setRemark(Integer.toString(statusCode) + ":" + responseBody);
                iWechatOrderService.updateById(wechatOrder);
                throw new ApiException(ApiResponMessage.SHOP_GOODS_WEIXIN_PAY_ERROR, null);
            }
        } catch (Exception e) {
            wechatOrder.setModifyTime(LocalDateTime.now());
            wechatOrder.setState(2);
            iWechatOrderService.updateById(wechatOrder);
            throw new ApiException(ApiResponMessage.SHOP_GOODS_WEIXIN_PAY_ERROR, null);
        }
    }

    private OperateResult<Map<String, Object>> buildPayInfo(String prepay_id, WechatOrder wechatOrder) {
        List<String> keys = new ArrayList<String>();
        Map<String, Object> obj = new HashMap<>();
        obj.put("appid", appidConfig);
        keys.add("appid");
        obj.put("noncestr", genNonceStr());
        keys.add("noncestr");
        obj.put("package", "Sign=WXPay");
        keys.add("package");
        obj.put("partnerid", mchidConfig);
        keys.add("partnerid");
        obj.put("prepayid", prepay_id);
        keys.add("prepayid");
        obj.put("timestamp", (int) (System.currentTimeMillis() / 1000));
        keys.add("timestamp");
        obj.put("appid", appidConfig);
        obj.put("partnerid", mchidConfig);
        String sign = genPackageSign(keys, obj, apiKeyCofig);
        obj.put("sign", sign);

        //更新状态
        wechatOrder.setState(1);
        wechatOrder.setModifyTime(LocalDateTime.now());
        iWechatOrderService.updateById(wechatOrder);

        HashMap<String, Object> argmap = new HashMap<>();
        argmap.put("msg", obj.toString());
        return new OperateResult<Map<String, Object>>(argmap);
    }

    private String genNonceStr() {
        Random random = new Random();
        return MD5Util.getMD5(String.valueOf(random.nextInt(10000)).getBytes());
    }

    private static String genPackageSign(List<String> keys, Map<String, Object> params,
                                         String API_KEY) {
        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            sb.append(key);
            sb.append("=");
            sb.append(params.get(key));
            sb.append('&');
        }
        sb.append("key=");
        sb.append(API_KEY);
        System.out.println("sign:" + sb);
        String packageSign = MD5Util.getMD5(sb.toString().getBytes())
                .toUpperCase();
        return packageSign;
    }

    private String toXml(Map<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for (Iterator<String> localIterator = params.keySet().iterator(); localIterator
                .hasNext(); ) {
            String key = (String) localIterator.next();
            sb.append("<" + key + ">");
            sb.append(params.get(key));
            sb.append("</" + key + ">");
        }
        sb.append("</xml>");
        return sb.toString();
    }

    private JSONObject xml2jsonObject(String xmlString)
            throws DocumentException {
        Document doc = DocumentHelper.parseText(xmlString);
        Element rootElement = doc.getRootElement();
        Map map = new HashMap();
        ele2map(map, rootElement);
        JSONObject obj = new JSONObject(map);
        return obj;
    }

    private void ele2map(Map map, Element ele) {
        List elements = ele.elements();
        if (elements.size() == 0) {
            map.put(ele.getName(), ele.getText());
        } else if (elements.size() == 1) {
            Map tempMap = new HashMap();
            ele2map(tempMap, (Element) elements.get(0));
            map.put(ele.getName(), tempMap);
        } else {
            for (Iterator localIterator = elements.iterator(); localIterator
                    .hasNext(); ) {
                Element element = (Element) localIterator.next();
                ele2map(map, element);
            }
        }
    }

    /**
     * 生成商品参数
     *
     * @param ip          用户ip
     * @param total_fee   一共多少钱
     * @param productName 商品名称
     * @return
     */
    private HashMap<String, String> genProductArgs(String ip, int total_fee, String productName, String buyProductId, Long userId) {
        WechatOrder wechatOrder = new WechatOrder();
        String nonceStr = genNonceStr();
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> keys = new ArrayList<String>();
        params.put("appid", appidConfig);// 公众账号ID
        wechatOrder.setAppId(appidConfig);
        keys.add("appid");
        params.put("body", productName);// 商品或支付单简要描述
        keys.add("body");
        wechatOrder.setBody(productName);
        params.put("mch_id", mchidConfig);// 商户号
        wechatOrder.setMchId(mchidConfig);
        keys.add("mch_id");
        params.put("nonce_str", nonceStr);// 随机字符串,不长于32位。推荐随机数生成算法
        keys.add("nonce_str");
        wechatOrder.setNonceStr(nonceStr);
        params.put("notify_url", wechatCallBack);// 接收微信支付异步通知回调地址
        keys.add("notify_url");
        wechatOrder.setNotifyUrl(wechatCallBack);
        params.put("out_trade_no", buyProductId);// 商户系统内部的订单号,32个字符内、可包含字母,
        // 其他说明见商户订单号
        keys.add("out_trade_no");
        wechatOrder.setOutTradeNo(buyProductId);
        params.put("spbill_create_ip", ip);// APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
        keys.add("spbill_create_ip");
        wechatOrder.setSpbillCreateIp(ip);
        params.put("time_expire",
                formatDate(System.currentTimeMillis() + Long.valueOf(lostTime) * 60L * 1000L));// 超时时间
        keys.add("time_expire");                                                            // 10分钟
        wechatOrder.setTimeExpire(formatDate(System.currentTimeMillis() + 10L * 60L * 1000L));
        params.put("total_fee", total_fee);// 订单总金额,只能为整数,详见支付金额
        keys.add("total_fee");
        wechatOrder.setTotalFee(total_fee);
        params.put("trade_type", "APP");// 取值如下:JSAPI,NATIVE,APP,WAP,详细说明见参数规定
        keys.add("trade_type");
        wechatOrder.setTradeType("APP");
        //是通用的
        params.put("appid", appidConfig);// 公众账号ID
        wechatOrder.setAppId(appidConfig);
        params.put("mch_id", mchidConfig);// 商户号
        wechatOrder.setMchId(mchidConfig);
        params.put("attach", userId);
        wechatOrder.setUserId(userId);
        wechatOrder.setAttach(userId.toString());
        String sign = genPackageSign(keys, params, apiKeyCofig);// 生成sign
        //是通用的
        //sign = genPackageSign(keys, params, apikey);// 生成sign
        params.put("sign", sign);// 签名
        wechatOrder.setSign(sign);
        wechatOrder.setState(0);// 是否冲值成功
        wechatOrder.setCreateTime(LocalDateTime.now());// 创建时间
        String xmlstring = toXml(params);// 转成xml
        //变成传过来的 回调的时候好判读
        wechatOrder.setNotifyUrl(wechatCallBack);
        iWechatOrderService.save(wechatOrder);
        HashMap<String, String> map = new HashMap<>();
        map.put("xmlString", xmlstring);
        map.put("id", wechatOrder.getId().toString());
        return map;
    }

    /**
     * 处理微信支付回调回来的方法
     *
     * @param xmlStr 微信返回来的信息
     * @return
     */
    @Override
    public String weiXinCallBack(String xmlStr) {
        Map<String, Object> reslut = new HashMap<String, Object>();
        String out_trade_no = "";
        try {
            JSONObject res = xml2jsonObject(xmlStr);
            System.out.println("微信回调:" + res.toJSONString());
            String return_code = res.getString("return_code");
            if ("SUCCESS".equals(return_code)) {
                String appid = res.getString("appid");// 微信分配的公众账号ID
                String bank_type = res.getString("bank_type");// 银行类型,采用字符串类型的银行标识,银行类型见附表
                int cash_fee = res.getInteger("cash_fee");// 现金支付金额订单现金支付金额,详见支付金额
                String fee_type = res.getString("fee_type");// 货币类型,符合ISO
                // 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
                String is_subscribe = res.getString("is_subscribe");// 用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效
                String mch_id = res.getString("mch_id");// 微信支付分配的商户号
                String nonce_str = res.getString("nonce_str");// 随机字符串,不长于32位
                String openid = res.getString("openid");// 用户在商户appid下的唯一标识
                out_trade_no = res.getString("out_trade_no");// 商户系统的订单号,与请求一致。
                String result_code = res.getString("result_code");// 业务结果
                // SUCCESS/FAIL
                // 支付结果
                String sign = res.getString("sign");// 签名,详见签名算法
                String time_end = res.getString("time_end");// 支付完成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
                int total_fee = res.getInteger("total_fee");// 订单总金额,单位为分
                String trade_type = res.getString("trade_type");// 交易类型
                // JSAPI、NATIVE、APP
                String transaction_id = res.getString("transaction_id");// 微信支付订单号
                if (!processOrder.add(out_trade_no)) {
                    reslut.put("return_code", "FAIL");
                    return toXml(reslut);
                }

                //业务代码-----------------

                reslut.put("return_code", "SUCCESS");
                return toXml(reslut);
            } else {
                System.out.println("支付失败");
                reslut.put("return_code", "FAIL");
                return toXml(reslut);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            reslut.put("return_code", "FAIL");
            return toXml(reslut);
        } finally {
            processOrder.remove(out_trade_no);
        }
    }

    private static SimpleDateFormat format = new SimpleDateFormat(
            "yyyyMMddHHmmss");

    private static String formatDate(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        return format.format(calendar.getTime());
    }

}

主要的代码都在上面,删除掉业务代码即可

相关文章:

  • 2021-04-28
  • 2021-05-18
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
猜你喜欢
  • 2021-10-15
  • 2021-12-05
  • 2021-11-26
  • 2022-02-09
  • 2022-01-08
相关资源
相似解决方案