-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.bal
108 lines (98 loc) · 4.36 KB
/
main.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import ballerina/http;
import ballerina/log;
const SAP_REQUEST_PATH = "/purchaseorder/0001/PurchaseOrder";
const SAP_URL = "https://my401785.s4hana.cloud.sap/sap/opu/odata4/sap/api_purchaseorder_2/srvd_a2x/sap/";
const HTTP_STATUS_OK = 200;
const HTTP_STATUS_CREATED = 201;
const HEADER_KEY_CSRF_TOKEN = "x-csrf-token";
const HEADER_FETCH_VALUE = "fetch";
map<string> supplierMap = {"7336631533890": "17300096"};
map<string> organizationMap = {"US Steel Corp": "1710"};
map<string> groupMap = {"US Steel Corp": "001"};
map<string> plantMap = {"US Steel Corp": "1710"};
map<string> materialMap = {"8882299109698": "E001"};
map<string> taxJurisdictionMap = {"United States": "KY00000000"};
final http:Client sapHttpClient = check getSAPHttpClient();
configurable SAPAuthConfig sapAuthConfig = ?;
service /sap_bridge on new http:Listener(9090) {
resource function post orders(@http:Payload ShopifyOrder shopifyOrder) {
log:printInfo("Received order with confirmation number: " + shopifyOrder.confirmation_number);
SAPPurchaseOrder|error sapOrder = transformCustomerData(shopifyOrder);
if sapOrder is error {
log:printError("Error while transforming order: " + sapOrder.message());
return;
}
error? sapResponse = createSAPSalesOrder(sapOrder);
if sapResponse is error {
log:printError("Error while creating SAP order: " + sapResponse.message());
}
}
}
isolated function getSAPHttpClient() returns http:Client|error {
http:CredentialsConfig basicAuthHandler = {username: sapAuthConfig.username, password: sapAuthConfig.password};
return new (url = SAP_URL, auth = basicAuthHandler, cookieConfig = {enabled: true});
}
function transformCustomerData(ShopifyOrder shopifyOrder) returns SAPPurchaseOrder|error {
string purchaseOrderType = "NB";
string supplier = supplierMap.get(shopifyOrder.customer.id.toString());
string company = shopifyOrder.customer.default_address.company;
string organization = organizationMap.get(company);
string group = groupMap.get(company);
string companyCode = organizationMap.get(company);
string plant = organizationMap.get(company);
string currency = shopifyOrder.currency;
string taxJurisdiction = taxJurisdictionMap.get(shopifyOrder.customer.default_address.country);
OrderItem[] orderItems = from LineItem lineItem in shopifyOrder.line_items
select
{
Plant: plant,
OrderQuantity: lineItem.quantity,
PurchaseOrderQuantityUnit: "kg",
AccountAssignmentCategory: "U",
Material: materialMap.get(lineItem.product_id.toString()),
NetPriceAmount: check float:fromString(lineItem.price),
DocumentCurrency: currency,
TaxJurisdiction: taxJurisdiction
};
return {
PurchaseOrderType: purchaseOrderType,
Supplier: supplier,
PurchasingOrganization: organization,
PurchasingGroup: group,
CompanyCode: companyCode,
_PurchaseOrderItem: orderItems
};
}
isolated function createSAPSalesOrder(SAPPurchaseOrder sapOrder) returns error? {
string csrfToken = check getCsrfToken();
map<string|string[]> headerParams = {[HEADER_KEY_CSRF_TOKEN] : csrfToken};
http:Response|http:ClientError response = sapHttpClient->post(
path = SAP_REQUEST_PATH,
message = sapOrder,
headers = headerParams
);
if response is http:Response {
if response.statusCode == HTTP_STATUS_CREATED {
json responseBody = check response.getJsonPayload();
string purchaseOrderId = check responseBody.PurchaseOrder;
log:printInfo("Successfully created an SAP purchase order with id: " + purchaseOrderId);
return;
}
log:printError("Error: " + check response.getTextPayload());
return;
}
log:printError("Error: " + response.message());
}
isolated function getCsrfToken() returns string|error {
http:Response|http:ClientError response = sapHttpClient->get(
path = SAP_REQUEST_PATH,
headers = {[HEADER_KEY_CSRF_TOKEN] : HEADER_FETCH_VALUE}
);
if response is http:Response {
if response.statusCode == HTTP_STATUS_OK {
return (check response.getHeaders(HEADER_KEY_CSRF_TOKEN))[0];
}
return error(string `Error: ${response.statusCode}`);
}
return response;
}