@@ -15,6 +15,20 @@ export function escapeXml(str: string): string {
1515 . replace ( / ' / g, ''' ) ;
1616}
1717
18+ /** Format HS code to Canada Post format ####.##.##.## (6, 8 or 10 digits with dots) */
19+ function formatHsTariffCode ( code : string | null | undefined ) : string {
20+ if ( ! code ) return '' ;
21+ const digits = code . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
22+ if ( digits . length < 6 ) return '' ;
23+ // ####.## (6 digits)
24+ let formatted = digits . substring ( 0 , 4 ) + '.' + digits . substring ( 4 , 6 ) ;
25+ // ####.##.## (8 digits)
26+ if ( digits . length >= 8 ) formatted += '.' + digits . substring ( 6 , 8 ) ;
27+ // ####.##.##.## (10 digits)
28+ if ( digits . length >= 10 ) formatted += '.' + digits . substring ( 8 , 10 ) ;
29+ return formatted ;
30+ }
31+
1832async function cropLabelTo4x6 ( pdfBuffer : ArrayBuffer ) : Promise < Uint8Array > {
1933 const srcDoc = await PDFDocument . load ( pdfBuffer ) ;
2034 const page = srcDoc . getPage ( 0 ) ;
@@ -134,7 +148,7 @@ export function buildCreateShipmentXml(params: {
134148 <customs-number-of-units>${ oi . quantity } </customs-number-of-units>
135149 <customs-description>${ escapeXml ( item . name . substring ( 0 , 44 ) ) } </customs-description>
136150 <sku>${ escapeXml ( item . sku || '' ) } </sku>
137- <hs-tariff-code>${ escapeXml ( ( item . hsCode || '' ) . replace ( / [ ^ 0 - 9 ] / g , '' ) ) } </hs-tariff-code>
151+ <hs-tariff-code>${ escapeXml ( formatHsTariffCode ( item . hsCode ) ) } </hs-tariff-code>
138152 <unit-weight>${ unitWeightKg } </unit-weight>
139153 <customs-value-per-unit>${ valuePerUnit . toFixed ( 2 ) } </customs-value-per-unit>
140154 <country-of-origin>CA</country-of-origin>
0 commit comments