package cc.mrbird.febs.pay.service.impl;
|
|
import cc.mrbird.febs.common.entity.FebsResponse;
|
import cc.mrbird.febs.pay.model.UnipayDto;
|
import cc.mrbird.febs.pay.service.UnipayService;
|
import cc.mrbird.febs.pay.util.HttpRequester;
|
import cc.mrbird.febs.pay.util.HttpRespons;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.codec.digest.DigestUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.io.IOException;
|
import java.util.*;
|
|
@Slf4j
|
@Service
|
public class UnipayServiceImpl implements UnipayService {
|
|
public static final String notifyUrl = "http://47.111.90.145:8800/api/unipay/unipayCallBack";
|
|
@Override
|
@Transactional
|
public String unipay(UnipayDto unipayDto) {
|
String key = "2e95f6a3e11e47fa8a4386d6aefe1735";/** md5密钥商户后台-商户中心-商户设置-密钥管理获取 必填!*/
|
Map<String, String> map = new HashMap<String, String>();
|
map.put("p0_Version", "1.0");/** 版本号 */
|
map.put("p1_MerchantNo", "888118000001971");/** 商户编号 */
|
map.put("p2_OrderNo", unipayDto.getOrderNo()); /**商户订单号*/
|
map.put("p3_Amount", unipayDto.getAmount().toString());/**订单金额*/
|
map.put("p4_Cur", "1"); /**交易币种 */
|
map.put("p5_ProductName", unipayDto.getProductName()); /** 商品名称 */
|
map.put("p9_NotifyUrl", notifyUrl); /** 服务器异步通知地址 */
|
map.put("q1_FrpCode", unipayDto.getFrpCode()); /** 交易类型*/
|
map.put("qa_TradeMerchantNo",unipayDto.getTradeMerchantNo()); /** 777开头的报备商户号 必填!*/
|
map.put("q7_AppId", "wx42ce0fbc27965fe9"); /** 777开头的报备商户号 必填!*/
|
|
String Strmap = createLinkStringByGet(map);
|
// 签名
|
String sign = "";
|
sign = SignByMD5(Strmap, key);
|
map.put("hmac", sign);/** 签名数据 */
|
System.out.println("发送:" + JSON.toJSONString(map).toString());
|
|
// post请求参数内容
|
HttpRequester hr = new HttpRequester();
|
HttpRespons HP = null;
|
try {
|
HP = hr.sendPost("https://www.joinpay.com/trade/uniPayApi.action", map);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
System.out.println("接收返回参数:" + HP.getContent());
|
Boolean result = false;
|
try {
|
result = nosign(HP.getContent(), key);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if(result){
|
return HP.getContent();
|
}else{
|
return "fail";
|
}
|
}
|
|
|
//参数的拼接整理
|
private String createLinkStringByGet(Map<String, String> params){
|
// TODO Auto-generated method stub
|
List<String> keys = new ArrayList<String>(params.keySet());
|
Collections.sort(keys);
|
String str1 ="";
|
for(int i=0;i<keys.size();i++) {
|
String key = keys.get(i);
|
|
Object value = params.get(key);//(String) 强制类型转换
|
if(value instanceof Integer) {
|
value = (Integer)value;
|
}
|
if(i==keys.size()-1) {
|
|
str1 = str1+value.toString();
|
}else {
|
|
str1 = str1+value;
|
}
|
}
|
System.out.println("整理"+str1);
|
return str1;
|
}
|
|
/**
|
* MD5签名
|
*
|
* @param requestSign 请求签名串
|
* @param merchantKey 商户秘钥
|
*/
|
private String SignByMD5(String requestSign, String merchantKey) {
|
|
String reqHmac = "";
|
try {
|
reqHmac = DigestUtils.md5Hex(requestSign + merchantKey).toUpperCase();
|
} catch (Exception e) {}
|
|
return reqHmac;
|
}
|
|
/**
|
* 验证返回参数
|
* @param hp
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
private boolean nosign(String hp, String key){
|
System.out.println("接收到:" + hp);
|
JSONObject myJson = JSONObject.parseObject(hp);
|
Map m = myJson;
|
|
// 返回hmac
|
String returnHmac = (String) m.remove("hmac");
|
System.out.println(m.toString());
|
String Strmap = createLinkStringByGet(m);
|
|
// 返回参数组装hmac
|
String hmac1 = SignByMD5(Strmap, key);
|
return hmac1.equalsIgnoreCase(returnHmac);
|
|
}
|
|
}
|