Skip to content

Commit

Permalink
beforeSave hook re-check postmates quote
Browse files Browse the repository at this point in the history
  • Loading branch information
CupNoodles committed Feb 18, 2021
1 parent b0960cb commit c7ed42e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
53 changes: 52 additions & 1 deletion Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
use App;
use Event;
use ApplicationException;
use Geocoder;

use Admin\Controllers\Orders;
use Admin\Widgets\Form;
use Admin\Widgets\Toolbar;
use Admin\Models\Location_areas_model;
use Admin\Models\Orders_model;

use System\Classes\BaseController;
use Igniter\Local\Facades\Location;

use CupNoodles\Postmates\Models\PostmatesSettings;
use CupNoodles\Postmates\Classes\PostmatesCoveredArea;

use CupNoodles\PriceByWeight\Components\CheckoutByWeight;

use Igniter\Cart\Models\Orders_Model;

class Extension extends BaseExtension
{
use SendsMailTemplate;
Expand Down Expand Up @@ -68,7 +72,14 @@ public function boot()
// unsetting just locationSlugResolver and then setting it back to what the constructor would have set it to allows us to sneak in and change the classtype of CoveredArea
// this certainly doesn't feel good so any advice or feedback about a better way to do this would be appreciated.

//$om = OrderManager::instance();



$location = App::make('location');



$location->locationSlugResolver(function(){});
if(is_array($location->coveredArea()->conditions) && isset($location->coveredArea()->conditions[0])){
if($location->coveredArea()->conditions[0]['delivery_service'] == 'postmates' &&
Expand All @@ -91,6 +102,7 @@ public function boot()

Event::listen('location.area.updated', function($location,$coveredArea){
$this->updatePostmatesDeliveryCost($location);
$this->saveUserAddressToSession($location);
});

// Put a 'postmates' button for type on delivery areas
Expand Down Expand Up @@ -140,6 +152,45 @@ public function boot()
});
} );


// Since the final delivery address can be entered in on the checkout screen, it may not match what was shown to the customer after entering a (possibly different) address into the localBox component.
// This final check creates a location object out of the order delivery data, and check that the last postmates delivery price set in session is equal to the quote as returned byu the checkout address.
Event::listen('igniter.checkout.beforeSaveOrder', function(Orders_Model $order, $data){

if($order->order_type == 'delivery'){
$collection = Geocoder::geocode($data['address']['address_1'] . ' ' . $data['address']['address_2'] . ' ' .$data['address']['city'] . ' ' .$data['address']['state'] . ' ' .$data['address']['postcode']);

if (!$collection OR $collection->isEmpty()) {
Log::error(implode(PHP_EOL, Geocoder::getLogs()));
throw new ApplicationException(lang('igniter.local::default.alert_invalid_search_query'));
}

$userLocation = $collection->first();
if (!$userLocation->hasCoordinates())
throw new ApplicationException(lang('igniter.local::default.alert_invalid_search_query'));

$location = App::make('location');
$postmates_quote_amt = session('postmates_delivery_quote');
$location->updateUserPosition($userLocation);
$this->updatePostmatesDeliveryCost($location);
$final_delivery_check = $location->deliveryAmount($order->order_total);
if(number_format($final_delivery_check, 2) != number_format($postmates_quote_amt, 2) ){
throw new ApplicationException('Postmates Delivery Quote has changed. Please confirm the new delivery estimate.');
}
}

});

}

public function saveUserAddressToSession($location){
session(['postmates_address_1' => $location->userPosition()->getStreetNumber() . " " . $location->userPosition()->getStreetName()]);
if(isset($location->userPosition()->data['subpremise'])){
session(['postmates_address_2' => $location->userPosition()->data['subpremise']]);
}
session(['postmates_city' => $location->userPosition()->getLocality()]);
session(['postmates_state' => $location->userPosition()->getAdminLevels()->last()->getCode()]);
session(['postmates_postcode' => $location->userPosition()->getPostalCode()]);
}

public function registerMailTemplates()
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ igniter.local::default.text_condition_below_total
igniter.local::default.text_delivery
```

Additionally, the postmates API is called with a full address from the localBox component. Since the API will fail unless it has a full address, you'll need to update

```
igniter.local::default.label_search_query
```

to specifically state that a full address needs to be entered (not just a postcode).

Since a phone number is required to make Postmates deliveries, you must make telephone a required field for any delivery order.


Expand Down
25 changes: 16 additions & 9 deletions classes/PostmatesCoveredArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ public function __construct(AreaInterface $model, $location)

public function deliveryAmount($cartTotal)
{


$delivery_cost_estimate = 0;
$user_position = $this->location->getSession('position');
if($user_position){
$delivery_cost_estimate = $this->curl_postmates_delivery_quote($user_position, $cartTotal);

// amount condition value is added as a surcharge
$delivery_cost_estimate += $this->getConditionValue('amount', $cartTotal);

return $delivery_cost_estimate;

if($delivery_cost_estimate >= 0 ){
// amount condition value is added as a surcharge
$delivery_cost_estimate += $this->getConditionValue('amount', $cartTotal);
session(['postmates_delivery_quote' => $delivery_cost_estimate]);

return $delivery_cost_estimate;
}
}
return 0;

return -1;

}

public function curl_postmates_delivery_quote($user_position, $cartTotal){

// get customer address string
$user_position->format();
// Postmates specifically requests comma-separated address formatting, so remove any potential commas in existing address fields
Expand Down Expand Up @@ -88,11 +93,13 @@ public function curl_postmates_delivery_quote($user_position, $cartTotal){
$result_json = curl_exec($ch);
$result = json_decode($result_json, true);


if(isset($result['fee'])){
return $result['fee'] / 100;
}
else{
return $this->getConditionValue('amount', $cartTotal);
//
return -1;
}
}

Expand Down

0 comments on commit c7ed42e

Please sign in to comment.