From 729a49920e2af07530923af70f8237c9363ba7e5 Mon Sep 17 00:00:00 2001 From: Ariful islam Date: Tue, 14 Jun 2022 17:59:50 +0600 Subject: [PATCH] code restructured --- src/Provider/PaperflyCourier.php | 245 ++++++++++++++++++------------- 1 file changed, 139 insertions(+), 106 deletions(-) diff --git a/src/Provider/PaperflyCourier.php b/src/Provider/PaperflyCourier.php index cf38616..1d53817 100644 --- a/src/Provider/PaperflyCourier.php +++ b/src/Provider/PaperflyCourier.php @@ -1,188 +1,221 @@ baseUrl($environment); - $data=[ - "merOrderRef"=>$orderInformation['OrderNo'], - "pickMerchantName"=>$orderInformation['MerchantName'], - "pickMerchantAddress"=>$orderInformation['MerchantAddress'], - "pickMerchantThana"=>$orderInformation['MerchantThana'], - "pickMerchantDistrict"=>$orderInformation['MerchantDistrict'], - "pickupMerchantPhone"=>$orderInformation['MerchantPhone'], - "productSizeWeight"=>$orderInformation['SizeWeight'], - "productBrief"=>$orderInformation['productDetails'], - "packagePrice"=>$orderInformation['packagePrice'], - "deliveryOption"=>$orderInformation['deliveryType'], - "custname"=>$orderInformation['CustomerName'], - "custaddress"=>$orderInformation['CustomerAddress'], - "customerThana"=>$orderInformation['CustomerThana'], - "customerDistrict"=>$orderInformation['CustomerDistrict'], - "custPhone"=>$orderInformation['CustomerPhone'] + $environment = env('APP_ENV'); + $baseurl = $this->baseUrl($environment); + $data = [ + "merOrderRef" => $orderInformation['OrderNo'], + "pickMerchantName" => $orderInformation['MerchantName'], + "pickMerchantAddress" => $orderInformation['MerchantAddress'], + "pickMerchantThana" => $orderInformation['MerchantThana'], + "pickMerchantDistrict" => $orderInformation['MerchantDistrict'], + "pickupMerchantPhone" => $orderInformation['MerchantPhone'], + "productSizeWeight" => $orderInformation['SizeWeight'], + "productBrief" => $orderInformation['productDetails'], + "packagePrice" => $orderInformation['packagePrice'], + "deliveryOption" => $orderInformation['deliveryType'], + "custname" => $orderInformation['CustomerName'], + "custaddress" => $orderInformation['CustomerAddress'], + "customerThana" => $orderInformation['CustomerThana'], + "customerDistrict" => $orderInformation['CustomerDistrict'], + "custPhone" => $orderInformation['CustomerPhone'] ]; - $headers=[ - 'Content-Type'=>'application/json', - 'Accept' => 'application/json', - 'paperflykey' => config('paperfly.paperflykey') - ]; - $content=array( - 'headers'=>$headers, - 'json'=>$data + + $content = array( + 'headers' => $this->getHeader(), + 'json' => $data ); - // dd($content); + $client = new Client([ 'auth' => [config('paperfly.username'), config('paperfly.password')], - 'verify'=> false, + 'verify' => false, ]); try { - $response =$client->request('POST',$baseurl.'/OrderPlacement',$content); + $response = $client->request('POST', $baseurl . '/OrderPlacement', $content); if ($response->getStatusCode() == 200) { - $x = $response->getBody(); - $response = json_decode($x->getContents()); - $message=$response->success->message; - $tracking_id=$response->success->tracking_number; - return $tracking_id; - }else{ + $responseBody = $response->getBody(); + $response = json_decode($responseBody->getContents()); + $response->success->message; + return $response->success->tracking_number; + } else { return 'Request Failed'; } } catch (ClientException $e) { - throw new \Exception($e->getMessage()); + throw new Exception($e->getMessage()); } } - public function trackOrder($orderId){ - $environment=env('APP_ENV'); - $baseurl=$this->baseUrl($environment); - $headers=[ - 'Content-Type'=>'application/json', - 'Accept' => 'application/json', - 'paperflykey' => config('paperfly.paperflykey') + /** + * @param $orderId + * @return string + * @throws GuzzleException + */ + public function trackOrder($orderId) + { + $environment = $this->getEnv(); + $baseurl = $this->baseUrl($environment); + $headers = $this->getHeader(); + $data = [ + "ReferenceNumber" => $orderId ]; - $data=[ - "ReferenceNumber"=>$orderId - ]; - $content=array( - 'headers'=>$headers, - 'json'=>$data + $content = array( + 'headers' => $headers, + 'json' => $data ); $client = new Client([ 'auth' => [config('paperfly.username'), config('paperfly.password')], - 'verify'=> false, + 'verify' => false, ]); try { - $response =$client->request('POST',$baseurl.'/API-Order-Tracking',$content); + $response = $client->request('POST', $baseurl . '/API-Order-Tracking', $content); if ($response->getStatusCode() == 200) { $x = $response->getBody(); $response = json_decode($x->getContents()); - $message=$response->success; + $message = $response->success; return $message; - }else{ + } else { return 'Request Failed'; } } catch (ClientException $e) { - throw new \Exception($e->getMessage()); + throw new Exception($e->getMessage()); } } - public function courierInvoice($orderId){ - $environment=env('APP_ENV'); - $baseurl=$this->baseUrl($environment); - $headers=[ - 'Content-Type'=>'application/json', - 'Accept' => 'application/json', - 'paperflykey' => config('paperfly.paperflykey') - ]; - $data=[ - "ReferenceNumber"=>$orderId + /** + * @param $orderId + * @return string + * @throws GuzzleException + * @throws Exception + */ + public function courierInvoice($orderId) + { + $environment = $this->getEnv(); + $baseurl = $this->baseUrl($environment); + + $data = [ + "ReferenceNumber" => $orderId ]; - $content=array( - 'headers'=>$headers, - 'json'=>$data + $content = array( + 'headers' => $this->getHeader(), + 'json' => $data ); $client = new Client([ 'auth' => [config('paperfly.username'), config('paperfly.password')], - 'verify'=> false, + 'verify' => false, ]); try { - $response =$client->request('POST',$baseurl.'/api/v1/invoice-details/',$content); - dd($response->getStatusCode()); + $response = $client->request('POST', $baseurl . '/api/v1/invoice-details/', $content); + if ($response->getStatusCode() == 200) { $x = $response->getBody(); $response = json_decode($x->getContents()); - $message=$response->success; - return $message; - }else{ + return $response->success; + } else { return 'Request Failed'; } } catch (ClientException $e) { - throw new \Exception($e->getMessage()); + throw new Exception($e->getMessage()); } } - public function CourierOrderCancel($orderId){ - $environment=env('APP_ENV'); - $baseurl=$this->baseUrl($environment); - $headers=[ - 'Content-Type'=>'application/json', - 'Accept' => 'application/json', - 'paperflykey' => config('paperfly.paperflykey') - ]; - $data=[ - "order_id"=>$orderId + /** + * @param $orderId + * @return string + * @throws GuzzleException + * @throws Exception + */ + public function CourierOrderCancel($orderId) + { + $environment = $this->getEnv(); + $baseurl = $this->baseUrl($environment); + + $data = [ + "order_id" => $orderId ]; - $content=array( - 'headers'=>$headers, - 'json'=>$data + $content = array( + 'headers' => $this->getHeader(), + 'json' => $data ); $client = new Client([ 'auth' => [config('paperfly.username'), config('paperfly.password')], - 'verify'=> false, + 'verify' => false, ]); try { - $response =$client->request('POST',$baseurl.'/api/v1/cancel-order/',$content); + $response = $client->request('POST', $baseurl . '/api/v1/cancel-order/', $content); if ($response->getStatusCode() == 200) { - $x = $response->getBody(); - $response = json_decode($x->getContents()); - $message=$response->success; - return $message; - }else{ + $responseBody = $response->getBody(); + $response = json_decode($responseBody->getContents()); + return $response->success; + } else { return 'Request Failed'; } } catch (ClientException $e) { - // dd($e); - throw new \Exception($e->getMessage()); + throw new Exception($e->getMessage()); } } - public function baseUrl($environment){ - if ($environment=='local') { + /** + * @param $environment + * @return string + */ + public function baseUrl($environment) + { + if ($environment == 'local') { return 'https://sandbox.paperfly-bd.com'; } else { return 'https://sandbox.paperfly-bd.com'; } } -} \ No newline at end of file + + /** + * @return mixed + */ + private function getEnv() + { + return env('APP_ENV'); + } + + /** + * @return array + */ + private function getHeader(): array + { + return [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'paperflykey' => config('paperfly.paperflykey') + ]; + } +}