You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.KdniaoApi.php

122 lines
3.4 KiB
PHP

<?php
class KdniaoApi {
private $eBusinessID;
private $appKey;
private $reqHost;
private $reqURL;
private $log;
private $curl;
public function __construct() {
$kdniaoConfig = $this->getKdniaoConfig();
$this->eBusinessID = $kdniaoConfig['eBusinessID'];
$this->appKey = $kdniaoConfig['appKey'];
$this->reqHost = $kdniaoConfig['reqHost'];
$this->curl = CommonTool::getCurl();
$this->log = Zc::getLog('tool/kdniao_api/log');
}
private function getKdniaoConfig() {
$topConfig = Zc::C('kdniao');
$env = Zc::C('kdniaoEnv');
return $topConfig[$env];
}
private function setReqURL($method) {
switch ($method) {
case 'subscribe':
$this->reqURL = $this->reqHost . 'api/dist';
break;
case 'Eorderservice':
$kdniaoEnv = Zc::C('kdniaoEnv');
if ($kdniaoEnv == 'live') {
$this->reqURL = $this->reqHost . 'api/Eorderservice';
} else {
$this->reqURL = $this->reqHost . 'kdniaosandbox/gateway/exterfaceInvoke.json';
}
break;
}
}
private function buildSysArgs($requestType, $requestData) {
$this->log->info("requestData: " . print_r($requestData, true));
$requestData = json_encode($requestData, JSON_UNESCAPED_UNICODE);
$datas = array (
'EBusinessID' => $this->eBusinessID,
'RequestType' => $requestType,
'RequestData' => urlencode($requestData),
'DataType' => '2',
);
$datas['DataSign'] = $this->encrypt($requestData);
return $datas;
}
public function getWaybill($requestData) {
$this->log = Zc::getLog('tool/kdniao_api/Eorderservice');
$this->setReqURL('Eorderservice');
$sysArgs = $this->buildSysArgs('1007', $requestData);
return $this->exec($sysArgs, 'Eorderservice');
}
public function logisticsSubscribe($orderId, $cpCode, $waybillCode) {
$this->log = Zc::getLog('tool/kdniao_api/subscribe');
$requestData = $this->packLogisticsSubscribeRequestData($orderId, $cpCode, $waybillCode);
$this->setReqURL('subscribe');
$sysArgs = $this->buildSysArgs('1008', $requestData);
return $this->exec($sysArgs, 'subscribe');
}
private function packLogisticsSubscribeRequestData($orderId, $cpCode, $waybillCode) {
$data = array();
$data['OrderCode'] = $orderId;
$data['ShipperCode'] = $cpCode;
$data['LogisticCode'] = $waybillCode;
$data['ExpType'] = 1; // 默认1-标准快递类型
$data['IsNotice'] = 1; // 默认1-不分发到快递公司
return $data;
}
private function exec($sysArgs) {
$this->log->info("sendPost url: $this->reqURL, params: " . json_encode($sysArgs));
$result = $this->curl->post($this->reqURL, $sysArgs);
$this->log->info('kdniao getInfo . ' . json_encode($this->curl->getInfo()));
$this->log->info("response: " . $result);
if (empty($result)) {
return CommonTool::failResult('请求失败');
}
return CommonTool::successResult('responseData', json_decode($result, true));
}
public function encrypt($datas) {
return urlencode(base64_encode(md5($datas . $this->appKey)));
}
public function verifyDataSign($requestData, $dataSign) {
$checkDataSign = $this->encrypt($requestData);
return $checkDataSign == $dataSign ? true : false;
}
public function buildCallbackResponse($result, $reason = null) {
$data = array(
'EBusinessID' => $this->eBusinessID,
'UpdateTime' => ZcDateHelper::now(),
'Success' => $result ? true : false,
'Reason' => $reason
);
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
}