加密方式为 DES3 航信官方要求是DES加密方式,经和航信技术员沟通后 开放DES3加密方式。DES加密比较low了
/**
* 航信开票
*/
public function HXInvoiceAction(){
$oid = empty($this->params["oid"]) ? '' : $this->params["oid"];
$OrdersData = $this->model->getAny($oid);
if($OrdersData['discount']){
$discount = isset($OrdersData['discount'])?1:0;
unset($OrdersData['discount']);
}
$Config = Config::getInstance();
$webSite = $Config->getWebSiteConfig();
$code = $webSite['HX_CODE'];
$parameter = [];
$details = [];
$parameter['kptype'] = 1;
$parameter['saletaxnum'] = $webSite['HX_DUTY'];
# 销方纳税人识别号 提供
$parameter['saleaddress'] = $webSite['HX_SELLER_ADDRESS'];
# 销方地址 提供
$parameter['salephone'] = $webSite['HX_SELLER_TEL'];
# 销方电话 提供
$parameter['clerk'] = $webSite['HX_SELLER_CLERK'];
# 开票员 提供
$parameter['orderno'] = $code.$OrdersData[0]['orderNo'].'_'.strtotime($OrdersData[0]['createdt']);
# orderno
foreach ($OrdersData as $k=>$itm){
# 我这里为什么要余2 是因为我前端有特殊显示,所以再推送的时候多判断了一次。$discount = 有抵扣
if($discount) {
if ($k % 2 == 0) {
$parameter['invoicedate'] = $itm['createdt'];
# 订单日期
$parameter['ordertotal'] = $itm['paidPrice'];
# 价税合计金额 付款金额
$parameter['cpy'] = 0;
# 成品油标识
$details[$k]['goodsname'] = $itm['name'];
# 商品名称
$details[$k]['hsbz'] = 1;
# 含税标志 表示单价、项目金额、折扣金额是否含税。0表示都不含税,1表示都含税 # 是否含税
$details[$k]['price'] = $itm['price2'];
# 单价
//
$details[$k]['spbm'] = isset($webSite['debug']) ? '1090511030000000000' : $itm['num'];
# 税收分类编码 提供
$details[$k]['spbm'] = $itm['num'];
# 税收分类编码 提供
$details[$k]['num'] = $itm['qty2'];
# 数量
$details[$k]['xmje'] = $itm['price2'] * $itm['qty2'];
# 小数点后2位,以元为单位精确到分。项目金额=单价*数量,根据含税标志,确定此金额是否含税
$details[$k]['se'] = '';
# 税额 小数点后2位,以元为单位精确到分,当hsbz为0时,必填
$details[$k]['zkse'] = '';
# 折扣税额
$details[$k]['zkje'] = $itm['zkje'];
# 折扣金额
if ($itm['rate'] <= 0) {
if(substr($itm['num'],0,1) == 6){
$details[$k]['zzstsgl'] = "不征税";
# 增值税特殊管理
$details[$k]['yhzcbs'] = "1";
# 0:不使用;1:使用。默认为空,表示不使用
}
$details[$k]['lslbs'] = 2;
# 零税率标识
$details[$k]['taxrate'] = 0;
} else {
$details[$k]['zzstsgl'] = "";
# 增值税特殊管理
$details[$k]['yhzcbs'] = "0";
$details[$k]['taxrate'] = $itm['rate'] / 100;
# 税率 用小数表示,如果税率为0,表示免税
$details[$k]['lslbs'] = '';
}
}
}else{
$parameter['invoicedate'] = $itm['createdt'];
$parameter['ordertotal'] = $itm['paidPrice'];
$parameter['cpy'] = 0;
$details[$k]['goodsname'] = $itm['name'];
$details[$k]['hsbz'] = 1;
$details[$k]['price'] = $itm['price2'];
$details[$k]['spbm'] = $itm['num'];
$details[$k]['num'] = $itm['qty2'];
$details[$k]['yhzcbs'] = "0";
$details[$k]['xmje'] = $itm['price2'] * $itm['qty2'];
$details[$k]['se'] = '';
$details[$k]['zkse'] = '';
$details[$k]['zkje'] = '';
$details[$k]['zzstsgl'] = "";
if ($itm['rate'] <= 0) {
$details[$k]['lslbs'] = 1;
$details[$k]['taxrate'] = 0;
} else {
$details[$k]['taxrate'] = $itm['rate'] / 100;
$details[$k]['lslbs'] = '';
}
}
}
$parameter['detail'] = array_values($details);
$parameter = json_encode($parameter,JSON_UNESCAPED_UNICODE);
//
var_dump($parameter);die;
$s = $this->des3_encrypt($parameter,$webSite['HX_SECRET_KRY']);
$s = $code.$s;
$url = $webSite['HX_URL'];
$log = new Logging();
$log->lfile('HXpiao');
$log->lwrite(' request:' . $parameter );
$log->lwrite(' request:' . $s );
$res = $this->httpCurl($url,$s);
$decrypt = $this->des3_decrypted($res,$webSite['HX_SECRET_KRY']);
$log->lwrite('decrypt:' . $decrypt );
$decrypt = strstr($decrypt,'}',true).'}'; # 航信会携带一些字符回来 所以这里做下处理 然后转数组
$decryptData = json_decode($decrypt,true);
if($decryptData['status'] == 000){ # 成功后 为所欲为 航信会携带开票地址回来,解密后
$_field = array();
$_field['HXResult'] = $decrypt;
$_field['updatedt'] = 'now()';
$where_fields = array("AND" => array("=" => array('id' => $OrdersData[0]['id'], 'isdeleted' => 0)));
$this->model->updateItem($_field, $where_fields, 'orders');
}
echo $decrypt;
}
function httpCurl($url,$data)
{
$log = new Logging();
$log->lfile('HXpiao');
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$output = curl_exec($ch);
curl_close($ch);
$log->lwrite(' reception:' . json_encode($output,JSON_UNESCAPED_UNICODE) );
return $output;
}
function des3_encrypt($input,$key){
return $data = openssl_encrypt($input,'des-ede3',$key,0);
}
function des3_decrypted($data,$key){
return $decrypted = openssl_decrypt(base64_decode($data),'des-ede3',$key,OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
}
效果(不同税率有抵扣,不同税率无抵扣,不征税):