/home/brandsfa/www/app/Http/Controllers/Admin/CourierApiController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Contracts\Repositories\BusinessSettingRepositoryInterface;
use App\Contracts\Repositories\DeliveryCountryCodeRepositoryInterface;
use App\Contracts\Repositories\DeliveryZipCodeRepositoryInterface;
use App\Enums\ViewPaths\Admin\CourierApi;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Admin\DeliveryCoutryCodeAddRequest;
use App\Http\Requests\Admin\DeliveryZipCodeAddRequest;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Traits\SettingsTrait;
use App\Services\SteadfastService;
use App\Models\Order;
class CourierApiController extends BaseController
{
use SettingsTrait;
protected $steadfastService;
public function __construct(
private readonly BusinessSettingRepositoryInterface $businessSettingRepo,
private readonly DeliveryCountryCodeRepositoryInterface $deliveryCountryCodeRepo,
private readonly DeliveryZipCodeRepositoryInterface $deliveryZipCodeRepo,
SteadfastService $steadfastService,
)
{
$this->steadfastService = $steadfastService;
}
/**
* @param Request|null $request
* @param string|null $type
* @return View Index function is the starting point of a controller
* Index function is the starting point of a controller
*/
public function index(Request|null $request, string $type = null): View
{
return $this->getView($request);
}
public function getView(): View
{
$storedCountries = $this->deliveryCountryCodeRepo->getListWhere(orderBy: ['id' => 'desc'], dataLimit: getWebConfig(name: 'pagination_limit'));
$countryRestrictionStatus = $this->businessSettingRepo->getFirstWhere(params: ['type' => 'steadfast_api_status']);
$zipCodeAreaRestrictionStatus = $this->businessSettingRepo->getFirstWhere(params: ['type' => 'pathao_api_status']);
$countries = COUNTRIES;
$storedCountryCode = $storedCountries->pluck('country_code')->toArray();
$storedZip = $this->deliveryZipCodeRepo->getListWhere(orderBy: ['id' => 'desc'], dataLimit: getWebConfig(name: 'pagination_limit'));
return view(CourierApi::VIEW[VIEW], compact('countries', 'storedCountries', 'storedCountryCode', 'storedZip', 'countryRestrictionStatus', 'zipCodeAreaRestrictionStatus'));
}
public function steadfast_setup(Request $request): RedirectResponse
{
// dd($request->all());
if (env('APP_MODE') == 'demo') {
Toastr::info(translate('you_can_not_update_this_on_demo_mode'));
return back();
}
try {
$this->setEnvironmentValue(envKey: 'ST_API_KEY', envValue: $request['st_api_key'] ?? env('ST_API_KEY'));
$this->setEnvironmentValue(envKey: 'ST_SECRET_KEY', envValue: $request['st_secret_key'] ?? env('ST_SECRET_KEY'));
$this->setEnvironmentValue(envKey: 'ST_BASE_URL', envValue: $request['st_base_url'] ?? env('ST_BASE_URL'));
Toastr::success(translate('steadfast_api_update_successfully'));
} catch (Exception $exception) {
Toastr::error(translate('steadfast_api_update_failed'));
}
return back();
}
public function pathao_setup(Request $request): RedirectResponse
{
// dd($request->all());
if (env('APP_MODE') == 'demo') {
Toastr::info(translate('you_can_not_update_this_on_demo_mode'));
return back();
}
try {
$this->setEnvironmentValue(envKey: 'PATHAO_CLIENT_ID', envValue: $request['pathao_client_id'] ?? env('PATHAO_CLIENT_ID'));
$this->setEnvironmentValue(envKey: 'PATHAO_STORE_ID', envValue: $request['pathao_store_id'] ?? env('PATHAO_STORE_ID'));
$this->setEnvironmentValue(envKey: 'PATHAO_CLIENT_SECRET', envValue: $request['pathao_client_secret'] ?? env('PATHAO_CLIENT_SECRET'));
$this->setEnvironmentValue(envKey: 'PATHAO_CLIENT_EMAIL', envValue: $request['pathao_client_email'] ?? env('PATHAO_CLIENT_EMAIL'));
$this->setEnvironmentValue(envKey: 'PATHAO_CLIENT_PASSWORD', envValue: $request['pathao_client_password'] ?? env('PATHAO_CLIENT_PASSWORD'));
$this->setEnvironmentValue(envKey: 'PATHAO_BASE_URL', envValue: $request['pathao_base_url'] ?? env('PATHAO_BASE_URL'));
Toastr::success(translate('pathao_api_update_successfully'));
} catch (Exception $exception) {
Toastr::error(translate('pathao_api_update_failed'));
}
return back();
}
public function steadfast_status(Request $request): RedirectResponse
{
dd($request->all());
foreach ($request->input('country_code') as $code) {
$this->deliveryCountryCodeRepo->add(data: ['country_code' => $code, 'created_at' => now()]);
}
Toastr::success(translate('delivery_country_added_successfully'));
return back();
}
public function pathao_status(Request $request): RedirectResponse
{
dd($request->all());
foreach ($request->input('country_code') as $code) {
$this->deliveryCountryCodeRepo->add(data: ['country_code' => $code, 'created_at' => now()]);
}
Toastr::success(translate('delivery_country_added_successfully'));
return back();
}
public function steadfast_send(Request $request){
$order_data = Order::where('id',$request->order_id)->first();
$data = [
'invoice' => $order_data->id,
'recipient_name' => $order_data->shipping_address_data ? $order_data->shipping_address_data->contact_person_name : 'N/A',
// 'recipient_address' =>
// ($order_data->shipping_address_data ? $order_data->shipping_address_data->address : 'N/A') .
// ', upzila: ' . ($order_data->shipping_address_data ? $order_data->shipping_address_data->upzila : 'N/A') .
// ', city: ' . ($order_data->shipping_address_data ? $order_data->shipping_address_data->city : 'N/A'),
'recipient_address' =>
($order_data->shipping_address_data ? $order_data->shipping_address_data->address : 'N/A'),
'recipient_phone' => $order_data->shipping_address_data ? $order_data->shipping_address_data->phone : '',
'cod_amount' => $order_data->order_amount,
'note' => $order_data->order_note ? $order_data->order_note : '',
];
// dd($data);
$response = $this->steadfastService->createShipment($data);
$order_data->delivery_type = 'third_party_delivery';
$order_data->delivery_service_name = "steadfast";
$order_data->third_party_delivery_tracking_id = $response['consignment']['tracking_code'];
$order_data->save();
if ($response['status'] == 200) {
return response()->json([
'message' => translate('successfully_send_at_steadfast'),
'status' => true,
]);
} else {
return back();
}
}
public function pathao_send(){
}
}