How is the DID encoded in the INVITE request?
Are you aware that chan_sip is not supported by Sangoma, and will eventually be removed entirely?
What the SIP community mis-names DIDs are places where calls are going, not coming from.
I believe that the most common way of providing the dialled number indication, from SIP providers, is the To header, so you will need to us the appropriate functions to extract that header and parse out the relevant part of the SIP URI, then probably use GoToIf or ExecIf, to either conditionally set the data part of the dial string, or to select an appropriate Dial() instance. I guess you might get away with one line, but the code will be easier to read if you do it a step at a time.
I believe that FreePBX has standard code for extracting this information, although it probably assumes the To URI. I don’t if it does it in pure dialplan, or uses AGI.
This article may help if the DID is encoded in the To header: