File tree Expand file tree Collapse file tree
routes/api/fulfillment/get-label Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { env } from '$env/dynamic/private';
22import xml2js from 'xml2js' ;
33import { PDFDocument } from 'pdf-lib' ;
44import { fetchChitChatsRates } from './chit-chats' ;
5+ import { resolveStateCode } from './countries' ;
56
67export const INCHES_TO_CM = 2.54 ;
78export const GRAMS_TO_KG = 0.001 ;
@@ -194,7 +195,7 @@ export function buildCreateShipmentXml(params: {
194195 <address-line-1>${ escapeXml ( ( order . addressLine1 || '' ) . substring ( 0 , 44 ) ) } </address-line-1>
195196 ${ ( order . addressLine2 || order . addressLine1 ?. length > 44 ) ? `<address-line-2>${ escapeXml ( ( order . addressLine2 || order . addressLine1 ?. substring ( 44 ) || '' ) . substring ( 0 , 44 ) ) } </address-line-2>` : '' }
196197 <city>${ escapeXml ( ( order . city || '' ) . substring ( 0 , 40 ) ) } </city>
197- <prov-state>${ escapeXml ( ( order . stateProvince || '' ) . substring ( 0 , 20 ) ) } </prov-state>
198+ <prov-state>${ escapeXml ( resolveStateCode ( order . stateProvince || '' ) . substring ( 0 , 20 ) ) } </prov-state>
198199 <country-code>${ escapeXml ( order . country ) } </country-code>
199200 <postal-zip-code>${ ( order . postalCode ?? '' ) . replace ( / \s / g, '' ) . toUpperCase ( ) } </postal-zip-code>
200201 </address-details>
Original file line number Diff line number Diff line change @@ -231,7 +231,10 @@ export const POST: RequestHandler = async ({ request, locals }) => {
231231 console . log ( `Creating shipment: country=${ order . country } , estimatedService=${ order . estimatedServiceName } , serviceCode=${ serviceCode } ` ) ;
232232
233233 // Try creating shipment, fall back to alternate international services if unavailable
234- const fallbackCodes = serviceCode === 'INT.TP' ? [ 'INT.XP' ] : serviceCode === 'INT.XP' ? [ 'INT.TP' ] : [ ] ;
234+ // INT.XP requires a contract, so only use it as fallback when contract is available
235+ const fallbackCodes : string [ ] = [ ] ;
236+ if ( serviceCode === 'INT.TP' && env . CP_CONTRACT_ID ) fallbackCodes . push ( 'INT.XP' ) ;
237+ if ( serviceCode === 'INT.XP' ) fallbackCodes . push ( 'INT.TP' ) ;
235238 let lastError : any ;
236239 for ( const code of [ serviceCode , ...fallbackCodes ] ) {
237240 try {
@@ -247,7 +250,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
247250 }
248251 }
249252 if ( lastError ) {
250- throw error ( 502 , `Canada Post shipment creation failed` ) ;
253+ throw error ( 502 , `Canada Post shipment creation failed: ${ lastError . message } ` ) ;
251254 }
252255 }
253256
You can’t perform that action at this time.
0 commit comments