| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Orders; |
| 4 |
|
| 5 |
use SyncBasalam\Config\Endpoints; |
| 6 |
use SyncBasalam\Services\ApiServiceManager; |
| 7 |
use SyncBasalam\Utilities\OrderManagerUtilities; |
| 8 |
|
| 9 |
class DelayReqOrderService |
| 10 |
{ |
| 11 |
public function delayReqOnBasalam(int $orderId, string $description, int $postponeDays) |
| 12 |
{ |
| 13 |
if (empty($orderId)) { |
| 14 |
return [ |
| 15 |
'success' => false, |
| 16 |
'message' => 'شناسه سفارش نا� |
| 17 |
عتبر است.', |
| 18 |
'status_code' => 400, |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
if (empty($description)) { |
| 23 |
return [ |
| 24 |
'success' => false, |
| 25 |
'message' => 'لطفاً توضیحات را وارد کنید.', |
| 26 |
'status_code' => 400, |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
if (empty($postponeDays)) { |
| 31 |
return [ |
| 32 |
'success' => false, |
| 33 |
'message' => 'لطفاً تعداد روزهای تاخیر را وارد کنید.', |
| 34 |
'status_code' => 400, |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
$syncBasalamOrderId = OrderManagerUtilities::getInvoiceId($wpdb, $orderId); |
| 41 |
if (!$syncBasalamOrderId) { |
| 42 |
return [ |
| 43 |
'success' => false, |
| 44 |
'message' => 'شناسه فاکتور سفارش یافت نشد.', |
| 45 |
'status_code' => 400, |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
$response = $this->sendDelayRequestToBasalam($syncBasalamOrderId, $description, $postponeDays); |
| 50 |
|
| 51 |
$statusCode = $response['status_code']; |
| 52 |
if ($statusCode !== 200 && $statusCode !== 201) { |
| 53 |
|
| 54 |
$body = $response['body']; |
| 55 |
$errorMessage = ''; |
| 56 |
|
| 57 |
if (is_array($body)) { |
| 58 |
if (!empty($body['errors']) && is_array($body['errors'])) { |
| 59 |
$errorMessage = $body['errors'][0]['message'] ?? ''; |
| 60 |
} |
| 61 |
} else { |
| 62 |
$errorMessage = $body; |
| 63 |
} |
| 64 |
|
| 65 |
return [ |
| 66 |
'success' => false, |
| 67 |
'message' => $errorMessage, |
| 68 |
'status_code' => $statusCode, |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
return [ |
| 73 |
'success' => true, |
| 74 |
'message' => 'درخواست تاخیر برای سفارش با � |
| 75 |
وفقیت ارسال شد.', |
| 76 |
'status_code' => 200, |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
private function sendDelayRequestToBasalam($orderId, $description, $postponeDays) |
| 81 |
{ |
| 82 |
$apiUrl = sprintf(Endpoints::ORDER_DELAY, $orderId); |
| 83 |
|
| 84 |
$body = [ |
| 85 |
"topic" => 5075, |
| 86 |
"metadata" => [ |
| 87 |
"postpone_days" => $postponeDays, |
| 88 |
"description" => $description, |
| 89 |
], |
| 90 |
]; |
| 91 |
|
| 92 |
$apiService = syncBasalamContainer()->get(ApiServiceManager::class); |
| 93 |
|
| 94 |
try { |
| 95 |
return $apiService->put($apiUrl, $body); |
| 96 |
} catch (\Exception $e) { |
| 97 |
return [ |
| 98 |
'status_code' => 500, |
| 99 |
'body' => $e->getMessage(), |
| 100 |
]; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|