Call API
<?php
//Call API to generate a wallet address
$url = "https://api.ubao.io/mch/address/create";
$merchantId = "12345"; //Merchant ID
$api_key = "123456789"; //Merchant Key
$timestamp = time();
//Result:1725607713
$nonce = rand(100000,999999);
//Result:212343
$body_data = array('chainType'=>160,'type'=>0,'callUrl'=>'http://www.xxx.com/xxx.html');
//Convert the Body array to a JSON string
$str = json_encode($body_data);
//Result:{"chainType":160,"type":0,"callUrl":"http:\/\/www.xxx.com\/xxx.html"}
//Use base64 encoding to obtain the final body string
$body = base64_encode($str);
//Result:eyJjaGFpblR5cGUiOjE2MCwidHlwZSI6MCwiY2FsbFVybCI6Imh0dHA6XC9cL3d3dy54eHguY29tXC94eHguaHRtbCJ9
//signature
$sign = md5($body . $api_key . $nonce . $timestamp);
//Result:5eab870f6a4fea6caabc6e0be4308bf0
//Data submitted to the gateway
$data = array('merchantId'=>$merchantId,'timestamp'=>$timestamp,'nonce'=>$nonce,'sign'=>$sign,'body'=>$body);
//Submit to the gateway via POST
$res = curlPost($url,$data);
//Result:{"code":200,"message":"SUCCESS","address":"TQcK2oKpeVAE1mvYEudYJHgCr47F5ba8hk","chainType":160}
//Business processing after obtaining execution results
//......
function curlPost($url, $post_data = array(),$timeout = 5, $header = array("content-type: application/x-www-form-urlencoded"), $data_type = "") {
$header = empty($header) ? '' : $header;
if($data_type == 'json'){
$post_string = json_encode($post_data);
}elseif($data_type == 'array') {
$post_string = $post_data;
}elseif(is_array($post_data)){
$post_string = http_build_query($post_data, '', '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
Recharge callback processing ubao_callback.php
<?php
$api_key = "123456789"; //Merchant Key
$body = $_POST['body'];
$sign = $_POST['sign'];
$nonce = $_POST['nonce'];
$timestamp = $_POST['timestamp'];
$msg = "";
try {
$sign2 = md5($body . $api_key . $nonce . $timestamp);
if($sign2 != $sign){
$msg = "sign error";
throw new Exception();
}
$json = json_decode(base64_decode($body),true);
$decimals = $json['decimals'];
$amount = number_format($json['amount']/pow(10,$decimals),3,'.','') + 0;
if($json['callbackType'] == 'recharge'){
//Business logic processing of coin deposit
}
if($json['callbackType'] == 'transfer'){
//Business logic processing for withdrawal or transfer
if($json['result'] == 1){
//Transfer successful
}
else{
//Transfer failed
$msg = $json['message'];
}
}
if($json['callbackType'] == 'balance'){
//Business logic processing for obtaining address balance
}
$msg = "SUCCESS";
}
catch (Exception $e) {
//
}
echo $msg;
?>