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
Mahesh Tandan 6Mahesh Tandan 6 

Basic Callout in salesforce

Hello guys.
I'm typing to get paypal access token and paypal uses basic auth

how can I post Basic auth in salesforce callouts
do I need to put it in request.setHeader('username','sldkj88***');
or in body request.setBody('username','sldkj88***');
Best Answer chosen by Mahesh Tandan 6
Maharajan CMaharajan C
Hi Mahesh,

Please try the below one:

        string username = 'sldkj88***'; 
        string password = '*********';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);  

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Mahesh,

Please try the below one:

        string username = 'sldkj88***'; 
        string password = '*********';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);  

Thanks,
Maharajan.C
This was selected as the best answer
Mahesh Tandan 6Mahesh Tandan 6
Hello Maharajan.C

Thank you so much for your Help your code really worked for me

I need one more favor from you please help

this is my visualforce code
<apex:page showHeader="false" controller="paypal_Integration">
     <script
    src="https://www.paypal.com/sdk/js?client-id=alsdjlkdj*********"> // Required. Replace SB_CLIENT_ID with your sandbox client ID.
  </script>
    
      <div id="paypal-button-container"></div>
    <apex:form >
        
        <!--<apex:actionFunction name="passStringToController" action="{!myMethod}" />-->
        
    </apex:form>
<script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name);
        


        I want to send this  to my controller how can I do this
        alert('Order Id '+ data.orderID);


        // Call your server to save the transaction
        return fetch('/paypal-transaction-complete', {
          method: 'post',
          headers: {
            'content-type': 'application/json'
          },
          body: JSON.stringify({
            orderID: data.orderID
          })
        });
      });
    }
  }).render('#paypal-button-container');
</script>
</apex:page>
I want to send this to my controller how can I do this
alert('Order Id '+ data.orderID);
 
Maharajan CMaharajan C
Use the Action Function:
 
<apex:page showHeader="false" controller="paypal_Integration">
     <script
    src="https://www.paypal.com/sdk/js?client-id=alsdjlkdj*********"> // Required. Replace SB_CLIENT_ID with your sandbox client ID.
  </script>
    
      <div id="paypal-button-container"></div>
    <apex:form >
        <apex:actionFunction name="passStringToController" action="{!myMethod}">
            <apex:param assignTo="{!passString}" name="prm" value=""/>
        </apex:actionFunction>
        <!--<apex:actionFunction name="passStringToController" action="{!myMethod}" />-->
        
    </apex:form>
<script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name);

        <b>I want to send this  to my controller how can I do this
        alert('Order Id '+ data.orderID);</b>
        passStringToController(data.orderID);        
        

        // Call your server to save the transaction
        return fetch('/paypal-transaction-complete', {
          method: 'post',
          headers: {
            'content-type': 'application/json'
          },
          body: JSON.stringify({
            orderID: data.orderID
          })
        });
      });
    }
  }).render('#paypal-button-container');
</script>
</apex:page>


======================================================

Apex Class:

public class paypal_Integration
{
    public string passString{get;set;}
    
    public void myMethod(){
        System.debug(' MyString==>  ' + passString);
    }
}

https://developer.salesforce.com/forums/?id=906F000000097TTIAY
https://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller
https://salesforce.stackexchange.com/questions/218097/how-to-pass-javascript-variable-value-in-apex-controller

Thanks,
Maharajan.C