Skip to content

Instantly share code, notes, and snippets.

@emoran
Created March 10, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emoran/6abd9952e48bde0e56c6325b2d038e6d to your computer and use it in GitHub Desktop.
Save emoran/6abd9952e48bde0e56c6325b2d038e6d to your computer and use it in GitHub Desktop.
Sample apex code to make a request to Walmart API service and generate the signature for the WM_SEC.AUTH_SIGNATURE header from Salesforce
/**
* Edgar Moran
* March 2022
*/
public with sharing class Walmart {
@InvocableMethod(label='Send Message')
public static List<String> sendMessage(List<String> message) {
String consumerId ='CONSUMER_ID';
String baseURL ='SERVICE_BASE_URL';
/**
* String encodedPrivateKey [You need to convert your .pem private key to PK8 format using this command: openssl pkcs8 -topk8 -nocrypt -in key.pem -outform PEM
* you need to paste the text between -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY-----]
*/
String privateEncodedKey='CONVERTED_KEY_PK8';
String httpMethod = 'REQUEST_METHOD';
String privateKeyVersion = '1';
String timestamp = String.valueOf(DateTime.now().getTime());
String stringToSign = consumerId + '\n' + timestamp + '\n' + privateKeyVersion + '\n';
String signature = signData(stringToSign,privateEncodedKey);
Walmart.WalmartRequestObject requestBody = new WalmartChatBot.WalmartRequestObject();
requestBody.channelId = 'c1';
requestBody.userId = 'u1';
requestBody.conversationId = 'conv1';
requestBody.verticalId = 'GROCERY';
requestBody.surfaceCapability = 'VOICE';
Walmart.Message messageBot = new WalmartChatBot.Message();
messageBot.typeVar = 'TEXT';
messageBot.query = 'What is the nearest Walmart store?';
requestBody.message = messageBot;
Walmart.Metadata meta = new WalmartChatBot.Metadata();
requestBody.metadata = meta;
Walmart.UserProfile userProfileData = new WalmartChatBot.UserProfile();
userProfileData.key = 'value';
requestBody.userProfile = userProfileData;
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(baseURL);
req.setHeader('Content-Type','application/json');
req.setHeader('WM_CONSUMER.ID',consumerId);
req.setHeader('WM_CONSUMER.INTIMESTAMP',timestamp);
req.setHeader('WM_SEC.AUTH_SIGNATURE',signature);
req.setHeader('WM_SEC.KEY_VERSION','1');
req.setHeader('WM_CONSUMER.ACCOUNT_ID','1111');
req.setMethod('POST');
req.setBody(JSON.serialize(requestBody).replaceAll('typeVar', 'type'));//Replacing the type reserved key here.
httpResponse response = http.send(req);
System.debug('Service Response: '+response.getBody());
return message;
}
/**
* Signs the data to generate a WM_SEC.AUTH_SIGNATURE
* String stringToBeSigned [Ex String stringToSign consumerId + '\n' + timestamp + '\n' + privateKeyVersion + '\n';]
* String encodedPrivateKey [You need to convert your .pem private key to PK8 format using this command: openssl pkcs8 -topk8 -nocrypt -in key.pem -outform PEM
* you need to paste the text between -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY-----]
*/
public static String signData(String stringToBeSigned, String encodedPrivateKey){
Blob encodedKeyBytes = EncodingUtil.base64Decode(encodedPrivateKey);
Blob rs256sig = Crypto.sign('RSA-SHA256', Blob.valueOf(stringToBeSigned), encodedKeyBytes);
return EncodingUtil.base64Encode(rs256sig);
}
public class WalmartRequestObject{
String userId;
String channelId;
String conversationId;
String verticalId;
String surfaceCapability;
Message message;
Metadata metadata;
UserProfile userProfile;
}
public class Message{
String typeVar;
String query;
}
public class Metadata{
}
public class UserProfile{
String key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment