Author Topic: Australia Post shipping  (Read 221814 times)

benneh

  • Beginner
  • *
  • Posts: 16
Re: Australia Post shipping
« Reply #15 on: January 14, 2006, 05:08:01 am »
Ok I have been doing this thing pretty for about 5 hours now, and I have it working perfect with the exception of one bug, the quantities don't seem to be affecting the total delivery as expected... anyhow here is an update on the code for those interested

Each time I increase the quantity 10 fold, the delivery cost seems to double  ???  I will look at it a bit later, but for now i think its a pretty good outcome, and 95% of the way there :D

Code: [Select]
<?php
defined
('_VALID_MOS') or die('Direct Access to this location is not allowed.');
/**
*
* @version $Id: auspost.php,v 1.0 2006/01/13 12:00:00 benneh Exp $
* @package VirtueMart
* @subpackage shipping
* @copyright Copyright (C) 2006 Ben Wilson. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/**
*
* This class will charge a shipping rate determined by passing parameters to 
* http://drc.edeliver.com.au/ 
* @copyright (C) 2006 Ben Wilson

*******************************************************************************
*/
class auspost {

var $classname "auspost";

function list_rates( &$d ) {
global $total$tax_total$CURRENCY_DISPLAY;

$cart $_SESSION['cart'];

/** Read current Configuration ***/
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");

if ( $_SESSION['auth']['show_price_including_tax'] != ) {
$taxrate 1;
$order_total $total $tax_total;
}
else {
$taxrate $this->get_tax_rate() + 1;
$order_total $total;
}

//Create DB User Object for Current User
$dbu = new ps_DB;
$q  "SELECT country,zip FROM #__{vm}_user_info WHERE user_info_id = '"$d["ship_to_info_id"] . "'";
$dbu->query($q);
if (!$dbu->next_record()) {
/*$vmLogger->err( $VM_LANG->_PHPSHOP_CHECKOUT_ERR_SHIPTO_NOT_FOUND );
return False;*/
}

//Create DB Vendor Object for Shop Vendor
$dbv = new ps_DB;
$q  "SELECT * from #__{vm}_vendor, #__{vm}_country WHERE vendor_id='" $_SESSION["ps_vendor_id"] . "' AND (vendor_country=country_2_code OR vendor_country=country_3_code)";
$dbv->query($q);
$dbv->next_record();

//$dbv = new ps_DB
//$q  = "SELECT * FROM #__{vm}_vendor WHERE vendor_id='".$_SESSION['ps_vendor_id']."'";
//$dbv->query($q);
//$dbv->next_record();

//set up the variables for Australia Post Query

//Postcode of the pick-up address (e.g. 3015)
//$Order_Pickup_Postcode = '2615';
//$Order_Pickup_Postcode = Pickup_Postcode;
$Order_Pickup_Postcode $dbv->f("vendor_zip");

//Postcode of the delivery destination (e.g. 2615)
//$Order_Destination_Postcode = '2001';
$Order_Destination_Postcode $dbu->f("zip");

//The country of delivery destination designated by two alpha characters. For example, AU stands for Australia
$Order_Country 'AU';

//The weight of the parcel or item measured in grams (g)
//$Order_Weight = '10000';
$Order_Weight $d['weight'];

//The type of servive, available types are "Standard", "Express", "Air", "Sea", and "Economy"
$Order_Service_Type Service_Type;
//$Order_Service_Type = 'STANDARD';

//The length of the item or parcel in millimetres (mm)
//Auspost returns same value so long as this is valid ie between 100 and 500, so we use a fixed 250 as a placeholder
$Order_Length '250';

//The width of the item or parcel in millimetres (mm)
$Order_Width '250';

//The height of the item or parcel in millimetres (mm)
$Order_Height '250';

//This is the quantity of items for which the customer is estimating the delivery charges
//Always set to one, as virtuemart does the multiplying for us based on quantity in cart
$Order_Quantity '1';

//Fee for packaging and handling, added to the delivery costs returned by auspost
$Order_Handling_Fee Handling_Fee;

    // Collect variables into the query URI for Australia Post
$myfile=file('http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode='.$Order_Pickup_Postcode.'&Destination_Postcode='.$Order_Destination_Postcode.'&Country='.$Order_Country.'&Weight='.$Order_Weight.'&Service_Type='.$Order_Service_Type.'&Length='.$Order_Length.'&Width='.$Order_Width.'&Height='.$Order_Height.'&Quantity='.$Order_Quantity);

echo 'http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode='.$Order_Pickup_Postcode.'&Destination_Postcode='.$Order_Destination_Postcode.'&Country='.$Order_Country.'&Weight='.$Order_Weight.'&Service_Type='.$Order_Service_Type.'&Length='.$Order_Length.'&Width='.$Order_Width.'&Height='.$Order_Height.'&Quantity='.$Order_Quantity;

// Get Australia Post charge value separate to 'charge='
$APchargeArray split('=',$myfile[0]);
$APcharge $APchargeArray[1];

// Get Australia Post Time separate to 'days='
$APtimeArray split('=',$myfile[1]);
$APtime $APtimeArray[1];

// error message
$APerrorArray split('=',$myfile[2]);
$APerrorMessage $APerrorArray[1];

$order_shipping $APcharge $Order_Handling_Fee;

$html .= "<label for=\"flex_shipping_rate\">Australia Post: ".$CURRENCY_DISPLAY->getFullValue($order_shipping);
$html .= "</label>";

$_SESSION[$shipping_rate_id] = 1;

echo $html;
return true;
}

function get_rate( &$d ) {

$shipping_rate_id $d["shipping_rate_id"];
$is_arr explode("|"urldecode(urldecode($shipping_rate_id)) );
$order_shipping $is_arr[3];

return $order_shipping;

}


function get_tax_rate() {

/** Read current Configuration ***/
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");

if( intval(FLEX_TAX_CLASS)== )
return( );
else {
require_once( CLASSPATH"ps_tax.php" );
$tax_rate ps_tax::get_taxrate_by_idintval(FLEX_TAX_CLASS) );
return $tax_rate;
}
}

