ShippoShipmentRepository.php 4.0 KB

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