【微信支付】Java实现微信APP支付流程
前言
微信登录网页授权与APP授权
微信JSAPI支付
微信APP支付
微信APP和JSAPI退款
支付宝手机网站支付
支付宝APP支付
支付宝退款
以上我都放到个人公众号,搜一搜:JAVA大贼船,文末有公众号二维码!觉得个人以后开发会用到的可以关注一下哦!少走点弯路…
官方文档
微信支付-APP支付文档
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
APP端开发步骤
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
后端代码实现
引入依赖
<!-- 微信支付 --><dependency><groupId>com.github.wxpay</groupId><artifactId>wxpay-sdk</artifactId><version>0.0.3</version></dependency>
配置参数
application.yml
# 微信相关配置
wx:#商户 ID(微信支付平台-账户中心-个人信息)MCH_ID: # APP_ID(微信开放平台查找)A_APP_ID: # 秘钥(微信开放平台查找)A_APP_SECRET: # 支付秘钥KEY(微信支付平台-账户中心-api安全-api秘钥)A_KEY: # 支付商户证书所载目录(微信支付平台-账户中心-api安全-API证书)A_CERT_PATH: #支付成功回调地址WX_CALLBACK_URL:
YmlParament
@Component
@Data
public class YmlParament {/*微信相关字段*/@Value("${wx.A_APP_ID}")private String a_app_id;@Value("${wx.A_APP_SECRET}")private String a_app_secret;@Value("${wx.MCH_ID}")private String mch_id;@Value("${wx.A_KEY}")private String a_key;@Value("${wx.A_CERT_PATH}")private String a_cert_path;@Value("${wx.WX_CALLBACK_URL}")private String wx_callback_url;
微信统一下单
微信统一下单接口文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
初始化微信支付配置
@Component
public class WxConfig {@Autowiredprivate YmlParament ymlParament;@Bean(autowire = Autowire.BY_NAME,value = WxParament.APP_WX_PAY)public WXPay setAppWXPay() throws Exception {return new WXPay(new WxPayConfig(ymlParament.getA_cert_path(),ymlParament.getA_app_id(),ymlParament.getMch_id(),ymlParament.getA_key()));}
WxPayConfig
public class WxPayConfig implements WXPayConfig {private byte[] certData;private String appID;private String mchID;private String key;public WxPayConfig(String certPath, String appID,String mchID,String key) throws Exception {File file = new File(certPath);InputStream certStream = new FileInputStream(file);this.certData = new byte[(int) file.length()];certStream.read(this.certData);certStream.close();this.appID = appID;this.mchID = mchID;this.key = key;}
}
微信下单接口,关键代码(服务层)
@Resource(name = WxParament.APP_WX_PAY)
private WXPay wxAppPay;/* 微信统一下单 */private Map<String, String> wxUnifiedOrder(String orderNo, String orderFee, String requestIp) throws RuntimeException {Map<String, String> data = new HashMap<String, String>();data.put("nonce_str", WXPayUtil.generateNonceStr());data.put("body", "我来下单啦");data.put("out_trade_no", orderNo);data.put("sign_type", "MD5");data.put("total_fee", orderFee);data.put("spbill_create_ip", requestIp);data.put("notify_url",ymlParament.getWx_callback_url());data.put("trade_type", "APP"); // 此处指定为APP支付Map<String, String> wxOrderResult = WxPay.unifiedorder(data,wxAppPay);if("FAIL".equals(wxOrderResult.get("return_code"))){throw new RuntimeException(wxOrderResult.get("return_msg"));}/* IsNull自定义,主要判断非空 */if (IsNull.isNull(wxOrderResult.get("prepay_id"))) {throw new RuntimeException("微信支付下单成功后,返回的prepay_id为空");}return wxOrderResult;}@Override
public Map<String, String> insertWxAppPay(String orderNo,String orderFee, String requestIp, String openid) {try {/* 微信下单 */Map<String, String> wxOrderResult = wxUnifiedOrder(orderNo, orderFee, requestIp, openid);Map<String, String> chooseWXPay = new HashMap<>();/* 这里的key值都是小写的,而JSAPI是驼峰式写法 */chooseWXPay.put("appid", ymlParament.getA_app_id());chooseWXPay.put("partnerid", ymlParament.getMch_id());chooseWXPay.put("prepayid", wxOrderResult.get("prepay_id"));chooseWXPay.put("package", "Sign=WXPay");chooseWXPay.put("noncestr", wxOrderResult.get("nonce_str"));chooseWXPay.put("timestamp", WxUtils.create_timestamp());String signature = WXPayUtil.generateSignature(chooseWXPay, ymlParament.getA_key());chooseWXPay.put("sign", signature);return chooseWXPay;} catch (Exception e) {e.printStackTrace();}
}
controller层(略)
微信支付成功回调
关键代码
@Resource(name = WxParament.APP_WX_PAY)private WXPay wxAppPay;@ApiOperation("微信支付回调")@PostMapping("callback")public String callback(HttpServletRequest request) throws Exception {try {// 1、获取参数Map<String, String> params = getMapByRequest(request);log.info("微信回调回来啦!!!!" + params);// 2、校验checkCallbackWxPay(params);//业务逻辑return wxPaySuccess();} catch (Exception e) {e.printStackTrace();return wxPayFail(e.getMessage());}}/*** 获取微信过来的请求参数* @param request* @return* @throws Exception*/private static Map<String, String> getMapByRequest(HttpServletRequest request) throws Exception{InputStream inStream = request.getInputStream();ByteArrayOutputStream outSteam = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inStream.read(buffer)) != -1) {outSteam.write(buffer, 0, len);}Map<String, String> ret= WXPayUtil.xmlToMap(new String(outSteam.toByteArray(), "utf-8"));outSteam.close();inStream.close();return ret;} /*校验 */private void checkCallbackWxPay(Map<String, String> params)throws Exception {if ("FAIL".equals(params.get("result_code"))) {throw new Exception("微信支付失败");}if (!checkWxCallbackSing(params, wxAppPay)) {throw new Exception("微信支付回调签名认证失败");}//校验金额//判断订单是否重复//....业务逻辑}/*** 检查微信回调签名 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7* * @param wp 传入:@Autowired WXPay*/private static boolean checkWxCallbackSing(Map<String, String> notifyMap, WXPay wp) throws Exception {return wp.isPayResultNotifySignatureValid(notifyMap);}/*** 微信支付返回参数结果封装*/private static String wxPaySuccess() throws Exception {Map<String, String> succResult = new HashMap<String, String>();succResult.put("return_code", "SUCCESS");succResult.put("return_msg", "OK");return WXPayUtil.mapToXml(succResult);}/*** @param mess 错误消息提示* @return 微信返回错我的提示* @throws Exception*/private static String wxPayFail(String mess) throws Exception {Map<String, String> succResult = new HashMap<String, String>();succResult.put("return_code", "FAIL");succResult.put("return_msg", IsNull.isNull(mess)?"自定义异常错误!":mess);return WXPayUtil.mapToXml(succResult);}
补充
如果不想验证签名,还有一种方式判断是否支付成功,就是调用微信查询订单接口查看是否支付成功
关键代码
/*调用微信查询订单接口*/Map<String, String> orderqueryRes = orderquery(wXH5Pay,params.get("out_trade_no"));/*交易成功*/if (!"SUCCESS".equals(orderqueryRes.get("trade_state"))){throw new Exception("<===微信支付失败====>订单号为【"+ params.get("out_trade_no")+ "】的订单");}/*** 查询支付结果 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2*/private static Map<String, String> orderquery(WXPay wxpay, String outTradeNo) throws Exception {Map<String, String> parament = new HashMap<String, String>();parament.put("out_trade_no", outTradeNo);return wxpay.orderQuery(parament);}
【微信支付】Java实现微信APP支付流程相关推荐
- app微信支付-java服务端接口 支付/查询/退款
app微信支付-java服务端接口 支付-查询-退款 个人看微信的文档,看了很多前辈的写法,终于调通了,在这里做一下记录. 首先来定义各种处理类(微信支付不需要特殊jar包,很多处理需要自己封装,当然 ...
- fastadmin 微信支付宝整合插件 支付宝APP支付 ALIN10146
1.调试微信支付宝整合插件支付宝APP支付,支付宝支付一直报错 ALIN10146调了6个小时 我使用的是 微信支付宝整合插件,以下为我调用支付的代码 $params = ['amount'=> ...
- Java实现支付宝APP支付实现记录
支付宝支付成功返回结果封装 import com.alibaba.fastjson.annotation.JSONField;import java.math.BigDecimal; import j ...
- 【移动支付】.NET支付宝App支付接入
一.前言 最近也是为了新产品忙得起飞,博客都更新的慢了.新产品为了方便用户支付,需要支付宝扫码接入.这活落到了我的身上.产品是Windows系统下的桌面软件,通过软件生成二维码支付.界面 ...
- 手把手教你完成App支付JAVA后台-微信支付JAVA
上篇我们记录了手机端的微信支付的大致流程,期间可能会遇到各种各样的错误,但这些问题没有得到官方的重视,所以我们只能一步步自己排查,要有足够的耐心. 这篇内容看标题已经很明确了,由于微信是用xml通讯的 ...
- 【支付宝支付】Java实现支付宝APP支付流程
前言 微信登录网页授权与APP授权 微信JSAPI支付 微信APP支付 微信APP和JSAPI退款 支付宝手机网站支付 支付宝APP支付 支付宝退款 以上我都放到个人公众号,搜一搜:JAVA大贼船,文 ...
- 微信支付-java实现微信支付-后端篇
微信支付系列文章 微信支付-java后端实现 微信支付-vue 前端实现 java demo: 下载地址文章底部 技术栈 Spring boot java XML (微信在http协议中数据传输方案) ...
- java微信web支付开发_微信支付java开发详细第三方支付功能开发之支付宝web端支...
这段时间把支付基本搞完了,因为做的过程中遇到许多问题,特地记录下来,同时方便其他java coder,废话少说,下面开始. 整体思路:在后台,根据参数创建支付宝客户端AlipayClient,发送参数 ...
- java app支付_【支付宝支付】Java实现支付宝APP支付流程
前言 官方文档 开放能力文档: APP支付接口API 开发准备工作 后端代码实现 参数配置 application.yml # 支付宝相关 ALIPAY: # 应用ID APP_ID: # 应用私钥 ...
- 支付宝支付-当面付和App支付
公司最近在做个视频桩的项目,需要在桩上用到支付宝支付功能. 去年项目当中有应用过支付宝,当时前端是用react,后台返回qcode到前端后,前端通过react的插件(其实就是支付宝的sdk),拼接qc ...
最新文章
- 好的 blog 整理
- 关于程序中的操作符左移和右移问题
- 个人的小项目mysql_mgr_test开放了
- vue2.0 之文本渲染-v-html、v-text
- 《Android游戏编程入门经典》——1.7节小结
- java中Date,SimpleDateFormat
- linux nginx 重启_Nginx 的介绍及安装
- python标识符、命名规则及关键字(含笔记)
- dataframe记录数_大数据系列之Spark SQL、DataFrame和RDD数据统计与可视化
- Zeusee 开源移动端车型识别系统HyperVID
- 基于tkinter模块创建GUI程序(python)
- java二进制命令_Java二进制指令代码解析
- mysql 手机归属地_盒子 - 手机归属地 MySql 数据
- 2021 年“认证杯”数学中国数学建模网络挑战赛 B题解题思路
- 【R语言】高维数据可视化| ggplot2中会“分身术”的facet_wrap()与facet_grid()姐妹花
- SPARK SQL ERROR: Detected cartesian product for INNER join between logical plans报错解决方法
- 全球与中国无线门铃对讲设备市场深度研究分析报告
- phpspreadsheet 读取 Excel 表格问题
- 【转】“产品策划大神 如何进行用户需求分析,这篇文章实在说的透彻!!!【互联网方向】
- HG Plugins 1.0 For JQuery