| 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 CancelOrderService |
| 10 |
{ |
| 11 |
public function cancelOrderOnBasalam(int $orderId, string $description, int $reasonId = 3481) |
| 12 |
{ |
| 13 |
if (empty($orderId)) { |
| 14 |
return [ |
| 15 |
'success' => false, |
| 16 |
'message' => 'شناسه سفارش نا� |
| 17 |
عتبر است.', |
| 18 |
'status_code' => 400, |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
$order = wc_get_order($orderId); |
| 23 |
if (!$order) { |
| 24 |
return [ |
| 25 |
'success' => false, |
| 26 |
'message' => 'سفارش یافت نشد.', |
| 27 |
'status_code' => 400, |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
if (empty($description)) { |
| 32 |
return [ |
| 33 |
'success' => false, |
| 34 |
'message' => 'لطفاً توضیحات را وارد کنید.', |
| 35 |
'status_code' => 400, |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
if (empty($reasonId)) { |
| 40 |
return [ |
| 41 |
'success' => false, |
| 42 |
'message' => 'لطفاً علت لغو را ثبت کنید.', |
| 43 |
'status_code' => 400, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
global $wpdb; |
| 48 |
|
| 49 |
$itemIds = OrderManagerUtilities::getAllItemIdsFromMeta($wpdb, $orderId); |
| 50 |
|
| 51 |
if (empty($itemIds)) { |
| 52 |
return [ |
| 53 |
'success' => false, |
| 54 |
'message' => 'هیچ شناسه آیت� |
| 55 |
ی یافت نشد.', |
| 56 |
'status_code' => 400, |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
$response = $this->sendCancelOrderRequest($itemIds, $description, $reasonId); |
| 61 |
$statusCode = $response['status_code']; |
| 62 |
|
| 63 |
if ($statusCode !== 200 && $statusCode !== 201) { |
| 64 |
|
| 65 |
$body = $response['body']; |
| 66 |
$errorMessage = ''; |
| 67 |
|
| 68 |
if (is_array($body)) { |
| 69 |
if (!empty($body['errors']) && is_array($body['errors'])) { |
| 70 |
$errorMessage = $body['errors'][0]['message'] ?? ''; |
| 71 |
} |
| 72 |
} else { |
| 73 |
$errorMessage = $body; |
| 74 |
} |
| 75 |
|
| 76 |
return [ |
| 77 |
'success' => false, |
| 78 |
'message' => $errorMessage, |
| 79 |
'status_code' => $statusCode, |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
$order->update_status('bslm-rejected', 'سفارش توسط اد� |
| 84 |
ین لغو شد.'); |
| 85 |
|
| 86 |
return [ |
| 87 |
'success' => true, |
| 88 |
'message' => 'سفارش با � |
| 89 |
وفقیت لغو شد.', |
| 90 |
'status_code' => 200, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
private function sendCancelOrderRequest($itemIds, $description, $reasonId) |
| 95 |
{ |
| 96 |
$apiUrl = Endpoints::ORDER_CANCEL; |
| 97 |
|
| 98 |
$orderItems = []; |
| 99 |
foreach ($itemIds as $id) { |
| 100 |
$orderItems[] = [ |
| 101 |
'item_id' => $id, |
| 102 |
'reason_id' => $reasonId, |
| 103 |
'description' => $description, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
$body = [ |
| 108 |
'order_items' => $orderItems, |
| 109 |
]; |
| 110 |
|
| 111 |
$apiService = syncBasalamContainer()->get(ApiServiceManager::class); |
| 112 |
|
| 113 |
try { |
| 114 |
return $apiService->post($apiUrl, $body); |
| 115 |
} catch (\Exception $e) { |
| 116 |
return [ |
| 117 |
'status_code' => 500, |
| 118 |
'body' => 'خطا در ارسال درخواست لغو سفارش: ' . $e->getMessage(), |
| 119 |
]; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|