代码demo
demo
php代码demo
<?php
$secret='yVttM7eAii1jHvVmnAsmAadp4uef4eJ9nLknDEMRLwU';
$url = 'https://all.atpay.vg/api/order/repay/create';
$param=array(
'sn'=>'abc',
'amount'=>10000,
'channel'=>'country_repay_pakistan',
'uid'=>'121323434563456',
'city'=>'',
'phone'=>null
);
function checkEmpty($value) {
if (!isset($value))
return true;
if ($value === null)
return true;
if (trim($value) === "")
return true;
return false;
}
function getSignContent($params) {
ksort($params);
$stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === checkEmpty($v) && "@" != substr($v, 0, 1)) {
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
}
unset ($k, $v);
return $stringToBeSigned;
}
$preSignStr=getSignContent($param);
$sign = hash_hmac('sha256', $preSignStr, $secret, false);
$param['sign']=$sign;
$body=json_encode($param);
$headers = array(
"Content-Type: application/json;charset=UTF-8"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$data = curl_exec($ch);
var_dump($data);
curl_close($ch);
?>java代码demo
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.security.crypto.codec.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) throws Exception {
String secret = "yVttM7eAii1jHvVmnAsmAadp4uef4eJ9nLknDEMRLwU";
String url = "https://all.atpay.vg/api/order/repay/create";
Map<String, String> param = new HashMap<>();
param.put("sn", "abc");
param.put("amount", "10000");
param.put("channel", "country_repay_pakistan");
param.put("uid", "121323434563456");
param.put("city", "");
param.put("phone", null);
String signStr = signString(param);
String sign = signBodyWithAppSecret(signStr, secret);
param.put("sign", sign);
String response = request(url, new Gson().toJson(param));
System.out.println(response);
}
public static String request(String url, String reqBody) throws Exception {
URI uri = new URI(url);
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(
EntityBuilder.create()
.setContentType(ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8))
.setText(reqBody).build()
);
try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault();) {
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
byte[] resultBytes = EntityUtils.toByteArray(entity);
EntityUtils.consumeQuietly(entity);
return new String(resultBytes);
}
}
public static String signBodyWithAppSecret(String reqBody, String secret) {
SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
Mac mac;
try {
mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("sign error");
}
byte[] rawHmac = mac.doFinal(reqBody.getBytes(StandardCharsets.UTF_8));
return new String(Hex.encode(rawHmac));
}
public static String signString(Map<String, String> param, String... remove) {
TreeMap<String, String> treeMap = new TreeMap<>(param);
treeMap.remove("sign");
for (String str : remove)
treeMap.remove(str);
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : treeMap.entrySet()) {
String key = entry.getKey(), value = (entry.getValue() == null ? null : entry.getValue());
if (key != null && value != null && !key.isEmpty() && !value.isEmpty()) {
if (first) first = false;
else result.append("&");
result.append(key).append("=").append(value);
}
}
return result.toString();
}
}Last updated