function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Juliver AnoosJuliver Anoos 

test class need heLp.

Hi , i need help. i need a test class for this method from the class name, ASPP_PaymentSource_Controller.cls
@AuraEnabled
    public static Map<String, String> savePS(ASPayment_Source__c ps, String gatewayId){
        System.debug('PS Handed: ' + ps);
        if(ps != null){
            Map<String, String> info = new Map<String, String>();

            Id cardRT = RecordTypeHelper.getRecordTypeIDByName('ASPayment_Source__c', 'Credit_Card');
            Id bankRT = RecordTypeHelper.getRecordTypeIDByName('ASPayment_Source__c', 'Bank');

            if(ps.RecordTypeId == cardRT || ps.RecordTypeId == bankRT){
                try{
                    upsert ps;
                    
                    info.put('message', 'Payment Source successfully created/udpdated.');
                    info.put('id', ps.Id);
                    info.put('isSucess', 'true');

                    return info;
                }catch(Exception e){
                    info.put('message', e.getMessage());
                    info.put('isSucess', 'false');

                    return info;
                }
            }// end if
            
            // Validate if Payment Source have contact or account
            if(ps.PPContact__c == null && ps.Account__c == null){
                info.put('message', 'Payment Source requires either Account or Contact.');
                info.put('isSucess', 'false');

                return info;
            }

            Map<String, String> result = new Map<String, String>();
            
            if(ps.Id != null){
                ASPayment_Source__c oldPS = [Select Id, Name, RecordTypeId, Account__c, 
                                                 PPContact__c, Active__c, Gateway_Payment_Type__c, 
                                                 Card_Name__c, Card_Expiry_Month__c, 
                                                 Card_Expiry_Year__c, token_Card_CCV__c, 
                                                 token_Card_Number__c, Credit_Card_Type__c, 
                                                 Account_BSB__c, Account_Name__c, 
                                                 token_Account_Number__c 
                                             FROM ASPayment_Source__c 
                                             WHERE Id =: ps.Id];
                
                if(oldPS != null){
                    if((ps.Gateway_Payment_Type__c == 'Bank' && (ps.Account_Name__c == oldPS.Account_Name__c && ps.Account_BSB__c == oldPS.Account_BSB__c && ps.token_Account_Number__c == oldPS.token_Account_Number__c)) || 
                        (ps.Gateway_Payment_Type__c == 'Credit Card' && (ps.Card_Name__c == oldPS.Card_Name__c && ps.token_Card_Number__c == oldPS.token_Card_Number__c && ps.Credit_Card_Type__c == oldPS.Credit_Card_Type__c && ps.Card_Expiry_Month__c == oldPS.Card_Expiry_Month__c && ps.Card_Expiry_Year__c == oldPS.Card_Expiry_Year__c && PS.token_Card_CCV__c == oldPS.token_Card_CCV__c))) 
                    {
                        try{
                            update ps;
                            result.put('PaymentSourceID',String.valueOf(ps.Id));
                            result.put('status','200');
                            result.put('message','Payment Source Updated');
                        }catch(Exception e){
                            result.put('status','500');
                            result.put('message',e.getMessage());
                        }
                    } else result = ProcessPayment.setPS(ps, gatewayId);
                } //end of if(oldPS != null)
            }else{
                if(ps.Gateway_Payment_Type__c == 'Credit Card'){
                    ps.Account_Name__c = null;
                    ps.token_Account_Number__c = null;
                    ps.Account_BSB__c = null;
                }else {
                    ps.Card_Name__c = null;
                    ps.token_Card_Number__c  = null;
                    ps.Credit_Card_Type__c = null;
                    ps.Card_Expiry_Month__c = null;
                    ps.Card_Expiry_Year__c = null;
                    ps.Card_CCV__c = null;
                }
                
                result = ProcessPayment.setPS(ps, gatewayId); 
            } 
            
            info.put('message', result.get('message'));
            if(result.get('status') !=  '500'){
                info.put('id', result.get('PaymentSourceID'));
                info.put('isSucess', 'true');
            }else info.put('isSucess', 'false');
            
            return info;    
        }else return null;
    }
thanks in advance..
 
Best Answer chosen by Juliver Anoos
edanna kedanna k
Dear Juliver Anoos,

1. Create 2 methods in your test class.
2. first method  - insert a ASPayment_Source__c record with either Credit_Card or Bank record types and make sure PPContact__c and Account__c are null.
3. second method - update a ASPayment_Source__c record with 'Bank' Account_Name__c and 'Credit Card' Card_Name__c(different values as compared in first method values).
    
Suggest you to post your test class code and which lines of code are not covering!

All Answers

edanna kedanna k
Dear Juliver Anoos,

1. Create 2 methods in your test class.
2. first method  - insert a ASPayment_Source__c record with either Credit_Card or Bank record types and make sure PPContact__c and Account__c are null.
3. second method - update a ASPayment_Source__c record with 'Bank' Account_Name__c and 'Credit Card' Card_Name__c(different values as compared in first method values).
    
Suggest you to post your test class code and which lines of code are not covering!
This was selected as the best answer
Juliver AnoosJuliver Anoos
Thanks . I aLready soLved this problem. :)