注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天高云淡的博客

A bad beginning makes a bad ending.

 
 
 

日志

 
 

USPS International  

2010-09-08 12:16:25|  分类: USPS |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Here is the example request from USPS website:
The header changes to: IntlRateRequest USERID=000AAAAA9999&XML=$xml...

<IntlRateRequest USERID="000AAAAA9999">
            <Package ID="1st">
                        <Pounds>4</Pounds>
                        <Ounces>3</Ounces>
                        <MailType>Package</MailType>
                        <Country>Canada</Country>
            </Package>
            <Package ID="2nd">
                        <Pounds>4</Pounds>
                        <Ounces>3</Ounces>
                        <MailType>Package</MailType>
                        <ValueOfContents>500</ValueOfContents>
                        <Country>Afghanistan</Country>
            </Package>
</IntlRateRequest>

And the sample response:

<IntlRateResponse>
            <Package ID="1st">
                        <Prohibitions>An issue of a publication s.</Prohibitions>
                        <Restrictions>Coins; nada.</Restrictions>
                        <Observations>1. Banty limit of $200.</Observations>
                        <CustomsForms>Lettircalc.usps.gov/custforms.htm)</CustomsForms>
                        <ExpressMail>Country Code:
                        CA
                        Reciprocal Service Name:
                        There is no reciprocal service.
                        Areas Served: All</ExpressMail>
                        <AreasServed>Please reference Express Mail for Areas Served.</AreasServed>
                        <Service ID="1">
                                    <Pounds>4</Pounds>
                                    <Ounces>3</Ounces>
                                    <MailType>Package</MailType>
                                    <Country>CANADA</Country>
                                    <Postage>30.25</Postage>
                                    <SvcCommitments>3 - 5 Days</SvcCommitments>
                                    <SvcDescription>Express Mail International (EMS)</SvcDescription>
                                    <MaxDimensions>Max. length 36", max. length plus girth 79"</MaxDimensions>
                                    <MaxWeight>66</MaxWeight>

                        </Service>
                        <Service ID="2">
                                    <Pounds>4</Pounds>
                                    <Ounces>3</Ounces>
                                    <MailType>Package</MailType>
                                    <Country>CANADA</Country>
                                    <Postage>21.2</Postage>
                                    <SvcCommitments>5 - 8 Days</SvcCommitments>
                                    <SvcDescription>Priority Mail International</SvcDescription>
                                    <MaxDimensions>9-1/2" x 12-1/2"</MaxDimensions>
                                    <MaxWeight>66</MaxWeight>
                        </Service>
            </Package>
</IntlRateResponse>


Here's what I have so far.. it's not working yet (even though it's almost a direct copy of the current function in uc_usps.module). I had to add a column of international country names, per the USPS website. That's what the "uspsName" column is in the shipto_country statement.

I'm not quite sure where else to go with this. Right now I'm just adding lines to the usps module, in the hopes that what I write will work and can be included either in a patch or in core. (Too lazy and a bit too much pressure right now to work on a separate module).

The other main thing to work on is the handling of the response from the API server. Any help is greatly appreciated.

<?php
function uc_usps_rate_request($packages, $origin, $destination){
 
$request  = '<IntlRateRequest USERID="' . variable_get('uc_usps_user_id', '') . '">';
 
$services_count = 0;
 
$shipto_country = uc_get_country_data(array('country_id' => $destination->country));
 
$shipto_country = $shipto_country[0]['uspsName']; // This needs to be international per website
 
 
foreach($packages as $package){
   
$request .= '<Package ID="1st">' .
     
'<Pounds>' . intval($package->pounds) . '</Pounds>' .
     
'<Ounces>' . intval($package->ounces) . '</Ounces>' .
     
'<MailType>' . $package->container . '</MailType>' .
     
'<Country>' .$shipto_country . '</Country>' .   
     
'</Package>';

   

$services_count++;
  }
 
$request .= '</IntlRateRequest>';

 

$request = 'API=IntlRate&XML=' . urlencode($request);
  return
$request;
}
?>


Still stuck ... I have the USPS international quote being returned successfully, but instead of giving me the different options, it just gives me the last one in the XML list (which also appears to be the cheapest).

It looks like I need to adjust this function:

<?php
$services
= array();
  if (
is_array($response->document->package)){
     
    
    foreach (
$response->document->package as $package){
      if (isset(
$package->error)){
       
      }
      else{
          
        foreach (
$package->service as $service){
       
            
$attr = $service->attributes();
            
                  if (
$attr['ID'] === 0 || $attr['ID'] === '0'){
                   
$attr['ID'] = 'zero';
                  }
                 
            
$services[$attr['classid']]['label'] = t('USPS @service', array('@service' => $service->svcdescription[0]->data()));
            
$services[$attr['classid']]['rate'] = uc_usps_markup($service->postage[0]->data());
   
       }       
      }     
    }   
  }
?>

Because now the structure of the XML is different. Instead of wanting to find multiple "packages" we are looking for multiple "services" within the one package. It seems to be one level deeper. (Arrays always make me a bit buggy)...


  评论这张
 
阅读(700)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017