Back to EIP
Process Manager
⋯
service /api/v1 on new http:Listener(8080) {
resource function post orders(OrderRequest orderReq) returns error? {
OrderResponse response = check shopify->/admin/api/orders\.json.post(orderReq);
string trackingNumber;
if response.address.country == "United States" {
FedexResponse fedexResp = check createFedexShipment(response);
trackingNumber = fedexResp.trackingNumber;
} else {
DHLResponse dhlResp = check creeateDhlShipment(response);
trackingNumber = dhlResp.trackingNumber;
}
var _ = start sendConfirmationMail(response.address.fullName, response.email, trackingNumber);
}
}
function createFedexShipment(OrderResponse response) returns FedexResponse|error {
ShipmentRequest fedexReq = {
amount: response.total,
currency: response.currency,
personName: response.address.fullName,
email: response.email,
address: {
address1: response.address.address1,
city: response.address.city,
country: response.address.country,
phoneNumber: response.address.phone
}
};
return check fedEx->/api/en\-us/catalog/ship/v1/shipments.post(fedexReq);
}
function creeateDhlShipment(OrderResponse response) returns DHLResponse|error {
ShipmentRequest dhlReq = {
amount: response.total,
currency: response.currency,
personName: response.address.fullName,
email: response.email,
address: {
name: response.address.fullName,
address1: response.address.address1,
city: response.address.city,
country: response.address.country,
phoneNumber: response.address.phone
}
};
return check dhlExpress->/mydhlapi/shipments.post(dhlReq);
}
function sendConfirmationMail(string name, string email, string trackingNumber) returns error? {
string body = string `<p>Hello ${name}!</p><p>Your Order has been shipped. ` +
string `Track your order using ${trackingNumber}</p>`;
var mailReq = {
toInfo: email,
fromInfo: "orders@blackwell.com",
subject: "Order Confirmation",
content: body
};
_ = check sendgrid->/v3/mail/send.post(mailReq, targetType = json);
}