本文只做了客户和公司自动签署合同的方式,其他类型的方式也可以参考,只是调用云合同的接口不同
流程:
1.官网注册云合同账号,企业实名认证
2.创建平台应用,测试阶段创建测试应用,获取AppId和AppKey
3.注册登录实名认证(此处被坑很惨,切记一定要向对接人员提前要实名认证的接口,而且确定好对方是开通的哪一种实名认证,我在做此项目的时候被坑的鬼火冒,完全就是接口写好之后,测试返回数据不对,对接的人各种让我们尝试修改bug,最后才爆出一句给我们开通的接口是哪一个,当时真是服了。。。)
4.获取token,此处返回的头信息是非常长的字符串,需要自己进行切割获取
5.创建客户也就是个人用户以及企业用户(此处也是坑,切记一定要单独创建一个企业自身的用户,我一直以为自己公司就是主体,客户是用户,实则不然,自己本公司也是一个用户,必须要创建)
6.创建个人用户印模和企业印模(因为企业印模是一样的,所以不需要重复创建,此处可以先行调用一遍创建企业印模的接口,生成企业印模,然后存入数据库,以后每次使用就直接用企业的signerId查询;或者直接在创建企业印模的接口做判断,如果数据库没有创建,则创建企业印模,如果有,则查询)
7.生成合同
8.添加签署者(此处一定要添加签署者才具有签署权限,本人也是从坑里爬出来的,说多了都是泪)
9.签署合同
10.完成
请求接口时一定要设置header
header("Content-type:text/html; charset=UTF-8");
把公共的和常用的设置为常量或者公共方法
此处把接口前部分url,appid,appkey设置为常量
const API_URL='云合同接口地址公共部分';
const API_APPID= '你的appid';
const API_APPKEY= '你的appkey';
public $token;
public $result;
public function __construct(){
$this->result=$this->gettokey();
}
public function getresult(){
return $this->result;
}
//获取tokey
public function gettokey(){
$url=self::API_URL.'/auth/login';
$postArr = array (
'appId' => self::API_APPID,
'appKey' => self::API_APPKEY,
'signerId' => '', //可以不传
);
$result = $this->curlPost2($url , $postArr);
//dump($result);die;
$header=$result['header'];
$header=explode('token: ',$header);
$header=explode('Set-Cookie: ',$header[1]);
$token=$header[0];
$this->token=$token;
//dump($result);die;
return $result;
//dump($token);die;
/*var_dump($result);die();
return $result;*/
}
private function curlPost2($url,$postFields){
//$url="http://www.sp.com/api/port/geturltest";
$postFields = json_encode($postFields);
//$postFields = urlencode($postFields);
//$postFields = http_build_query($postFields);
$ch = curl_init ();
curl_setopt( $ch, CURLOPT_URL, $url );
$token=$this->token;
//dump($token);die;
if($token){
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($postFields),
//'Content-Type: application/json;', //json版本需要填写 Content-Type: application/json;
'token:'.$token,
)
);
}
else{
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
)
);
}
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt( $ch, CURLOPT_TIMEOUT,60);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
// 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
curl_setopt($ch, CURLOPT_HEADER, true);
$ret = curl_exec ( $ch );
$headergo= curl_getinfo($ch, CURLINFO_HEADER_OUT);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容
$header = substr($ret, 0, $headerSize);
//dump($ret);die;
if (false == $ret) {
$result = curl_error( $ch);
} else {
$rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
if (200 != $rsp) {
$result = "请求状态 ". $rsp . " " . curl_error($ch);
} else {
$result = $ret;
}
}
curl_close ( $ch );
//dump($information);die;
$data['result']=$result;
$data['header']=$header;
$data['headergo']=$headergo;
$data['bodyparam']=$postFields;
//dump($postFields);die;
//dump($data);die;
return $data;
}
curl请求接口
此处curl参数一定要选好,本人测试了很久,发现少了1,2个,或者参数设置不对,直接就报错。个人感觉很坑,因为curl参数很多,而对接人员又没有范例,真的是一点一点测试成功的。。。
public function curlPost($url, $data)
{
$token=$this->token;
$header[0] = "Content-type:application/json;charset=UTF-8";
$token = str_replace("\r\n", "", $token);
$header[1] = "token:".$token;
//$data0=$data;
$data=json_encode($data,JSON_UNESCAPED_UNICODE);
//$data = '{"userName":"'.$data['userName'].'","phoneRegion":"'.$data['phoneRegion'].'","certifyNum":"'.$data['certifyNum'].'","phoneNo":"'.$data['phoneNo'].'","identityRegion":"'.$data['identityRegion'].'","caType":"B2"}';
//dump($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno' . curl_error($curl);
}
curl_close($curl);
return $response;
}
创建个人用户和企业用户
//创建个人用户
public function createuser($data){
$url=self::API_URL.'/user/person';
//$postArr = $data;
$postArr=$data;
$postArr['caType']='B2';
//dump($token);die;
/*$aa=$this->gettokey();
$header=explode('token: ',$aa);
$header=explode('Set-Cookie: ',$header[1]);
$token=$header[0];
$this->token=$token;*/
//dump($postArr);die;
$result = $this->curlPost($url,$postArr);//
//$result = $this->curlPost($url,$postArr);//
//var_dump($postArr);die();
//dump($result);die;
return $result;
}
/*
* 创建企业自身用户
*
* */
public function creqiye($data){
$url=self::API_URL.'/user/company';
$postArr=$data;
$result = $this->curlPost($url , $postArr);
//dump($result);die;
//dump($result);die;
return $result;
//dump($token);die;
/*var_dump($result);die();
return $result;*/
}
创建个人印模和企业印模
/*
* 创建个人印模
* */
public function createuseryingmo($data){
$url=self::API_URL.'/user/personMoulage';
$postArr = $data;
$result = $this->curlPost($url , $postArr);
//var_dump($postArr);die();
return $result;
}
/* * 创建企业印模
* */
public function createcompanyyingmo($data){
$url=self::API_URL.'/user/companyMoulage';
$postArr = $data;
$result = $this->curlPost($url , $postArr);
//var_dump($postArr);die();
return $result;
}
生成合同
public function createhetong($data){
$url=self::API_URL.'/contract/templateContract';
$postArr = $data;
$result = $this->curlPost($url , $postArr);
//var_dump($postArr);die();
return $result;
}
添加签署者
//添加签署者
public function addsigner($data){
$url=self::API_URL.'/contract/signer';
$postArr = $data;
$result = $this->curlPost($url , $postArr);
//var_dump($postArr);die();
return $result;
}
签署合同
//合同签署,点击签署,直接生成合同
public function sign($data){
$url=self::API_URL.'/contract/sign';
$postArr = $data;
$result = $this->curlPost($url , $postArr);
//var_dump($postArr);die();
return $result;
}
以上就是自动签署云合同,后台调用接口的过程,希望能帮助有需要的伙伴。
本人对接这个云合同的接口,完全处于炸的状态和即将要炸的状态,对接太坑了,让你绕一圈之后,完全否定你,然后一切都是白用功,从头再来,每个步骤怎么走,全是本人用众多问题炸出来的,一定是让你走了所有弯路,山穷水尽之后,对接人员才会告诉你走哪一步。。。最最最坑的就是接口兼容性真的是绝了,要求穿json数据格式,用data传始终报错参数不匹配,把能改的全改了,能试的全试了,行不通,绝望啊,最后把data改成array,居然可以了,谁能告诉我这是什么鬼,所以当你们遇到怎么也解决不了的问题的时候就测试一下兼容性吧。。。
本人随后会出一个写给前端的接口,所有的流程是通过后台进行控制的,所以小伙伴也可以参考一下。