医保移动支付加密解密请求工具封装【国密SM2SM4】

医保移动支付加密解密请求工具封装

定点医药机构向地方移动支付中心发起费用明细上传、支付下单、医保退费等交易时需要发送密文,由于各大医疗机构厂商的开发语各不相同,可能要有java的、c#的、python的、pb的、nodjs的、php的、还可能有Delphi的等。。。。很多开发语言在实现SM2签名SM4加密算法没有现成的库,让我等开发人员苦不堪言;鉴于此,本屌丝特意开发了这个小demo,可以直接使用http请求实现加密解密过程,让您不必为了实现加密解密过程而废寝忘食不得其解,该demo可同时支持微信和支付宝渠道的接入。

该demo为springboot开发导出的jar包,直接运行该jar包即可运行服务,请提前安装好ava8环境。文末提供网盘下载地址!

一、项目背景

image-20240120181915750
虽然过期了,但是我前期太忙了,没时间整理哦!

二、使用方法

  • 下载资料包解压后得到如下目录
    image-20240120184645999
  • 配置文件说明【数据已脱敏处理】

对照移动支付第三方渠道接入反馈单配置 config/application.yaml 文件

image-20240120203004924

=================【支付宝测试环境配置说明】================================
小程序名称:xxx医院
APPID:2021002000069869
PID:208800007461
org_app_id【定点医药机构小程序/H5应用ID】[appid]:1GD00000000000561E0
org_code【定点医药机构编码】:H341xxxxxxxx445
ol_biz_type_code【线上业务类型】:04107-医保移动支付业务 04106-线上身份核验业务
org_chnl_crtf_code【机构渠道认证编码】:BqK1kMStlhVDgN2000000000000000000swxWFS9blhtcf6EPJu
【定点医药机构小程序/H5应用名称】:xxxxxxx医院
【数字密钥】[appSecret]:1GD2SB7D000000000000004F842
【渠道私钥】[privateKey]:ANNEIfrVuLzv000000000000000GpAxYHpi
【渠道公钥】[pubKey]:BNvtt6Vy5l2ozmsk00000000000000000000000000000000PbI4eMC7oDanfXnSf5PtSlN9g=
【平台公钥】[publickey]:BOIfzplFtfjtsMau00000000000000000000000000000000000000LclJij4jfRqs4q2nDcSEhpa/3cGJw=
定点机构编号:H3000000000045
  • 运行工具

    执行 java -jar ./ybydzd-enc.jar --server.port=8888 端口请自行修改

    启动效果如下

    image-20240120191456835

三、接口调用

  • 支付宝加密接口

接口地址 /ali_encr_data
请求方式 POST

请求效果
image-20240120192939097

  • 支付宝解密接口

接口地址 /ali_decr_data
请求方式 POST

请求效果
image-20240120193709902

  • 微信加密

接口地址 /weichat_encr_data
请求方式 POST

请求效果
在这里插入图片描述

  • 微信解密

接口地址 /weichat_decr_data
请求方式 POST

请求效果
image-20240120193532181

四、源码介绍

有兴趣的小伙伴可以下载源码整合到自己的java项目中,这样可以有效减少http请求。

  • 支付宝 AliProcessData.java
package com.abc.tresff;

import com.abc.tresff.dao.Ali;
import com.abc.tresff.response.Resp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.mip.DataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;

@RestController
public class AliProcessData {

   @Autowired
   private Ali configParams;
   private DataHandler dataHandler;
   private String message;

   @PostConstruct
   public void init()
   {
       // DataHandler dataHandler = DataHandler.newInstance(appId, appSecret, publicKey, privateKey); // 初始化
       try
       {
           message+=configParams.getAppid()+"\r\n"+configParams.getApp_secret()+"\r\n"+configParams.getPublic_key()+"\r\n"+configParams.getPrivate_key();
           dataHandler = DataHandler.newInstance (configParams.getAppid(), configParams.getApp_secret(), configParams.getPublic_key(),configParams.getPrivate_key());
           System.out.println("APPID:"+configParams.getAppid());
           System.out.println("APPsecret:"+configParams.getApp_secret());
           System.out.println("Publickey:"+configParams.getPublic_key());
           System.out.println("PrivateKey:"+configParams.getPrivate_key());
       }
       catch(Exception ex)
       {
           //return Resp.error(500,"初始化对象发生了异常"+ex.getMessage());
           message += "初始化对象发生了异常"+ex.getMessage()+message;
       }

   }
   /*
    * 加密函数处理中加密调用
    *
    * */

