Appearance
SDK 下载
PHP 版本
统一下单请求示例
php
class payController{
const secret='123456'; //秘钥
const terminal='111'; //终端码
const key='222'; //开发者唯一标识
const appid='333'; //微信公众号APPID
//统一下单
public function pay(){
$url='https://api.hbsk.com/pay/order';
$time=time();
$data=[
'terminal' =>self::terminal, //终端码
'gateway' =>'weixin', //支付通道
'sequence' =>'123456', //订单流水号
'amount' =>10.00, //订单金额
'remark' =>'remark', //备注
'notifyurl' =>'http://www.baidu.com', //回调地址
'appid' =>self::appid, //公众号appid
'openid' =>'用户openid' //用户openid
];
$dataString=json_encode($data);
$sign=hash("sha256",$dataString.self::secret);
$post_data=[
'key'=>self::key, //开发者唯一标识
'time'=>$time, //当前unix时间戳,精确到秒
'sign'=>$sign, //数据签名
'data'=>$dataString //业务参数
];
$res=$this->curlPost($url,$post_data);
$resData=json_decode($res,true);
return $resData;
}
//订单退款
public function refund(){
$url='https://api.hbsk.com/pay/refund';
$time=time();
$data=[
'origin_sequence'=>'123456', //支付成功的订单流水号
'sequence' =>'1234', //退款订单流水号
'amount' =>10.00, //订单金额
'remark' =>'remark', //退款备注
'notifyurl' =>'http://www.baidu.com' //退款回调地址
];
$dataString=json_encode($data);
$sign=hash("sha256",$dataString.self::secret);
$post_data=[
'key'=>'', //开发者唯一标识
'time'=>$time, //当前unix时间戳,精确到秒
'sign'=>$sign, //数据签名
'data'=>$dataString //业务参数
];
$res=$this->curlPost($url,$post_data);
$resData=json_decode($res,true);
return $resData;
}
//统一下单回调
public function payResult(){
$res_data=file_get_contents('php://input');
parse_str($res_data,$resData);
$sign=$resData['sign'];
$dataString=$resData['data'];
//验签
$signRes=hash("sha256",$dataString.self::secret);
if($signRes==$sign){
//验签成功
$data=json_decode($dataString,true);
...
}else{
//验签失败
}
}
//CURL请求
private function curlPost($url,$post_data){
$curl = curl_init();
$headers = ["Content-Type: multipart/form-data"];
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_HEADER, false);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($curl);
curl_close($curl);
curl_close($ch);
return $ret;
}
}
文件上传请求示例
php
public function upload($file)
{
$url = 'https://api.hbsk.com/upload';
$headers[] = "Content-Type:multipart/form-data";
$cfile = curl_file_create($file->getPathname(), $file->getMimeType(), $file->getFilename());
$time = time();
$dataString = json_encode((object)null);
$sign = hash("sha256", $dataString.self::secret);
$post_data = [
'key' => self::key, //开发者唯一标识
'time' => $time, //当前unix时间戳,精确到秒
'sign' => $sign, //数据签名
'data' => $dataString //业务参数
];
$post_data['file'] = $cfile;
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl); // 执行操作
}