PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.1
ووسلام – همگام سازی ووکامرس و باسلام v1.10.1
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Services / Orders / DelayReqOrderService.php

DelayReqOrderService.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.1, at includes/Services/Orders/DelayReqOrderService.php

104 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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