   @RequestMapping(value = "/ali_encr_data",method= RequestMethod.POST)
   public Resp<JSONObject> EncrData(@RequestBody JSONObject json) {
       try
       {
           dataHandler.setVersion(configParams.getVersion()); // 可以根据需要修改版本号,默认是2.0.0
           String reqData = dataHandler.buildReqData(json);
           return Resp.success(JSON.parseObject(reqData));
       }
       catch(Exception ex)
       {
           return Resp.error(500,"调用业务发生了异常"+ex.getMessage()+message);
       }

   }


   /*
    * 加密函数处理中加密调用
    *
    * */

   @RequestMapping(value = "/ali_deccr_data",method= RequestMethod.POST)
   public Resp<JSONObject> DecrData(@RequestBody JSONObject json){
       try
       {
           dataHandler.setVersion(configParams.getVersion()); // 可以根据需要修改版本号,默认是2.0.0
           String rspData = dataHandler.processRspData(json.toJSONString());
           return Resp.success(JSON.parseObject(rspData));
       }
       catch(Exception ex)
       {
           return Resp.error(500,"调用业务发生了异常"+ex.getMessage());
       }

   }
}

  • 微信 WeichatProcessData.java
package com.abc.tresff;

import com.abc.tresff.dao.Ali;
import com.abc.tresff.dao.Weichat;
import com.abc.tresff.response.Resp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.mip.DataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
public class WeichatProcessData {



   @Autowired
   private Weichat configParams;
   private DataHandler dataHandler;
   private String message;

   @PostConstruct
   public void init()
   {
       // DataHandler dataHandler = DataHandler.newInstance(appId, appSecret, publicKey, privateKey); // 初始化
       try
       {
           dataHandler = DataHandler.newInstance (configParams.getAppid(), configParams.getApp_secret(), configParams.getPublic_key(),configParams.getPrivate_key());

       }
       catch(Exception ex)
       {
           //return Resp.error(500,"初始化对象发生了异常"+ex.getMessage());
           message = "初始化对象发生了异常"+ex.getMessage();
       }

   }
   /*
    * 加密函数处理中加密调用
    *
    * */

   @RequestMapping(value = "/weichat_encr_data",method= RequestMethod.POST)
   public Resp<JSONObject> EncrData(@RequestBody JSONObject json) {
       try
       {
           dataHandler.setVersion(configParams.getVersion()); // 可以根据需要修改版本号,默认是2.0.0
           String reqData = dataHandler.buildReqData(json);
           return Resp.success(JSON.parseObject(reqData));
       }
       catch(Exception ex)
       {
           return Resp.error(500,"调用业务发生了异常"+ex.getMessage());
       }

   }


   /*
    * 加密函数处理中加密调用
    *
    * */

   @RequestMapping(value = "/weichat_deccr_data",method= RequestMethod.POST)
   public Resp<JSONObject> DecrData(@RequestBody JSONObject json){
       try
       {
           dataHandler.setVersion(configParams.getVersion()); // 可以根据需要修改版本号,默认是2.0.0
           String rspData = dataHandler.processRspData(json.toJSONString());
           return Resp.success(JSON.parseObject(rspData));
       }
       catch(Exception ex)
       {
           return Resp.error(500,"调用业务发生了异常"+ex.getMessage());
       }

   }
}

五、下载地址

资源下载地址
链接:https://pan.baidu.com/s/1pDsGi8QcdZJQjDhhZsoNow
提取码:mt5t

为什么我觉得很简单的内容要写这么久。写博客真是太苦了!

在这里插入图片描述

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值