吐槽
做完APP微信支付,就两个字:心累,并不是这个功能有多难,就是想吐槽一下微信,太TMD的店大欺客了!签名,呵呵,参数顺序都得按照他们的排序。。。。。。。。
吐槽归吐槽,还是做一下知识复盘,下面是做APP微信支付步骤和代码,框架用的是SpringBoot
步骤
必备参数:
①:appid:微信开放平台上面的应用appid,和公众号appid不同
②:mch_id:商户ID,微信商户平台上商户信息
③:key:商户key(API秘钥)
登录微信商户平台--->账户中心--->API安全--->设置秘钥
步骤
①、根据账号参数拼接进行签名
②、根据参数和签名发起微信统一下单接口
③、把微信统一下单返回参数返回移动端
④、移动端根据参数拉起微信支付
⑤、支付成功后微信进行回调通知
⑥、判断微信返回状态,成功处理当前平台业务并返回微信return_code和return_msg两个参数,不然微信会一直进行回调
参数配置
#微信APP支付参数
wxpayconfig:
#商户应用appId
appid: wx383123456fbb7826
#商户ID
mch_id: 1234567011
#设备号
device_info: WEB
#商户key:api秘钥(32位)
key: VfnmAMI111111111EQjhvglWzDDO
#统一下单接口
url: https://api.mch.weixin.qq.com/pay/unifiedorder
#回调接口
notify_url: http://baidu.com/home/wechatnotify
wx_package: Sign=WXPay
微信配置类
/**
* WxpayConfig.java
* com.prereadweb.order.config
* Copyright (c) 2019,
*/
package com.prereadweb.order.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description: 微信配置类
* @author: Administrator
* @date: 2019/6/17 19:35
*/
@Data
@Component
@ConfigurationProperties(prefix="wxpayconfig")
public class WxpayConfig {
private String appid; // 公众账号ID
private String mch_id; // 商户号
private String device_info; // 设备号
private String key; // 商户的key【API密匙】
private String url; // api请求地址
private String notify_url; // 服务器异步通知页面路径
private String return_url; // 服务器同步通知页面路径
private String wx_package;
}
统一下单代码
controller层
/**
* @Function: 去支付
* @author: YangXueFeng
* @Date: 2019/6/14 16:46
*/
@RequestMapping("/gowechatpay")
public Object goWeChatPay(@Param("orderId") Long orderId, HttpServletRequest request, HttpServletResponse response) throws Exception {
return weChatService.goWeChatPay(orderId, request);
}
service层代码
/**
* @Function: 去支付
* @author: YangXueFeng
* @Date: 2019/6/14 16:50
*/
@Override
public Map<String, Object> goWeChatPay(Long orderId, HttpServletRequest request) {
Map<String, Object> map = new HashMap<>();
if(Util.isEmpty(orderId)) {
map.put("code", UserStatusEnum.ERROR.intKey());
map.put("msg", UserStatusEnum.ERROR.value());
return map;
}
//获取订单信息
PayParameterForm payParameter = orderMapper.getPayParameter(orderId);
double price = payParameter.getActualPrice();
System.out.println("price:" + price);
// 微信开放平台审核通过的应用APPID
System.out.println("appid是:" + wxpayconfig.getAppid());
System.out.println("mch_id是:" + wxpayconfig.getMch_id());
String nonce_str = Util.getRandomString(30);
System.out.println("随机字符串是:" + nonce_str);
int total_fee = (int) (price * 100);
String total_price = null;// 订单总金额,单位为分,详见支付金额
String spbill_create_ip = WXSignUtils.getRemortIP(request);// "127.0.0.1";
System.out.println("spbill_create_ip===="+spbill_create_ip);
String notify_url = wxpayconfig.getNotify_url();
System.out.println("notify_url是:" + notify_url);
String trade_type = "APP";
// 参数:开始生成签名
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
parameters.put("appid", wxpayconfig.getAppid());
parameters.put("body", payParameter.getTitle());
parameters.put("mch_id", wxpayconfig.getMch_id());
parameters.put("nonce_str", nonce_str);
parameters.put("notify_url", notify_url);
parameters.put("out_trade_no", String.valueOf(payParameter.getOrderId()));
/*
parameters.put("total_fee", total_fee);
*/
parameters.put("spbill_create_ip",spbill_create_ip);
parameters.put("total_fee", 1);
parameters.put("trade_type", trade_type);
String sign = WXSignUtils.createSign("UTF-8", parameters);
System.out.println("签名是:" + sign);
Unifiedorder unifiedorder = new Unifiedorder();
unifiedorder.setAppid(wxpayconfig.getAppid());
unifiedorder.setBody(payParameter.getTitle());
unifiedorder.setMch_id(wxpayconfig.getMch_id());
unifiedorder.setNonce_str(nonce_str);
unifiedorder.setNotify_url(notify_url);
unifiedorder.setOut_trade_no(String.valueOf(payParameter.getOrderId()));
unifiedorder.setSpbill_create_ip(spbill_create_ip);
unifiedorder.setTotal_fee(1);
unifiedorder.setTrade_type(trade_type);
unifiedorder.setSign(sign);
// 构造xml参数
String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);
System.out.println("xmlInfo:" + xmlInfo);
String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
String method = "POST";
String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();// 请求微信
System.out.println("weixinPost:" + weixinPost);
UnifiedorderResult unifiedorderResult = ParseXMLUtils.jdomParseXml(weixinPost);// 解析微信的反馈
if (unifiedorderResult != null) {
if ("SUCCESS".equals(unifiedorderResult.getReturn_code())) {
if("INVALID_REQUEST".equals(unifiedorderResult.getErr_code())){
map.put("code", UserStatusEnum.ERROR.intKey());
map.put("msg", "参数错误");
return map;
}
// 开始拼接App调起微信的参数
SortedMap<Object, Obj