mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-20 21:14:04 +00:00
110 lines
3.5 KiB
PHP
110 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\WePost\ControllersLogic;
|
|
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\WePost\Services\CreatesWePostNotification;
|
|
use App\Classes\Modules\WePost\DataTransferObjects\WePostNotificationObject;
|
|
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;
|
|
|
|
/**
|
|
* ProcessWePostNotificationLogic constructor.
|
|
* @param CreatesWePostNotification $createsWePostNotification
|
|
*/
|
|
public function __construct(
|
|
CreatesWePostNotification $createsWePostNotification)
|
|
{
|
|
$this->createsWePostNotification = $createsWePostNotification;
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
{
|
|
$result = null;
|
|
if($this->verifySignature($request)){
|
|
$object = new WePostNotificationObject(
|
|
$request->input('logisticsOrderCode'), $request->input('executeTime'), $request->input('timeZone'),
|
|
$request->input('status'),$request->input('packageInfo'),$request->input('client_id'),
|
|
$request->input('type'),$request->input('timestamp'),$request->input('sign'));
|
|
$result = $this->createsWePostNotification->execute($object);
|
|
}
|
|
else{
|
|
$request->merge([
|
|
'code' => 1101, //Signature verification failed
|
|
]);
|
|
}
|
|
// $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(Request $request){
|
|
$result = false;
|
|
// public parameters
|
|
$client_id = "1234";
|
|
$type = "wepost.service.conso.inbound";
|
|
$timestamp = 1234; //time();
|
|
$client_secret = 'cief-not-so-secret';
|
|
|
|
$params = [
|
|
'client_id' => $client_id,
|
|
'type' => $type,
|
|
'timestamp' => $timestamp
|
|
];
|
|
|
|
// $businessParams = [
|
|
// 'test1' => '1',
|
|
// ];
|
|
|
|
ksort($params);
|
|
$str = '';
|
|
foreach ($params as $key => $value) {
|
|
if(is_array($value)) {
|
|
$str .= $key.json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
continue;
|
|
}
|
|
$str .= $key. $value;
|
|
}
|
|
$str = $client_secret.$str.$client_secret;
|
|
$sign = strtoupper(md5($str));
|
|
|
|
if($request->input('sign')){
|
|
$request->merge([
|
|
'code' => 1100, //Signature is empty
|
|
]);
|
|
}
|
|
|
|
if($request->input('sign') == $sign){
|
|
$result = true;
|
|
}
|
|
return $result;
|
|
}
|
|
}
|