Files
shipping-portal/app/Classes/Modules/WePost/ControllersLogic/ProcessWePostNotificationLogic.php

168 lines
6.0 KiB
PHP

<?php
namespace App\Classes\Modules\WePost\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\General\WePostHelper;
use App\Classes\Modules\WePost\Services\CreatesWePostNotification;
use App\Classes\Modules\WePost\DataTransferObjects\WePostNotificationObject;
use App\Classes\Modules\WePost\DataTransferObjects\WePostProcessNotificationObject;
use App\Classes\Modules\WePost\Standards\Rules\CanProcessWePostNotification;
use App\Http\Resources\WePostNotificationResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ProcessWePostNotificationLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Process WePost Notification',
'message' => 'Payload proccessed'
];
}
/** @var CreatesWePostNotification */
private $createsWePostNotification;
/** @var CanProcessWePostNotification */
private $canProcessWePostNotification;
/**
* ProcessWePostNotificationLogic constructor.
* @param CreatesWePostNotification $createsWePostNotification
* @param CanProcessWePostNotification $canProcessWePostNotification
*/
public function __construct(
CreatesWePostNotification $createsWePostNotification, CanProcessWePostNotification $canProcessWePostNotification)
{
$this->createsWePostNotification = $createsWePostNotification;
$this->canProcessWePostNotification = $canProcessWePostNotification;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function logic(Request $request) : JsonResponse
{
$request->merge([
'isApiPub' => true,
]);
$object = new WePostProcessNotificationObject($request->query('api-key'));
$this->canProcessWePostNotification->passes($object);
$result = null;
$code = 0;
$logisticsOrderCode = "";
$executeTime = "";
$timeZone = "";
$status = "";
$packageInfo = [];
$clientId = "";
$type = "";
$timestamp = 0;
$sign = "";
$orderCode = "";
$bizAction = "";
$bizActionDesc= "";
$segmentCode = "";
$isSplitOut = "";
$packageInfos = [];
if($request->has('type')){
$type = $request->input('type');
if($type === 'wepost.service.conso.inbound'){
$logisticsOrderCode = $request->input('logisticsOrderCode');
$executeTime = $request->input('executeTime');
$timeZone = $request->input('timeZone');
$status = $request->input('status');
$packageInfo = $request->input('packageInfo');
$clientId = $request->input('client_id');
$type = $request->input('type');
$timestamp = $request->input('timestamp');
$sign = $request->input('sign');
}
else if($type === 'wepost.service.conso.co.biz.action.notify'){
$executeTime = $request->input('executeTime');
$timeZone = $request->input('timeZone');
$clientId = $request->input('client_id');
$type = $request->input('type');
$timestamp = $request->input('timestamp');
$sign = $request->input('sign');
$orderCode = $request->input('orderCode');
$bizAction = $request->input('bizAction');
$bizActionDesc = $request->input('bizActionDesc');
}
else if($type === 'wepost.service.conso.outbound'){
$executeTime = $request->input('executeTime');
$timeZone = $request->input('timeZone');
$clientId = $request->input('client_id');
$type = $request->input('type');
$timestamp = $request->input('timestamp');
$sign = $request->input('sign');
$orderCode = $request->input('orderCode');
$segmentCode = $request->input('segmentCode');
$isSplitOut = $request->input('isSplitOut');
$packageInfos = $request->input('packageInfos');
}
else{
$code = 1202; //Missing required fields
}
}
else{
$code = 1202; //Missing required fields
}
if(!$sign){
$code = 1100; //Signature is empty
}
if($code == 0){
if($this->verifySignature($type, $sign)){
$object = new WePostNotificationObject(
$logisticsOrderCode, $executeTime, $timeZone, $status,$packageInfo,
$clientId, $type, $timestamp, $sign,
$orderCode, $segmentCode, $bizAction, $bizActionDesc, $isSplitOut, $packageInfos);
$result = $this->createsWePostNotification->execute($object);
}
else{
$code = 1101; //Signature verification failed
}
}
if($code != 0){
$request->merge([
'code' => $code,
]);
}
// $result = [];
// $result['logisticsOrderCode'] = $notification->logistics_order_code;
// return $this->collectionResponse(OrderItemResource::collection($order->orderItems));
// return $this->resourceResponse(new OrderResource($order));
//return $this->response(['data' => $result]);
return $this->resourceResponse(new WePostNotificationResource($result));
}
private function verifySignature($type, $sign){
$result = false;
$signToCompare = WePostHelper::generateSignature(config('wepost.client_id'), config('wepost.client_secret'), $type);
// dd($signToCompare);
if($sign == $signToCompare){
$result = true;
}
return $result;
}
}