Last active
October 24, 2023 13:50
-
-
Save johntran/64ffd9374c958971587dc2f1c2a5f23e to your computer and use it in GitHub Desktop.
ES7 async/await with SOAP requests in NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import soap from 'soap'; | |
import moment from 'moment'; | |
const taxServiceUrl = 'http://hehexd.com/services/1.2/taxservice.asmx?WSDL' | |
export function generateJsonForTaxRate(json) { | |
const EffectiveDate = moment().toISOString(); | |
const { | |
city: City, | |
state: State, | |
addressLineOne: Street1, | |
addressLineTwo: Street2, | |
Zip1, | |
Username, | |
Password, | |
PartnerKey | |
} = json; | |
return ({ | |
'Request': { | |
'Security': { | |
PartnerKey, | |
Password, | |
Username, | |
}, | |
'Address': { | |
City, | |
State, | |
Street1, | |
Street2, | |
Zip1, | |
}, | |
EffectiveDate, | |
'TaxSaleType': 'Offsite' | |
} | |
}); | |
} | |
export function getTaxRate(xml) { | |
return new Promise ((resolve, reject) => { | |
soap.createClient(taxServiceUrl, (err, client) => { | |
client.GetSalesTaxRatesByAddress(xml, (err, result, body) => { | |
const wineTax = result['GetSalesTaxRatesByAddressResult']['TaxRates']['WineSalesTaxPercent']; | |
return resolve(wineTax) | |
}) | |
}); | |
}); | |
} | |
export async function shipCompliantTaxRateGet(request, response) { | |
try { | |
const { zip } = request.body; | |
const json = generateJsonForTaxRate(zip) | |
const wineTax = await getTaxRate(json); | |
response.status(200).json({ wineTax }); | |
} catch (error) { | |
response.status(200).json({ error: 'No Results' }); | |
} | |
} |
@dardanos Good luck dealing with ShipCompliant / SOAP Requests. lol.
Many thanks @johntran!
@johntran Thanks !
Legend! FedEx SOAP is a nightmare at times...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@johntran you are the real MVP