首页 > 分享 > SpringBoot通联支付接口实现案例之Httpclient(后端篇)

SpringBoot通联支付接口实现案例之Httpclient(后端篇)

首先需要两个一个

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

</dependency>

这个是用来转换JSON格式的,不需要可以不引入

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.2.75</version>

<scope>compile</scope>

</dependency>

首先,看看前端传过来的数据(订单号和随机字符串我直接用前端生成了)

form: {

cusid: "990581007426001",

appid: "00000051",

trxamt: "1",

reqsn: "",

paytype: "U01",

randomstr: "",

signtype: "SM2",

version: "11",

sign: null,

orgid: null,

body: null,

remark: null,

validtime: null,

acct: null,

notify_url: null,

limit_pay: null,

sub_appid: null,

goods_tag: null,

benefitdetail: null,

chnlstoreid: null,

subbranch: null,

extendparams: null,

cusip: null,

front_url: null,

idno: null,

truename: null,

asinfo: null,

fqnum: null,

terminfo: null,

operatorid: null

}

(其实也不需要这么多空参数,只需要把非空的传过来就行,这也是后端对应是实体类或QO)

然后,是后端

需要先生成签名sign,文档的安全规范中有详细生成步骤(paymentQO是自己写的实体类,其中smUtil等工具类是通过下载文档中的javaDemo得到的)

TreeMap<String, String> params = new TreeMap();

for(Field item : paymentQO.getClass().getDeclaredFields()) {

item.setAccessible(true);

if(item.get(paymentQO) != null) {

params.put(item.getName(), item.get(paymentQO) + "");

}

}

StringBuilder s1 = new StringBuilder();

for (String key : params.keySet()) {

s1.append(key).append("=").append(params.get(key)).append("&");

}

s1.deleteCharAt(s1.length() - 1);

PrivateKey privateKey = SmUtil.privKeySM2FromBase64Str(SM2PPRIVATEKEY);

String sign = SmUtil.signSM3SM2RetBase64(privateKey, paymentQO.getAppid(), String.valueOf(s1).getBytes("UTF-8"));

:这里没有把sign的值放到treeMap里,你们自己加一下)

然后是发送请求到测试环境接口

因为文档中说明

 这种方式提交,所以我们编辑Content-type

具体实现,如下

CloseableHttpResponse response = null;

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost("https://syb-test.allinpay.com/apiweb/unitorder/pay");

httpPost.addHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

List<BasicNameValuePair> nameValuePairs = new ArrayList();

if (params.size() != 0) {

Set keySet = params.keySet();

Iterator it = keySet.iterator();

while (it.hasNext()) {

String k = it.next().toString();

String v = params.get(k);

nameValuePairs.add(new BasicNameValuePair(k, v));

}

}

httpPost.setEntity( new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

response = httpClient.execute(httpPost);

HttpEntity entity = response.getEntity();

if (entity != null) {

String content = EntityUtils.toString(entity, "UTF-8");

JSONObject obj = JSONObject.parseObject(content);

return new Response(true, obj, "200", Tools.success);

}

最后结果图:

相关知识

基于springboot玉米病虫害远程咨询系统的设计与实现
Springboot+vue的客户关系管理系统(有报告),Javaee项目,springboot vue前后端分离项目
基于springboot玉米病虫害远程咨询系统的设计与实现(源码+文档+部署讲解等)
springboot基于springboot 的豪华婚车租赁系统的设计与实现 PPT
Springboot鲜花售卖管理系统593y6(程序+源码+数据库+调试部署+开发环境)
中航军工:关于通联支付系统升级的通知
app调起支付宝-后端预支付
基于net的鲜花销售系统
【开题报告】基于SpringBoot的鲜花销售系统的设计与实现
基于springboot模式鲜花售卖商城花店网站的设计与实现 nqs65限时秒杀

网址: SpringBoot通联支付接口实现案例之Httpclient(后端篇) https://m.huajiangbk.com/newsview949050.html

所属分类:花卉
上一篇: 支付接口集成制作
下一篇: 牵牛花又叫什么花 牵牛花又叫哪些