Class: FedexWebService

Inherits:
Object
  • Object
show all
Defined in:
lib/fedex_web_service.rb,
lib/fedex_web_service/address_verification.rb

Direct Known Subclasses

AddressVerification

Defined Under Namespace

Classes: AddressVerification, AddressVerificationResponse

Constant Summary collapse

@@gem_default_options =
{
  :drop_off_type      => "REGULAR_PICKUP",
  :residential        => true,
  :label              => 'GROUND_HOME_DELIVERY',
  :drop_off_type      => 'REGULAR_PICKUP',
  :packaging_type     => 'YOUR_PACKAGING',
  :label_format_type  => 'COMMON2D',
  :label_stock_type   => 'PAPER_4X6',
  :image_type         => "PNG",
  :dimensions         => {:length => 5, :width => 5, :height => 5}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFedexWebService

Returns a new instance of FedexWebService.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fedex_web_service.rb', line 33

def initialize
  raw_config = File.read(RAILS_ROOT + "/config/fedex_config.yml")
  @fedex_conf = YAML.load(raw_config)[RAILS_ENV].symbolize_keys
  
  @key            = @fedex_conf[:key]
  @password       = @fedex_conf[:password]
  @accountnumber  = @fedex_conf[:accountnumber]
  @meternumber    = @fedex_conf[:meternumber]
  @ss_wsdl        = @fedex_conf[:ss_wsdl]
  @default_options= @@gem_default_options.merge( @fedex_conf[:defaults] )
end

Instance Attribute Details

#full_responseObject

Returns the value of attribute full_response.



8
9
10
# File 'lib/fedex_web_service.rb', line 8

def full_response
  @full_response
end

#labelObject

Returns the value of attribute label.



8
9
10
# File 'lib/fedex_web_service.rb', line 8

def label
  @label
end

#temp_imageObject

Returns the value of attribute temp_image.



8
9
10
# File 'lib/fedex_web_service.rb', line 8

def temp_image
  @temp_image
end

#tracking_numberObject

Returns the value of attribute tracking_number.



8
9
10
# File 'lib/fedex_web_service.rb', line 8

def tracking_number
  @tracking_number
end

Instance Method Details

#future_shipment(address_info, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fedex_web_service.rb', line 45

def future_shipment(address_info, options = {})
  # this method sends a request to Fedex to get the future shipment date, a tracking number and the image file of a label
  # the method returns the tracking number as a result to show that the request was done correctly, those class attributes
  # are set to include the tracking number, the label (img) and the full response (SOAP object ... yech)
    
  options = @default_options.merge( options ).symbolize_keys!
  
  wsdl = File.expand_path( RAILS_ROOT + '/lib/wsdl/' + @fedex_conf[:ss_wsdl] )
  driver = build_wsdl_driver(wsdl)
  

  call = {:WebAuthenticationDetail => {:UserCredential => {:Key => @key, :Password => @password} },
    :ClientDetail => {:AccountNumber => @accountnumber, :MeterNumber => @meternumber },
    :TransactionDetail=> {:CustomerTransactionId => '*** Ground Domestic Shipping Request v7' },
    :Version => {:ServiceId => 'ship', :Major => '7', :Intermediate => '0', :Minor => '0'},
    :RequestedShipment => {
    :ShipTimestamp => "#{3.days.from_now.strftime("%Y-%m-%d")}T9:00:00-07:00", 
    :DropoffType => options[:drop_off_type],
    :ServiceType => options[:label], # 'FEDEX_2_DAY' 'FEDEX_GROUND' 'GROUND_HOME_DELIVERY'
    :PackagingType => options[:packaging_type], # valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
    :SpecialServicesRequested => {:SpecialServiceType => "FUTURE_DATE_SHIPMENT"},
    :Recipient => { #should have same address as Origin per docs
      :Contact => {
        :PersonName => address_info.first_name + " " + address_info.last_name,
        :PhoneNumber => address_info.phone #params[:phone] 
        
      },
      :Address => {
        :CountryCode => 'US',
        :StreetLines => address_info.shipping_address_1, #params[:shipAddress],
        :City => address_info.shipping_city, #params[:shipCity],
        :StateOrProvinceCode => address_info.shipping_state, #params[:shipState],
        :PostalCode => address_info.shipping_zip, #params[:shipZip]
        :Residential => options[:residential]
      }
    },
    :Shipper => {
      :Contact => {
        :PersonName =>  options[:person_name],
        :PhoneNumber => options[:phone_number]
      },
      :Address => {
        :CountryCode => 'US',
        :StreetLines => options[:address],
        :City => options[:city],
        :StateOrProvinceCode => options[:state],
        :PostalCode => options[:postal_code]
        #:Residential => TRUE
      }
    },
    :ShippingChargesPayment => {
      :PaymentType => 'SENDER',
      :Payor => {
        :AccountNumber => @accountnumber,
        :CountryCode => 'US'
      }
    },
    :LabelSpecification => {
      :LabelFormatType => options[:label_format_type],
      :LabelStockType => options[:label_stock_type],
      :ImageType => options[:image_type], #// valid values DPL, EPL2, PDF, ZPLII and PNG
      :CustomerSpecifiedDetail => {:MaskedData => "SHIPPER_ACCOUNT_NUMBER"}
    },
    :RateRequestTypes => 'ACCOUNT', #, // valid values ACCOUNT and LIST
    :PackageCount => 1, 
    :PackageDetail => "INDIVIDUAL_PACKAGES",
    :RequestedPackageLineItems => [{
      :SequenceNumber=>1, 
      :Weight => { :Units => "LB", :Value => 1 },
      :Dimensions => {
        :Length => options[:dimensions][:length], 
        :Width => options[:dimensions][:width], 
        :Height => options[:dimensions][:height], 
        :Units => "IN"
      },
      :CustomerReferences => [{:CustomerReferenceType => "INVOICE_NUMBER", :Value => "#{options[:invoice_number]}"} ],      #in a string so if it's empty it's just an empty string
    }]
  }
}
  result = driver.processShipment(call)
  begin
    self.label = Base64.decode64(result.completedShipmentDetail.completedPackageDetails.label.parts.image)
    self.full_response = result
    self.tracking_number = result.completedShipmentDetail.completedPackageDetails.trackingIds.trackingNumber
  rescue  Exception => e
    #the FEDEX call returned an error
    self.label = "error"
    self.full_response = result
    self.tracking_number = "error"
  end
end