微信支付V3的WXUtil文件

企业微信三方开发(一):回调验证及重要参数获取 前言 我们在新建好一个网页应用后,需要填一些基础配置。其中最重要的就是一些回调配置,回调路径指向我们自己的服务器,需要正确接收微信的响应。 微信接口开发无非就是通过一些带token的http请求来实现相关功能,而获取这些token往往需要先获取一些重要参数,这里有两个参数需要先获取:suite_ticket 和 auth_code 技术栈及工具 开发框架:spring-boot 开发工具: idea 一、后台配置 应用回调配置 首先我们进入新建的应用: 其中最主要的配置就是这个回调配置, 其中有三 阅读详情
package cn.com.siyuan.eshop.weixinepay;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;


import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


import junit.framework.Assert;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;


public class WXUtil {

private static final String TAG = "SDK_Sample.Util";

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}

byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}

return result;
}


public static byte[] httpGet(final String url) {
if (url == null || url.length() == 0) {
Log.e(TAG, "httpGet, url is null");
return null;
}


HttpClient httpClient = getNewHttpClient();
HttpGet httpGet = new HttpGet(url);


try {
HttpResponse resp = httpClient.execute(httpGet);
if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode());
return null;
}


return EntityUtils.toByteArray(resp.getEntity());


} catch (Exception e) {
Log.e(TAG, "httpGet exception, e = " + e.getMessage());
e.printStackTrace();
return null;
}
}

public static byte[] httpPost(String url, String entity) {
if (url == null || url.length() == 0) {
Log.e(TAG, "httpPost, url is null");
return null;
}

HttpClient httpClient = getNewHttpClient();

HttpPost httpPost = new HttpPost(url);

try {
httpPost.setEntity(new StringEntity(entity));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

HttpResponse resp = httpClient.execute(httpPost);
if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode());
return null;
}


return EntityUtils.toByteArray(resp.getEntity());
} catch (Exception e) {
Log.e(TAG, "httpPost exception, e = " + e.getMessage());
e.printStackTrace();
return null;
}
}

private static class SSLSocketFactoryEx extends SSLSocketFactory {      
     
   SSLContext sslContext = SSLContext.getInstance("TLS");      
     
   public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {      
       super(truststore);      
     
       TrustManager tm = new X509TrustManager() {      
     
           public X509Certificate[] getAcceptedIssuers() {      
               return null;      
           }      
     
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
}


@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
}  
       };      
     
       sslContext.init(null, new TrustManager[] { tm }, null);      
   }      
     
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}


@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
} 
}  


private static HttpClient getNewHttpClient() { 
  try { 
      KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
      trustStore.load(null, null); 


      SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); 
      sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 


      HttpParams params = new BasicHttpParams(); 
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 


      SchemeRegistry registry = new SchemeRegistry(); 
      registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      registry.register(new Scheme("https", sf, 443)); 


      ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 


      return new DefaultHttpClient(ccm, params); 
  } catch (Exception e) { 
      return new DefaultHttpClient(); 
  } 
}

public static byte[] readFromFile(String fileName, int offset, int len) {
if (fileName == null) {
return null;
}


File file = new File(fileName);
if (!file.exists()) {
Log.i(TAG, "readFromFile: file not found");
return null;
}


if (len == -1) {
len = (int) file.length();
}


Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));


if(offset <0){
Log.e(TAG, "readFromFile invalid offset:" + offset);
return null;
}
if(len <=0 ){
Log.e(TAG, "readFromFile invalid len:" + len);
return null;
}
if(offset + len > (int) file.length()){
Log.e(TAG, "readFromFile invalid file len:" + file.length());
return null;
}


byte[] b = null;
try {
RandomAccessFile in = new RandomAccessFile(fileName, "r");
b = new byte[len]; // ���������ļ���С������
in.seek(offset);
in.readFully(b);
in.close();


} catch (Exception e) {
Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
e.printStackTrace();
}
return b;
}

private static final int MAX_DECODE_PICTURE_SIZE = 1920 * 1440;
public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) {
Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);


BitmapFactory.Options options = new BitmapFactory.Options();


