微信小程序:微信支付和退款

微信支付的前期准备:

  1. 小程序的appId和密钥(小程序配置界面)
  2. 商户号和api密钥(商家后台自己设置)

 整理支付的逻辑:

附:官方微信统一下单传送门API

  1. 在微信小程序端调用支付前,先组装支付的金额给后台发送请求,后台需要调用微信API统一下单
  2. 微信统一下单成功后,微信返回支付的5个参数
  3. 拿到5个参数,方可在小程序端调用wx.requestPayment(),在此微信弹出支付填写密码的界面
  4. 用户支付成功,微信发起支付成功通知,后台接受通知整理支付成功逻辑

 微信退款逻辑:

附:微信官方退款传送门API

  1. 用户主动发起退款,拿到订单信息,退款理由(可选)
  2. 调用退款接口
  3. 退款成功后,微信发起退款成功回调,整理退款后的逻辑

流程图如下:

微信小程序:微信支付和退款_第1张图片

微信wxml代码:

微信js代码:

 //在这里演示支付的过程,获取openid不做解释
  payment:function(){
    //请求后台发起支付,获取5个参数,data中放入支付的总额及其openid
    //请求为示例
    wx.request({
      url: 'http:127.0.0.1:8080/project/payment',
      data: { openid: openid, amount: amount},
      method:'POST',
      header: { 'Content-Type': 'application/x-www-form-urlencoded'},
      success:res => {
        if(res){
          //接受的5个参数,调用这个方法成功,微信就会弹出输入密码的界面
          wx.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success:payRes => {
              //支付成功后,可以做一些逻辑判断
              console.log('支付成功!');
              console.dir(payRes);
            },
            fail:payFail => {
              console.log('支付失败!');
              console.dir(payFail);
            }
          })
        }else{
          console.log('后台没有接受到5个参数');
        }
      },
      fail:fail => {
        console.log('支付获取参数失败!');
        console.dir(fail);
      }
    })
  }

java后台代码:

后台的支付主要使用了两个包:

  • com.jpay.ext.kit.PaymentKit 【附参考微信官方的sdk和demo】
  • com.jpay.weixin.api.WxPayApi【附包中源码】
package com.test.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import java.security.KeyStore;
import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.jpay.ext.kit.PaymentKit;

import io.swagger.util.Json;
import net.sf.json.JSONObject;

