shipppit-canada-post 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00ff5aab5456ba9e2c713466439d72de463a8a9a
4
+ data.tar.gz: 6e68939726c48b6b49272421d8118fd62f5a0cdb
5
+ SHA512:
6
+ metadata.gz: 267d00ca8147cd62d41788b8f20a7ee49732c6ce6e79af1da8921e8908523758a49fe1bf68c07f2c00aa39a3393c8614791566549c5ff1bc47c67cf634f39b45
7
+ data.tar.gz: 386c4d513111ef8fd82e423eedf62e7ed9a2b957dc752af9084a2765e4a991bc1891a72a3c9f0f8deacd752d308529a4b93efa5d089ff741843a0f916c41c1cf
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ spec/vcr
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ canada_post_credentials.yml
17
+ TODO.md
18
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in canada-post-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 JONBRWN
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,312 @@
1
+ # CanadaPost REST API V3 Wrapper
2
+
3
+ [![Build Status](https://semaphoreci.com/api/v1/projects/719f5dd5-e5ff-47f3-a6ff-833ad667ef76/646929/badge.svg)](https://semaphoreci.com/olimart/shipppit-canada-post)
4
+
5
+ A Ruby wrapper for the CanadaPost REST API. Based extensively off the [fedex](https://github.com/jazminschroeder/fedex) gem.
6
+ Thanks to [jazminschroeder](https://github.com/jazminschroeder) and all contributors who helped make that a gem worth recreating for the Canada Post API
7
+
8
+ For more info see the [Official Canada Post Developer Docs](https://www.canadapost.ca/cpotools/apps/drc/home)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'shipppit-canada-post'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install shipppit-canada-post
25
+
26
+ ## Usage
27
+
28
+ Require the gem:
29
+
30
+ ```ruby
31
+ require 'canada_post'
32
+ ```
33
+
34
+ Create a service:
35
+
36
+ ```ruby
37
+ canada_post_service = CanadaPost::Client.new( username: 'xxxx',
38
+ password: 'xxxx',
39
+ customer_number: 'xxxx',
40
+ mode: 'development' )
41
+ # mode can be 'development' or 'production'
42
+ ```
43
+
44
+ ### Define shipper:
45
+
46
+ ```ruby
47
+ shipper = { postal_code: 'M5X1B8', country_code: 'CA' }
48
+ # Post Code is required for US and CA shipments, not for International Shipments
49
+ ```
50
+
51
+ ### Define recipient:
52
+
53
+ ```ruby
54
+ recipient = { postal_code: 'M5R1C6', country_code: 'CA' }
55
+ ```
56
+
57
+ ### Define package:
58
+
59
+ ```ruby
60
+ package = { weight: { value: 1, units: 'KG' },
61
+ dimensions: { length: 25, width: 15, height: 10, units: 'CM' } }
62
+ # weight is the only requirement for your package, Canada Post only accepts KG and CM for the time being
63
+ ```
64
+
65
+ ### To get service codes:
66
+ see [Canada Post Rating API docs](https://www.canadapost.ca/cpo/mc/business/productsservices/developers/services/rating/getrates/default.jsf) for service code descriptions
67
+ ```ruby
68
+ $ CanadaPost::Request::Rate::SERVICE_CODES
69
+ $ => ["DOM.RP", "DOM.EP", "DOM.XP", "DOM.XP.CERT", "DOM.PC.CERT", "DOM.PC", "DOM.DT", "DOM.LIB", "USA.EP", "USA.PW.ENV", "USA.PW.PAK", "USA.PW.PARCEL", "USA.SP.AIR", "USA.TP", "USA.TP.LVM", "USA.XP", "INT.XP", "INT.IP.AIR", "INT.IP.SURF", "INT.PW.ENV", "INT.PW.PAK", "INT.PW.PARCEL", "INT.SP.AIR", "INT.SP.SURF", "INT.TP"]
70
+ ```
71
+
72
+ ### Get Rates:
73
+
74
+ ```ruby
75
+ $ rates = canada_post_service.rate(
76
+ shipper: shipper, recipient: recipient, package: package)
77
+ ```
78
+
79
+ Not specifying a service type will return an array of all available rates
80
+
81
+ ```ruby
82
+ # complete response
83
+ $ [#<CanadaPost::Rate:0x007fd783f42a88 @service_type="Expedited Parcel", @service_code="DOM.EP", @total_net_charge="8.76", @total_base_charge="7.77", @gst_taxes="0.00", @pst_taxes="0.00", @hst_taxes="1.01", @expected_transit_time="1">, #<CanadaPost::Rate:0x007fd783f42a60 @service_type="Priority", @service_code="DOM.PC", @total_net_charge="19.14", @total_base_charge="16.21", @gst_taxes="0.00", @pst_taxes="0.00", @hst_taxes="2.20", @expected_transit_time="1">, #<CanadaPost::Rate:0x007fd783f42a10 @service_type="Regular Parcel", @service_code="DOM.RP", @total_net_charge="8.76", @total_base_charge="7.77", @gst_taxes="0.00", @pst_taxes="0.00", @hst_taxes="1.01", @expected_transit_time="2">, #<CanadaPost::Rate:0x007fd783f429e8 @service_type="Xpresspost", @service_code="DOM.XP", @total_net_charge="11.31", @total_base_charge="9.58", @gst_taxes="0.00", @pst_taxes="0.00", @hst_taxes="1.30", @expected_transit_time="1">]
84
+ ```
85
+
86
+ Specifying the service type will return one result
87
+
88
+ ```ruby
89
+ $ service_type = 'DOM.EP'
90
+ $ rates = canada_post_service.rate( shipper: shipper,
91
+ recipient: recipient,
92
+ package: package,
93
+ service_type: service_type )
94
+ # complete response
95
+ $ [
96
+ #<CanadaPost::Rate:0x007fd783fea238
97
+ @service_type="Expedited Parcel",
98
+ @service_code="DOM.EP",
99
+ @total_net_charge="8.76",
100
+ @total_base_charge="7.77",
101
+ @gst_taxes="0.00",
102
+ @pst_taxes="0.00",
103
+ @hst_taxes="1.01",
104
+ @expected_transit_time="1",
105
+ @expected_delivery_date="2015-12-25",
106
+ @guaranteed_delivery="false",
107
+ @am_delivery="false">
108
+ ]
109
+ ```
110
+
111
+ Your final amount will be under `total_net_charge`:
112
+
113
+ ```ruby
114
+ $ rates.first.total_net_charge => '8.76' # all monetary values are CAD
115
+ ```
116
+
117
+ ### Create Shipping
118
+
119
+ ```ruby
120
+ sender = {
121
+ name: 'John Doe',
122
+ company: 'sender company',
123
+ shipping_point: 'M5X1B8',
124
+ address_details: {
125
+ address: '123 street',
126
+ phone: '343434',
127
+ state: 'QC'
128
+ zip: 'M5X1B8',
129
+ city: 'Gatineau',
130
+ country: 'CA'
131
+ }
132
+ }
133
+ destination = {
134
+ name: 'John Doe',
135
+ company: 'receiver company',
136
+ address_details: {
137
+ address: '4394 Rue Saint-Denis',
138
+ state: 'QC'
139
+ zip: 'H2J2L1',
140
+ city: 'Montréal',
141
+ country: 'CA'
142
+ }
143
+ }
144
+ package = {
145
+ weight: 2,
146
+ unpackaged: false,
147
+ mailing_tube: false,
148
+ dimensions: {
149
+ length: 2,
150
+ width: 2
151
+ height: 2
152
+ }
153
+ }
154
+ notification = {
155
+ email: 'example@gmail.com',
156
+ on_shipment: true,
157
+ on_exception: true,
158
+ on_delivery: true
159
+ }
160
+ preferences = {
161
+ show_packing_instructions: true,
162
+ show_postage_rate: true,
163
+ show_insured_value: true
164
+ }
165
+ settlement_info = {
166
+ contract_id: 2514533 // 2514533 for sendbox mode
167
+ }
168
+
169
+ canada_post_service.create(
170
+ sender: sender,
171
+ destination: destination,
172
+ package: package,
173
+ notification: notification,
174
+ preferences: preferences,
175
+ settlement_info: settlement_info,
176
+ group_id: '5241556',
177
+ mailing_date: '2016-01-20',
178
+ contract_id: '2514533',
179
+ service_code: 'DOM.RP'
180
+ )
181
+
182
+ Response:
183
+ {
184
+ create_shipping: {create shipping response},
185
+ transmit_shipping: {transmit shipping response}
186
+ }
187
+
188
+ Error Code:
189
+
190
+ {
191
+ create_shipping: {errors: 'comma separated error essages'},
192
+ transmit_shipping: {errors: 'comma separated error messages'}
193
+ }
194
+ ```
195
+
196
+ ### Create shipping on behalf of
197
+
198
+ ```ruby
199
+ Pass additional information to create shipment on behalf of merchant.
200
+ Merchant information can be retrieved after registering merchant in your platform.
201
+
202
+ canada_post_service.create(
203
+ mobo: {
204
+ username: 'xxx',
205
+ password: 'password',
206
+ customer_number: '123456789',
207
+ contract_number: '987654321'
208
+ }
209
+ )
210
+ ```
211
+
212
+ ### Merchant registration
213
+
214
+ Use this call to get a unique registration token (token-id) required to launch a merchant into the Canada Post sign-up process.
215
+
216
+ ```ruby
217
+ @token = canada_post_service.registration
218
+ {'token-id' => '11111111111111111111111'}
219
+ ```
220
+
221
+ With the token-id in hand, complete the registration process by making another POST request to https://www.canadapost.ca/cpotools/apps/drc/merchant with the following fields:
222
+ - return-url // callback url to your application
223
+ - token-id
224
+ - platform-id
225
+
226
+ Canada Post service will redirect the user to the designated callback URL along with the required information to perform shipping transactions for the merchant (username, password, customer_number and contract_number).
227
+
228
+ ### Get shipping price
229
+
230
+ ```ruby
231
+ response = canada_post_service.get_price(shipping_id, mobo = optional)
232
+
233
+ {
234
+ :shipment_price=>{
235
+ :xmlns=>"http://www.canadapost.ca/ws/shipment-v7", :service_code=>"DOM.EP", :base_amount=>"10.21",
236
+ :priced_options=>{
237
+ :priced_option=> {:option_code=>"DC", :option_price=>"0.00"}
238
+ },
239
+ :adjustments=>{
240
+ :adjustment=>{
241
+ :adjustment_code=>"FUELSC", :adjustment_amount=>"0.43"
242
+ }
243
+ },
244
+ :pre_tax_amount=>"10.64", :gst_amount=>"0.53", :pst_amount=>"0.00", :hst_amount=>"0.00", :due_amount=>"11.17", :service_standard=>{
245
+ :am_delivery=>"false", :guaranteed_delivery=>"true", :expected_transmit_time=>"1", :expected_delivery_date=>"2016-01-14"
246
+ },
247
+ :rated_weight=>"2.000"
248
+ }
249
+ }
250
+ ```
251
+
252
+ ### Get shipping details
253
+
254
+ ```ruby
255
+ response = canada_post_service.details(shipping_id, mobo = optional)
256
+
257
+ {:shipment_details=>{:xmlns=>"http://www.canadapost.ca/ws/shipment-v7", :shipment_status=>"created", :final_shipping_point=>"M5X1C0", :shipping_point_id=>"7100", :tracking_pin=>"123456789012", :shipment_detail=>{:group_id=>"5241556", :expected_mailing_date=>"2016-01-13", :delivery_spec=>{:service_code=>"DOM.EP", :sender=>{:name=>"John Doe", :company=>"Apple", :contact_phone=>"343434", :address_details=>{:address_line_1=>"600 blvd Alexandre Taché", :city=>"Gatineau", :prov_state=>"QC", :country_code=>"CA", :postal_zip_code=>"M5X1B8"}}, :destination=>{:name=>"receiver", :company=>"receiver company", :address_details=>{:address_line_1=>"4394 Rue Saint-Denis", :city=>"Montréal", :prov_state=>"QC", :country_code=>"CA", :postal_zip_code=>"H2J2L1"}}, :options=>{:option=>{:option_code=>"DC"}}, :parcel_characteristics=>{:weight=>"2.000", :dimensions=>{:length=>"2.0", :width=>"2.0", :height=>"2.0"}, :unpackaged=>"false", :mailing_tube=>"false", :oversized=>"false"}, :notification=>{:email=>"user@gmail.com", :on_shipment=>"true", :on_exception=>"false", :on_delivery=>"true"}, :print_preferences=>{:output_format=>"8.5x11", :encoding=>"PDF"}, :preferences=>{:show_packing_instructions=>"true", :show_postage_rate=>"false", :show_insured_value=>"true"}, :settlement_info=>{:paid_by_customer=>"0002004381", :contract_id=>"0042708517", :intended_method_of_payment=>"Account"}}}}}
258
+
259
+ ```
260
+
261
+ ### Get shipping label
262
+
263
+ ```ruby
264
+ canada_post_service.get_label(label_url)
265
+ ```
266
+ this return a pdf response with label details.
267
+
268
+ ### Get manifest
269
+
270
+ ```ruby
271
+ response = canada_post_service.get_manifest(manifest_url)
272
+
273
+ {:manifest=>{:xmlns=>"http://www.canadapost.ca/ws/manifest-v7", :po_number=>"P123456789", :links=>{:link=>[{:rel=>"self", :href=>"https://ct.soa-gw.canadapost.ca/rs/0002004381/0002004381/manifest/96011452532284803", :media_type=>"application/vnd.cpc.manifest-v7+xml"}, {:rel=>"details", :href=>"https://ct.soa-gw.canadapost.ca/rs/0002004381/0002004381/manifest/96011452532284803/details", :media_type=>"application/vnd.cpc.manifest-v7+xml"}, {:rel=>"manifestShipments", :href=>"https://ct.soa-gw.canadapost.ca/rs/0002004381/0002004381/shipment?manifestId=96011452532284803", :media_type=>"application/vnd.cpc.shipment-v7+xml"}, {:rel=>"artifact", :href=>"https://ct.soa-gw.canadapost.ca/ers/artifact/6e93d53968881714/400811/0", :media_type=>"application/pdf"}]}}}
274
+ ```
275
+
276
+ ### Get artifact
277
+
278
+ ```ruby
279
+ response = canada_post_service.get_artifact(artifact_url)
280
+ Response:
281
+ {
282
+ status: true,
283
+ artifact: artifact // pdf response
284
+ }
285
+
286
+ Error:
287
+ {
288
+ status: false,
289
+ error: 'artifact error message'
290
+ }
291
+ ```
292
+
293
+ ### Void shipping
294
+
295
+ ```ruby
296
+ response = canada_post_service.void_shipping(shipping_id, mobo = optional)
297
+ Error: {
298
+ status: false,
299
+ error: 'void shipping error'
300
+ }
301
+ ```
302
+
303
+ This is still a work in progress but feel free to contribute if it will benefit you!
304
+
305
+ ## Contributing
306
+
307
+ 1. Fork it ( https://github.com/shipppit/shipppit-canada-post/fork )
308
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
309
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
310
+ 4. Run test suite `bundle exec rspec spec`
311
+ 5. Push to the branch (`git push origin my-new-feature`)
312
+ 6. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'canada_post/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shipppit-canada-post"
8
+ spec.version = CanadaPost::VERSION
9
+ spec.authors = ["Olivier"]
10
+ spec.email = ["olivier@yafoy.com"]
11
+ spec.summary = %q{Canada Post API}
12
+ spec.description = %q{Ruby wrapper for the Canada Post API V3}
13
+ spec.homepage = "https://github.com/shipppit/shipppit-canada-post"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httparty", "~> 0.13.7"
22
+ spec.add_dependency "nokogiri", '~> 1.6', '>= 1.6.7.1'
23
+ spec.add_dependency "activesupport", "~> 4.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+
28
+ # FOR TESTING ONLY
29
+ spec.add_development_dependency "rspec", "~> 3.4"
30
+ spec.add_development_dependency "webmock", "~> 1.22"
31
+ spec.add_development_dependency "vcr", "~> 3.0"
32
+ end
@@ -0,0 +1,14 @@
1
+ require "canada_post/version"
2
+ require "canada_post/request/base"
3
+ require "canada_post/request/rate"
4
+ require "canada_post/request/shipping"
5
+ require "canada_post/request/registration"
6
+ require "canada_post/shipment"
7
+ require "canada_post/rate"
8
+ require "canada_post/credentials"
9
+
10
+ module CanadaPost
11
+ # Exceptions: CandaPost::RateError
12
+ class RateError < StandardError; end
13
+ class ShipmentError < StandardError; end
14
+ end
@@ -0,0 +1,45 @@
1
+ module CanadaPost
2
+ class Client
3
+
4
+ def initialize(options={})
5
+ @credentials = Credentials.new(options)
6
+ end
7
+
8
+ def rate(options={})
9
+ Request::Rate.new(@credentials, options).process_request
10
+ end
11
+
12
+ def shipment(options={})
13
+ Request::Shipment.new(@credentials, options).process_request
14
+ end
15
+
16
+ def create(options = {})
17
+ Request::Shipping.new(@credentials, options).process_request
18
+ end
19
+
20
+ def get_price(shipping_id, mobo = @credentials.customer_number)
21
+ Request::Shipping.new(@credentials).get_price(shipping_id, mobo)
22
+ end
23
+
24
+ def get_label(label_url)
25
+ Request::Shipping.new(@credentials).get_label(label_url)
26
+ end
27
+
28
+ def details(shipping_id, mobo = @credentials.customer_number)
29
+ Request::Shipping.new(@credentials).details(shipping_id, mobo)
30
+ end
31
+
32
+ def void_shipment(shipping_id, mobo = @credentials.customer_number)
33
+ Request::Shipping.new(@credentials).void_shipping(shipping_id, mobo)
34
+ end
35
+
36
+ def manifest(options={})
37
+ Request::Manifest.new(@credentials, options).process_request
38
+ end
39
+
40
+ def registration_token
41
+ Request::Registration.new(@credentials).get_token
42
+ end
43
+
44
+ end
45
+ end