扫一扫完成支付的模式。该模式适用于PC网站支付、实体店单品或订单支付、媒体广告支付等场景。微信公众账号或开放平台APP的唯一标识商户号,稍后用配置文件中的partner代替,见名知意商户密钥数字签名,根据微信官方提供的密钥和一套算法生成的一个加密信息,就是为了保证交易的安全性WXPayUtil.generateNonceStr()WXPayUtil.generateSignedXml(map, partnerkey)WXPayUtil.xmlToMap(result)<dependencies>
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
</dependencies>
spring.redis.host=your_ip
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
#关联的公众号appid
wx.appid=your_app_id
#商户号
wx.partner=your_partner
#商户key
wx.partnerkey=your_partner_key
@Component
public class WxPayProperties implements InitializingBean {
@Value("${wx.appid}")
private String appId;
@Value("${wx.partner}")
private String partner;
@Value("${wx.partnerkey}")
private String partnerKey;
public static String WX_APP_ID;
public static String WX_PARTNER;
public static String WX_PARTNER_KEY;
@Override
public void afterPropertiesSet() throws Exception {
WX_APP_ID = appId;
WX_PARTNER = partner;
WX_PARTNER_KEY = partnerKey;
}
}
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/** http请求客户端 */
public class HttpClient {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
private boolean isCert = false;
//证书密码 微信商户号(mch_id)
private String certPassword;
public HttpClient(String url, Map<String, String> param) {
this.url = url;
this.param = param;
}
public HttpClient(String url) {
this.url = url;
}
public void addParameter(String key, String value) {
if (param == null) param = new HashMap<>();
param.put(key, value);
}
public void post() throws ClientProtocolException, IOException {
HttpPost http = new HttpPost(url);
setEntity(http);
execute(http);
}
public void put() throws ClientProtocolException, IOException {
HttpPut http = new HttpPut(url);
setEntity(http);
execute(http);
}
public void get() throws ClientProtocolException, IOException {
if (param != null) {
StringBuilder completeUrl = new StringBuilder(this.url);
boolean isFirst = true;
for(Map.Entry<String, String> entry: param.entrySet()) {
if(isFirst) completeUrl.append("?");
else completeUrl.append("&");
completeUrl.append(entry.getKey()).append("=").append(entry.getValue());
isFirst = false;
}
// 拼接成完整的url
this.url = completeUrl.toString();
}
HttpGet http = new HttpGet(url);
execute(http);
}
/** 设置post和put参数 */
private void setEntity(HttpEntityEnclosingRequestBase http) {
if (param != null) {
List<NameValuePair> nvps = new LinkedList<>();
for(Map.Entry<String, String> entry: param.entrySet())
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); // 参数
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
}
if (!StringUtils.isEmpty(xmlParam)) http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
/** 执行get、post、put请求 */
private void execute(HttpUriRequest http) throws ClientProtocolException, IOException {
CloseableHttpClient httpClient = null;
try {
if (isHttps) {
if(isCert) {
FileInputStream inputStream = new FileInputStream(WxPayProperties.CERT);
KeyStore keystore = KeyStore.getInstance("PKCS12");
char[] partnerId2charArray = certPassword.toCharArray();
keystore.load(inputStream, partnerId2charArray);
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" },
null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
} else {
TrustSelfSignedStrategy strategy = new TrustSelfSignedStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
// 信任所有
return true;
}
};
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, strategy).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = httpClient.execute(http);
try {
if (response != null) {
if (response.getStatusLine() != null) statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
// 响应内容
content = EntityUtils.toString(entity, Consts.UTF_8);
}
} finally {
assert response != null;
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
assert httpClient != null;
httpClient.close();
}
}
public int getStatusCode() {
return statusCode;
}
public String getContent() throws ParseException, IOException {
return content;
}
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public boolean isCert() {
return isCert;
}
public void setCert(boolean cert) {
isCert = cert;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public String getCertPassword() {
return certPassword;
}
public void setCertPassword(String certPassword) {
this.certPassword = certPassword;
}
public void setParameter(Map<String, String> map) {
param = map;
}
}
@Service
public class WxPayServiceImpl implements WxPayService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/** 生成微信支付二维码 */
@Override
public Map<String, Object> createNative(String orderId) {
try {
// 如果缓存中有,就直接返回
Map<String, Object> payMap = (Map<String, Object>) redisTemplate.opsForValue().get(orderId);
if(payMap != null) return payMap;
// 根据实际的业务需求编写相应的代码
// 省略业务代码.......
// 设置参数,
// 把参数转换xml格式,使用商户key进行加密
Map<String, String> paramMap = new HashMap<>();
paramMap.put("appid", WxPayProperties.WX_APP_ID);
paramMap.put("mch_id", WxPayProperties.WX_PARTNER);
// 随机字符串
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
String body = "请求体,应该是扫码支付时的文本信息";
paramMap.put("body", body);
// 实际开发中还是要保存当前字段到数据表中,供其他需求(如退款)使用
paramMap.put("out_trade_no", "交易流水号,自定义即可,保证唯一性");
paramMap.put("total_fee", "1"); // 支付的费用
// 当前支付的IP地址
paramMap.put("spbill_create_ip", "可以通过HttpServletRequest对象获取请求的IP地址");
// 微信支付的回调地址
paramMap.put("notify_url", "申请微信支付时填的回调地址");
// 微信扫码支付
paramMap.put("trade_type", "NATIVE");
//4 调用微信生成二维码接口,httpclient调用,请求地址一般固定
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
//设置xml参数(Map类型转换为xml)
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, WxPayProperties.WX_PARTNER_KEY));
// 是https协议
client.setHttps(true);
client.post();
//5 返回相关数据
String xml = client.getContent();
// xml数据转换为Map类型
Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
//6 封装返回结果集
Map<String, Object> map = new HashMap<>();
map.put("orderId", orderId);
map.put("totalFee", orderInfo.getAmount());
// 状态码
map.put("resultCode", resultMap.get("result_code"));
//二维码地址
map.put("codeUrl", resultMap.get("code_url"));
if(resultMap.get("result_code") != null) {
// 保存到Redis缓存中
redisTemplateString.opsForValue().set(orderId.toString(),map,120, TimeUnit.MINUTES);
}
return map;
}catch (Exception e) {
e.printStackTrace();
return new HashMap<>();
}
}
}
npm install vue-qriouslyimport VueQriously from 'vue-qriously'
Vue.use(VueQriously)
<!-- 微信支付弹出框 -->
<el-dialog :visible.sync="dialogPayVisible" style="text-align: left" :append-to-body="true" width="500px" @close="closeDialog">
<div class="container">
<div class="operate-view" style="height: 350px;">
<div class="wrapper wechat">
<div>
<qriously :value="payObj.codeUrl" :size="220"/>
<div style="text-align: center;line-height: 25px;margin-bottom: 40px;">
请使用微信扫一扫<br/>
扫描二维码支付
</div>
</div>
</div>
</div>
</div>
</el-dialog>
<script>
import orderInfoApi from '@/api/order/orderInfo'
import weixinApi from '@/api/order/weixin'
export default {
data() {
return {
orderId: null,
orderInfo: {
param: {}
},
dialogPayVisible: false,
payObj: {},
timer: null // 定时器名称
}
},
created() {
this.orderId = this.$route.query.orderId
this.init()
},
methods: {
init() {
orderInfoApi.getOrderInfo(this.orderId).then(response => {
this.orderInfo = response.data
})
},
pay() {
this.dialogPayVisible = true
weixinApi.createNative(this.orderId).then(response => {
this.payObj = response.data
if(this.payObj.codeUrl == '') {
this.dialogPayVisible = false
this.$message.error("支付错误")
} else {
this.timer = setInterval(() => {
// 查看微信支付状态
this.queryPayStatus(this.orderId)
}, 3000);
}
})
},
queryPayStatus(orderId) {
weixinApi.queryPayStatus(orderId).then(response => {
if (response.message == '支付中') {
return
}
clearInterval(this.timer);
window.location.reload()
})
},
}
}
</script>
@Service
public class WxPayServiceImpl implements WxPayService {
/** 查询支付状态 */
@Override
public Map<String, String> queryPayStatus(Long orderId) {
try {
// 封装提交参数
Map<String, String> paramMap = new HashMap<>();
paramMap.put("appid", WxPayProperties.WX_APP_ID);
paramMap.put("mch_id", WxPayProperties.WX_PARTNER);
// 实际开发中还是要保存当前字段到数据表中,供其他需求(如退款)使用
paramMap.put("out_trade_no", "当前支付订单的流水号");
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
// 设置请求内容
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, WxPayProperties.WX_PARTNER_KEY));
client.setHttps(true);
client.post();
// 得到微信接口返回数据
String xml = client.getContent();
// 其中返回的数据中会有transaction_id字段的数据,这是交易流水号,供退款使用
return WXPayUtil.xmlToMap(xml);
}catch(Exception e) {
e.printStackTrace();
return new HashMap<>();
}
}
}
wx.cert=absolute_path\\apiclient_cert.p12
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private WxPayService wxPayService;
@Override
public boolean cancelOrder(Long orderId) {
// 退款成功前关于订单、库存等其他业务的需求,需要根据实际开发场景编写,这里暂时忽略不写
// ..........
// 判断当前的订单是否可以取消
if(OrderStatusEnum.PAID.getStatus().equals(orderInfo.getOrderStatus())) {
// 调用退款方法
boolean isRefund = wxPayService.refund(orderId);
if(!isRefund) throw new YyghException(ResultCodeEnum.CANCEL_ORDER_FAIL);
// 退款成功后业务需求代码省略不写
// .........
}
return false;
}
}
@Service
public class WxPayServiceImpl implements WxPayService {
/** 微信退款 */
@Override
public boolean refund(Long orderId) {
try {
// 其他业务代码需求,省略不写
// ......
// 封装map参数
Map<String, String> paramMap = new HashMap<>();
paramMap.put("appid", WxPayProperties.WX_APP_ID);
paramMap.put("mch_id", WxPayProperties.WX_PARTNER);
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
// 交易流水号(和支付成功之后的交易流水号保持一致)
paramMap.put("transaction_id", paymentInfo.getTradeNo());
// 商户订单号(和支付成功之后的交易流水号保持一致)
paramMap.put("out_trade_no", paymentInfo.getOutTradeNo());
// 商户退款单号
paramMap.put("out_refund_no", "tk" + paymentInfo.getOutTradeNo());
paramMap.put("total_fee", "1");
paramMap.put("refund_fee", "1");
// map类型数据转换为xml格式数据
String paramXml = WXPayUtil.generateSignedXml(paramMap, WxPayProperties.WX_PARTNER_KEY);
// 调用微信退款接口地址
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/secapi/pay/refund");
// 设置xml参数
client.setXmlParam(paramXml);
client.setHttps(true);
// 设置退款证书信息
client.setCert(true);
client.setCertPassword(WxPayProperties.WX_PARTNER);
client.post();
// 返回结果数据(xml格式)
String xml = client.getContent();
// xml格式数据转换为map类型数据
Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
// 根据返回的退款结果做其他部分的需求,省略不写
// ......
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
前言一般来说,前端根据后台返回code码展示对应内容只需要在前台判断code值展示对应的内容即可,但要是匹配的code码比较多或者多个页面用到时,为了便于后期维护,后台就会使用字典表让前端匹配,下面我将在微信小程序中通过wxs的方法实现这个操作。为什么要使用wxs?{{method(a,b)}}可以看到,上述代码是一个调用方法传值的操作,在vue中很常见,多用于数据之间的转换,但由于微信小程序诸多限制的原因,你并不能优雅的这样操作,可能有人会说,为什么不用if判断实现呢?但是if判断的局限性在于如果存在数据量过大时,大量重复性操作和if判断会让你的代码显得异常冗余。wxswxs相当于是一个独立
项目介绍随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱小学生兴趣延时班预约小程序的设计与开发被用户普遍使用,为方便用户能够可以随时进行小学生兴趣延时班预约小程序的设计与开发的数据信息管理,特开发了小程序的设计与开发的管理系统。小学生兴趣延时班预约小程序的设计与开发的开发利用现有的成熟技术参考,以源代码为模板,分析功能调整与小学生兴趣延时班预约小程序的设计与开发的实际需求相结合,讨论了小学生兴趣延时班预约小程序的设计与开发的使用。开发环境开发说明:前端使用微信微信小程序开发工具:后端使用ssm:VU
@作者:SYFStrive @博客首页:HomePage📜:微信小程序📌:个人社区(欢迎大佬们加入)👉:社区链接🔗📌:觉得文章不错可以点点关注👉:专栏连接🔗💃:感谢支持,学累了可以先看小段由小胖给大家带来的街舞👉微信小程序(🔥)目录自定义组件-behaviors 1、什么是behaviors 2、behaviors的工作方式 3、创建behavior 4、导入并使用behavior 5、behavior中所有可用的节点 6、同名字段的覆盖和组合规则总结最后自定义组件-behaviors 1、什么是behaviorsbehaviors是小程序中,用于实现
我正在处理http://prepwork.appacademy.io/mini-curriculum/array/中概述的数组问题我正在尝试创建函数my_transpose,它接受一个矩阵并返回其转置。我对写入二维数组感到很困惑!这是一个代码片段,突出了我的困惑。rows=[[0,1,2],[3,4,5],[6,7,8]]columns=Array.new(3,Array.new(3))putscolumns.to_s#Outputisa3x3arrayfilledwithnilcolumns[0][0]=0putscolumns.to_s#Outputis[[0,nil,nil],[
我需要使用ActiveMerchant库在我们的一个Rails应用程序中设置支付解决方案。尽管这个问题非常主观,但人们对主要网关(BrainTree、Authorize.net等)的体验如何?它必须:处理定期付款。有能力记入个人帐户。能够取消付款。有办法存储用户的付款详细信息(例如Authotize.netsCIM)。干杯 最佳答案 ActiveMerchant很棒,但在过去一年左右的时间里,我在使用它时发现了一些问题。首先,虽然某些网关可能会得到“支持”——但并非所有功能都包含在内。查看功能矩阵以确保完全支持您选择的网关-http
提供3种Ubuntu系统安装微信的方法,在Ubuntu20.04上验证都ok。1.WineHQ7.0安装微信:ubuntu20.04安装最新版微信--可以支持微信最新版,但是适配的不是特别好;比如WeChartOCR.exe报错。2.原生微信安装:linux系统下的微信安装(ubuntu20.04)--微信适配的最好,反应最快,但是微信版本只到2.1.1,版本太老,很多功能都没有。3.深度deepin-wine6安装微信:ubuntu20.04+系统deepin-wine6安装新版微信--综合比较好,当前个人使用此种方法1个月,微信版本3.4;没什么大问题,尚可。一、WineHQ7.0安装微信
对传统的餐饮商家来说,小程序很好地解决了餐厅线下线上连接的问题,在引流获客、节约人力、营销宣传、塑造会员体系、改善消费体验等方面都有很大帮助。小程序点餐可以帮助餐饮企业节省一大把人力开支。一个包含扫码点单、菜品管理、优惠券推送、外卖配送的小程序,商家花几万元就能完成开发测试并投入。商家为什么要开通“扫码点餐”1.解决服务员不够用的问题。2.不怕顾客跑单漏单。3.在微信就能管理菜品、查看营业额。4.订单小票显示顾客桌号和已点菜品。5.可在“附近的小程序”找到您的门店。如今餐饮业常用的三种经营模式:1堂食点单模式客人通过小程序堂食点单。商家可以在微信扫码点餐小程序管理后台根据自己店内情况来设置不同
技术选型1,前端小程序原生MINA框架cssJavaScriptWxml2,管理后台云开发Cms内容管理系统web网页3,数据后台小程序云开发云函数云开发数据库(基于MongoDB)云存储4,人脸识别算法基于百度智能云实现人脸识别一,用户端效果图预览老规矩我们先来看效果图,如果效果图符合你的需求,就继续往下看,如果不符合你的需求,可以跳过。1-1,登录注册页可以看到登录页有注册入口,注册页如下我们的注册,需要管理员审核,审核通过后才可以正常登录使用小程序1-2,个人中心页登录成功以后,我们会进入个人中心页我们在个人中心页可以注册人脸,因为我们做人脸识别签到,需要先注册人脸才可以进行人脸比对,进
我想在rubyonrails中生成QR码,以便在我用rails编写的网站后台运行。看到这个http://code.google.com/p/qrcode-rails/但无法弄清楚如何让它为我工作。基本上在RoR中,我想:向生成器传递一个字符串、我的唯一代码、一个20个字符长度的数字(例如32032928889998887776)并生成一个名为“代码”_qr.jpg的图像并保存在资源文件夹中以附加到我的电子邮件中程序将发出。我该怎么做,有人知道吗?虽然我在问(不是很重要,我现在得到这个答案)但是我如何实现QR码读取,以从网络摄像头取回该代码?谢谢。 最佳答
这是我的代码,可以运行,但它太大了。我想重构它。req_row=-1req_col=-1a.each_with_indexdo|row,index|row.each_with_indexdo|col,i|ifcol==0req_row=indexreq_col=ibreakendendendifreq_col>-1andreq_row>-1a.each_with_indexdo|row,index|row.each_with_indexdo|col,i|print(req_row==indexori==req_col)?0:colprint""endputs"\r"endend输入:二