前言
之前公司的小程序的微信支付使用的是微信自己的扣款通道,现在觉得费率太高想更换,通联那边工作人员说是可以支持,但是问他怎么跟现有微信支付打通确说不清楚,网上也找不到详细的流程,折腾了几天才弄好,写个流程帮助大家节省时间吧
具体步骤
1.通联收银宝开户(略)
链接: 收银宝平台
2.获取分配的appid和key
点击获取参数,这个后面下单接口要用的,显示需要输入交易密码
3.配置微信APPID关联
进入 产品中心-微信支付设置 ,添加小程序APPID
需要输入交易密码,然后会跳转到新页面确认
ps:我用的谷歌页面不跳转,找他们工作人员登录才加好的,加好以后APPID会显示在输入框下方
支付授权目录也配置一下,填写自己小程序的后台地址
4.开发预下单接口
链接: 收银宝开发者中心
使用收付宝提供的API
点击统一下单,按照对应的API进行开发,该页有java的demo提供参考
代码如下
@Override
public ResponseJson wxPayTL(Map<String, Object> paraMap, int cmd) {
ResponseJson responseJson = new ResponseJson();
String openid = paraMap.get("OPEN_ID").toString();
if (openid == null) {
return responseJson.getResponseJson(ErrorCMD.DB_NOT_DATA, cmd, "OPEN_ID不能为空", null);
}
// 支付金额
Object objtotal_fee = paraMap.get("total_fee");
if (objtotal_fee == null) {
return responseJson.getResponseJson(ErrorCMD.DB_NOT_DATA, cmd, "total_fee不能为空", null);
}
BigDecimal big = new BigDecimal(objtotal_fee.toString());
big = big.multiply(new BigDecimal(100));
NumberFormat nf = NumberFormat.getNumberInstance();
String total_fee = String.valueOf(nf.format(big)).replaceAll(",", "");
try {
HttpConnectionUtil http = new HttpConnectionUtil(tlConfig.getApiurl() + "/pay");
http.init();
TreeMap<String, String> params = new TreeMap<String, String>();
params.put("cusid", tlConfig.getCusid());
params.put("appid", tlConfig.getAppid());
params.put("version", "11");
params.put("trxamt", total_fee);
params.put("reqsn", String.valueOf(System.currentTimeMillis()));
params.put("paytype", "W06");
params.put("randomstr", SybUtil.getValidatecode(8));
params.put("body", "---支付");
params.put("remark", "");
params.put("acct", openid);
params.put("authcode", "123");
params.put("notify_url", "");
params.put("limit_pay", "");
params.put("idno", "");
params.put("truename", "");
params.put("asinfo", "");
params.put("sub_appid",Global.getConfig("pay.appid"));//小程序appid
params.put("sign", SybUtil.sign(params, tlConfig.getAppkey()));
logger.info("通联预下单发送报文:"+params);
byte[] bys = http.postParams(params, true);
String result = new String(bys, "UTF-8");
logger.info("通联预下单返回报文:"+result);
Map<String, String> map = handleResult(result,tlConfig.getAppkey());
Map<String, Object> response = new HashMap<String, Object>();
if ("SUCCESS".equals(map.get("retcode")) && "0000".equals(map.get("trxstatus"))) {
JSONObject payinfo = JSONObject.fromObject(map.get("payinfo"));
response.put("nonceStr", payinfo.get("nonceStr"));
response.put("package", payinfo.get("package"));
response.put("timeStamp", payinfo.get("timeStamp"));// 这边要将返回的时间戳转化成字符串,不然小程序端调用wx.requestPayment方法会报签名错误
response.put("paySign", payinfo.get("paySign"));
response.put("signType", payinfo.get("signType"));
//System.out.println(response);
return responseJson.getResponseJson(ErrorCMD.DB_SUCCESS, cmd, ErrorCMD.SUCCESS_MESSAGE, response);
}
responseJson = new ResponseJson(ErrorCMD.DB_ERROR, cmd, "支付异常");
} catch (Exception e) {
e.printStackTrace();
logger.error("通联支付异常",e);
responseJson = new ResponseJson(ErrorCMD.ERROR, cmd, "支付异常");
}
return responseJson;
}
代码中的配置参数注意修改,SybUtil 在demo里面也有
5.调用微信支付
调用下单接口以后,拿到的返回报文里面有个payinfo,里面的参数是微信支付需要的
小程序端拿到数据以后使用wx.requestPayment唤起微信支付组件
常见错误:sub_appid与sub_mch_id不匹配
原因:小程序的APPID没有在收银宝的管理平台配置好
至此任务完成,如果需要开发退款的话需要联系通联的工作人员填写变更表和风险承诺函,不然接口会返回失败