@Service
@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class testService {
	
	private static final Logger logger = LoggerFactory.getLogger(test.class);
	
	private static final String appid = "wx6edb*******9c18";
	private static final String secret = "*****cff44ae9fe25*********e7c";
	private static final String grant_type = "authorization_code";
	private static final String mch_id = "83595*****";//商户号 
	private static final String partnerKey = "EpTC3d7i9YGKBg9a********";//商户平台设置的密钥key
	private static final String transaction_type = "JSAPI";//微信小程序支付交易类型
	private static final String refund_path = "https://api.mch.weixin.qq.com/secapi/pay/refund";//微信退款地址
	/**
	 * 微信付款或者退款车成功后,需要配置外网可以访问后台接口进行一些逻辑操作
	 */
	private static final String pay_notify_url = "";//支付成功以后回调接口地址
	private static final String refund_notify_url = "";//退款成功以后回调接口地址
	
	private int socketTimeout = 10 * 1000;// 连接超时时间,默认10s
        private int connectTimeout = 30 * 1000;// 传输超时时间,默认30s
        private static RequestConfig requestConfig;// 请求器的配置
        private static CloseableHttpClient httpClient;// HTTP请求器

	/**
	 * 微信支付统一下单
	 * @param map
	 * @param request
	 * @return
	 */
	public JSONObject payment(Map map, HttpServletRequest request) {
		//用户已经登录openid在小程序端发送过来
	    String openId = String.valueOf(map.get("openid"));
	    //参数中获取订单总额	    
	    BigDecimal amount = new BigDecimal(String.valueOf(map.get("amount")));
	    BigDecimal beishu = new BigDecimal("100");
	    amount = amount.multiply(beishu);
	
	    try{
		      String body = "XXX程序-支付";
		      SortedMap paramMap = new TreeMap();
		      //小程序的appid
		      paramMap.put("appid", appid);
		      //商户id
		      paramMap.put("mch_id", mch_id);
		      //随机字符串
		      paramMap.put("nonce_str", getRandomString());
		      //商品描述
		      paramMap.put("body", body);
		      //商户订单号码,自己生成调用即可
		      paramMap.put("out_trade_no", "1234567890");
		      //标价金额
		      paramMap.put("total_fee", String.valueOf(amount));
		      //终端IP
		      paramMap.put("spbill_create_ip", request.getRemoteAddr());
		      //通知地址
		      paramMap.put("notify_url", pay_notify_url);
		      //交易类型	
		      paramMap.put("trade_type", transaction_type);
		      //openid(在接口文档中 如果交易类型设置成'JSAPI'则必须传入openid)
		      paramMap.put("openid", openId);
		      //随机签名
		      paramMap.put("sign", PaymentKit.createSign(paramMap, partnerKey));
		      //统一下单
		      String xmlResult = WxPayApi.pushOrder(false, paramMap);
		      //解析统一下单返回结果的xml
		      Map xmlMap = PaymentKit.xmlToMap(xmlResult);
		      String returnCode = String.valueOf(xmlMap.get("return_code"));
		      String resultMsg = String.valueOf(map.get("return_msg"));
		      //组装返回小程序的支付参数
		      Map resultMap = new HashMap();
		      if ("SUCCESS".equals(returnCode)){
		        resultMap.put("appId", appid);
		        resultMap.put("nonceStr", getRandomString());
		        resultMap.put("package", "prepay_id=" + String.valueOf(xmlMap.get("prepay_id")));
		        resultMap.put("signType", "MD5");
		        resultMap.put("timeStamp", String.valueOf(getCurrentTimestampMs()));
		        String paySign = PaymentKit.createSign(resultMap, partnerKey).toUpperCase();
		        resultMap.put("paySign", paySign);
		        return JSONObject.fromObject(resultMap);
		      }else{
		    	  logger.info("支付返回状态码错误 ===>" + returnCode);
		    	  logger.info("支付返回状态码错误 ===>" + getMsg(returnCode));
		    	  return JSONObject.fromObject(getMsg(returnCode));
		      }
	    }catch (Exception e){
	      System.out.println(e);
	      logger.error(java.lang.Thread.currentThread().getStackTrace()[1].getMethodName() + "支付异常是: ", e);
	    }
	  }
	
	
    /**
     * 申请退款
     * 
     * @param out_trade_no 订单编号
     * @param total_fee 订单金额
     * @param refund_fee 退款金额
     * @param refund_desc 退款原因
     * @throws Exception
     */
    public Map refund(String out_trade_no, Double total_fee, Double refund_fee,String refund_desc)
            throws Exception {
        Map resultMap = new HashMap();
        Map paramMap = new HashMap();
        try {
            paramMap.put("appid", appid);
            paramMap.put("mch_id", mch_id);
            paramMap.put("nonce_str", getRandomString());
            paramMap.put("sign_type", "MD5");
            // 商户订单号,官方API这个参数和微信订单号二选一
            paramMap.put("out_trade_no", out_trade_no);
            //商户退款单号
            paramMap.put("out_refund_no", getRandomString());
            // 支付金额,微信支付提交的金额是不能带小数点的,且是以分为单位,这边需要转成字符串类型,否则后面的签名会失败
            paramMap.put("total_fee", String.valueOf(Math.round(refund_fee * 100)));
            // 退款总金额,订单总金额,单位为分,只能为整数
            paramMap.put("refund_fee", String.valueOf(Math.round(refund_fee * 100)));
            paramMap.put("notify_url", refund_notify_url);// 退款成功后的回调地址
            // 退款原因,退款金额大于1块,且是完全退款才会显示
            paramMap.put("refund_desc", refund_desc);//退款原因
            // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
            String preStr = WXPayUtil.createLinkString(paramMap);
            // MD5运算生成签名,这里是第一次签名,用于调用统一下单接口
            String sign = WXPayUtil.sign(preStr, partnerKey, "utf-8").toUpperCase();
            paramMap.put("sign", sign);
            // 拼接统一下单接口使用的xml数据,要将上一步生成的签名一起拼接进去
            logger.info("微信请求xml=====>",PaymentKit.toXml(paramMap));
            //微信支付是以xml通知
            String xmlStr = sendPostReques(refund_path, PaymentKit.toXml(paramMap)); 
            logger.info("微信退款的拼接xml=====>",xmlStr);
            // 把xml转成map
            Map notifyMap = PaymentKit.xmlToMap(xmlStr);
            // 退款成功
            if ("SUCCESS".equals(notifyMap.get("result_code"))) {
            	// 返回的预付单信息
                String prepay_id = notifyMap.get("prepay_id");
                logger.info("微信退款返回的预付单信息=====>",prepay_id);
                // 拼接签名参数
                String stringSignTemp =
                        "appId=" + appid + "&nonceStr=" + getRandomString() + "&package=prepay_id="
                                + prepay_id + "&signType=MD5&timeStamp=" + String.valueOf(getCurrentTimestamp());
                resultMap.put("package", "prepay_id=" + prepay_id);
                resultMap.put("timeStamp", String.valueOf(getCurrentTimestamp()));
                resultMap.put("paySign", WXPayUtil.sign(stringSignTemp, partnerKey, "utf-8").toUpperCase());
                resultMap.put("result", "success");
            } else {
            	resultMap.put("result", "fail");
            	resultMap.put("msg", notifyMap.get("return_msg"));
            	logger.info("退款失败:",notifyMap.get("return_msg"));
            }
        } catch (Exception e) {
        	resultMap.put("result", "fail");
        	resultMap.put("msg", e.getMessage());
            logger.error(e.toString(), e);
        }
        return resultMap;
    }
    
    
    /**
     * 通过Https往API post xml数据
     * 
     * @param url API地址
     * @param xmlObj 要提交的XML数据对象
     * @return
     */
    public String sendPostReques(String url, String xmlObj) {
        // 加载证书
        try {
        	loadingCert();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = null;
        HttpPost httpPost = new HttpPost(url);
        // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
        StringEntity postEntity = new StringEntity(xmlObj, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.setEntity(postEntity);
        // 根据默认超时限制初始化requestConfig
        requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        // 设置请求器的配置
        httpPost.setConfig(requestConfig);
        try {
            HttpResponse response = null;
            try {
                response = httpClient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
            HttpEntity entity = response.getEntity();
            try {
                result = EntityUtils.toString(entity, "UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            httpPost.abort();
        }
        return result;
    }
	
    /**
     * 加载证书,先取到证书在项目的位置,然后读取证书中的内容
     * @throws Exception
     */
    private void loadingCert() throws Exception {
        // 证书密码,默认为商户ID
        String key = mch_id;
        String realPath = testService.class.getClassLoader().getResource("").getPath();
        try {
        	realPath = URLEncoder.encode(realPath,"UTF-8");
		} catch (Exception e) {
			logger.info("转换url出错:" + e);
			realPath = realPath.replace("%20", " ");
		}
        // 拿到证书的根目录(根据证书所在项目的位置来拿)
        // realPath = realPath.replace("/classes", "");
        // 商户证书PKCS12的路径
        String path = realPath + "cert/apiclient_cert.p12";
        // 指定读取证书格式为PKCS12
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        // 读取本机存放的PKCS12证书文件
        FileInputStream instream = new FileInputStream(new File(path));
        try {
            // 指定PKCS12的密码(商户ID)
            keyStore.load(instream, key.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();
        // 指定TLS版本
        SSLConnectionSocketFactory sslsf =
                new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null,
                        SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        // 设置httpclient的SSLSocketFactory
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }
    
        /**
	 * 提示信息
	 * 
	 * @param code
	 * @return
	 */
	private String getMsg(String code) {
        switch (code) {
            case "NOTENOUGH":
                return "您的账户余额不足!";
            case "ORDERPAID":
                return "该订单已支付完成,请勿重复支付!";
            case "ORDERCLOSED":
                return "当前订单已关闭,请重新下单!";
            case "SYSTEMERROR":
                return "系统超时,请重新支付!";
            case "OUT_TRADE_NO_USED":
                return "请勿重复提交该订单!";
            default:
                return "网络正在开小差,请稍后再试!";
        }
    }
    
	/**
	 * 随机字符串
	 * @return
	 */
	private static String getRandomString(){
		final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		final Random RANDOM = new SecureRandom();
		char[] nonceChars = new char[32];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
	}
	
    /**
     * 获取当前时间戳,单位秒
     * @return
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis()/1000;
    }

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

支付和退款使用的工具类:

package com.test.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

public class WXPayUtil {
	
	/**
     * 签名字符串
     * 
     * @param text 需要签名的字符串
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + "&key=" + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }
	
    /**
     * @param content
     * @param charset
     * @return
     * @throws SignatureException
     * @throws UnsupportedEncodingException
     */
    public static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }
    
	/**
     * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * 
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map params) {
        List keys = new ArrayList(params.keySet());
        Collections.sort(keys);
        String preStr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
                preStr = preStr + key + "=" + value;
            } else {
                preStr = preStr + key + "=" + value + "&";
            }
        }
        return preStr;
    }

    /**
     *
     * @param requestUrl 请求地址
     * @param requestMethod 请求方法
     * @param outputStr 参数
     */
    public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {
        // 创建SSLContext
        StringBuffer buffer = null;
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(requestMethod);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            // 往服务器端写内容
            if (null != outputStr) {
                OutputStream os = conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }
            // 读取服务器端返回的内容
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

    public static String urlEncodeUTF8(String source) {
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
     * 
     * @param strxml
     * @return
     * @throws IOException
     */
    public static InputStream String2Inputstream(String strxml) throws IOException {
        return new ByteArrayInputStream(strxml.getBytes("UTF-8"));
    }

    public static String GetMapToXML(Map param) {
        StringBuffer sb = new StringBuffer();
        sb.append("");
        for (Map.Entry entry : param.entrySet()) {
            sb.append("<" + entry.getKey() + ">");
            sb.append(entry.getValue());
            sb.append("");
        }
        sb.append("");
        return sb.toString();
    }
    
}

 

你可能感兴趣的:(微信小程序,java)