Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Bagisto UPS Shipping module in v2.0 #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It packs in lots of demanding features that allows your business to scale in no

### 2. Requirements:

* **Bagisto**: v0.1.6 or higher.
* **Bagisto**: v2.0.0

### 3. Installation:

Expand Down
63 changes: 26 additions & 37 deletions src/Carriers/Ups.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,53 @@ class Ups extends AbstractShipping
* Payment method code
*
* @var string
*/
*/
protected $code = 'ups';

/**
* Returns rate for flatrate
* Returns rate for ups
*
* @return array
*/
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a space

public function calculate()
{
$uspsMethod = '';

$shippingMethods = [];

$rates = [];
if (! $this->isAvailable()) {
return false;
}

$shippingHelper = app(ShippingMethodHelper::class);
$shippingMethods = $rates = [];

$cart = Cart::getCart();

$address = $cart->shipping_address;
$cartProducts = $shippingHelper->getAllCartProducts($address);

$cartProducts = app(ShippingMethodHelper::class)->getAllCartProducts($address);

$marketplaceShipping = session()->get('marketplace_shipping_rates');

if (! $this->isAvailable()){

return false;
}
if (isset ($cartProducts) && $cartProducts == true) {
if (isset($cartProducts)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currect this condition.

&& $cartProducts == true) {

foreach ($cartProducts as $key => $fedexServices) {
$rate = 0;
$totalShippingCost = 0;
$upsMethod = $key;
$methodCode = $key;
$rate = $totalShippingCost = 0;
$upsMethod = $methodCode = $key;

foreach ($fedexServices as $methods => $upsRate) {

$rate += $upsRate['rate'] * $upsRate['itemQuantity'];

$sellerId = $upsRate['marketplace_seller_id'];

$itemShippingCost = $upsRate['rate'] * $upsRate['itemQuantity'];
$itemShippingCost = $upsRate['rate'] * $upsRate['itemQuantity'];

$rates[$key][$sellerId] = [
'amount' => core()->convertPrice($itemShippingCost),
'base_amount' => $itemShippingCost
'amount' => core()->convertPrice($itemShippingCost),
'base_amount' => $itemShippingCost,
];

if (isset ($rates[$key][$sellerId])) {
if (isset($rates[$key][$sellerId])) {
$rates[$key][$sellerId] = [
'amount' => core()->convertPrice($rates[$key][$sellerId]['amount'] + $itemShippingCost),
'base_amount' => $rates[$key][$sellerId]['base_amount'] + $itemShippingCost
'amount' => core()->convertPrice($rates[$key][$sellerId]['amount'] + $itemShippingCost),
'base_amount' => $rates[$key][$sellerId]['base_amount'] + $itemShippingCost,
];
}

Expand All @@ -76,34 +69,30 @@ public function calculate()
$object = new CartShippingRate;

$object->carrier = 'mpups';
$object->carrier_title = $this->getConfigData('title');
$object->method = 'mpups_' . '' . $methodCode;
$object->method_title = $this->getConfigData('title');
$object->carrier_title = $this->getConfigData('title');
$object->method = 'mpups_' . '' . $methodCode;
$object->method_title = $this->getConfigData('title');
$object->method_description = $upsMethod;

$object->price = core()->convertPrice($totalShippingCost);
$object->base_price = $totalShippingCost;

$marketplaceShippingRates = session()->get('marketplace_shipping_rates');

if (! is_array($marketplaceShipping)) {

$marketplaceShippingRates['mpupsshipping'] = ['mpupsshipping' => $rates];
session()->put('marketplace_shipping_rates', $marketplaceShippingRates);

} else {
$marketplaceFedexShipping = ['mpupshipping' => $rates];
}

array_push($shippingMethods, $object);
}

if (isset ($marketplaceFedexShipping)) {

if (isset($marketplaceFedexShipping)) {
session()->put('marketplace_shipping_rates.mpupshipping', $marketplaceFedexShipping);
}

return $shippingMethods;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
], [
'title' => 'Live',
'value' => "LIVE",
]
],
],
], [
'name' => 'access_license_key',
Expand Down Expand Up @@ -104,7 +104,7 @@
], [
'title' => 'KGS',
'value' => 'KGS',
]
],
],
], [
'name' => 'services',
Expand Down
82 changes: 25 additions & 57 deletions src/Helpers/ShippingMethodHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@

class ShippingMethodHelper
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;

/**
* RateServiceWsdl
*
Expand All @@ -36,9 +29,7 @@ class ShippingMethodHelper
* Create a new controller instance.
*
* @param \Webkul\Checkout\Repositories\CartAddressRepository $cartAddress;
*
* @param \Webkul\Core\Repositories\ChannelRepository $channel
*
* @param \Webkul\Shipping\Repositories\UpsRepository $upsRepository;
*/
public function __construct(
Expand All @@ -50,28 +41,25 @@ public function __construct(
}

/**
* display methods
* display cart products
*
* @return array
*
* @param $address
*
* @return array
*/
public function getAllCartProducts($address)
{
$data = $this->_createSoapClient($address);

return $data;
return $this->createSoapClient($address);
}

/**
* Soap client for wsdl
*
* @param string $wsdl
* @param bool|int $trace
* @param $address
*
* @return \SoapClient
*/
protected function _createSoapClient($address)
protected function createSoapClient($address)
{
$cart = Cart::getCart();

Expand All @@ -83,17 +71,12 @@ protected function _createSoapClient($address)

$adminCompany = $adminData->hostname;

if (! core()->getConfigData('sales.carriers.ups.ups_active')){
if (! core()->getConfigData('sales.carriers.ups.ups_active')) {

return false;

} else {
$status = true;
}

if ($status) {
$sellerAdminData = $this->upsRepository->getSellerAdminData($cart->items()->get(), 'ups_postcode');
}
$sellerAdminData = $this->upsRepository->getSellerAdminData($cart->items()->get(), 'ups_postcode');

$sellerAdminServices = $allServices = [];

Expand Down Expand Up @@ -167,8 +150,8 @@ protected function _createSoapClient($address)
$shipToAddress->addChild ( "City", $address->city );

if ($address->country == 'PR') {

$shipToAddress->addChild ( "PostalCode", '00'. $address->postcode );

} else {
$shipToAddress->addChild ( "PostalCode", $address->postcode );
}
Expand Down Expand Up @@ -203,7 +186,7 @@ protected function _createSoapClient($address)
));

curl_setopt($ch, CURLOPT_POSTFIELDS,$requestXML);

$response = curl_exec($ch);

curl_close($ch);
Expand All @@ -212,22 +195,19 @@ protected function _createSoapClient($address)

$upsServices = json_decode(json_encode($upsServiceArray));

if ( isset($cartProduct->marketplace_seller_id)) {
if (isset($cartProduct->marketplace_seller_id)) {
$sellerId = $cartProduct->marketplace_seller_id;

} else {
$sellerId = 0;
}

if ($response) {

if ( isset($upsServices->Response->ResponseStatusDescription)
if (isset($upsServices->Response->ResponseStatusDescription)
&& $upsServices->Response->ResponseStatusDescription == 'Success') {

if ( isset($upsServices->RatedShipment)) {
if (isset($upsServices->RatedShipment)) {

foreach ($upsServices->RatedShipment as $services) {

$serviceCode = $services->Service->Code;

$matchResult = $this->upsRepository->validateAllowedMethods($serviceCode, $sellerAdminServices);
Expand All @@ -236,47 +216,41 @@ protected function _createSoapClient($address)

if ($matchResult) {
$cartProductServices[$serviceName] = [
'classId' => $serviceCode,
'rate' => $services->RatedPackage->TotalCharges->MonetaryValue,
'currency' => $services->RatedPackage->TotalCharges->CurrencyCode,
'weight' => $services->BillingWeight->Weight,
'weightUnit' => $services->BillingWeight->UnitOfMeasurement->Code,
'classId' => $serviceCode,
'rate' => $services->RatedPackage->TotalCharges->MonetaryValue,
'currency' => $services->RatedPackage->TotalCharges->CurrencyCode,
'weight' => $services->BillingWeight->Weight,
'weightUnit' => $services->BillingWeight->UnitOfMeasurement->Code,
'marketplace_seller_id' => $sellerId,
'itemQuantity' => $cartProduct->quantity
'itemQuantity' => $cartProduct->quantity,
];
}

}

if ( !empty($cartProductServices)) {
if (! empty($cartProductServices)) {
$allServices[] = $cartProductServices;
}
}

} else {
$errorLog = $this->getErrorLog($upsServices);

return false;
//to check the error generated in the method;
return false; //to check the error generated in the method;
}
}
} catch (\Exception $e) {

return false;
}
}

if ( !empty($allServices) )
{
if (! empty($allServices) ) {
return $this->upsRepository->getCommonMethods($allServices);

} else {

return false;
}
}


/**
* Map service code
*
Expand Down Expand Up @@ -305,13 +279,12 @@ protected function getServiceName($serviceCode)
'03' => 'Ups Ground',
];

foreach ($mapServices as $key => $service){
foreach ($mapServices as $key => $service) {

if ($key == $serviceCode) {
return $service;
}
}

return $serviceCode;
}

Expand All @@ -331,17 +304,14 @@ public function getWeight($weight)
if ($coreWeightUnit == 'LBS') {

if ($upsWeightUnit == 'LBS') {

$convertedWeight = $weight;

} else {
//kgs to lbs
$convertedWeight = $weight/0.45359237;
}
} else {
$convertedWeight = $weight/0.45359237;
}

return $convertedWeight;
}

Expand All @@ -352,15 +322,13 @@ public function getWeight($weight)
**/
public function getErrorLog($errors)
{
foreach ($errors->Response->Error as $errorLog){

foreach ($errors->Response->Error as $errorLog) {
$exception[] = $errorLog->ErrorDescription;
}

$status = $errors->Response->ResponseStatusDescription;

if (gettype($errors->Response->Error) !== 'array') {

if (gettype($errors->Response->Error) !== 'array') {
$status = $errors->Response->Error->ErrorSeverity;

$exception[] = $errors->Response->Error->ErrorDescription;
Expand Down
Loading