获取微信发票功能
流程概述:
发起请求:初始化IWXAPI->IWXAPI.sendReq(ChooseCardFromWXCardPackage.Req)
接收:需要WXEntryActivity->onResp(BaseResp resp){ChooseCardFromWXCardPackage.Resp cardResp = (ChooseCardFromWXCardPackage.Resp) resp;}
与H5的JS交互:JS调用安卓:JsCallAndroid->getInvoice() {WXInvoiceUtils.getInvoice();}
安卓回传JS: WebView.evaluateJavascript(“javascript:window.PostWxInvoiceList(DATA)”, callBack);
1. 初始化IWXAPI
像支付一样,需要先初始化IWXAPI,定义全局变量 private IWXAPI wechat;
public void initWechat(Activity context,String wxAppid) {
if (null == wechat) {
wechat = WXAPIFactory.createWXAPI(context.getApplicationContext(), wxAppid, true);
boolean isSuccsee = wechat.registerApp(wxAppid);
Log.e("initWechat","isSuccsee"+isSuccsee)
}
}
2. 发起获取发票请求
请求方法:IWXAPI.sendReq(ChooseCardFromWXCardPackage.Req );
ChooseCardFromWXCardPackage.Req 参数配置方法如下:
/**
* 发起获取发票请求
*/
public static void getInvoice() {
//时间戳
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
ChooseCardFromWXCardPackage.Req req = new ChooseCardFromWXCardPackage.Req();
//微信开发者账号appId
req.appId = wxAppid;
//类型:获取发票
req.cardType = "INVOICE";
//随机字符串
req.nonceStr = timeStamp;
//时间戳
req.timeStamp = timeStamp;
//签名类型SHA1
req.signType = "SHA1";
req.locationId = "";
req.cardId = "";
req.canMultiSelect = "1";
//签名
req.cardSign = createSign(req.appId, req.nonceStr, req.nonceStr, req.cardType);
wechat.sendReq(req);
}
3. 生成请求微信签名信息
利用SortedMap 保存并排序app_id,nonce_str,card_type,time_stamp等参数,然后进行SHA1加密。
/**
* 微信支付签名算法sign
*/
private static String createSign(String appId, String nonceStr, String timestamp, String card_type) {
SortedMap<Object, Object> parameters = new TreeMap<>();
parameters.put("app_id", appId);
parameters.put("nonce_str", nonceStr);
parameters.put("card_type", timestamp);
parameters.put("time_stamp", card_type);
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();
// 所有参与传参的参数按照accsii排序(升序)
Iterator it = es.iterator();
while (it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
Object v = entry.getValue();
if (null != v && !"".equals(v) && !"sign".equals(k)
&& !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
String sign = shaEncode(sb.toString()).toUpperCase();
return sign;
}
4. Java SHA1实现方法
/**
* SHA1实现
*
* @author haojy
* 2020年7月29日
*/
public static String shaEncode(String inStr) {
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = new byte[0];
try {
byteArray = inStr.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] md5Bytes = sha.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
5. 接收返回数据
需要注意WXEntryActivity位置,需要放到app包名的Pageage下
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WechatManager.getInstance().getApi().handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq baseReq) {
}
//发送到微信请求的响应结果
@Override
public void onResp(BaseResp resp) {
if (resp instanceof ChooseCardFromWXCardPackage.Resp) {
ChooseCardFromWXCardPackage.Resp cardResp = (ChooseCardFromWXCardPackage.Resp) resp;
SPUtils.getInstance().put("CardItemList", cardResp.cardItemList);
}
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
WechatManager.getInstance().getApi().handleIntent(intent, this);
finish();
}
}
————————————————
原文链接:https://blog.csdn.net/hjy258/article/details/109021271