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也实现相同的功能的想法. 要实现这个目标我个人认为最困难的地方是不知 ...
随机推荐
- Android获取获取悬浮窗一下的view办法
getwindows可以获取当前手机屏幕所有有交互功能的view getactitywindows只能获取最顶层有交互的view
- UIAutomator API定位元素
一.根据class name和text属性 包名不可省略 code= new UiSelector().text("我的").classname("android.wi ...
- [版本控制-Git]-git学习总结
1.如何将本地的文件添加到已经建好的远程分支上: 1.1 本地文件夹内,右键-git bash - 创建新仓 git init 1.2 git remote add origin 远程仓库的githu ...
- 监控平台prometheus+grafana+snmp_explorer+blackbox_exporter+alertmanager
一.背景介绍 公司需要监控交换机和IP设备,能够放在展示屏幕,及时发出告警信息.网上有很多监控软件,prometheus系列已经能够满足我们需求.prometheus功能强大,本次只用到一部功能.咱们 ...
- Oracle 查看表空间使用率
SELECT Upper(F.TABLESPACE_NAME) "TablespaceName", D.TOT_GROOTTE_MB "Total(M)", D ...
- cisco-RIPv2重分发缺省路由
项目设计: 网络拓扑图: IP地址规划表 设备 端口 接口模式 绑定vlan IP地址 对端设备 端口 IP地址 R1 Fa0/0 无 无 172.16.1.1/30 R2 Fa0/0 172.16. ...
- Monstache的安装和使用
一.什么是Monstache Monstache 是Golang语言实现的基于MongoDB的oplog实现实时数据同步及订阅的插件,支持MongoDB与ES之间的数据同步.其中MongoDB需要搭建 ...
- M1芯片的Mac上如何安装Windows系统
其实和安装非M1的mac没什么区别,唯一就是找到arm版本的win10镜像文件. 一.安装 Parallels Desktop 16 1. 双击打开dmg格式的安装包,并双击 「Install P ...
- JavaSE——遍历字符串与统计字符个数
package com.zhao.stringtest; import java.util.Scanner; public class test2 { //键盘录入一个字符串,统计该字符串中大写字母, ...
- 前端下载csv文件
var str = [ 'ssssssssssssssssssssssssssssssssssssssss' ]; var uri = 'data:text/csv;charset=utf-8,%EF ...