|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Shipping\Shippo; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Addresses\Address; |
|
6
|
|
|
use App\Shop\Customers\Customer; |
|
7
|
|
|
use App\Shop\Products\Product; |
|
8
|
|
|
use App\Shop\Shipping\ShippingInterface; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Shippo; |
|
11
|
|
|
use Shippo_Shipment; |
|
12
|
|
|
|
|
13
|
|
|
class ShippoShipmentRepository implements ShippingInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Customer |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $customer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The address where to pick up the item for delivery |
|
22
|
|
|
* |
|
23
|
|
|
* @var $warehouseAddress |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
protected $warehouseAddress; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The address of the customer where the item is to be delivered |
|
29
|
|
|
* |
|
30
|
|
|
* @var $deliveryAddress |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
protected $deliveryAddress; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The item/s |
|
36
|
|
|
* |
|
37
|
|
|
* @var $parcel |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
|
|
protected $parcel; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Shipment |
|
43
|
|
|
* |
|
44
|
|
|
* @var $shipment |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
protected $shipment; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* ShippoShipment constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Customer $customer |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Customer $customer) |
|
54
|
|
|
{ |
|
55
|
|
|
Shippo::setApiKey(env('SHIPPO_API_TOKEN')); |
|
56
|
|
|
|
|
57
|
|
|
$this->customer = $customer; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Address where the shipment will be picked up |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setPickupAddress() |
|
64
|
|
|
{ |
|
65
|
|
|
$warehouse = [ |
|
66
|
|
|
'name' => config('app.name'), |
|
67
|
|
|
'street1' => config('shop.warehouse.address_1'), |
|
68
|
|
|
'city' => config('shop.warehouse.city'), |
|
69
|
|
|
'state' => config('shop.warehouse.state'), |
|
70
|
|
|
'zip' => config('shop.warehouse.zip'), |
|
71
|
|
|
'country' => config('shop.warehouse.country'), |
|
72
|
|
|
'phone' => config('shop.phone'), |
|
73
|
|
|
'email' => config('shop.email') |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
$this->warehouseAddress = $warehouse; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Address $address |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setDeliveryAddress(Address $address) |
|
83
|
|
|
{ |
|
84
|
|
|
$delivery = [ |
|
85
|
|
|
'name' => $address->alias, |
|
86
|
|
|
'street1' => $address->address_1, |
|
87
|
|
|
'city' => $address->city, |
|
88
|
|
|
'state' => $address->state_code, |
|
89
|
|
|
'zip' => $address->zip, |
|
90
|
|
|
'country' => $address->country->iso, |
|
91
|
|
|
'phone' => '', |
|
92
|
|
|
'email' => $this->customer->email |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
$this->deliveryAddress = $delivery; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return Shippo_Shipment |
|
100
|
|
|
*/ |
|
101
|
|
|
public function readyShipment() |
|
102
|
|
|
{ |
|
103
|
|
|
$shipment = Shippo_Shipment::create(array( |
|
104
|
|
|
'address_from'=> $this->warehouseAddress, |
|
105
|
|
|
'address_to'=> $this->deliveryAddress, |
|
106
|
|
|
'parcels'=> $this->parcel, |
|
107
|
|
|
'async'=> false |
|
108
|
|
|
) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
return $shipment; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $id |
|
116
|
|
|
* @param string $currency |
|
117
|
|
|
* |
|
118
|
|
|
* @return \Shippo_Get_Shipping_Rates |
|
|
|
|
|
|
119
|
|
|
*/ |
|
120
|
|
|
public function getRates(string $id, string $currency = 'USD') |
|
121
|
|
|
{ |
|
122
|
|
|
return Shippo_Shipment::get_shipping_rates(compact('id', 'currency')); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param Collection $collection |
|
127
|
|
|
* |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function readyParcel(Collection $collection) |
|
131
|
|
|
{ |
|
132
|
|
|
$weight = $collection->map(function ($item) { |
|
133
|
|
|
return [ |
|
134
|
|
|
'weight' => $item->product->weight * $item->qty, |
|
135
|
|
|
'mass_unit' => $item->product->mass_unit |
|
136
|
|
|
]; |
|
137
|
|
|
})->map(function ($item) { |
|
138
|
|
|
$total = 0; |
|
139
|
|
|
switch ($item['mass_unit']) { |
|
140
|
|
|
case Product::MASS_UNIT['OUNCES'] : |
|
141
|
|
|
$oz = $item['weight'] / 16; |
|
142
|
|
|
$total += $oz; |
|
143
|
|
|
break; |
|
144
|
|
|
case Product::MASS_UNIT['GRAMS'] : |
|
145
|
|
|
$oz = $item['weight'] * 0.0022; |
|
146
|
|
|
$total += $oz; |
|
147
|
|
|
break; |
|
148
|
|
|
default: |
|
149
|
|
|
$total += $item['weight']; |
|
150
|
|
|
} |
|
151
|
|
|
return [ |
|
152
|
|
|
'weight' => $total |
|
153
|
|
|
]; |
|
154
|
|
|
})->sum('weight'); |
|
155
|
|
|
|
|
156
|
|
|
$parcel = array( |
|
157
|
|
|
'length'=> '5', |
|
158
|
|
|
'width'=> '5', |
|
159
|
|
|
'height'=> '5', |
|
160
|
|
|
'distance_unit'=> 'in', |
|
161
|
|
|
'weight'=> $weight, |
|
162
|
|
|
'mass_unit'=> 'lb', |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
|
|
$this->parcel = $parcel; |
|
166
|
|
|
} |
|
167
|
|
|
} |