forked from kirill-zhirnov/boundless-checkout-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3fc759
commit b0f80cd
Showing
11 changed files
with
1,184 additions
and
152 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
import { XMLParser, XMLBuilder } from "fast-xml-parser"; | ||
import { XMLParser } from "fast-xml-parser"; | ||
|
||
export const canadaShippingRate = async (zip: string): Promise<any> => { | ||
// @todo: should not have hardcoded customer number, weight and authorization | ||
export const canadaShippingRate = async (zip: string, weight: number): Promise<any> => { | ||
const XMLdata = ` | ||
<mailing-scenario xmlns=”http://www.canadapost.ca/ws/ship/rate-v4”> | ||
<customer-number>0009861747</customer-number> | ||
<mailing-scenario xmlns="http://www.canadapost.ca/ws/ship/rate-v3"> | ||
<customer-number>${process.env.CP_CUSTOMER_ID}</customer-number> | ||
<services> | ||
<service-code>DOM.RP</service-code> | ||
</services> | ||
<origin-postal-code>V2A5K6</origin-postal-code> | ||
<parcel-characteristics><weight>1.0</weight></parcel-characteristics> | ||
<destination><domestic><postal-code>${zip}</postal-code></domestic></destination> | ||
<parcel-characteristics><weight>${weight}</weight></parcel-characteristics> | ||
<destination><domestic><postal-code>${zip.trim().replace(/\s+/g, '').toUpperCase()}</postal-code></domestic></destination> | ||
</mailing-scenario>`; | ||
const parser = new XMLParser(); | ||
let result = parser.parse(XMLdata); | ||
console.log(JSON.stringify(result, null,4)); | ||
const builder = new XMLBuilder(); | ||
const output = builder.build(result); | ||
const rates = await fetch('https://soa-gw.canadapost.ca/rs/ship/price', { | ||
method: 'POST', | ||
mode: 'no-cors', | ||
headers: { | ||
'Content-Type': 'application/vnd.cpc.ship.rate-v4+xml', | ||
'Accept': 'application/vnd.cpc.ship.rate-v4+xml', | ||
'Authorization': 'Basic ' + btoa('c68ad9c01ff34064:4e5b13b80727ba8364f566'), | ||
'Accept-language': 'en-CA' | ||
}, | ||
body: output | ||
}) | ||
// @todo: implement proper error handling and handle response | ||
return rates | ||
|
||
try { | ||
const headers = new Headers(); | ||
headers.append("Accept", "application/vnd.cpc.ship.rate-v3+xml"); | ||
headers.append("Content-Type", "application/vnd.cpc.ship.rate-v3+xml"); | ||
if (process.env.CP_API_KEY) headers.append("Authorization", "Basic " + btoa(process.env.CP_API_KEY)); | ||
headers.append("Accept-language", "en-CA"); | ||
|
||
const requestOptions = { | ||
method: "POST", | ||
mode: "no-cors" as RequestMode, | ||
headers: headers, | ||
body: XMLdata | ||
}; | ||
|
||
const resp = await fetch("https://soa-gw.canadapost.ca/rs/ship/price", requestOptions) | ||
.then(async (response) => parser.parse(await response.text())) | ||
.then((result) => { | ||
return result | ||
}) | ||
.catch((error) => console.error(error)); | ||
return resp; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.