try {
options.inJustDecodeBounds = true;
Bitmap tmp = BitmapFactory.decodeFile(path, options);
if (tmp != null) {
tmp.recycle();
tmp = null;
}


Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
final double beY = options.outHeight * 1.0 / height;
final double beX = options.outWidth * 1.0 / width;
Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
if (options.inSampleSize <= 1) {
options.inSampleSize = 1;
}


// NOTE: out of memory error
while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
options.inSampleSize++;
}


int newHeight = height;
int newWidth = width;
if (crop) {
if (beY > beX) {
newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
} else {
newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
}
} else {
if (beY < beX) {
newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
} else {
newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
}
}


options.inJustDecodeBounds = false;


Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x" + options.outHeight + ", sample=" + options.inSampleSize);
Bitmap bm = BitmapFactory.decodeFile(path, options);
if (bm == null) {
Log.e(TAG, "bitmap decode failed");
return null;
}


Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
if (scale != null) {
bm.recycle();
bm = scale;
}


if (crop) {
final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1, (bm.getHeight() - height) >> 1, width, height);
if (cropped == null) {
return bm;
}


bm.recycle();
bm = cropped;
Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
}
return bm;


} catch (final OutOfMemoryError e) {
Log.e(TAG, "decode bitmap failed: " + e.getMessage());
options = null;
}


return null;
}

public static String sha1(String str) {
if (str == null || str.length() == 0) {
return null;
}

char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes());

byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}

public static List<String> stringsToList(final String[] src) {
if (src == null || src.length == 0) {
return null;
}
final List<String> result = new ArrayList<String>();
for (int i = 0; i < src.length; i++) {
result.add(src[i]);
}
return result;
}
}
【微信支付】springboot-java接入微信支付-JSAPI支付/查单/退款/发送红包(四)---发送红包 在发放现金红包之前,请确保你的资金充足。操作路径:【登录商户平台——>交易中心——>资金管理——>充值】和红包相关的参数,你可以在页面上自主设置和更改。操作路径如下:【登录商户平台——>产品中心——>现金红包——>产品设置】在使用现金红包之前,请前往开通现金红包功能。操作路径:【登录微信支付商户平台——>产品中心——>现金红包——>开通】至此,整个微信支付的教程基本结束了,如果有小伙伴有其他问题,欢迎留言或者私信。商户调用微信红包接口时,微信支付服务器会进行证书验证,请现在商户平台下载证书。 阅读详情

相关推荐

WxUtils.java

基于第三方企业微信SDK 编写的WxUtils, 添加到项目即可使用, 要在pom.xml 添加<!-- 企业微信SDK --> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-cp</artifactId> <version>3.7.0</version> </dependency>

【亲测免费】 探索高效的微信小程序开发利器 —— `wxutil`

探索高效的微信小程序开发利器 —— wxutil 在微信小程序开发的世界里,效率和代码质量是开发者最关注的问题。 是一个由 YYJeffrey 创建的开源库,旨在为开发者提供一系列便捷的工具函数,帮助优化开发流程,提升开发体验。 项目简介 wxutil 是一套强大的微信小程序辅助工具集,包含了大量的实用函数,涵盖了数据处理、网络请求、页面导航、日志记录等多个方面。通过引入这个库,你可以快速实现一些...

gitblog_00026的博客 566

微信提现到零钱V3接口的对接java实现demo.rar

微信提现到零钱V3接口的对接java实现,此源码没有包结构,请自行组织包结构

微信支付V3版本接入代码

/ 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新。logger.error("生成预支付订单失败:{}", ex.getMessage());throw new GlobalApiException("签名验证失败");advancePayDto.setAppid("公众号appId");logger.error("签名验证失败");

xiaojie_std的专栏 548

从STM32F411到华大HC32F460:一个真实项目移植的踩坑与填坑全记录

本文详细记录了从STM32F411移植到华大HC32F460芯片的全过程,包括开发环境搭建、时钟树配置、中断处理、外设驱动、看门狗陷阱、Flash操作和低功耗设计等关键坑点及解决方案。特别针对HC32F460的200MHz主频、双PLL架构和HSRUN模式等特性进行了深入分析,为工程师提供实用的移植指南,帮助节省大量调试时间。

weixin_33691817的博客 424

