$dff = $dao_read->getorderbaowenByConditions($oidbw);
$xml = new ArrayToXML;
$guid = $xml->getGuidOnlyValue();
$guid1 = $xml->GetRandStr(3).$guid;
$guid2 = $xml->GetRandStr(3).$guid;
$customsConfig = [
'customsCode' => 'xxxxxxxxxx',
'customsName' => 'xxxxxxxxxx限公司',
'DXP' => 'DXPENT0000015248'
];
//身份证转码
$id_number = $xml->decrypt($dff[0]['id_number'],$dff[0]['uid']);
$addTime = date("YmdHis");
$OrderList = [];
$i = 0;
foreach($dff as $k1 => $v1){
$i++;
//查询海关的国别代码
//查询海关的计量代码
$OrderList[]['ceb:OrderList'] = [
"ceb:gnum" => $i,
"ceb:itemNo" => $v1['oid'], //企业商品货号,电商企业自定义的商品货号(SKU)(可为空)
"ceb:itemName" => $v1['title'], //交易平台销售商品的中文名称
"ceb:itemDescribe" => '', //交易平台销售商品的描述信息(可为空)
"ceb:barCode" => $v1['barcode1'], //国际通用的商品条形码(可为空)
"ceb:unit" => '', //计量单位,填写海关标准的参数代码 1
"ceb:qty" => $v1['weight'], //商品实际数量
"ceb:price" => $v1['price'],
"ceb:totalPrice" => $v1['pay']*0.88,
"ceb:currency" => "142",
"ceb:country" => '',
"ceb:note" => '',
];
}
//查询海关的城市代码
$order = [
'ceb:OrderHead'=>[
'ceb:guid' => $guid2,
'ceb:appType' => '1', //企业报送类型。1-新增 2-变更 3-删除。默认为1
'ceb:appTime' => $addTime, //企业报送时间
'ceb:appStatus' => '2', //业务状态:1-暂存,2-申报,默认为1。填写2时,Signature节点必须填写
'ceb:orderType' => 'I', //电子订单类型:I进口
'ceb:orderNo' => $dff[0]['oid'], //交易平台的订单编号,同一交易平台的订单编号应唯一。长度不能超过60
'ceb:ebpCode' => $customsConfig['customsCode'], //电商平台的海关注册登记编号
'ceb:ebpName' => $customsConfig['customsName'],
'ceb:ebcCode' => $customsConfig['customsCode'],
'ceb:ebcName' => $customsConfig['customsName'],
'ceb:goodsValue' => $dff[0]['pay']*0.88, //商品实际成交价,含非现金抵扣金额
'ceb:freight' => '0', //不包含在商品价格中的运杂费,无则填写"0"。
'ceb:discount' => '0', //使用积分、虚拟货币、代金券等非现金支付金额,无则填写"0"
'ceb:taxTotal' => '0', //企业预先代扣的税款金额,无则填写“0”
'ceb:acturalPaid' => $dff[0]['pay']*0.88, //商品价格+运杂费+代扣税款-非现金抵扣金额,与支付凭证的支付金额一致。
'ceb:currency' => '142', //限定为人民币,填写“142”
'ceb:buyerRegNo' => $dff[0]['uid'], //订购人的交易平台注册号(可以传自家平台的ID,也可以随便传,不校验)
'ceb:buyerName' => $dff[0]['id_name'], //订购人的真实姓名
'ceb:buyerIdType' => '1', //订购人证件类型,1-身份证,2-其它。限定为身份证,填写“1”
'ceb:buyerIdNumber' => $id_number, //订购人身份证号
'ceb:payCode' => '', //支付企业代码 支付企业需在JC2006注册备案
'ceb:payName' => '', //支付企业名称
'ceb:payTransactionId' => '', //支付交易流水号
'ceb:batchNumbers' => '', //商品批次号(选填)
'ceb:consignee' => $dff[0]['name'], //收货人姓名,必须与电子运单的收货人姓名一致。(收货人同订购人信息一致)
'ceb:consigneeTelephone'=> $dff[0]['tel_real'], //收货人联系电话,必须与电子运单的收货人电话一致。
'ceb:consigneeAddress' => $dff[0]['sendaddress'], //收货地址,必须与电子运单的收货地址一致。
'ceb:consigneeDistrict' => '', //收货地址行政区划代码,参照国家统计局公布的国家行政区划标准填制。1
'ceb:note' => 'test',
]
];
$order = array_merge($order,$OrderList);
$res = [];
$res['ceb:CEB311Message'] = [
'guid' => $guid1,
'version' => '1.0',
'xmlns:ceb' => 'http://www.chinaport.gov.cn/ceb',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'ceb:Order' => $order,
'ceb:BaseTransfer' => [
'ceb:copCode' => $customsConfig['customsCode'],
'ceb:copName' => $customsConfig['customsName'],
'ceb:dxpMode' => 'DXP',
'ceb:dxpId' => $customsConfig['DXP'],
'ceb:note' => 'test'
]
];
//header("Content-type: application/xml");
//die();
$s = $xml->toXml($res);
//$s = $this->toXml($res);
header("Content-type: text/html; charset=utf-8");
print $s;
<?php
class ArrayToXML{
function arrForeach($arr) {
if (!is_array ($arr)) {
return false;
}
$xml='';
foreach($arr as $key => $value){
$xml .= is_numeric($key)?"":"<{$key}>";
$xml .= is_array($value)? self::arrForeach($value):$value;
$xml .= is_numeric($key)?"":"</{$key}>\n";
}
return $xml;
}
//字符串转xml
function toXml($post){
foreach ($post as $key => $value) {
$attribute = '';
$str = '';
$strFot ='';
foreach ($value as $k1 => $v1) {
if(is_array($v1)){ //如果含有数组
$str .= is_numeric($k1)?"":"<{$k1}>";
$str .= $this->arrForeach($v1);
$str .= is_numeric($k1)?"":"</{$k1}>";
}else{
$attribute .= $k1.'="'.$v1.'" ';
}
}
$strHead = '<?xml version="1.0" encoding="UTF-8"?>';
$strHead .= '<'.$key.' '.$attribute.'>'; //key为第一行标签名称 + 第一行的属性
$strFot .= '</'.$key.'>';
}
$string = $strHead.$str.$strFot;
return $string;
}
// 解密字符串
function decrypt($str, $key) {
if ($str!='') {
$str = base64_decode($str);
$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
$block = mcrypt_get_block_size('des', 'ecb');
$pad = ord($str[($len = strlen($str)) - 1]);
if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
$str = substr($str, 0, strlen($str) - $pad);
}
}
return unserialize($str);
}
//海关生成唯一guid值
function getGuidOnlyValue(){
$a4 = uniqid().rand(10,99);
$a4 = $this->insertToStr($a4,4,'-'); //这里是后面两组的唯一值 如5770-A529AD987M
$a4 = $a4.$this->GetRandStr(1);
$a1 = $this->GetRandStr(5);
$a2 = $this->GetRandStr(4);
$a3 = $this->GetRandStr(4);
$val = $a1.'-'.$a2.'-'.$a3.'-'.$a4;
return strtoupper($val);
}
/**
* 指定位置插入字符串
* @param $str 原字符串
* @param $i 插入位置
* @param $substr 插入字符串
* @return string 处理后的字符串
*/
function insertToStr($str, $i, $substr){
$startstr="";
for($j=0; $j<$i; $j++){
$startstr .= $str[$j];
}
//指定插入位置后的字符串
$laststr="";
for ($j=$i; $j<strlen($str); $j++){
$laststr .= $str[$j];
}
//将插入位置前,要插入的,插入位置后三个字符串拼接起来
$str = $startstr . $substr . $laststr;
//返回结果
return $str;
}
/**
* 获得指定位数随机数
* @param $length 指定位数
* @return string 处理后的字符串
*/
function GetRandStr($length){
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len=strlen($str)-1;
$randstr='';
for($i=0;$i<$length;$i++){
$num=mt_rand(0,$len);
$randstr .= $str[$num];
}
return $randstr;
}
}
?>
本文介绍了一个用于生成海关申报订单报文的过程,涉及订单信息获取、数据结构化处理、XML文件生成及加密解密操作等内容。重点包括订单详情的读取、自定义XML类的使用、唯一标识符的创建及敏感信息的加密解密。
858

被折叠的 条评论
为什么被折叠?