/* Validate this Shipping method by checking if the SESSION contains the key
* @returns boolean False when the Shipping method is not in the SESSION
*/
function validate$d ) {

$shipping_rate_id $d["shipping_rate_id"];

if( array_key_exists$shipping_rate_id$_SESSION )) {

return true;
}
else {
return false;
}
}
/**
    * Show all configuration parameters for this Shipping method
    * @returns boolean False when the Shipping method has no configration
    */
function show_configuration() {
global $VM_LANG;
/** Read current Configuration ***/
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
    
?>

      <table>
    <tr>
        <td><strong>Pickup Postcode:</strong></td>
<td>
            <input type="text" name="Pickup_Postcode" class="inputbox" value="<?php echo Pickup_Postcode ?>" />
</td>
<td>
        <?php echo mm_ToolTip("Postcode of the pick-up address (e.g. 2615)"?>
        </td>
    </tr>
    <tr>
        <td><strong>Service Type:</strong>
</td>
<td>
            <input type="text" name="Service_Type" class="inputbox" value="<?php echo Service_Type ?>" />
</td>
<td>
        <?php echo mm_ToolTip("The Australia Post delivery service type (Types of service available include Standard|Express|Air|Sea|Economy)"?>
        </td>
    </tr>
    <tr>
        <td><strong>Handling Fee:</strong>
</td>
<td>
            <input type="text" name="Handling_Fee" class="inputbox" value="<?php echo Handling_Fee ?>" />
</td>
<td>
            <?php echo mm_ToolTip("This is your fee for packaging and handling, and is added to the delivery costs returned by auspost"?>
        </td>
    </tr>
  <tr>
<td><strong><?php echo $VM_LANG->_PHPSHOP_UPS_TAX_CLASS ?></strong></td>
<td>
  <?php
  require_once(CLASSPATH.'ps_tax.php');
  ps_tax::list_tax_value("FLEX_TAX_CLASS"FLEX_TAX_CLASS?>

</td>
<td><?php echo mm_ToolTip($VM_LANG->_PHPSHOP_UPS_TAX_CLASS_TOOLTIP?><td>
  </tr>
</table>
   <?php
   
// return false if there's no configuration
   
return true;
}
/**
  * Returns the "is_writeable" status of the configuration file
  * @param void
  * @returns boolean True when the configuration file is writeable, false when not
  */
function configfile_writeable() {
return is_writeableCLASSPATH."shipping/".$this->classname.".cfg.php" );
}

/**
* Writes the configuration file for this shipping method
* @param array An array of objects
* @returns boolean True when writing was successful
*/
function write_configuration( &$d ) {
    global $vmLogger;

$my_config_array = array("Pickup_Postcode" => $d['Pickup_Postcode'],
"Service_Type" => $d['Service_Type'],
"Handling_Fee" => $d['Handling_Fee'],
"FLEX_TAX_CLASS" => $d['FLEX_TAX_CLASS']
);
$config "<?php\n";
$config .= "defined('_VALID_MOS') or die('Direct Access to this location is not allowed.'); \n\n";
foreach( $my_config_array as $key => $value ) {
$config .= "define ('$key', '$value');\n";
}

$config .= "?>
";

if ($fp = fopen(CLASSPATH ."shipping/".$this->classname.".cfg.php", "w")) {
fputs($fp, $config, strlen($config));
fclose ($fp);
return true;
}
else {
$vmLogger->err( "Error writing to configuration file" );
return false;
}
}
}


?>

bcoble

  • Beginner
  • *
  • Posts: 9
Re: Australia Post shipping
« Reply #16 on: January 14, 2006, 06:09:16 am »
Hey benneh, nice work! :o

I've had a look at the code and done a test on my site, and I've got a few comments.

By putting the selection of the delivery type in the config file and only getting the Aust Post price for that one delivery type, the user doesn't get a chance to select whether they want standard delivery or express post for domestic, for instance. In my testing, the checkout process produced one price option only. The Aust Post value was correct, but without the radio buttons, it didn't seem to pass the value to the next page, and keeps asking the user to select a shipping option.

Also, we probably need some flexibility in the implementation of the handling charge.

For instance, I'm looking at selling a range of products - from 100g CDs to 2kg framed pictures. The cost of a CD box is not the same as the cost of a large box for a glass-framed picture. I therefore need some way to add the (varied) cost of the packaging to the cost of the postage. The shipping rates form allows you to do this on a per-shipping-rate-basis, but because this module is getting it directly from auspost, it can't be specified anywhere yet, aside from the one-fee-for-all box in the config.

For the time being, the single handling-fee box approach should suffice. Just a note: the handling-fee is not currently being added to the AusPost price...

I will have a go at addressing some of these issues tonight...see how I go...


bcoble

  • Beginner
  • *
  • Posts: 9
Re: Australia Post shipping
« Reply #17 on: January 14, 2006, 06:11:16 am »
After posting my note, I saw that you'd posted your update...I'll check it out...

bcoble

  • Beginner
  • *
  • Posts: 9
Re: Australia Post shipping
« Reply #18 on: January 14, 2006, 06:35:29 am »
Okay, just run a quick test on my site, and got an error. My weight was entered as 0.225. It seems that the DLC can't handle amounts below 1.00...the Auspost DLC site says of the weight data is requires:

The weight of the parcel or item measured in grams (g).

Not sure how to deal with this, but I thought you should know about it...

bcoble

  • Beginner
  • *
  • Posts: 9
Re: Australia Post shipping
« Reply #19 on: January 14, 2006, 07:48:43 am »
Further to the weight issue, I checked my product, which has a weight of 225 and a unit of measurement I have called "g" (for grams). During the order process, my product weight of 225g gets converted to .225 somehow when it's passed to the shipping module array $d. It's almost as if the weight is formatted to be delivered in kg, whereas DLC requires weights in grams...

So I changed auspost.php line 88 from
Code: [Select]
$Order_Weight = $d['weight']; to
Code: [Select]
$Order_Weight = $d['weight']*1000; . This removed the error and seemed to produce the correct postage charge as well  :)

However, it still crashes for me at the select a shipping method stage, asking me to select a method...the handling fee *is* being added to the auspost charge now, which is good!

benneh

  • Beginner
  • *
  • Posts: 16
Re: Australia Post shipping
« Reply #20 on: January 14, 2006, 11:16:35 am »
hey bcoble, thanks for the feedback.  there are a few bugs to iron out yet, I just wanted , I also need to implement some error checking, which should be easy enough as DLC has nice meaningful errors when something ain't right.  At the moment it just says delivery = $0.00 if something errors, which isn't good for business, but nice for customers, yay free delivery!!  ;D

So current todo list is:

Error checking
Radio Buttons for customer to select service type, ie standard, express, etc
update line 88 for grams

as for the packaging and handling charge being variable for each item, the database currently has no field for this purpose, but it does have a "custom_attribute" which i may be able to utilise for this purpose.

i am all out of time to work on this for now, but hopefully sometime in the next week I will be able to tackle the above... post any more updates here and I will incorporate those too.

keep the feedback and suggestions coming

benneh

  • Beginner
  • *
  • Posts: 16
Re: Australia Post shipping
« Reply #21 on: January 16, 2006, 07:27:21 am »
WOOHOO!  Australia Post Shipping Module after many hours of effort (more than i thought it would take when i set about this) is now finished and attached for your pleasure :D

It now handles weight correctly, passes variables correctly to allow progression to final step of the checkout, and has built in error checking if drc passes it back some kind of error.

As for the choices between standard, express, etc what I suggest you do is have two copies of this module in your shipping list, one for regular and one for express, then the user will be presented with both options including  showing them the cost comparisons.

Files are attached, upload them to your administrator/components/com_virtuemart/classes/shipping directory and away you go.  Enjoy everyone and keep open source open :)

[attachment cleanup by admin]

bcoble

  • Beginner
  • *
  • Posts: 9
Re: Australia Post shipping
« Reply #22 on: January 30, 2006, 15:45:09 pm »
Thanks for that Benneh! Really appreciated. But I'm having a problem with it using Paypal as the payment method - I don't know whether you can help me with this one, but I'm absolutely stuck:  ???

Doing a test order within Australia (which adds a 10% GST to the product and deducts 1/11th from the GST-inclusive shipping charge returned by Australia Post), everything adds up correctly within the module at the time of checkout.

For example,
- A $30 CD has a total of $33, of which $3 is correctly the GST.
- The shipping for my test is $8.60, of which $.78 is GST.
- So the order total is correctly $41.60, which includes $3.78 of GST.

But when the customer is transferred to PayPal for processing the payment, the order total is $33.78.  :o

It appears that the PayPal processor is receiving the product total inclusive of GST ($33), and the GST on the shipping ($0.78), but the value of the shipping itself ($8.60)  ::)