Computer Networks 与 Computer & Security:从投稿准备到流程执行的实战指南

本文提供了一份针对CCF B类期刊《Computer Networks》与《Computer & Security》的投稿实战指南。从投稿前的心理与材料准备入手,详细拆解了账号注册、文件上传、信息填写等关键步骤,并重点对比了两本期刊在Cover Letter、推荐审稿人等环节的差异。文章旨在帮助研究者,尤其是初次投稿者,系统性地掌握投稿流程,规避常见错误,提升投稿效率与成功率。

zz67890的博客 996

android 项目集成 微信支付

0.环境 下载libammsdk.jar 1.需要的常量值 public class Constant { /** 微信中用到的常量值 */ public static final class WX_CONSTANT { /** 微信支付 APP_ID */ public static final String APP_ID =...

weixin_33810006的博客 123

Zynq工程IP核封装:XCI引用与约束继承实战指南

IP核封装是FPGA系统设计中实现模块复用、团队协作与技术资产沉淀的关键环节。其本质是将功能逻辑、时序约束、接口协议和硬件配置统一抽象为可配置、可验证、可演进的标准化组件。核心原理在于通过XCI文件保留IP核的参数化定义能力,依托Vivado IP Packager实现约束继承、端口自动映射与时钟域解耦,从而保障跨项目集成的时序可信性与约束完备性。该技术广泛应用于Zynq SoC视频处理、高速接口(如HDMI/TMDS)、多时钟域控制系统等场景,尤其在需要长期维护与版本迭代的工业级嵌入式开发中具有不可替代的

weixin_42593701的博客 131

WxUtile拓展组件介绍

在C#开发过程中通常情况下会需要一些对字符串。特殊的数据格式入json数据、xml数据等字符串数据的解析与操作;而开应用程序时;程序员需要重复的去编写对应的数据解析程序;这个过程是繁琐而复杂的。 为了解决这些繁琐重复的编程工作,特此开发此工具软件,通过该工具软件可方便的调用对应的方法来实现针对数据的不同功能进行操作与处理。 该工具总共分为几个模块进行设计,attributes特性模块、const常量模块、data数据模块、Log日志模块、Requests数据请求模块、String操作模块。.......

herees的博客 361

Spring Boot Admin 完整教程

SpringBootAdmin是一个用于管理和监控SpringBoot应用的Web工具,提供健康检查、性能指标、日志管理等核心功能。系统由AdminServer(管理服务器)和AdminClient(客户端)组成,通过Actuator端点收集数据。配置需添加Maven/Gradle依赖,通过@EnableAdminServer注解启动服务端,客户端需配置注册信息。支持UI自定义、安全认证(基本/OAuth2)、多种通知方式(邮件/Slack)及与Eureka/Consul等服务发现的集成。最佳实践包括分层监

weixin_51040479的博客 2564

【WRFDA实操第一期】服务器中安装 WRFPLUS 和 WRFDA

【WRFDA实操第一期】安装 WRFPLUS 和 WRFDA

WW、forever的博客 2061

微信开发请求工具类WxUtils

Import org.apache.commons.codec.binary.Base64; Import javax.crypto.Cipher; Import javax.crypto.KeyGenerator; Import javax.crypto.spec.IvParameterSpec; Import javax.crypto.spec.SecretKeySpec; I...

GoodMorning_1992的博客 1382

245. 项目195:简易文字冒险游戏——《跟老吕学Python·新手》

如果您在学习本专栏教程时遇到任何问题,或有任何技术交流的意愿,欢迎在文章评论区留言,或通过CSDN私信与老吕取得联系。老吕将及时回复您的留言,并与您共同探讨,以期为大家提供更为精准和有效的帮助。老吕珍视每一位读者的反馈和支持,期待与您共同学习、共同进步,共同创造美好的未来!我们的目标是不仅教会您Python语言,更是培养您解决问题的思维方式。请随意翻阅,按照自己的节奏学习,让这本专栏成为您编程旅程中的可靠伙伴。开发一个简易的文字冒险游戏,通过文本描述场景和事件,让玩家通过选择不同的选项来推动故事发展。

Python老吕的博客 716

微信商家转账到零钱 V3(JAVA)转账与查询

商家转账到零钱 v3 java实现

wwwwwwweq的博客 6386

【YOLOv12改进trick】高效多尺度卷积注意力EMCAD_Block模块引入YOLOv12,实现涨点和效率提升,含创新点Python代码

EMCAD引入YOLOv12模块,实现高效多尺度注意力模块引入到YOLOv12中的快速涨点及效率提升

向上生长的程序媛的博客 857

XCP详解「3.1·ASAP2新建A2L文件」

后面是ASAP2新建A2L的操作步骤,如果是APE中新建的A2L就无需以下操作,APE只需要导入现成的A2L即可。退出点击保存时会提示保存A2L文件,是因为前面导出的是1.70版本,工程最低1.71,可以另外起个名字即可。导入elf文件,elf路径建议直接索引编译生成的Debug下的,防止编译后忘记更新。导出V1.70格式A2L文件,APE17支持V1.70。通过ASAP2生成需要A2L文件更常用些。同一个信号可以创建不同属性量。ASAP2中新建A2L文件。设置标定信号数量最大值。XCP详解「总目录」

光明磊学习&积累的地方 3066

【微信小程序】快捷util封装

微信小程序中经常会用到一些快捷的全局方法,比如日期格式化,时间转换等,所以提供以下简单的公共方法的封装。 日期格式化 //util.js 引入js文件即可调用 const util = require('../../../utils/util.js'); const formatTime = date => { const year = date.getFullYear() ...

梦和远方的博客 6315

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

微信支付v3整合Spring boot

v3都是基于json,v2是基于xml的 <dependency> <groupId>com.github.wechatpay-apiv3</groupId> <artifactId>wechatpay-apache-httpclient</artifactId> <version>0.3.0</version> <

lxsxkf的博客 1093

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

微信V3版本支付相关功能

微信支付V3版本,支付、退款

qq_41923769的博客 558

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

java APP微信支付API V3

因项目需要,有用到微信支付,这里对java微信支付的开发流程和注意事项到做一次记录,以遍后面有需要的时候翻阅,方便回顾。 快速开发一个功能,需要了解整个开发过程以及开发过程中需要注意的点,开发的功能才能足够安全高效 如下是微信支付官方给出的微信支付V3流程图: 微信支付V3开发流程(阅读下面代码时,需要对照当前流程): 1、首先是接入前准备 选择接入模式 参数申请 配置API key 下载并配置商户证书 App支付页面规范 具体参考微信支付V3接入前准备:https://pay.wei

bings_l的博客 3086

微信支付V3版商家转账到零钱

对于一个从未接触过微信支付的小白来说,开发中处处受挫,开发中主要遇到的问题一是微信支付平台序列号是要自己通过接口去拿取的以及等等的百度。以下是自己开发中遇到的真实经历,有点菜,请各位大佬多多指点。若要可以正常使用则需要产品设置设置调用的ip以及api发起勾选,还需设置用户的转账额度等信息,模式为为0的是发起转账是转不出去的。.........

weixin_45554823的博客 3170

微信小程序 - Util工具类实现

Util工具类可以提高开发效率,比如POST、GET请求,图片上传,数组操作等等,将常用的方法进行封装,方便使用。 下面是我在做项目的时候编写整理的一部分。 先上代码 const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const mi

ziqinchao的博客 3028

微信支付--调起支付(整理、思路)

微信小程序支付、微信支付下单

qq_44869984的博客 2183

springboot利用官方SDK(wechatpay-apache-httpclient)接入微信支付V3

https://blog.csdn.net/weixin_48855888/article/details/127491496

竹林幽深 2159

wechatpay-apiv3/wechatpay-java对接微信支付V3-亲测可用

因项目集成新项目,app嵌套业务板块实现对接后台新项目,新项目对接的是V2版本微信支付APP使用的sdk是V3版本,和后台对接微信V2版本不适配,app调用微信APP支付sdk提示签名错误,升级后台对接微信支付V3版本后解决微信SDK适配问题。

weixin_44556848的博客 4333
上一篇: 车牌号码验证正则表达式
下一篇: 验证用户名只包含字母,中文
白云飘絮
博客等级 码龄11年 50粉丝 83原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值