0% found this document useful (0 votes)
12 views

Service

The document describes code for handling cart API requests from Magento. It retrieves existing account, lead, and product records by phone, email, or SKU. If existing records are not found, it creates new ones. It then creates order and order item records, linking them to the associated account, lead, and product records.

Uploaded by

Manoranjan S R
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Service

The document describes code for handling cart API requests from Magento. It retrieves existing account, lead, and product records by phone, email, or SKU. If existing records are not found, it creates new ones. It then creates order and order item records, linking them to the associated account, lead, and product records.

Uploaded by

Manoranjan S R
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

public static List<WSMagentoAPIResponseWrapper>

WSMagentoCartAPIServiceClassHandler(String jsonReqBody){

List<WSMagentoCartAPIResponseWrapper> listOfCartAPIResponseWrapper = new


List<WSMagentoCartAPIResponseWrapper>();

Type resultType =
Type.forName('List<WSMagentoCartAPIRequestWrapper>.class');
List<WSMagentoCartAPIRequestWrapper> listOfCartAPIreqWrapWrapper
=(List<WSMagentoCartAPIRequestWrapper>)JSON.deserialize(jsonReqBody, resultType);
System.debug('listOfCartAPIreqWrapWrapper'+listOfCartAPIreqWrapWrapper);
// insert Account, order and orderitem
List<Account> accounts = new List<Account>();

// get Existing Accounts


Set<String> emailSet = new Set<String>();
Set<String> phoneSet = new Set<String>();
Set<String> skuIds = new Set<String>();

if(listOfCartAPIreqWrapWrapper.size() > 0){


for(WSMagentoCartAPIRequestWrapper cart : listOfCartAPIreqWrapWrapper){

System.debug('cart**'+cart);
System.debug('cart.customer**'+cart.customer);

if(!String.isEmpty(cart.customer.email)){
emailSet.add(cart.customer.email);
}

if(!String.isEmpty(cart.billing_address.email)){
emailSet.add(cart.billing_address.email);
}

if(!String.isEmpty(cart.customer.phone)){
phoneSet.add(cart.customer.phone);
}

if(!String.isEmpty(cart.billing_address.telephone)){
phoneSet.add(cart.billing_address.telephone);
}

for (WSMagentoCartAPIRequestWrapper.Item item : cart.items) {

skuIds.add(item.sku);
}
}
}

List<Account> listOfExistingAccounts;
try{
listOfExistingAccounts =
AccountSelector.getAccountByPhoneEmailAndAlternatePhone(phoneSet,emailSet,phoneSet)
;
}catch(Exception e){
LogHandler.logInformation('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage());
LogHandler.logException('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage(), e.getLineNumber());
System.debug('Error Occured'+e.getMessage());
System.debug('Error Occured'+e.getLineNumber());
}

List<sObject> listOfsObjectToUpsertOrderAndOLI = new List<sObject>();


//Map of CartId with Line Item
Map<String,List<Cart_Item__c>> mapOfCartIdWithListOfOrderItem = new
Map<String,List<Cart_Item__c>>();
//Map of Phone+Email with account
Map<String,Account> mapOfPhoneAndEmailWithAccount = new
Map<String,Account>();
// If(listOfExistingAccounts.size() > 0){
for(Account exAcc : listOfExistingAccounts){
if(!mapOfPhoneAndEmailWithAccount.containsKey(exAcc.Phone)){
mapOfPhoneAndEmailWithAccount.put(exAcc.Phone,exAcc);
}
if(!mapOfPhoneAndEmailWithAccount.containsKey(exAcc.PersonEmail)){
mapOfPhoneAndEmailWithAccount.put(exAcc.PersonEmail,exAcc);
}
// feedbac;- missing.
if(!
mapOfPhoneAndEmailWithAccount.containsKey(exAcc.Alternative_Phone__c)){

mapOfPhoneAndEmailWithAccount.put(exAcc.Alternative_Phone__c,exAcc);
}
}

// Pending :- search the lead also and apply same logic if existing
then create a activity of type website cart.
// or else create a new lead with V4 and source as website cart.

List<Lead> listOfExistingLeads;
try{
listOfExistingLeads =
LeadSelector.getLeadByPhoneEmailAndAlternatePhone(phoneSet,emailSet,phoneSet);
}catch(Exception e){
LogHandler.logInformation('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage());
LogHandler.logException('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage(), e.getLineNumber());
System.debug('Error Occured'+e.getMessage());
System.debug('Error Occured'+e.getLineNumber());
}

Map<String,Lead> mapOfPhoneAndEmailWithLead = new Map<String,Lead>();


for(Lead exLead : listOfExistingLeads){
if(!mapOfPhoneAndEmailWithLead.containsKey(exLead.Phone)){
mapOfPhoneAndEmailWithLead.put(exLead.Phone,exLead);
}
if(!mapOfPhoneAndEmailWithLead.containsKey(exLead.Email)){
mapOfPhoneAndEmailWithLead.put(exLead.Email,exLead);
}
// feedbac;- missing.
if(!
mapOfPhoneAndEmailWithLead.containsKey(exLead.Alternative_Phone__c)){
mapOfPhoneAndEmailWithLead.put(exLead.Alternative_Phone__c,exLead);
}
}

RelationshipUtiltiy relUtil = new RelationshipUtiltiy();


List<Task> listOfTask = new List<Task>();
List<Account> newAccLst = new List<Account>();
List<Lead> newLeadLst = new List<Lead>();
List<Cart__c> newOrderLst = new List<Cart__c>(); // Change this to
Cart
List<Cart_Item__c> newOrderItemsLst = new List<Cart_Item__c>(); //
Change this to Cart Item.
// Pricebook2 pb=Utility.getStandardPricebook();
Map<string,Product2>
skuToProductId=Utility.getSKUIdToProductMap(skuIds);

for(WSMagentoCartAPIRequestWrapper reqWrap :
listOfCartAPIreqWrapWrapper){

String custPhoneKey = reqWrap.customer.phone;


String custEmailKey = reqWrap.customer.email;
String billPhoneKey = reqWrap.billing_address.telephone;
String billEmailKey = reqWrap.billing_address.email;

Account acc;
if(!String.isEmpty(custPhoneKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(custPhoneKey)){

acc = mapOfPhoneAndEmailWithAccount.get(custPhoneKey);
}
else if(!String.isEmpty(custEmailKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(custEmailKey)){

acc = mapOfPhoneAndEmailWithAccount.get(custEmailKey);
}
else if(!String.isEmpty(billPhoneKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(billPhoneKey)){

acc = mapOfPhoneAndEmailWithAccount.get(billPhoneKey);
}
else if(!String.isEmpty(billEmailKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(billEmailKey)){

acc = mapOfPhoneAndEmailWithAccount.get(billEmailKey);

}
else{

}
if(acc==null){
acc= New Account();
acc = CartAPIHelper.createAccount(reqWrap);

newAccLst.add(acc);
}// end of acc

Lead ld;
Task task;
if(!String.isEmpty(custPhoneKey) &&
mapOfPhoneAndEmailWithLead.containsKey(custPhoneKey)){

ld = mapOfPhoneAndEmailWithLead.get(custPhoneKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else if(!String.isEmpty(custEmailKey) &&
mapOfPhoneAndEmailWithLead.containsKey(custEmailKey)){

ld = mapOfPhoneAndEmailWithLead.get(custEmailKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else if(!String.isEmpty(billPhoneKey) &&
mapOfPhoneAndEmailWithLead.containsKey(billPhoneKey)){

ld = mapOfPhoneAndEmailWithLead.get(billPhoneKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else if(!String.isEmpty(billEmailKey) &&
mapOfPhoneAndEmailWithLead.containsKey(billEmailKey)){

ld = mapOfPhoneAndEmailWithLead.get(billEmailKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);

}
else{

}
if(ld==null){

ld= new Lead();


ld = CartAPIHelper.createLead(reqWrap);
ld.Priority__c=LeadConstants.PRIORITY_V4;
ld.LeadSource= LeadConstants.Lead_Source_Cart;
if(acc.id!=null){
ld.Account__c = acc.id;
}
else{
relUtil.registerRelationship(ld,acc,Lead.Account__c);
}

newLeadLst.add(ld);
}
Cart__c cartOrder = CartAPIHelper.createOrder(reqWrap,acc); //
pending :- change this to Cart and lead logic.
if(acc.id==null){

relUtil.registerRelationship(cartOrder,acc,Cart__c.Account__c);
}

newOrderLst.add(cartOrder);

for (WSMagentoCartAPIRequestWrapper.Item item : reqWrap.items) {

Cart_Item__c cartItem =
CartAPIHelper.createOrderItem(item,reqWrap.cart_id); // pending :-change this to
Cart Item.

if(skuToProductId.containskey(item.sku.toUppercase())){

cartItem.Product__c=skuToProductId.get(item.sku.toUppercase()).Id; // pending :-
once change to Cart item remove commment.
}
else{
// Need to handle this exception or insert product.

relUtil.registerRelationship(cartItem,cartOrder,Cart_Item__c.Cart__c);

newOrderItemsLst.add(cartItem);
}

try{

if(newAccLst.size()>0){

insert newAccLst;

if(relUtil.relationshipMap.containskey('Lead')){
relUtil.resolve('Lead');
}

if(newLeadLst.size()>0){
upsert newLeadLst Id;
}

if(relUtil.relationshipMap.containskey('Cart__c')){
relUtil.resolve('Cart__c');
}
if(newOrderLst.size()>0){

upsert newOrderLst Cart_Id__c;

relUtil.resolve('Cart_Item__c');

upsert newOrderItemsLst Item_Id__c;

for(Cart__c ord : newOrderLst){

WSMagentoCartAPIResponseWrapper resp = new


WSMagentoCartAPIResponseWrapper();
resp.status ='success';
resp.cart_id=ord.Cart_Id__c;
listOfCartAPIResponseWrapper.add(resp);
}

}catch(Exception e){

// Handle error logs. here


// Also add rollback if transaction fail. and send error in response.

system.debug('***exception '+e.getMessage()+' at line


'+e.getLineNumber());

for(WSMagentoCartAPIRequestWrapper cart : listOfCartAPIreqWrapWrapper){

WSMagentoCartAPIResponseWrapper resp = new


WSMagentoCartAPIResponseWrapper();
resp.status ='fail';
resp.cart_id=cart.cart_id;
resp.response_message='Exception in SF order creation. Error :-
'+e.getMessage()+' at line '+e.getLineNumber();
listOfCartAPIResponseWrapper.add(resp);
}
}

return listOfCartAPIResponseWrapper;

You might also like