diff --git a/app/Classes/Modules/WePost/ControllersLogic/ProcessWePostNotificationLogic.php b/app/Classes/Modules/WePost/ControllersLogic/ProcessWePostNotificationLogic.php new file mode 100644 index 00000000..057b9fc3 --- /dev/null +++ b/app/Classes/Modules/WePost/ControllersLogic/ProcessWePostNotificationLogic.php @@ -0,0 +1,109 @@ + '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; + } +} diff --git a/app/Classes/Modules/WePost/DataTransferObjects/WePostNotificationObject.php b/app/Classes/Modules/WePost/DataTransferObjects/WePostNotificationObject.php new file mode 100644 index 00000000..4cf284c8 --- /dev/null +++ b/app/Classes/Modules/WePost/DataTransferObjects/WePostNotificationObject.php @@ -0,0 +1,142 @@ +logisticsOrderCode = $logisticsOrderCode; + $this->executeTime = $executeTime; + $this->timeZone = $timeZone; + $this->status = $status; + $this->packageInfo = $packageInfo; + $this->client_id = $client_id; + $this->type = $type; + $this->timestamp = $timestamp; + $this->sign = $sign; + } + + /** + * @return string + */ + public function getLogisticsOrderCode(): string + { + return $this->logisticsOrderCode; + } + + /** + * @return string + */ + public function getExecuteTime(): string + { + return $this->executeTime; + } + + /** + * @return string + */ + public function getTimeZone(): string + { + return $this->timeZone; + } + + /** + * @return string + */ + public function getStatus(): string + { + return $this->status; + } + + /** + * @return array + */ + public function getPackageInfo(): array + { + return $this->packageInfo; + } + + /** + * @return string + */ + public function getClientId(): string + { + return $this->client_id; + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @return int + */ + public function getTimestamp(): int + { + return $this->timestamp; + } + + /** + * @return string + */ + public function getSign(): string + { + return $this->sign; + } +} diff --git a/app/Classes/Modules/WePost/Services/CreatesWePostNotification.php b/app/Classes/Modules/WePost/Services/CreatesWePostNotification.php new file mode 100644 index 00000000..5999a74c --- /dev/null +++ b/app/Classes/Modules/WePost/Services/CreatesWePostNotification.php @@ -0,0 +1,32 @@ +logistics_order_code = $object->getLogisticsOrderCode(); + $model->execute_time = $object->getExecuteTime(); + $model->timezone = $object->getTimeZone(); + $model->status = $object->getStatus(); + $model->package_info = $object->getPackageInfo(); + $model->client_id = $object->getClientId(); + $model->type = $object->getType(); + $model->timestamp = $object->getTimestamp(); + $model->sign = $object->getSign(); + + return $this->handler($model); + } +} diff --git a/app/Http/Controllers/WePost/ProcessWePostNotificationController.php b/app/Http/Controllers/WePost/ProcessWePostNotificationController.php new file mode 100644 index 00000000..6e761e2f --- /dev/null +++ b/app/Http/Controllers/WePost/ProcessWePostNotificationController.php @@ -0,0 +1,20 @@ +execute($request); + } +} diff --git a/app/Http/Resources/WePostNotificationResource.php b/app/Http/Resources/WePostNotificationResource.php new file mode 100644 index 00000000..c88fbe35 --- /dev/null +++ b/app/Http/Resources/WePostNotificationResource.php @@ -0,0 +1,34 @@ + $this->logistics_order_code, + ]; + } catch(\Exception $exception){ + // throw new MalformedRequestException('Signature verification failed'); + $code = 0; + if($request->input('code')){ + $code = $request->input('code'); + } + return [ + 'code' => $code, + ]; + } + } +} diff --git a/app/Models/WePostNotification.php b/app/Models/WePostNotification.php new file mode 100644 index 00000000..c810860b --- /dev/null +++ b/app/Models/WePostNotification.php @@ -0,0 +1,27 @@ + 'array', + 'execute_time' => 'datetime', + ]; +} diff --git a/database/migrations/2023_12_02_131541_create_we_post_notifications_table.php b/database/migrations/2023_12_02_131541_create_we_post_notifications_table.php new file mode 100644 index 00000000..a5901e56 --- /dev/null +++ b/database/migrations/2023_12_02_131541_create_we_post_notifications_table.php @@ -0,0 +1,41 @@ +id(); + $table->string('logistics_order_code'); + $table->timestamp('execute_time'); + $table->string('timezone'); + $table->string('status'); + $table->json('package_info'); + $table->string('client_id'); + $table->string('type'); + $table->bigInteger('timestamp'); + $table->string('sign'); + // $table->string('notification_type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('wepost_notifications'); + } +} diff --git a/routes/apipub.php b/routes/apipub.php index aecebdb3..b093615c 100644 --- a/routes/apipub.php +++ b/routes/apipub.php @@ -9,4 +9,9 @@ Route::group(['middleware' => 'apipub', 'prefix' => 'v1', 'as' => 'apipub.'], fu Route::get('transactions/mappable/query', 'Transactions\ListMappableTransactionsController@list')->name('transaction.mappable.list'); Route::get('groups/query', 'Transactions\ListGroupsController@list')->name('group.list'); }); + + Route::group(['prefix' => 'wepost', 'as' => 'wepost.'], function () { + Route::post('process', 'WePost\ProcessWePostNotificationController@process')->name('notification'); + }); + });