forked from VinceG/USPS-php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSPSRate.php
209 lines (206 loc) · 6.07 KB
/
USPSRate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Load required classes
*/
require_once('USPSBase.php');
/**
* USPS Rate calculator class
* used to get a rate for shipping methods
* @since 1.0
* @author Vincent Gabriel
*/
class USPSRate extends USPSBase {
/**
* @var string - the api version used for this type of call
*/
protected $apiVersion = 'RateV4';
/**
* @var array - list of all addresses added so far
*/
protected $packages = array();
/**
* Perform the API call
* @return string
*/
public function getRate() {
return $this->doRequest();
}
/**
* returns array of all packages added so far
* @return array
*/
public function getPostFields() {
return $this->packages;
}
/**
* sets the type of call to perform domestic or international
* @return array
*/
public function setInternationalCall($status) {
$this->apiVersion = $status === true ? 'IntlRateV2' : 'RateV4';
}
/**
* Add other option for International & Insurance
* @param string|int $key
* @param string|int $value
*/
public function addExtraOption(USPSRatePackage $data, $key, $value) {
$this->packages[$key][] = $value;
}
/**
* Add Package to the stack
* @param USPSRatePackage object $data
* @param string $id the address unique id
* @return void
*/
public function addPackage(USPSRatePackage $data, $id=null) {
$packageId = $id !== null ? $id : ((count($this->packages)+1));
$this->packages['Package'][] = array_merge(array('@attributes' => array('ID' => $packageId)), $data->getPackageInfo());
}
}
/**
* USPS Rate Package
* used by the ups rate class to create packages represented as objects
* @since 1.0
* @author Vincent Gabriel
*/
class USPSRatePackage extends USPSRate {
/**
* @var array - list of all packages added so far
*/
protected $packageInfo = array();
/**
* Services constants
*/
const SERVICE_FIRST_CLASS = 'FIRST CLASS';
const SERVICE_FIRST_CLASS_COMMERCIAL = 'FIRST CLASS COMMERCIAL';
const SERVICE_FIRST_CLASS_HFP_COMMERCIAL = 'FIRST CLASS HFP COMMERCIAL';
const SERVICE_PRIORITY = 'PRIORITY';
const SERVICE_PRIORITY_COMMERCIAL = 'PRIORITY COMMERCIAL';
const SERVICE_PRIORITY_HFP_COMMERCIAL = 'PRIORITY HFP COMMERCIAL';
const SERVICE_EXPRESS = 'EXPRESS';
const SERVICE_EXPRESS_COMMERCIAL = 'EXPRESS COMMERCIAL';
const SERVICE_EXPRESS_SH = 'EXPRESS SH';
const SERVICE_EXPRESS_SH_COMMERCIAL = 'EXPRESS SH COMMERCIAL';
const SERVICE_EXPRESS_HFP = 'EXPRESS HFP';
const SERVICE_EXPRESS_HFP_COMMERCIAL = 'EXPRESS HFP COMMERCIAL';
const SERVICE_PARCEL = 'PARCEL';
const SERVICE_MEDIA = 'MEDIA';
const SERVICE_LIBRARY = 'LIBRARY';
const SERVICE_ALL = 'ALL';
const SERVICE_ONLINE = 'ONLINE';
/**
* First class mail type
* required when you use one of the first class services
*/
const MAIL_TYPE_LETTER = 'LETTER';
const MAIL_TYPE_FLAT = 'FLAT';
const MAIL_TYPE_PARCEL = 'PARCEL';
const MAIL_TYPE_POSTCARD = 'POSTCARD';
const MAIL_TYPE_PACKAGE_SERVICE = 'PACKAGE SERVICE';
/**
* Container constants
*/
const CONTAINER_VARIABLE = 'VARIABLE';
const CONTAINER_FLAT_RATE_ENVELOPE = 'FLAT RATE ENVELOPE';
const CONTAINER_PADDED_FLAT_RATE_ENVELOPE = 'PADDED FLAT RATE ENVELOPE';
const CONTAINER_LEGAL_FLAT_RATE_ENVELOPE = 'LEGAL FLAT RATE ENVELOPE';
const CONTAINER_SM_FLAT_RATE_ENVELOPE = 'SM FLAT RATE ENVELOPE';
const CONTAINER_WINDOW_FLAT_RATE_ENVELOPE = 'WINDOW FLAT RATE ENVELOPE';
const CONTAINER_GIFT_CARD_FLAT_RATE_ENVELOPE = 'GIFT CARD FLAT RATE ENVELOPE';
const CONTAINER_FLAT_RATE_BOX = 'FLAT RATE BOX';
const CONTAINER_SM_FLAT_RATE_BOX = 'SM FLAT RATE BOX';
const CONTAINER_MD_FLAT_RATE_BOX = 'MD FLAT RATE BOX';
const CONTAINER_LG_FLAT_RATE_BOX = 'LG FLAT RATE BOX';
const CONTAINER_REGIONALRATEBOXA = 'REGIONALRATEBOXA';
const CONTAINER_REGIONALRATEBOXB = 'REGIONALRATEBOXB';
const CONTAINER_REGIONALRATEBOXC = 'REGIONALRATEBOXC';
const CONTAINER_RECTANGULAR = 'RECTANGULAR';
const CONTAINER_NONRECTANGULAR = 'NONRECTANGULAR';
/**
* Size constants
*/
const SIZE_LARGE = 'LARGE';
const SIZE_REGULAR = 'REGULAR';
/**
* Set the service property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setService($value) {
return $this->setField('Service', $value);
}
/**
* Set the first class mail type property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setFirstClassMailType($value) {
return $this->setField('FirstClassMailType', $value);
}
/**
* Set the zip origin property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setZipOrigination($value) {
return $this->setField('ZipOrigination', $value);
}
/**
* Set the zip destination property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setZipDestination($value) {
return $this->setField('ZipDestination', $value);
}
/**
* Set the pounds property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setPounds($value) {
return $this->setField('Pounds', $value);
}
/**
* Set the ounces property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setOunces($value) {
return $this->setField('Ounces', $value);
}
/**
* Set the container property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setContainer($value) {
return $this->setField('Container', $value);
}
/**
* Set the size property
* @param string|int $value
* @return object USPSRatePackage
*/
public function setSize($value) {
return $this->setField('Size', $value);
}
/**
* Add an element to the stack
* @param string|int $key
* @param string|int $value
* @return object USPSAddress
*/
public function setField($key, $value) {
$this->packageInfo[ ucwords($key) ] = $value;
return $this;
}
/**
* Returns a list of all the info we gathered so far in the current package object
* @return array
*/
public function getPackageInfo() {
return $this->packageInfo;
}
}