-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSPS.php
executable file
·176 lines (116 loc) · 3.77 KB
/
USPS.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
<?
class USPS extends Shipping{
var $server = 'http://production.shippingapis.com/ShippingAPI.dll';
var $docURL = 'http://www.usps.com/webtools/htm';
var $userID;
var $packages;
var $error;
function USPS($params){
$this->userID = $params[0];
}
/* UTILITY METHODS */
function addPackage($data){
$data = $this->_verifyRequiredPackageData($data);
if($data) $this->packages[] = $data;
}
/* SERVICE METHODS */
function getRate(){
if(!$this->packages){
$this->error("No packages added");
return;
}
$map = $this->_getRateMap();
$xml = '<RateV3Request USERID="'.$this->userID.'" PASSWORD="">';
foreach($this->packages as $package => $data){
$xml .= '<Package ID="'.$package.'">';
foreach($map as $node => $field){
if(array_key_exists($field, $data)){
$value = $data[$field];
$xml .= '<'.$node.'>'.$value.'</'.$node.'>';
}
}
$xml .= '</Package>';
}
$xml .= '</RateV3Request>';
$request = 'API=RateV3&XML='.$xml;
$response = parent::send($this->server, $request);
// Check For Connection Error
if(isset($response['ERROR'])){
$this->_responseError($response);
return;
}
$response = $response['RATEV3RESPONSE'][0]['PACKAGE'];
$retval = array();
$x = 0;
foreach($response as $package){
// Check For Error
if(isset($package['ERROR'])){
$this->_responseError($package);
continue;
}
foreach($package['POSTAGE'] as $rate){
$retval[$x][$rate['MAILSERVICE'][0]['VALUE']]['rate'] = $rate['RATE'][0]['VALUE'];
$retval[$x][$rate['MAILSERVICE'][0]['VALUE']]['commitmentDate'] = isset($rate['COMMITMENTDATE']) ? $rate['COMMITMENTDATE'][0]['VALUE'] : '';
}
$x++;
}
if(count($retval) == 1) $retval = $retval[0];
return $retval;
}
/* VERIFICATION METHODS */
function _verifyRequiredPackageData($data){
$retval = array();
$required = array('service' => 'All',
'fromZip' => '',
'destZip' => '',
'pounds' => 0,
'ounces' => 0,
'size' => 'Regular',
'machinable' => 'true',
'type' => 'parcel',
'shipDate' => date('d-M-Y', mktime(0,0,0,date('m'), date('d')+1,date('Y'))) /* Defaults to tomorrow */
);
foreach($required as $key => $default){
$value = array_key_exists($key, $data) ? $data[$key] : $required[$key];
$retval[$key] = $value;
}
if(strtoupper($retval['service']) != 'FIRST CLASS') unset($retval['type']);
return $retval;
}
/* FIELD MAPPING */
function _getRateMap(){
$data = array('Service' => 'service' ,
'FirstClassMailType' => 'type' ,
'ZipOrigination' => 'fromZip' ,
'ZipDestination' => 'destZip' ,
'Pounds' => 'pounds' ,
'Ounces' => 'ounces' ,
'Size' => 'size' ,
'Width' => 'width' ,
'Height' => 'height' ,
'Length' => 'length' ,
'Girth' => 'girth' ,
'Machinable' => 'machinable',
'ShipDate' => 'shipDate' ,
'Container' => 'container'
);
return $data;
}
/* ERROR HANDLING */
function _responseError($response){
if(isset($response['ERROR'])) {
$msg = '';
$error = array();
$error['Number'] = $response['ERROR'][0]['NUMBER'][0]['VALUE'];
$error['Source'] = $response['ERROR'][0]['SOURCE'][0]['VALUE'];
$error['Description'] = $response['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
foreach($error as $key => $value){
$msg .= '<strong>'.$key.'</strong> - '.$value.'<br>';
}
$this->error($msg);
return true;
}
else return false;
}
}
?>