-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpsTrack.dbl
1928 lines (1657 loc) · 60.9 KB
/
UpsTrack.dbl
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;*****************************************************************************
;
; Title: UpsTrack
;
; Description: An example of using the UPS consignment tracking web service
;
; Author: Steve Ives (Synergex Professional Services Group)
;
; Date: 28th September 2005
;
; Versions: Synergy/DE V8.1.3a and higher (requires HTTPS)
;
; Platforms: All platforms (requires OpenSSL to be installed)
;
;*****************************************************************************
;
; This code is supplied as seen and without warranty or support, and is used
; at your own risk. Neither the author or Synergex accept any responsability
; for any loss or damage which may result from the use of this code.
;
;*****************************************************************************
;
; The following code provides an example of using the Synergy XML API and the
; Synergy HTTP API to interact with the "UPS OnLine Tools Tracking" web service.
;
; THIS CODE WILL NOT WORK UNTIL YOU HAVE TAKEN SEVERAL STEPS:
;
; - Sign up for a login at UPS.COM
; - Obtain a UPS "Developer Key" and a UPS "XML Access Key"
; - Install and configure OpenSSL on your system
; - Create an HTTPS "CA File"
; - Setup the environment by specifying various environment variables
;
; Before you can do anything, you will need to sign up for developer access
; at UPS.COM. Once you have obtained a username and password, you next need to
; apply (on-line) for a "developer key" which will be delivered to you by
; e-mail. Once you have your developer key, you will have access to download
; the tools and documentation for the Tracking application. There are two
; variants, an HTTP version, and an XML version - go for the XML version.
; You will also need to obtain an "XML Access Key", again by filling out a
; form on the web site. Once you have your username, password, and XML access
; key, you will need to define them using these environment variables:
;
; UPS_USERID Your UPS user ID (obtained from UPS.COM)
; UPS_PASWORD The password associated with the UPS user id
; UPS_ACCESS_CODE Your XML access code (obtained from UPS.COM)
;
; UPS only exposes their tracking service via HTTPS (SSL), so in order to use
; this software you will need to install and configure the freeware OpenSSL
; product. OpenSSL is available in binary form for some operating systems
; (including OpenVMS and Linux). If you can't find a binary OpenSSL
; distribution for your system then you can download the source code from:
;
; http://www.openssl.org
;
; At the time of writing, Synergy currently uses OpenSSL V0.9.7, however I have
; also tested this software with OpenSSL V0.9.8. If you do have to download
; OpenSSL in source code format, then you will need a Perl interpreter, a C
; compiler, and a macro assembler. Don't panic, all of these things are freely
; available, and although it sounds quite daunting at first, obtaining the tools
; and building OpenSSL is relatively painless. Refer to the installation
; instructions that you will find in text files in the OpenSSL root folder.
; If OpenSSL is installed or configured, you will see a message "HTTPS is not
; available".
;
; In order to use HTTPS as a client, you must provide a "CA File". This is a
; file which contains the public key information for all of the HTTPS
; certification authorities that you are willing to trust. The easiest way
; to so this is to make sure that Internet Explorer has the appropriate
; certificates installed (refer to the developer documentation from UPS for
; more information on this) and then export those certificates to a file which
; you can use with the Synergy HTTPS API. CA files exported from Internet
; explorer are in a different format to that required for use with the Synergy
; API, but you will find instructions on exporting and converting the file in
; the Synergy Language Reference Manual.
;
; If you need to go through a proxy server in order to access the internet then
; you will need to define the proxy server for the HTTP API. This is done with
; these environment variables:
;
; HTTP_PROXY_HOST Host name or IP address of proxy server
; HTTP_PROXY_PORT Proxy server port number
; HTTP_PROXY_SUBNET (may not be needed)
; HTTP_PROXY_LOCAL (may not be needed)
;
; Refer to the Synergy Language Reference manual for further information.
;
;*****************************************************************************
.ifdef UPSTRACK_INCLUDE ;BEGINNING OF INCLUDE SECTION
;*****************************************************************************
.undefine UPSTRACK_INCLUDE
;-----------------------------------------------------------------------------
.ifdef UPSTRACK_INTERNAL
.undefine UPSTRACK_INTERNAL
;
; HTTP configuration. You will need to create a CA file containing appropriate
; SSL certificates for the UPS servers. Threre is a discussion on making sure
; that you have the correct certificates in the developer documentation for the
; UPS OnLine Tools Tracking service. Follow these instructions to install the
; certificates into an IE browser, then refer to the Synergy HTTPS documentation
; for instructions on how to export certificates from IE and use the OpenSSL
; utility to convert them into the correct .pem format. The default CA file
; name is CA_FILE.PEM, but can be chjanged below.
;
.define D_HTTPS_CA_FILE "ca_file.pem" ;HTTPS "CA file"
.define D_HTTPS_LOG_FILE "UpsTrack.log" ;HTTP log file
.define D_HTTPS_TIMEOUT 10 ;HTTP request timeout
;
.endc ;UPSTRACK_INTERNAL
;-----------------------------------------------------------------------------
.ifndef UPSTRACK_DEFINES
.define UPSTRACK_DEFINES
;Shipment field
.define SH_DATA(shipment,field)
& ^m(ups_shipment.field,shipment)
;Number of packages in a shipment
.define SH_PKGCNT(shipment)
& SH_DATA(shipment,sh_package_count)
;Packages data handle in a shipment
.define SH_PKGS(shipment)
& SH_DATA(shipment,sh_packages)
;A package record
.define PK_REC(shipment,pkgno)
& ^m(ups_package[pkgno],SH_PKGS(shipment))
;A package field
.define PK_DATA(shipment,pkgno,field)
& ^m(ups_package[pkgno].field,SH_PKGS(shipment))
;Number of activities for a package
.define PK_ACTCNT(shipment,pkgno)
& PK_DATA(shipment,pkgno,pk_activity_count)
;Activities data handle in a package
.define PK_ACTS(shipment,pkgno)
& PK_DATA(shipment,pkgno,pk_activities)
;An activity record
.define AC_REC(shipment,pkgno,acno)
& ^m(ups_activity[acno],PK_ACTS(shipment,pkgno))
;An activity field
.define AC_DATA(shipment,pkgno,acno,field)
& ^m(ups_activity[acno].field,PK_ACTS(shipment,pkgno))
.endc
external function
CanadianProvince ,a
FormatDate ,a
FormatTime ,a
IsoToCountry ,a
UpsPackageType ,a
UpsPickupType ,a
UsState ,a
structure ups_shipment ;A shipment
sh_shipmentid ,a35 ;Shipment identification (tracking) #
sh_shipper ,a10 ;UPS shipper number
sh_from_add1 ,a35 ;Shipper address
sh_from_add2 ,a35 ;Address 2
sh_from_add3 ,a35 ;Address 3
sh_from_city ,a30 ;City
sh_from_state ,a5 ;State or province code
sh_from_zip ,a16 ;Zip or postal code
sh_from_cntry ,a3 ;ISO country code
sh_to_add1 ,a35 ;Ship to address
sh_to_add2 ,a35 ;Address 2
sh_to_add3 ,a35 ;Address 3
sh_to_city ,a30 ;City
sh_to_state ,a5 ;State or province code
sh_to_zip ,a16 ;Zip or postal code
sh_to_cntry ,a3 ;ISO country code
sh_service_code ,a3 ;UPS service code
sh_service_desc ,a35 ;Ups service description
sh_refcode ,a2 ;Reference number type code
sh_refval ,a35 ;Reference number value
sh_pickupdate ,d8 ;Pickup date
sh_sched_deldate ,d8 ;Scheduled delivery date
sh_sched_deltime ,d6 ;Scheduled delivery time
sh_resched_deldate ,d8 ;Rescheduled delivery date
sh_resched_deltime ,d6 ;Rescheduled delivery time
group sh_ivars,i
sh_package_count ,i4 ;Number of packages in shipment
sh_packages ,D_HANDLE ;Packages data handle
endgroup
;A UPS shipment contains one or more packages
structure ups_package ;A package
pk_trackno ,a21 ;Tracking number
pk_weight ,d9.2 ;Weight
pk_weight_uom ,a3 ;Unit of measure, default "LBS"
pk_weight_uom_desc ,a35 ;Unit of measure description
pk_refcode ,a2 ;Reference number type code
pk_refvalue ,a35 ;Reference number value
group pk_ivars ,i
pk_activity_count ,i4 ;Number of activities for this package
pk_activities ,D_HANDLE ;Activity data handle
endgroup
;A UPS package may have one or more activities
structure ups_activity ;An activity
ac_add1 ,a30 ;Address 1
ac_add2 ,a30 ;Address 2
ac_add3 ,a30 ;Address 3
ac_city ,a30 ;City
ac_state ,a5 ;State or province
ac_zip ,a16 ;Zip or postal code
ac_country ,a3 ;ISO country code
ac_lcode ,a2 ;Activity location code
ac_ldesc ,a30 ;Activity location description
ac_lsigned ,a15 ;Signed for by name
ac_status ,a1 ;Status type code
ac_stdesc ,a80 ;Status type description
ac_stcode ,a2 ;Status code (more detail)
ac_date ,d8 ;Activity date
ac_time ,d6 ;Activity time
;*****************************************************************************
.else ;UPSTRACK_INCLUDE ;END OF INCLUDE SECTION, BEGINNING OF PROGRAM CODE
;*****************************************************************************
;=============================================================================
;
; Title: UpsTrackTest
;
; Description: An example of using the UPS consignment tracking web service
;
; Author: Steve Ives (Synergex Professional Services Group)
;
; Date: 28th September 2005
;
;=============================================================================
;
.main UpsTrackTest
.define UPSTRACK_INCLUDE
.include "INC:UpsTrack.dbl"
record ivars
ok ,i4 ;OK to continue
tt ,i4 ;Terminal channel
status ,i4 ;Status from tracking routine
ups_error ,i4 ;UPS error number
shipment ,i4 ;Shipment data handle
count ,i4 ;Loop counter
count1 ,i4 ;Loop counter
len ,i4 ;Length of a string
record avars
userid ,a20 ;UPS user id
password ,a20 ;UPS password
accesscode ,a20 ;UPS XML access code
url ,a64 ;URL of server to use
errtxt ,a76 ;Error text
char ,a1 ;A character
trackno ,a18 ;Tracking number to use
tracknos ,[7]a18 ;UPS test tracking numbers
& ,"1Z12345E0291980793" ;2nd day air letter
& ,"1Z12345E6692804405" ;International worldwide express
& ,"1Z12345E0390515214" ;Multiple packages UPS ground
& ,"1Z12345E1392654435" ;Next day air saver letter
& ,"1Z12345E6892410845" ;2nd delivery attempt
& ,"1Z12345E029198079" ;Invalid tracking number
& ,"1Z12345E1591910450" ;Valid but no tracking available
.define D_URL_TEST "https://wwwcie.ups.com/ups.app/xml/Track" ;Testing
.define D_URL_LIVE "https://www.ups.com/ups.app/xml/Track" ;Live
.proc
ok = 1
open(tt=%syn_freechn,i,"tt:")
xcall flags(7004020,1)
writes(tt,"")
writes(tt,"")
writes(tt,"This example uses an API from 2005")
writes(tt," and does not use the current UPS API")
writes(tt,"")
xcall getlog("UPS_USERID",userid,len)
if (!len)
begin
writes(tt,"Environment variable UPS_USERID has not been defined!")
clear ok
end
xcall getlog("UPS_PASSWORD",password,len)
if (!len)
begin
writes(tt,"Environment variable UPS_PASSWORD has not been defined!")
clear ok
end
xcall getlog("UPS_ACCESS_CODE",accesscode,len)
if (!len)
begin
writes(tt,"Environment variable UPS_ACCESS_CODE has not been defined!")
clear ok
end
if (!ok) then
sleep 5
else
begin
repeat
begin
writes(tt,"")
writes(tt,"")
writes(tt,"UPS Tracking")
writes(tt,"")
display(tt,"Test, Live or Exit (T/L/E): ")
accept(tt,char)
upcase char
using char select
("T"), call test
("L"), call live
("E"), exitloop
(),
begin
writes(tt,"")
writes(tt,"Invalid option")
sleep 0.5
end
endusing
end
end
close tt
stop D_EXIT_SUCCESS
test,
url = D_URL_TEST
writes(tt,"")
writes(tt,"UPS Tracking - TEST MODE")
repeat
begin
writes(tt,"")
display(tt,"Tracking number (1-7 or E): ")
accept(tt,char)
upcase char
using char select
("1" thru "7"),
begin
trackno = tracknos[^d(char)]
call track_it
end
("E"),
exitloop
(),
begin
writes(tt,"")
writes(tt,"Invalid option")
sleep 0.5
end
endusing
end
return
live,
url = D_URL_LIVE
writes(tt,"")
writes(tt,"UPS Tracking - LIVE MODE")
repeat
begin
writes(tt,"")
display(tt,"Tracking number (enter to exit): ")
reads(tt,trackno)
if (!trackno)
exitloop
upcase trackno
call track_it
end
return
track_it,
writes(tt,"")
writes(tt,"Contacting UPS...")
status = %UpsTrack(userid,password,accesscode,url,trackno,shipment,ups_error,errtxt)
using status select
(0),
call display_it
(-1),
begin
using ups_error select
(151018), ;Invalid tracking number
writes(tt,%atrim(errtxt))
(151044), ;No tracking information available
writes(tt,%atrim(errtxt))
(),
writes(tt,"UPS error "+%string(ups_error)+" ("+%atrim(errtxt)+")")
endusing
sleep 1
writes(tt,"")
writes(tt,"")
end
(-2),
begin
writes(tt,"You have not configured D_UPS_USERID, "+
& "D_UPS_PASSWORD, or D_UPS_ACCESS_CODE")
sleep 1
writes(tt,"")
writes(tt,"")
end
(),
begin
writes(tt,"Failure, error="+%string(status)+" ("+%atrim(errtxt)+")")
sleep 1
writes(tt,"")
writes(tt,"")
end
endusing
return
display_it,
writes(tt,"")
writes(tt,"Tracking #: "+%atrim(SH_DATA(shipment,sh_shipmentid)))
writes(tt,"Shipper #: "+%atrim(SH_DATA(shipment,sh_shipper)))
writes(tt,"")
writes(tt,"Shipped from:")
writes(tt,SH_DATA(shipment,sh_from_add1))
writes(tt,SH_DATA(shipment,sh_from_add2))
if (SH_DATA(shipment,sh_from_city))
writes(tt,
& %atrim(SH_DATA(shipment,sh_from_city))+", "+
& %atrim(SH_DATA(shipment,sh_from_state))+", "+
& %atrim(SH_DATA(shipment,sh_from_zip)))
writes(tt,%IsoToCountry(SH_DATA(shipment,sh_from_cntry)))
writes(tt,"")
writes(tt,"Shipped to:")
writes(tt,SH_DATA(shipment,sh_to_add1))
writes(tt,SH_DATA(shipment,sh_to_add2))
writes(tt,%atrim(SH_DATA(shipment,sh_to_city))+", "+
& %atrim(SH_DATA(shipment,sh_to_state))+", "+
& %atrim(SH_DATA(shipment,sh_to_zip)))
writes(tt,%IsoToCountry(SH_DATA(shipment,sh_to_cntry)))
writes(tt,"")
writes(tt,"UPS Service: "+%atrim(SH_DATA(shipment,sh_service_desc)))
writes(tt,"Customer Reference:"+%atrim(SH_DATA(shipment,sh_refval)))
writes(tt,"Pickup date: "+%FormatDate(SH_DATA(shipment,sh_pickupdate)))
if (SH_DATA(shipment,sh_sched_deldate)) then
writes(tt,"Scheduled delivery:"+
& %FormatDate(SH_DATA(shipment,sh_sched_deldate))+", "+
& %FormatTime(SH_DATA(shipment,sh_sched_deltime)))
else
writes(tt,"Scheduled delivery:")
if (SH_PKGCNT(shipment))
begin
writes(tt,"")
writes(tt,"Packages")
for count from 1 thru SH_PKGCNT(shipment)
begin
writes(tt,%atrim(PK_DATA(shipment,count,pk_trackno)))
for count1 from 1 thru PK_ACTCNT(shipment,count)
begin
writes(tt," "+
& %FormatDate(AC_DATA(shipment,count,count1,ac_date))+
& " "+
& %FormatTime(AC_DATA(shipment,count,count1,ac_time))+
& " "+
& %atrim(AC_DATA(shipment,count,count1,ac_stdesc(1:43))))
end
end
end
;Release dynamic memory allocated by UpsTrack
xcall UpsTrackRelease(shipment)
return
.end
;=============================================================================
;
; Title: UpsTrack
;
; Description: Function to call the UPS consignment tracking XML web service
;
; Author: Steve Ives (Synergex Professional Services Group)
;
; Date: 28th September 2005
;
;=============================================================================
;
.function UpsTrack, ^val
;Arguments
a_userid ,a
a_password ,a
a_accesscode ,a
a_url ,a
a_tracking_number ,a
a_shipment ,i
a_ups_error ,i
a_errtxt ,a
;End of arguments
;
.include "DBLDIR:synxml.def"
.define UPSTRACK_INCLUDE
.define UPSTRACK_INTERNAL
.include "INC:UpsTrack.dbl"
.align
stack record ivars
retval ,i4 ;Return value
err ,i4 ;Error number
doc ,XML_DOC_TYPE ;An XML document
xmlstr ,XML_STRING_TYPE ;An XML string
parser ,XML_PARSER_TYPE ;An XML parser
root ,XML_ELEM_TYPE ;Root element
rootcount ,i4 ;Loop counter
rootchildren ,XML_ELEMLIST_TYPE ;An XML element list
rootchildcount ,i4 ;Loop counter
rootchild ,XML_ELEM_TYPE ;An element
child ,XML_ELEM_TYPE ;An element
children ,XML_ELEMLIST_TYPE ;An XML element list
count ,i4 ;Loop counter
child1 ,XML_ELEM_TYPE ;An element
children1 ,XML_ELEMLIST_TYPE ;An XML element list
count1 ,i4 ;Loop counter
child2 ,XML_ELEM_TYPE ;An element
children2 ,XML_ELEMLIST_TYPE ;An XML element list
count2 ,i4 ;Loop counter
child3 ,XML_ELEM_TYPE ;An element
children3 ,XML_ELEMLIST_TYPE ;An XML element list
count3 ,i4 ;Loop counter
child4 ,XML_ELEM_TYPE ;An element
children4 ,XML_ELEMLIST_TYPE ;An XML element list
count4 ,i4 ;Loop counter
ah ,D_HANDLE ;Handle for authorization XML
th ,D_HANDLE ;Handle foe track request XML
sh ,D_HANDLE ;Handle for request to send
sl ,i4 ;Length of send data
rh ,D_HANDLE ;Handle for received data
rl ,i4 ;Length of received data
package ,i4 ;Current package number
activity ,i4 ;Current activity number
stack record avars
hdr ,[2]a60
errtxt ,a60 ;Error text
char ,a1 ;A character
elem_name ,a256
elem_text ,a1024
ups_status ,d1
ups_status_text ,a15
ups_error_text ,a50
.proc
;Initialize data to a known state
clear ^i(ivars), avars, a_ups_error, a_errtxt
;Make sure the calling routine has not already allocated memory to
;the shipment and associated handles
xcall UpsTrackRelease(a_shipment)
;Build the access request
doc = %xml_doc_create
.ifdef DBLV83
xcall xml_option("ENCODE",SYNESCAPE_ESCAPE)
.endc
;Name the root element and add attributes
root = %xml_doc_getroot(doc)
xcall xml_elem_setname(root,"AccessRequest")
xcall xml_elem_setattribute(root,"xml:lang","en-US")
child = %xml_elem_create
xcall xml_elem_setname(child,"AccessLicenseNumber")
xcall xml_elem_settext(child,%atrim(a_accesscode))
xcall xml_elem_addchild(root,child)
child = %xml_elem_create
xcall xml_elem_setname(child,"UserId")
xcall xml_elem_settext(child,%atrim(a_userid))
xcall xml_elem_addchild(root,child)
child = %xml_elem_create
xcall xml_elem_setname(child,"Password")
xcall xml_elem_settext(child,%atrim(a_password))
xcall xml_elem_addchild(root,child)
;Get the XML for the auth request into a handle
xmlstr = %xml_doc_tostring(doc,1)
xcall xml_doc_delete(doc)
ah = %mem_proc(DM_ALLOC+DM_BLANK,%xml_string_getsize(xmlstr))
^m(ah) = ^m(%xml_string_gethandle(xmlstr))
xcall xml_string_delete(xmlstr)
;Build the tracking request handle
doc = %xml_doc_create
root = %xml_doc_getroot(doc)
;Set root element name and arrtibutes
xcall xml_elem_setname(root,"TrackRequest")
xcall xml_elem_setattribute(root,"xml:lang","en-US")
;Create child "Request" element
child = %xml_elem_create
xcall xml_elem_setname(child,"Request")
xcall xml_elem_addchild(root,child)
;Now add things under "Request"
root = child
child = %xml_elem_create
xcall xml_elem_setname(child,"TransactionReference")
xcall xml_elem_addchild(root,child)
child2 = %xml_elem_create
xcall xml_elem_setname(child2,"CustomerContext")
xcall xml_elem_settext(child2,%datetime)
xcall xml_elem_addchild(child,child2)
child2 = %xml_elem_create
xcall xml_elem_setname(child2,"XpciVersion")
xcall xml_elem_settext(child2,"1.0001")
xcall xml_elem_addchild(child,child2)
child = %xml_elem_create
xcall xml_elem_setname(child,"RequestAction")
xcall xml_elem_settext(child,"Track")
xcall xml_elem_addchild(root,child)
child = %xml_elem_create
xcall xml_elem_setname(child,"RequestOption")
xcall xml_elem_settext(child,"none")
xcall xml_elem_addchild(root,child)
;Go back to adding things under the main root
root = %xml_doc_getroot(doc)
child = %xml_elem_create
xcall xml_elem_setname(child,"TrackingNumber")
;xcall xml_elem_setname(child,"ShipmentIdentificationNumber")
xcall xml_elem_settext(child,a_tracking_number)
xcall xml_elem_addchild(root,child)
;Get the XML for the tracking request into a handle
xmlstr = %xml_doc_tostring(doc,1)
xcall xml_doc_delete(doc)
th = %mem_proc(DM_ALLOC+DM_BLANK,%xml_string_getsize(xmlstr))
^m(th) = ^m(%xml_string_gethandle(xmlstr))
xcall xml_string_delete(xmlstr)
;Consolidate the two xml documents into one
sl = %mem_proc(DM_GETSIZE,ah)+%mem_proc(DM_GETSIZE,th)
sh=%mem_proc(DM_ALLOC,sl)
^m(sh) = ^m(ah) + ^m(th)
ah = %mem_proc(DM_FREE,ah)
th = %mem_proc(DM_FREE,th)
;Set HTTP headers
hdr[1]="Content-Type: application/x-www-form-urlencoded"
hdr[2]="Content-Length: " + %string(sl)
;Delete the last log file
xcall delet(D_HTTPS_LOG_FILE)
;Call the web service
err = %http_client_post(a_url,D_HTTPS_TIMEOUT,sh,sl,rh,rl,errtxt,hdr,
& D_HTTPS_LOG_FILE,,,,D_HTTPS_CA_FILE,HTTP_RELURI)
;Did it work?
if (err) then
begin
;No
retval=-3
a_errtxt = "HTTP post failed: " + errtxt
end
else
begin
;Yes, did we get any data back?
if (rh && rl)
call parse_results
end
;Clean up
sh = %mem_proc(DM_FREE,sh)
if (rh)
rh = %mem_proc(DM_FREE,rh)
freturn retval
;-----------------------------------------------------------------------------
;
parse_results,
.ifdef DBLV83
xcall xml_option("ENCODE",SYNESCAPE_UNESCAPE)
.endc
xmlstr = %xml_string_create
xcall xml_string_appendhandle(xmlstr,rh,rl)
parser = %xml_parser_create
doc = %xml_parser_parsestring(parser,xmlstr)
xcall xml_string_delete(xmlstr)
if (!doc)
begin
retval=-4
xcall xml_parser_error(parser,errtxt)
a_errtxt = "Failed to parse response XML: " + errtxt
end
xcall xml_parser_delete(parser)
if (!retval)
begin
;Parse <TrackResponse>
root = %xml_doc_getroot(doc)
xcall xml_elem_getname(root,elem_name)
if (elem_name!="TrackResponse")
begin
retval=-5
a_errtxt = "Unexpected response root element: " + elem_name
end
end
if (!retval)
begin
rootchildren = %xml_elem_children(root)
rootchildcount = %xml_elemlist_count(rootchildren)
for rootcount from 1 thru rootchildcount
begin
rootchild = %xml_elemlist_item(rootchildren,rootcount)
xcall xml_elem_getname(rootchild,elem_name)
using elem_name select
("Response"), ;TrackResponse/Response
begin
call parse_response
if (!ups_status)
begin
retval=-1
a_errtxt = ups_error_text
exitloop
end
end
("Shipment"), ;TrackResponse/Shipment
call parse_shipment
endusing
end
end
if (doc)
xcall xml_doc_delete(doc)
return
;-----------------------------------------------------------------------------
;TrackResponse/Response
;
parse_response,
children = %xml_elem_children(rootchild)
for count from 1 thru %xml_elemlist_count(children)
begin
child = %xml_elemlist_item(children,count)
xcall xml_elem_getname(child,elem_name)
xcall xml_elem_gettext(child,elem_text)
using elem_name select
("ResponseStatusCode"),
ups_status = elem_text
("ResponseStatusDescription"),
ups_status_text = elem_text
("Error"),
call parse_ups_error
endusing
end
return
;-----------------------------------------------------------------------------
;TrackResponse/Response/Error
;
parse_ups_error,
children1 = %xml_elem_children(child)
for count1 from 1 thru %xml_elemlist_count(children1)
begin
child1 = %xml_elemlist_item(children1,count1)
xcall xml_elem_getname(child1,elem_name)
xcall xml_elem_gettext(child1,elem_text)
using elem_name select
("ErrorSeverity"),
nop
("ErrorCode"),
a_ups_error = %integer(%atrim(elem_text))
("ErrorDescription"),
ups_error_text = elem_text
endusing
end
return
;-----------------------------------------------------------------------------
;TrackResponse/Shipment
;
parse_shipment,
;We have a shipment element, allocate memory for the returned shipment data
a_shipment=%mem_proc(DM_ALLOC|DM_BLANK|DM_STATIC,^size(ups_shipment))
clear ^i(SH_DATA(a_shipment,sh_ivars))
children = %xml_elem_children(rootchild)
for count from 1 thru %xml_elemlist_count(children)
begin
child = %xml_elemlist_item(children,count)
xcall xml_elem_getname(child,elem_name)
xcall xml_elem_gettext(child,elem_text)
using elem_name select
("Shipper"),
call parse_shipper
("ShipTo"),
call parse_shipto
("Service"),
call parse_service
("ReferenceNumber"),
call parse_referenceNumber
("ShipmentIdentificationNumber"),
SH_DATA(a_shipment,sh_shipmentid) = elem_text
("PickupDate"),
SH_DATA(a_shipment,sh_pickupdate) = %atrim(elem_text)
("ScheduledDeliveryDate"),
SH_DATA(a_shipment,sh_sched_deldate) = %atrim(elem_text)
("ScheduledDeliveryTime"),
SH_DATA(a_shipment,sh_sched_deltime) = %atrim(elem_text)
("RescheduledDeliveryDate"),
SH_DATA(a_shipment,sh_resched_deldate) = %atrim(elem_text)
("RescheduledDeliveryTime"),
SH_DATA(a_shipment,sh_resched_deltime) = %atrim(elem_text)
("Package"),
call parse_package
endusing
end
return
;-----------------------------------------------------------------------------
;TrackResponse/Shipment/Shipper
;
parse_shipper,
children1 = %xml_elem_children(child)
for count1 from 1 thru %xml_elemlist_count(children1)
begin
child1 = %xml_elemlist_item(children1,count1)
xcall xml_elem_getname(child1,elem_name)
using elem_name select
("ShipperNumber"),
begin
xcall xml_elem_gettext(child1,elem_text)
SH_DATA(a_shipment,sh_shipper) = elem_text
end
("Address"),
call parse_shipper_address
endusing
end
return
;-----------------------------------------------------------------------------
;TrackResponse/Shipment/Shipper/Address
;
parse_shipper_address,
children2 = %xml_elem_children(child1)
for count2 from 1 thru %xml_elemlist_count(children2)
begin
child2 = %xml_elemlist_item(children2,count2)
xcall xml_elem_getname(child2,elem_name)
xcall xml_elem_gettext(child2,elem_text)
using elem_name select
("AddressLine1"),
SH_DATA(a_shipment,sh_from_add1) = elem_text
("AddressLine2"),
SH_DATA(a_shipment,sh_from_add2) = elem_text
("AddressLine3"),
SH_DATA(a_shipment,sh_from_add3) = elem_text
("City"),
SH_DATA(a_shipment,sh_from_city) = elem_text
("StateProvince"),
SH_DATA(a_shipment,sh_from_state) = elem_text
("PostalCode"),
SH_DATA(a_shipment,sh_from_zip) = elem_text
("CountryCode"),
SH_DATA(a_shipment,sh_from_cntry) = elem_text
endusing
end
return
;-----------------------------------------------------------------------------
;TrackResponse/Shipment/ShipTo
;
parse_shipto,
children1 = %xml_elem_children(child)
for count1 from 1 thru %xml_elemlist_count(children1)
begin
child1 = %xml_elemlist_item(children1,count1)
xcall xml_elem_getname(child1,elem_name)
using elem_name select
("Address"),
call parse_shipto_address
endusing
end
return
;-----------------------------------------------------------------------------
;TrackResponse/Shipment/ShipTo/Address
;
parse_shipto_address,
children2 = %xml_elem_children(child1)
for count2 from 1 thru %xml_elemlist_count(children2)
begin
child2 = %xml_elemlist_item(children2,count2)
xcall xml_elem_getname(child2,elem_name)
xcall xml_elem_gettext(child2,elem_text)
using elem_name select
("AddressLine1"),
SH_DATA(a_shipment,sh_to_add1) = elem_text
("AddressLine2"),
SH_DATA(a_shipment,sh_to_add2) = elem_text
("AddressLine3"),
SH_DATA(a_shipment,sh_to_add3) = elem_text