//md5('aaa')为:47bce5c74f589f4867dbd57e9ca9f808
//md5('中')为:aed1dfbc31703955e64806b799b67645
如果加密对不上,请使用其他方法 //下面为PHP支付接口代码(参数以接口参数为准,加密方式一样)$url = 'https://api.leadnew.com.cn/pay/index'; $data = array(
'merchantid'=>'平台分配merchantid','action'=>'do.pay',
'pay_code'=>'wxjsapi',
'serial_number'=>'574618357010137088',
'amount'=>'0.01',
'subject'=>'话费test',
'mobile'=>'18368493887',
'remark1'=>'',
'remark2'=>'',
'notify_url'=>'http://www.baidu.com',
'server_notify_url'=>'http://xxx/cashier/aboclx/callback', 'gateway'=>'partners.mini', 'sub_appid'=>'xxxx',
'sub_openid'=> 'xxx', );ksort($data);//按照键名顺序排序
$str = '';foreach($data as $k=>$v){
if(is_array($v)){
$data[$k] = json_encode($v,JSON_UNESCAPED_UNICODE);$v = json_encode($v,JSON_UNESCAPED_UNICODE);
$str = $str.'key=平台分配pubkey';
}
$str .= $k.'='.$v.'&';
}str字符串组合后(参考各接口说明):action=do.pay&amount=0.01&merchantid=XXX&mobile=notify_url=XXX&pay_code=abc&remark1=&remark2=&serial_number=1565583202&server_notify_url=XXX&subject=XXX&key=XXX
$data['sign'] = md5($str); //支付接口 $sHtml = "<form id='paysubmit' name='paysubmit' action='".$url."' method='POST'>";
foreach($data as $k=>$v){
$sHtml.= "<input type='hidden' name='".$k."' value='".$v."'/>";
}
$sHtml = $sHtml.'<input type="hidden" type="submit" value="Submit" />';
$sHtml = $sHtml."</form>";
$sHtml = $sHtml."<script>document.forms['paysubmit'].submit();</script>";
print_r($sHtml); //其他接口//初始化
$curl = curl_init();
//设置抓取的url
//下面为java支付代码(参数以接口参数为准,加密方式一样)
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//执行命令
$result = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//显示获得的数据
//$result = json_decode($result,true);
print_r($result);public class test {
public static String doPost(String url, HashMap map) {
String response = null;
HttpClient client = new HttpClient();
//HttpMethod method = new GetMethod(url);
PostMethod method = new PostMethod(url);
method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
for(Object obj : map.keySet()) {
Object key = obj;
Object value = map.get(obj);
method.addParameter(key.toString(), value.toString());
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
response = method.getResponseBodyAsString();
}
} catch (URIException e) {
System.out.println("执行HTTP Get请求时,编码查询字符串发生异常!");
} catch (IOException e) {
System.out.println("执行HTTP Get请求" + url + "时,发生异常!");
} finally {
method.releaseConnection();
}
return response;
}
public static void main(String[] args) {
try {
HashMap<String, String> map = new HashMap<>();
map.put("merchantid","平台分配merchantid");
map.put("action","do.pay");
map.put("pay_code","abc");
//排序
Map<String, String> resMap = sortMapByKey(map);
//加密
String param = "";
for (Map.Entry<String, String> entry : resMap.entrySet()) {
param = param + entry.getKey() + "=" + entry.getValue() + "&";
}
param = param + "key=平台分配pubkey";
String sign = getMD5(param);
map.put("sign",sign);
1.支付功能请输出form表单//doPost后台请求用于主扫或被扫等其他服务器请求
String response = doPost("https://api.leadnew.com.cn/pay/index",map);
System.out.println(response);
2.form表单输出 String param = "<form id='paysubmit' name='paysubmit' action='".$url."' method='POST'>"; for (Map.Entry<String, String> entry : resMap.entrySet()) {
param = param + "<input type='hidden' name='"+entry.getKey() +"' value='"+entry.getValue()+"'/>";
}
param = param +'<input type="hidden" type="submit" value="Submit" />';
param = param+"</form>";
param = param+"<script>document.forms['paysubmit'].submit();</script>";
System.out.println(param ); } catch (Exception e) {
e.printStackTrace();
}
}
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
sortMap.putAll(map);
return sortMap;
}
public static String timeStamp(){
long time = System.currentTimeMillis();
String t = String.valueOf(time/1000);
return t;
}
public static String getMD5(String str) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(str.getBytes());
byte[] md5Bytes = md5.digest();
String res = "";
for (int i = 0; i < md5Bytes.length; i++){
int temp = md5Bytes[i] & 0xFF;
if (temp <= 0XF){
res += "0";
}
res += Integer.toHexString(temp);
}
return res;
}
}//实现一个比较器类
class MapKeyComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2); //从小到大排序
}
}