Can you Benneh or anyone else give me any clues as to why PayPal is only getting some of the data? It's kind of critical to everything I'm trying to do if PayPal is not charging my shipping  :'(

bcoble

  • Beginner
  • *
  • Posts: 9
Re: Australia Post shipping
« Reply #23 on: January 30, 2006, 16:58:56 pm »
It's alright folks, I've found the answer after a bit of rustling around (for those of you using paypal only - I haven't tested this for use with other payment methods aside from paypal).

In the Payment Methods listing, I selected the PayPal method, and clicked on the Configuration tab. In the code section, go to the section at the top of the code (lines 11 & 12) which refer to "shipping" and "amount", and replace them with the following:
Code: [Select]
"shipping" => sprintf("%.2f", $db->f("order_shipping")),
"amount" => (sprintf("%.2f", $db->f("order_shipping"))) + (round( $db->f("order_subtotal")+$tax_total, 2)),

What I've done is add the shipping to the order amount (which appears to be just the total of the products inclusive of GST in the default PayPal code).

Then in PayPal, I have selected to not charge postage (as it's included in the data sent from VirtueMart to PayPal).

So far, it all seems to work quite dandy...

By the way, I'm testing a update I'm working on that displays a range of shipping options for each order depending on the country of destination for the order. In short, if the order is going within Australia, the user gets the standard and the express post options. If the destination is outside of Australia, the user is presented with Australia Post's international service options...I'll post when it's been given a run through... ;)

benneh

  • Beginner
  • *
  • Posts: 16
Re: Australia Post shipping
« Reply #24 on: January 31, 2006, 05:06:47 am »
I dont believe this is a problem with the auspost module, but rather a problem with the paypal module.  make sure you choose the right tax mode in the auspost configuration too, otherwise it will not include GST.

spinfx

  • Beginner
  • *
  • Posts: 38
Re: Australia Post shipping
« Reply #25 on: February 06, 2006, 05:47:10 am »
benneh

This is great!!! We have been struggling to get this together for ages - trying to interface with Australia Post's  online pricing database so we didn't have to recode everything when they change rates. Really cool mate!

Don't suppose you know if anyone is working on payment modules for the major Aussie Banks do you?

Cheers - David

benneh

  • Beginner
  • *
  • Posts: 16
Re: Australia Post shipping
« Reply #26 on: February 06, 2006, 06:57:02 am »
nope not aware of anything for aus banks, there is support for eway, a very highly rated aussie payment gateway though...

glad you like the module :D

UncleBuck

  • Beginner
  • *
  • Posts: 14
Re: Australia Post shipping
« Reply #27 on: February 15, 2006, 22:11:41 pm »
benneh.... Mate.... Great job on the shipping module.

I have been playing with the code of your shipping module and have managed to get it to the point where the configuration allows you to select the type(s) of service that you want to allow in the shop. The shipping for each service type is calculated correctly by DRC and displayed on the shipping selection page, but then I have an error when you go to the checkout that causes it to fall in a heap.

I am not what you would call a programer and php is all new to me. I was wondering if you would be interested in colaborating on this to try and get this working. I don't think it will be a major effort to fix the last step, I just think I don't have enough experience with virtuemart and php to jump this last hurdle.

Col.

benneh

  • Beginner
  • *
  • Posts: 16
Re: Australia Post shipping
« Reply #28 on: February 15, 2006, 23:22:53 pm »
yep, i'd be interested in taking a look at what you have done, send me a PM where I can download your code from...

UncleBuck

  • Beginner
  • *
  • Posts: 14
Re: Australia Post shipping
« Reply #29 on: February 16, 2006, 10:10:09 am »
This is basically what I have as a look so far:


[attachment cleanup by admin]