ups_pickup 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ <xsd:schema targetNamespace="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
2
+ <xsd:element name="UPSSecurity">
3
+ <xsd:complexType>
4
+ <xsd:sequence>
5
+ <xsd:element name="UsernameToken">
6
+ <xsd:complexType>
7
+ <xsd:sequence>
8
+ <xsd:element name="Username" type="xsd:string"/>
9
+ <xsd:element name="Password" type="xsd:string"/>
10
+ </xsd:sequence>
11
+ </xsd:complexType>
12
+ </xsd:element>
13
+ <xsd:element name="ServiceAccessToken">
14
+ <xsd:complexType>
15
+ <xsd:sequence>
16
+ <xsd:element name="AccessLicenseNumber" type="xsd:string"/>
17
+ </xsd:sequence>
18
+ </xsd:complexType>
19
+ </xsd:element>
20
+ </xsd:sequence>
21
+ </xsd:complexType>
22
+ </xsd:element>
23
+ </xsd:schema>
@@ -0,0 +1,48 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Rajendra Upadhya (UPS GACOR850393,) -->
3
+ <!-- edited with XMLSpy v2006 sp2 U (http://www.altova.com) by MC (United Parcel Service) -->
4
+ <xsd:schema targetNamespace="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ups="http://www.ups.com/XMLSchema" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" elementFormDefault="qualified">
5
+ <xsd:element name="Request" type="common:RequestType"/>
6
+ <xsd:element name="Response" type="common:ResponseType"/>
7
+ <xsd:element name="ClientInformation" type="common:ClientInformationType"/>
8
+
9
+ <xsd:complexType name="ClientInformationType">
10
+ <xsd:sequence>
11
+ <xsd:element name="Property" minOccurs="0" maxOccurs="unbounded">
12
+ <xsd:complexType>
13
+ <xsd:simpleContent>
14
+ <xsd:extension base="xsd:string">
15
+ <xsd:attribute name="Key" type="xsd:string" use="required"/>
16
+ </xsd:extension>
17
+ </xsd:simpleContent>
18
+ </xsd:complexType>
19
+ </xsd:element>
20
+ </xsd:sequence>
21
+ </xsd:complexType>
22
+
23
+ <xsd:complexType name="RequestType">
24
+ <xsd:sequence>
25
+ <xsd:element name="RequestOption" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
26
+ <xsd:element name="TransactionReference" type="common:TransactionReferenceType" minOccurs="0"/>
27
+ </xsd:sequence>
28
+ </xsd:complexType>
29
+ <xsd:complexType name="TransactionReferenceType">
30
+ <xsd:sequence>
31
+ <xsd:element name="CustomerContext" type="xsd:string" minOccurs="0"/>
32
+ <xsd:element name="TransactionIdentifier" type="xsd:string" minOccurs="0" ups:usage="notused"/>
33
+ </xsd:sequence>
34
+ </xsd:complexType>
35
+ <xsd:complexType name="ResponseType">
36
+ <xsd:sequence>
37
+ <xsd:element name="ResponseStatus" type="common:CodeDescriptionType"/>
38
+ <xsd:element name="Alert" type="common:CodeDescriptionType" minOccurs="0" maxOccurs="unbounded"/>
39
+ <xsd:element name="TransactionReference" type="common:TransactionReferenceType" minOccurs="0"/>
40
+ </xsd:sequence>
41
+ </xsd:complexType>
42
+ <xsd:complexType name="CodeDescriptionType">
43
+ <xsd:sequence>
44
+ <xsd:element name="Code" type="xsd:string"/>
45
+ <xsd:element name="Description" type="xsd:string"/>
46
+ </xsd:sequence>
47
+ </xsd:complexType>
48
+ </xsd:schema>
data/lib/ups_pickup.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'pry'
2
+ require 'savon'
3
+ require 'httparty'
4
+ require "ups_pickup/version"
5
+ require "ups_pickup/pickup_request"
6
+ require "ups_pickup/error_response"
7
+ require "ups_pickup/shipper"
8
+ require "ups_pickup/address"
9
+ require "ups_pickup/fault_response"
10
+ require "ups_pickup/pickup_creation"
11
+ require "ups_pickup/pickup_creation_success"
12
+ require "ups_pickup/pickup_piece"
13
+ require "ups_pickup/pickup_cancel"
14
+ require "ups_pickup/pickup_cancel_success"
15
+
16
+ module UpsPickup
17
+ # Your code goes here...
18
+ end
@@ -0,0 +1,63 @@
1
+ module UpsPickup
2
+
3
+ class Address
4
+
5
+ attr_accessor :address_line,:city, :state, :country,:postal_code,:country_code
6
+ def initialize(options={})
7
+ options ||={}
8
+ @address_line = options[:address_line]
9
+ @city = options[:city]
10
+ @state = options[:state]
11
+ @postal_code = options[:postal_code]
12
+ #@country = options[:country_code]
13
+ @country_code = options[:country_code] || "US"
14
+ end
15
+
16
+ def to_ups_hash
17
+ {
18
+ "ns2:AddressLine"=>@address_line,
19
+ "ns2:City"=>@city,
20
+ "ns2:StateProvince"=>@state,
21
+ "ns2:PostalCode"=>@postal_code,
22
+ "ns2:CountryCode"=>@country_code
23
+ }
24
+ end
25
+ end
26
+
27
+ class PickupAddress < Address
28
+ ADDRESS_TYPES = {:residential=>"Y",:commercial=>"N"}
29
+ attr_accessor :company_name,:contact_name,:room,:floor , :residential_indicator,:pickup_point,:phone_number,:extension
30
+ def initialize(options={})
31
+ super(options)
32
+ @company_name =options[:company_name]
33
+ @contact_name = options[:contact_name]
34
+ @room = options[:room]
35
+ @floor = options[:floor]
36
+ @residential_indicator = options[:residential_indicator] || "N" #ADDRESS_TYPES[options[:type]]
37
+ @pickup_point = options[:pickup_point]
38
+ @phone_number = options[:phone_number]
39
+ @extension = options[:extension]
40
+
41
+ end
42
+ def to_ups_hash
43
+ {
44
+ "ns2:CompanyName"=>@company_name,
45
+ "ns2:ContactName"=>@contact_name,
46
+ "ns2:AddressLine"=>@address_line,
47
+ "ns2:Floor"=>@floor,
48
+ "ns2:Room"=>@room,
49
+ "ns2:City"=>@city,
50
+ "ns2:StateProvince"=>@state,
51
+ "ns2:PostalCode"=>@postal_code,
52
+ "ns2:CountryCode"=>@country_code,
53
+ "ns2:ResidentialIndicator"=>@residential_indicator,
54
+ "ns2:PickupPoint"=>@pickup_point,
55
+ "ns2:Phone"=>{
56
+ "ns2:Number"=>@phone_number,
57
+ "ns2:Extension"=>@extension
58
+ }
59
+
60
+ }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ require 'ups_pickup/util'
2
+ module UpsPickup
3
+ class ErrorResponse
4
+ include Util
5
+ attr_accessor :error_code,:severity,:description
6
+ # currently building for error
7
+ def initialize(response)
8
+ response_hash = response.to_hash
9
+
10
+ @severity = response_hash.deep_fetch(:severity)
11
+ @error_code = response_hash.deep_fetch(:code)
12
+ @description =response_hash.deep_fetch(:description)
13
+ end
14
+
15
+
16
+
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module UpsPickup
2
+ class FaultResponse < ErrorResponse
3
+ attr_accessor :response
4
+ def initialize(client,response)
5
+ fault_client = client
6
+ fault_client.config.raise_errors = false
7
+ @response = Savon::SOAP::Response.new(fault_client.config,response.http)
8
+
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,51 @@
1
+
2
+ module UpsPickup
3
+ class PickupCancel < PickupRequest
4
+ def initialize(user_name, password, license, options={})
5
+ super(user_name, password, license, options)
6
+ @cancel_by = options[:cancel_by] || "02"
7
+ @prn = options[:prn]
8
+ end
9
+
10
+ def to_ups_hash
11
+ {
12
+ "ns2:CancelBy"=>@cancel_by,
13
+ "ns2:PRN"=>@prn
14
+ }
15
+ end
16
+
17
+ def build_request
18
+ self.to_ups_hash
19
+ end
20
+
21
+ def commit
22
+ begin
23
+ @client_response = @client.request :ns2,"PickupCancelRequest" do
24
+ #soap.namespaces["xmlns:S"] = "http://schemas.xmlsoap.org/soap/envelope/"
25
+ #soap.namespaces["xmlns:upss"] = "http://www.ups.com/XMLschema/XOLTWS/UPSS/v1.0"
26
+ #soap.namespaces["xmlns:ns1"] = "http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0"
27
+ soap.namespaces["xmlns:ns3"] = "http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0"
28
+ soap.namespaces["xmlns:ns2"] = "http://www.ups.com/XMLSchema/XOLTWS/Pickup/v1.1"
29
+ # soap.header = SOAP_HEADER1
30
+ soap.header = access_request
31
+ soap.body = build_request
32
+ end
33
+ rescue Savon::SOAP::Fault => fault
34
+ @client_response = fault
35
+ rescue Savon::Error => error
36
+ @client_response = error
37
+ end
38
+ build_response
39
+ end
40
+
41
+ def build_response
42
+ if success?
43
+ @response = UpsPickup::PickupCancelSuccess.new(@client_response)
44
+ elsif soap_fault?
45
+ fault_response = UpsPickup::FaultResponse.new(@client,@client_response)
46
+ @error = UpsPickup::ErrorResponse.new(fault_response.response)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,13 @@
1
+ module UpsPickup
2
+ class PickupCancelSuccess
3
+ include Util
4
+ attr_accessor :response_status,:code,:description,:pickup_type
5
+ def initialize(response)
6
+ response_hash = response.to_hash
7
+ @response_status = response_hash.deep_fetch(:response_status)
8
+ @code = response_hash.deep_fetch(:code)
9
+ @description = response_hash.deep_fetch(:description)
10
+ @pickup_type = response_hash.deep_fetch(:pickup_type)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,141 @@
1
+ require 'ups_pickup/util'
2
+ module UpsPickup
3
+ class PickupCreation < PickupRequest
4
+ include Util
5
+ attr_accessor :response,:error
6
+ def initialize(user_name, password, license, options={})
7
+ super(user_name, password, license, options)
8
+ @rate_pickup_indicator = set_yes_or_no_option(@options[:rate_pickup_indicator])
9
+ # iF any one packge is above 70lbs or 32 kg then its true
10
+ @over_weight_indicator = set_yes_or_no_option(@options[:rate_pickup_indicator])
11
+ @reference_number = @options[:reference_number]
12
+ @special_instruction = @options[:special_instruction]
13
+ @pickup_address = UpsPickup::PickupAddress.new(options)
14
+ @shipper = UpsPickup::Shipper.new(options)
15
+ @alternate_address_indicator = @options[:alternate_address_indicator].nil? ? "N" : @options[:alternate_address_indicator]
16
+ @pickup_pieces = set_pickup_piece
17
+ set_weight
18
+ set_pickup_date_info
19
+ set_payment_method
20
+ set_notification
21
+ set_undelivered_email
22
+ end
23
+ #options=>{:pickup_piece=>{:service_code=>,:qty}}
24
+ # or options=>{:pickup_piece=>[{:service_code=>,:qty},{:service_code=>,:qty}]}
25
+
26
+ #TODO need to check wether savon is iterating array or not
27
+ def set_pickup_piece
28
+ if @options[:pickup_piece]
29
+ if @options[:pickup_piece].is_a?(Array)
30
+ @options[:pickup_piece].each do |pp|
31
+ @pickup_pieces << Ups::PickupPiece.new(pp)
32
+ end
33
+ elsif @options[:pickup_piece].is_a?(Hash)
34
+ @pickup_pieces =[UpsPickup::PickupPiece.new(@options[:pickup_piece])]
35
+ end
36
+ else
37
+ @pickup_pieces =[UpsPickup::PickupPiece.new(@options)]
38
+ end
39
+ end
40
+ def pickup_piece_ups_hash
41
+ pickup_hashes = []
42
+ if @pickup_pieces
43
+ @pickup_pieces.flatten.each do |pp|
44
+
45
+ pickup_hashes<< [pp.to_ups_hash]
46
+ end
47
+ end
48
+ pickup_hashes.flatten
49
+ end
50
+
51
+ def set_weight
52
+ @weight = @options[:weight]
53
+ #LBS or KGS
54
+ @unit_of_weight = @options[:unit_of_weight] || "LBS"
55
+ end
56
+
57
+ def set_pickup_date_info
58
+ @close_time = @options[:close_time]
59
+ @ready_time = @options[:ready_time]
60
+ @pickup_date = Date.parse(@options[:pickup_date]).strftime("%Y%m%d")
61
+ end
62
+ # The payment method to pay for this on call pickup.
63
+ # 00 = No payment needed
64
+ # 01 = Pay by shipper account
65
+ # 03 = Pay by charge card
66
+ # 04 = Pay by tracking number
67
+ # 05 = Pay by check or money order
68
+ def set_payment_method
69
+ @payment_method = options[:payment_method] if !options[:payment_method].nil? && [0,1,2,3,4,5].include?(options[:payment_method].to_i)
70
+ @payment_method ||="00"
71
+ end
72
+
73
+ # it might be an array of email address or single email address
74
+ # max allowed limit is 5
75
+ def set_notification
76
+ @confirmation_email_address = [@options[:confirmation_email_address]][0...5].flatten.uniq.compact
77
+ end
78
+
79
+ def set_undelivered_email
80
+ @undelivered_email
81
+ end
82
+
83
+ def build_request
84
+ {
85
+ "ns2:Request"=>{
86
+ "ns2:RequestOption"=>"1"
87
+ },
88
+ "ns2:RatePickupIndicator"=>@rate_pickup_indicator,
89
+
90
+ "ns2:Shipper"=>@shipper.to_ups_hash,
91
+ "ns2:PickupDateInfo"=>{
92
+ "ns2:CloseTime"=>@close_time,
93
+ "ns2:ReadyTime" => @ready_time,
94
+ "ns2:PickupDate" =>@pickup_date
95
+ },
96
+ "ns2:PickupPiece"=>pickup_piece_ups_hash,
97
+ "ns2:PickupAddress"=>@pickup_address.to_ups_hash,
98
+ "ns2:AlternateAddressIndicator"=>@alternate_address_indicator,
99
+ "ns2:PaymentMethod"=>@payment_method,
100
+ "ns2:OverweightIndicator"=>@over_weight_indicator,
101
+ "ns2:SpecialInstruction"=>@special_instruction,
102
+ "ns2:ReferenceNumber"=>@reference_number,
103
+ "ns2:Notfication"=>{
104
+ "ns2:ConfirmationEmailAddress"=>@confirmation_email_address
105
+ }
106
+ }
107
+ end
108
+
109
+ def commit
110
+ begin
111
+ @client_response = @client.request :ns2,"PickupCreationRequest" do
112
+ #soap.namespaces["xmlns:S"] = "http://schemas.xmlsoap.org/soap/envelope/"
113
+ #soap.namespaces["xmlns:upss"] = "http://www.ups.com/XMLschema/XOLTWS/UPSS/v1.0"
114
+ #soap.namespaces["xmlns:ns1"] = "http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0"
115
+ soap.namespaces["xmlns:ns3"] = "http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0"
116
+ soap.namespaces["xmlns:ns2"] = "http://www.ups.com/XMLSchema/XOLTWS/Pickup/v1.1"
117
+ # soap.header = SOAP_HEADER1
118
+ soap.header = access_request
119
+ soap.body = build_request
120
+ end
121
+ rescue Savon::SOAP::Fault => fault
122
+ @client_response = fault
123
+ rescue Savon::Error => error
124
+ @clien_response = error
125
+ end
126
+ build_response
127
+ end
128
+
129
+ def build_response
130
+ if success?
131
+ @response = UpsPickup::PickupCreationSuccess.new(@client_response)
132
+ elsif soap_fault?
133
+ fault_response = UpsPickup::FaultResponse.new(@client,@client_response)
134
+ @error = UpsPickup::ErrorResponse.new(fault_response.response)
135
+ end
136
+ end
137
+
138
+
139
+
140
+ end
141
+ end
@@ -0,0 +1,13 @@
1
+ require 'ups_pickup/util'
2
+ module UpsPickup
3
+ class PickupCreationSuccess
4
+ include Util
5
+ attr_accessor :prn,:rate_status,:success_code
6
+ def initialize(response)
7
+ response_hash = response.to_hash
8
+ @prn = response_hash.deep_fetch(:prn)
9
+ @rate_status = response_hash.deep_fetch(:rate_status)
10
+ @success_code = response_hash.deep_fetch(:code)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module UpsPickup
2
+ class PickupPiece
3
+ #attr_accessor :service_code
4
+ def initialize(options={})
5
+ @service_code = options[:service_code] || "001"
6
+ @quantity =options[:quantity] || "1"
7
+ @destination_country_code = options[:destination_country_code] || "US"
8
+ @container_code = options[:container_code] || "01"
9
+ end
10
+
11
+ def to_ups_hash
12
+ {
13
+ "ns2:ServiceCode"=>@service_code,
14
+ "ns2:Quantity" =>@quantity,
15
+ "ns2:DestinationCountryCode"=>@destination_country_code,
16
+ "ns2:ContainerCode"=>@container_code
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+
2
+ module UpsPickup
3
+ class PickupRequest
4
+ LIVE_URL = "https://onlinetools.ups.com/webservices/Pickup"
5
+ TEST_URL= "https://wwwcie.ups.com/webservices/Pickup"
6
+ include HTTParty
7
+ base_uri LIVE_URL
8
+ attr_accessor :user_name,:password,:license,:options,:client,:client_response,:response,:error
9
+ def initialize(user_name, password, license, options={})
10
+ @user_name,@password,@license,@options = user_name,password,license,options
11
+ @client=Savon::Client.new(File.expand_path("../../schema/Pickup.wsdl", __FILE__))
12
+ @request_body = {}
13
+ set_wsdl_endpoint
14
+ set_soap_namespace
15
+ if (options[:test] == true)
16
+ self.class.base_uri TEST_URL
17
+ elsif options[:url] && [LIVE_URL,TEST_URL].include?(options[:url])
18
+ self.class.base_uri options[:url]
19
+ end
20
+
21
+
22
+ end
23
+
24
+
25
+ # For Test environment set test url
26
+ def set_wsdl_endpoint
27
+ if @options[:test] == true
28
+ @client.wsdl.endpoint = TEST_URL
29
+ else
30
+ @client.wsdl.endpoint = LIVE_URL
31
+ end
32
+ end
33
+ # Set name space
34
+ # ns2 for pickup
35
+ def set_soap_namespace
36
+
37
+ # @client.wsdl.namespaces["xmlns:ns2"] = "http://www.ups.com/XMLSchema/XOLTWS/Pickup/v1.1"
38
+ # @client.wsdl.namespaces["xmlns:ns3"] = "http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0"
39
+ end
40
+
41
+ #Set security passed in client request block
42
+ def access_request
43
+ {
44
+ "ns3:UPSSecurity" => {
45
+ "ns3:UsernameToken"=>{
46
+ "ns3:Username"=>@user_name,
47
+ "ns3:Password" => @password
48
+ },
49
+ "ns3:ServiceAccessToken"=>{
50
+ "ns3:AccessLicenseNumber"=> @license
51
+ }
52
+ }
53
+ }
54
+ end
55
+ def soap_fault?
56
+ @client_response.is_a?(Savon::SOAP::Fault)
57
+ end
58
+
59
+ def http_error?
60
+ @client_response.is_a?(Savon::Error)
61
+ end
62
+
63
+ def success?
64
+ @client_response.is_a?(Savon::SOAP::Response)
65
+ end
66
+ end
67
+ end
68
+
69
+ # pickup = Ups::PickupRequest.new("contego.mtaylor","C0nt3g0!","6C82D38E41F9C600")