1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HomeDesignShops\LaravelBolComRetailer; |
4
|
|
|
|
5
|
|
|
use HomeDesignShops\LaravelBolComRetailer\Models\Transport; |
6
|
|
|
use HomeDesignShops\LaravelBolComRetailer\Models\TransportData; |
7
|
|
|
use HomeDesignShops\LaravelBolComRetailer\Models\TransportItem; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Picqer\BolRetailer\Client as BolRetailerClient; |
10
|
|
|
use Picqer\BolRetailer\Exception\HttpException; |
11
|
|
|
use Picqer\BolRetailer\Model\OrderItem; |
12
|
|
|
use Picqer\BolRetailer\Model\ReducedOrder; |
13
|
|
|
use Picqer\BolRetailer\Order; |
14
|
|
|
use Picqer\BolRetailer\ProcessStatus; |
15
|
|
|
use Picqer\BolRetailer\Shipment; |
16
|
|
|
|
17
|
|
|
class BolComRetailerService |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Max retry counts for the API requests. |
22
|
|
|
* |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $maxRetries; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Holds the retry counts. |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $retriesCount = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* BolComRetailer constructor. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
BolRetailerClient::setDemoMode(config('bol-com-retailer.use_demo_mode')); |
39
|
|
|
BolRetailerClient::setCredentials( |
40
|
|
|
config('bol-com-retailer.client_id'), |
41
|
|
|
config('bol-com-retailer.client_secret') |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->maxRetries = config('bol-com-retailer.max_retries'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a collection of the open orders. |
49
|
|
|
* |
50
|
|
|
* @return Collection |
51
|
|
|
*/ |
52
|
|
|
public function getOpenOrders(): Collection |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
return collect(Order::all()) |
56
|
|
|
->transform(static function (ReducedOrder $reducedOrder) { |
57
|
|
|
return Order::get($reducedOrder->orderId); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
} catch (HttpException $e) { |
61
|
|
|
$this->retriesCount++; |
62
|
|
|
$retryInSeconds = str_replace(['Too many requests, retry in ', ' seconds.'], '', $e->getDetail()); |
63
|
|
|
|
64
|
|
|
sleep( (int) $retryInSeconds ); |
65
|
|
|
|
66
|
|
|
if($this->retriesCount <= $this->maxRetries) { |
67
|
|
|
return $this->getOpenOrders(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw $e; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a Bol.com order. |
76
|
|
|
* Null if order not found. |
77
|
|
|
* |
78
|
|
|
* @param string $orderId |
79
|
|
|
* @return Order|null |
80
|
|
|
*/ |
81
|
|
|
public function getOrder(string $orderId) |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
return Order::get($orderId); |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Ships a order item |
92
|
|
|
* |
93
|
|
|
* @param OrderItem $orderItem |
94
|
|
|
* @param Transport $transport |
95
|
|
|
* @return ProcessStatus |
96
|
|
|
*/ |
97
|
|
|
public function shipOrderItem(OrderItem $orderItem, Transport $transport): ProcessStatus |
98
|
|
|
{ |
99
|
|
|
try { |
100
|
|
|
return Shipment::create($orderItem, [ |
101
|
|
|
'transport' => [ |
102
|
|
|
'transporterCode' => $transport->transporterCode, |
103
|
|
|
'trackAndTrace' => $transport->trackingCode |
104
|
|
|
] |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
} catch (HttpException $e) { |
108
|
|
|
$this->retriesCount++; |
109
|
|
|
$retryInSeconds = str_replace(['Too many requests, retry in ', ' seconds.'], '', $e->getDetail()); |
110
|
|
|
|
111
|
|
|
sleep( (int) $retryInSeconds ); |
112
|
|
|
|
113
|
|
|
if($this->retriesCount <= $this->maxRetries) { |
114
|
|
|
return $this->shipOrderItem($orderItem, $transport); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
throw $e; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|