快捷搜索: 王者荣耀 脱发

小程序开发调用微信支付以及微信回调地址配置

首先在程序中配置申请的固定参数

wx.open.app_id=用户的appid
wx.open.app_secret=这是做登陆用的
weixin.pay.partner=商户号
wexxin.pay.partenerkey=商户号秘钥

编写工具类实现对固定值的读取

@Component
//@PropertySource("classpath:application.properties")
public class ConstantPropertiesUtil implements InitializingBean {
    //读取配置文件并赋值
    @Value("${wx.open.app_id}")
    private String appId;
    @Value("${wx.open.app_secret}")
    private String appSecret;
    @Value("{weixin.pay.partner}")
    private String partner;
    @Value("{wexxin.pay.partenerkey}")
    private String partenerkey;

    public static String WX_OPEN_APP_ID;
    public static String WX_OPEN_APP_SECRET;
    public static String PARTNER;
    public static String PARTNERKET;

    @Override
    public void afterPropertiesSet() throws Exception {
        WX_OPEN_APP_ID = appId;
        WX_OPEN_APP_SECRET = appSecret;
        PARTNER = partner;
        PARTNERKET = partenerkey;
    }
}

当用户点击购买会生成订单,这里代码省略

点击登陆时调用后端传给前端需要的值

可以看到,除了一些固定值,需要我们自己处理的有

签名:根据文档可以发现签名是有一定要求的

简单来说就将其他传入固定值字段进行排序拼接,在根据商家号的key进行加密处理。

支付接口

@Autowired
    private WXService wxService;
    @GetMapping("pay")
    public R creatNative(Integer orderid){
        try {
            Map map = wxService.payment(orderid);
            return R.ok().data(map);
        } catch (UnsupportedEncodingException e) {
           return R.error().message("支付失败");
        }
    }

编写service逻辑,根据文档进行传值

签名工具类,以及时间戳方法

public class WXUtil {

    public static String genSignature(String secretKey, Map<String, String> params) throws UnsupportedEncodingException {
        if (secretKey == null || params == null || params.size() == 0) {
            return "";
        }
        // 1. 参数名按照ASCII码表升序排序
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        // 2. 按照排序拼接参数名与参数值
        StringBuffer paramBuffer = new StringBuffer();
        for (String key : keys) {
            paramBuffer.append("&"+key).append(params.get(key) == null ? "" : "="+params.get(key));
        }
        // 3. 将secretKey拼接到最后
        paramBuffer=paramBuffer.append("&key="+secretKey);
        String pa =paramBuffer.substring(1);
        // 4. MD5是128位长度的摘要算法,用16进制表示,一个十六进制的字符能表示4个位,所以签名后的字符串长度固定为32个十六进制字符。
        return DigestUtils.md5Hex(pa.getBytes("UTF-8")).toUpperCase();
    }
    /**
     * 获取当前时间戳,单位秒
     * @return
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis()/1000;
    }

    /**
     * 获取当前时间戳,单位毫秒
     * @return
     */
    public static long getCurrentTimestampMs() {
        return System.currentTimeMillis();
    }

}

回调接口

接收输入流转换工具类

public class StreamUtils {
    private static int _buffer_size = 1024;
    /**
     * InputStream流转换成String字符串
     * @param inStream InputStream流
     * @param encoding 编码格式
     * @return String字符串
     */
    public static String inputStream2String(InputStream inStream, String encoding){
        String result = null;
        ByteArrayOutputStream outStream = null;
        try {
            if(inStream != null){
                outStream = new ByteArrayOutputStream();
                byte[] tempBytes = new byte[_buffer_size];
                int count = -1;
                while((count = inStream.read(tempBytes, 0, _buffer_size)) != -1){
                    outStream.write(tempBytes, 0, count);
                }
                tempBytes = null;
                outStream.flush();
                result = new String(outStream.toByteArray(), encoding);
                outStream.close();
            }
        } catch (Exception e) {
            result = null;
        } finally {
            try {
                if(inStream != null) {
                    inStream.close();
                    inStream = null;
                }
                if(outStream != null) {
                    outStream.close();
                    outStream = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
经验分享 程序员 微信小程序 职场和发展