diff --git a/src/GwallWms.php b/src/GwallWms.php index 8f494d9..17ea2cb 100644 --- a/src/GwallWms.php +++ b/src/GwallWms.php @@ -397,4 +397,9 @@ class GwallWms extends BaseWms implements WmsInterface }, $params); return $params; } + + public function callback($params) + { + + } } diff --git a/src/WmsInterface.php b/src/WmsInterface.php index 94626ae..b430b7b 100644 --- a/src/WmsInterface.php +++ b/src/WmsInterface.php @@ -19,4 +19,6 @@ Interface WmsInterface public function stockInCreate(array $params, int $type); public function stockOutCreate(array $params); + + public function callback(array $params); } diff --git a/src/WmsStrategy.php b/src/WmsStrategy.php index 2ae435f..aa7f758 100644 --- a/src/WmsStrategy.php +++ b/src/WmsStrategy.php @@ -52,4 +52,9 @@ class WmsStrategy public function stockOutCreate($params) { return $this->wmsClient->stockOutCreate($params); } + + public static function callback($params) { + $client = new XinyiWms(); + return $client->callback($params); + } } diff --git a/src/XinyiWms.php b/src/XinyiWms.php index 74e146c..2c97a95 100644 --- a/src/XinyiWms.php +++ b/src/XinyiWms.php @@ -2,6 +2,11 @@ namespace PdWms; +use App\Service\Goods\GoodsWarehouseService; +use App\Service\Order\OrderItemLackReportService; +use App\Service\Order\OrderProcessReportService; +use App\Service\Order\OrderService; +use App\Service\Order\RefundService; use PdWms\Service\Array2XML; use PdWms\Service\XML2Array; use GuzzleHttp\Client; @@ -447,6 +452,185 @@ class XinyiWms extends BaseWms implements WmsInterface return $ret; } + public function xmlPost($method, $params) + { + $xml = Array2XML::createXML('request', $params); + $body = $xml->saveXML(); + $res = $this->createSign($method, $body); + $result = $this->post($body, $res); + + $isSuccess = (empty($result) || !is_array($result) || $result['response']['flag'] != 'success') ? false : true; + $response = [ + 'success' => $isSuccess, + 'response' => $isSuccess ? $result['response'] : $result, + ]; + return $response; + } + + /** + * 确认回传方法 + * @var array + */ + private static $callbackMethods = [ + 'taobao.qimen.deliveryorder.confirm' => 'saleOrderConfirm', + 'taobao.qimen.entryorder.confirm' => 'stockInConfirm', + 'taobao.qimen.itemlack.report' => 'itemLackReport', + 'taobao.qimen.orderprocess.report' => 'orderProcessReport', + 'taobao.qimen.returnorder.confirm' => 'saleReturnOrderConfirm', + 'taobao.qimen.stockchange.report' => 'stockChangeReport', + 'taobao.qimen.stockout.confirm' => 'stockOutConfirm', + ]; + + public function callback($params) + { + if (!isset($params['method']) || empty($params['method'])) { + return $this->response('缺少参数method', false); + } + if (!isset($params['timestamp']) || empty($params['timestamp'])) { + return $this->response('缺少参数timestamp', false); + } + if (!isset($params['format']) || empty($params['format'])) { + return $this->response('缺少参数format', false); + } + if (!isset($params['app_key']) || empty($params['app_key'])) { + return $this->response('缺少参数key', false); + } + if (!isset($params['v']) || empty($params['v'])) { + return $this->response('缺少版本参数', false); + } + if (!isset($params['sign']) || empty($params['sign'])) { + return $this->response('缺少参数sign', false); + } + if (!isset($params['sign_method']) || empty($params['sign_method'])) { + return $this->response('缺少加密方式', false); + } + if (!isset($params['customerId']) || empty($params['customerId'])) { + return $this->response('缺少参数customerId', false); + } + if (empty(($params['body']))) { + return $this->response('缺少消息body', false); + } + + $method = $params['method']; + if (!array_key_exists($method, self::$callbackMethods)) { + return $this->response('调用的方法不存在', false); + } + if (!$this->checkSign($params)) { + return $this->response('签名错误', false); + } + $body = XML2Array::createArray($params['body']); + if (!isset($body['request']) || empty($body['request'])) { + return $this->response('消息body错误', false); + } + $result = call_user_func([$this, self::$callbackMethods[$method]], $body['request']); + return $result; + } + + /** + * 发货单确认 + * @param $params + * @return array + */ + private function saleOrderConfirm ($params) + { + $service = new OrderService(); + $result = $service->deliverOrderConfirm($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + + /** + * 入库单确认 + * @param $params + * @return array + */ + private function stockInConfirm($params) + { + $service = new GoodsWarehouseService(); + $result = $service->entryorderConfirm($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + + /** + * 发货单缺货通知 + * @param $params + * @return array + */ + private function itemLackReport($params) + { + $service = new OrderItemLackReportService(); + $result = $service->createReceipts($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + + /** + * 订单流水通知 + * @param $params + * @return array + */ + private function orderProcessReport($params) + { + $service = new OrderProcessReportService(); + $result = $service->orderProcessReport($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + + /** + * 退货入库单确认 + * @param $params + * @return array + */ + private function saleReturnOrderConfirm($params) + { + $service = new RefundService(); + $result = $service->refundOrderConfirm($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + + /** + * 库存异动通知 + * @param $params + * @return array + */ + private function stockChangeReport($params) + { + $service = new GoodsWarehouseService(); + $result = $service->StockChangeReport($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + + /** + * 出库单确认 + * @param $params + * @return array + */ + private function stockOutConfirm($params) + { + $service = new OrderService(); + $result = $service->stockOutOrderConfirm($params); + if (!$result) { + return $this->response('失败', false); + } + return $this->response(); + } + /** * @检查签名 * @param $params @@ -469,18 +653,19 @@ class XinyiWms extends BaseWms implements WmsInterface return true; } - public function xmlPost($method, $params) + public function response($message = '成功', $flag = true) { - $xml = Array2XML::createXML('request', $params); - $body = $xml->saveXML(); - $res = $this->createSign($method, $body); - $result = $this->post($body, $res); - - $isSuccess = (empty($result) || !is_array($result) || $result['response']['flag'] != 'success') ? false : true; $response = [ - 'success' => $isSuccess, - 'response' => $isSuccess ? $result['response'] : $result, + 'flag' => $flag ? 'success' : 'failure', + 'code' => $flag ? '001' : '002', + 'message' => $message, + ]; + $xml = Array2XML::createXML('response', $response); + $body = $xml->saveXML(); + return [ + 'content' => $body, + 'status' => 200, + 'headers' => ['Content-Type' => 'xml'], ]; - return $response; } } \ No newline at end of file