app对接微信支付(app支付)
(先补充一下,app唤醒微信支付失败的话,在确保没错的情况下,建议换一个手机或者重新下载微信,不知道是微信缓存还是什么原因)
1、先申请好开发环境
app支付不需要公众号,所以申请好开发商号和开发平台的app应用即可,获取到如下图的几个

还有就是在申请应用的时候记得设置正确的应用包名和应用签名。
2、可以查看微信开发文档(https://pay.weixin.qq.com/)。
controller层
/**
* @description: 微信支付开始下单
* @date: 2021/6/7
*/
@GetMapping("/createOrder")
public JsonData createOrder(@RequestParam(value = "videoId", required = true) int videoId,
@RequestParam(value = "userId", required = true) int userId,
HttpServletRequest request,
HttpServletResponse response) throws Exception { Map<String, String> map = videoOrderService.wxSave(videoId, userId,request);
// if (codeUrl == null) {
// throw new NullPointerException();
// }
return JsonData.buildSuccess(map,"微信支付获取codeUrl")}
serviceImpl层
需要先引入pom文件
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
/**
* 微信生成订单
*
* @param videoId
* @param userId
*/
@Override
public Map<String, String> wxSave(int videoId, int userId,HttpServletRequest request) throws Exception { Video video = videoMapper.selectById(videoId);
//AppUser appUser = appUserMapper.selectById(userId);
//生成订单
VideoOrder videoOrder = new VideoOrder();
videoOrder.setVideoId(video.getId());
videoOrder.setOutTradeNo(CommonUtils.generateUUID());
//未支付的状态
videoOrder.setState(0);
videoOrder.setCreateTime(new Date());
videoOrder.setTotalFee(video.getPrice());
videoOrder.setVideoId(video.getId());
videoOrder.setVideoTitle(video.getTitle());
videoOrder.setVideoImg(video.getCoverImg());
videoOrder.setUserId(userId);
//先生成订单信息,支付状态设置为未支付
Map<String, String> codeUrl = unifiedOrder(videoOrder,request); return codeUrl;
}
private Map<String, String> unifiedOrder(VideoOrder videoOrder,HttpServletRequest request) throws Exception {
//生成未支付订单
int insert = videoOrderMapper.insert(videoOrder);
//这些字段都可以在开发文档上找到,尽量直接复制,以免打错
SortedMap<String,String> params = new TreeMap<>();
params.put("appid",weChatConfig.getAppid());
params.put("mch_id",weChatConfig.getMchId());
params.put("nonce_str",CommonUtils.generateUUID());
params.put("body",videoOrder.getVideoTitle());
params.put("out_trade_no",videoOrder.getOutTradeNo());
params.put("total_fee",String.valueOf(1));
params.put("spbill_create_ip",getRealIp(request));
params.put("notify_url",weChatConfig.getNotifyUrl());
params.put("trade_type","APP");
//生成签名/这里也可以用微信支付的自带的工具类,WXPayUtil.generateSignedXml(params,weChatConfig.getKey());
String sign = WxPayUtil.createSign(params,weChatConfig.getKey());
params.put("sign",sign);
String payXml = WxPayUtil.mapToXml(params);
System.out.println(payXml);
//统一下单
String orderStr = HttpUtils.doPost(weChatConfig.getUnifiedOrderUrl(),payXml,4000);
if(null == orderStr) {
return null;
}
Map<String, String> unifiedOrderMap = WXPayUtil.xmlToMap(orderStr);
System.out.println(unifiedOrderMap.toString());
//unifiedOrderMap.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
Long timeStamp = System.currentTimeMillis() / 1000;
SortedMap<String, String> ps = new TreeMap();
ps.put("appid", unifiedOrderMap.get("appid"));
ps.put("partnerid", weChatConfig.getMchId());
ps.put("noncestr", unifiedOrderMap.get("nonce_str"));
ps.put("package","Sign=WXPay");
ps.put("prepayid",unifiedOrderMap.get("prepay_id"));
ps.put("timestamp", String.valueOf(timeStamp));
//二次签名
String sign2 = WxPayUtil.createSign(ps,weChatConfig.getKey());
ps.put("sign",sign2);
String s = WxPayUtil.mapToXml(ps);
System.out.println(s);
System.out.println(ps.toString());
return ps;
}
统一下单封装方法
/**
* 封装post
* @return
*/
public static String doPost(String url, String data,int timeout){
CloseableHttpClient httpClient = HttpClients.createDefault();
//超时设置 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout) //连接超时
.setConnectionRequestTimeout(timeout)//请求超时
.setSocketTimeout(timeout)
.setRedirectsEnabled(true) //允许自动重定向
.build(); HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-Type","text/html; chartset=UTF-8"); if(data != null && data instanceof String){ //使用字符串传参
StringEntity stringEntity = new StringEntity(data,"UTF-8");
httpPost.setEntity(stringEntity);
} try{ CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpResponse.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(httpEntity);
return result;
} }catch (Exception e){
e.printStackTrace();
}finally {
try{
httpClient.close();
}catch (Exception e){
e.printStackTrace();
}
} return null; }
app对接微信支付(app支付)的更多相关文章
- php对接微信小程序支付
前言:这里我就假装你已经注册了微信小程序,并且基本的配置都已经好了.注: 个人注册小程序不支持微信支付,所以我还是假装你是企业或者个体工商户的微信小程序,其他的商户号注册,二者绑定,授权,支付开通,就 ...
- java对接微信支付
对接微信扫码支付(模式2),前端使用velocity技术 (1)调用微信支付接口(view层) 此部分业务逻辑部分可以省略 @RequestMapping("/wxpay.htm" ...
- app使用微信支付成功后,点击返回到该app却跳到另外一个app去了
刚接手了公司iOS的两个APP, 现在碰到了这样一个问题: 有一台iPhone在一个APP中使用了微信支付,支付成功后,点击返回到该APP,结果却跳到了另外一个APP去了. 这两个APP都是公司开发的 ...
- 微信支付 APP 支付方式的服务器端处理程序
对于微信的APP的支付,客户服务说只能通过微信开放平台申请.后来在公众帐号确实发现了证据: 微信支付在申请的时候就比较严(麻烦),对服务类的一些支付,本来商品就是虚拟的,所以需要将商品描述的比较详细 ...
- App对接支付宝移动支付功能
前段时间看了下app对接支付宝移动支付的功能,并自己总结了下支付宝移动支付的实现流程 一.申请流程 前提是已有现成的应用. 1. 申请地址 https://b ...
- 微信公众号支付|微信H5支付|微信扫码支付|小程序支付|APP微信支付解决方案总结
最近负责的一些项目开发,都用到了微信支付(微信公众号支付.微信H5支付.微信扫码支付.APP微信支付).在开发的过程中,在调试支付的过程中,或多或少都遇到了一些问题,今天总结下,分享,留存. 先说注意 ...
- iOS实现微信外部H5支付完成后返回原APP
看到微信最近放开了微信H5支付,公司决定把H5集成到多款APP上.下面记录下了开发过程. 由于是微信新推出的支付方式,在网上搜索到的相关资料并不多,其中有一篇文件(点此跳转)对我的整个开发过程起到了很 ...
- asp.net core 微信扫码支付(扫码支付,H5支付,公众号支付,app支付)之1
2018-08-13更新生成二维码的方法 在做微信支付前,首先要了解你需要什么方式的微信支付,目前本人做过的支付包含扫码支付.H5支付.公众号支付.App支付等,本人使用的是asp.net mvc c ...
- 微信支付-微信公众号支付,微信H5支付,微信APP支付,微信扫码支付
在支付前,如果使用第三方MVC框架,则使用重写模式,服务器也需要配置该项 if (!-e $request_filename){ rewrite ^/(.*)$ /index.php/$ last; ...
- 不接入微信sdk,在APP中实现微信分享,支付
前段时间在很多地方接入了微信的sdk,发现过程比较繁琐,此外因为导入的sdk比较大会影响最终APP打包的体积,所以就有了不接入sdk也实现相同的功能的想法. 要实现这个目标我个人认为最困难的地方是不知 ...
随机推荐
- 安装torch_scatter,torch-sparse,torch-cluster,torch-spline-conv,torch-geometric
1. 查询torch版本号 进入https://pytorch-geometric.com/whl/ 找到对应的torch版本>>点击进入 2. 找到匹配的包 点击下载即可 3. 使用pi ...
- Mysql数据库基础第五章:(二)视图
Mysql数据库基础系列 软件下载地址 提取码:7v7u 数据下载地址 提取码:e6p9 mysql数据库基础第一章:(一)数据库基本概念 mysql数据库基础第一章:(二)mysql环境搭建 mys ...
- UIAutomator API定位元素
一.根据class name和text属性 包名不可省略 code= new UiSelector().text("我的").classname("android.wi ...
- 最火小游戏《羊了个羊》最新H5升级通关版
背景 最近大火的微信小游戏<羊了个羊>刷爆朋友圈和群聊,大家都在搜索羊了个羊第二关怎么过.羊了个羊通关秘籍.这款小游戏实在难度太高,及其容易上瘾,而且很多朋友怎么也通不过. 这一次小编给大 ...
- flannel 关闭SNAT
flannel 关闭SNAT 默认情况下,flannel 访问集群外网络是通过 SNAT 成宿主机 ip 方式,在一些金融客户环境中为了能实现防火墙规则,需要直接针对 POD ip 进行进行规则配置, ...
- 用浏览器打开pdf格式的文件默认全屏显示
打开地址如 http://xxxxx.pdf 在文件地址后面添加#view=FitH,top,可实现全屏查看pdf文件. http://xxxxx.pdf#view=FitH,top,
- list变成String类型
list变成String类型 CollectionUtils.isEmpty(vo.getImgs())?"" : String.join(";", (Stri ...
- shell中算术运算加减
在shell脚本中,所有的变量默认都是字符串存储,操作也是按照字符串进行处理的,但我们不可避免的需要对各中数值进行算术运算,如算术的相加减,那么这时怎么处理呢?在shell脚本中,我们要实现对数值的算 ...
- JS 开始时间/结束时间和当前时间进行比较
项目需求:到截止日期一些功能不可以再使用,那么需要判断当前时间与截止时间进行比较,记录一下吧 注意: 1.橙色字体的代码换成你自己的变量 2. .valueOf()其实就是将中国时间转为时间戳 3.截 ...
- RabbitMQ的使用介绍
一.RabbitMQ是什么 RabbitMQ是一种常用的消息中间件,是基于AMQP协议,采用erlang语言开发的面向消息服务的中间件,是一个独立的系统应用程序,可以管理服务器计算资源和网络通信.一般 ...