Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active October 24, 2023 13:50
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johntran/64ffd9374c958971587dc2f1c2a5f23e to your computer and use it in GitHub Desktop.
Save johntran/64ffd9374c958971587dc2f1c2a5f23e to your computer and use it in GitHub Desktop.
ES7 async/await with SOAP requests in NodeJS
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
Copy link

dardanos commented Aug 1, 2020

@johntran you are the real MVP

@johntran
Copy link
Author

johntran commented Aug 1, 2020

@dardanos Good luck dealing with ShipCompliant / SOAP Requests. lol.

@alu0100891843
Copy link

Many thanks @johntran!

@re8260
Copy link

re8260 commented Dec 13, 2020

@johntran Thanks !

@NathanD19
Copy link

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