# Step 3: Register external services

Register external services for Tax Calculation and Payment Services. You can run these scripts from the Developer Console in an Execute Anonymous window or by the method you choose when registering other services as part of the site setup before starting the connector setup.

{% hint style="warning" %}
**Important:** The Digital River connector currently only supports a Tax Type of Net. Ensure the tax type is set to Net BEFORE activating your store, as you cannot change it afterward.
{% endhint %}

## Tax integration service

The next sections provide details on how to use the tax integration service.

### Using the Cart Calculate API Tax extensions

If you are using the Cart Calculate API extensions, follow these steps to register the Digital River tax extension.

#### Prerequisites

* Ensure that you have [enabled the Cart Calculate API](https://developer.salesforce.com/docs/commerce/salesforce-commerce/guide/cart-calculate-api.html#enable-and-disable-the-cart-calculate-api-for-a-webstore) for the webstore.
* Install the [Salesforce Commerce plugin](https://developer.salesforce.com/docs/commerce/salesforce-commerce/guide/extensions.html#manage-extensions-with-salesforce-cli) for Salesforce CLI.

#### Steps to register

{% hint style="info" %}
**Note:** These instructions assume you are using Salesforce CLI to register the tax integration. It is also possible to register the integration through alternative methods which are not documented here. Refer to salesforce’s documentation if you prefer this approach.
{% endhint %}

1. Run this command to register the tax integration class:

```
sfdx commerce:extension:register --targetusername YOUR_ORG_USERNAME --apex-class-name DRB2B_TaxCalculationServiceCartCalculate --extension-point-name Commerce_Domain_Tax_CartCalculator --registered-extension-name DRB2B_TaxCalculationServiceCartCalculate
```

2. Link the newly registered tax integration to the storefront. From the store’s Administration menu, select Tax Calculation. Under Custom Provider, link the newly registered DRB2B\_TaxCalculationServiceCartCalculate in ‘Extension: Tax - Cart & Checkout Calculator.’

<figure><img src="/files/8PrPLMRvwXQtBuPZCi87" alt=""><figcaption></figcaption></figure>

3. Alternatively you can use Salesforce CLI to map the extension to the store using the following command:

<pre><code><strong>sfdx commerce:extension:map --registered-extension-name DRB2B_TaxCalculationServiceCartCalculate --store-id YOUR_STORE_ID
</strong></code></pre>

### Using the Aura integrations

If you are using the Aura integrations, follow the steps in this section to register the tax integration. This is a case where you **have** **not** registered the external service for tax integration:

1. Use the following script to insert the Tax Calculation Apex class that comes with the package into `RegisteredExternalService`. To use the script, modify the `<<storeName>>` variable before running.

   ```
   // WebStore query values
   String webstoreName = 'DR Sync Store'; // Enter your webstore name
    
   // ApexClass query values
   String apexClassname = 'DRB2B_CartTaxCalculations';
   String status = 'Active';
    
   // RegisteredExternalService insert values
   String registeredProviderType = 'Tax';
   String registeredDevName = 'DR_COMPUTE_TAXES';
   String registeredLabel = registeredDevName;
    
   // StoreIntegratedService insert values
   String devname = registeredDevName;
   String prefix = registeredProviderType;
   String prefixedName = prefix + '__' + devname;
    
   // locate webstore
   WebStore webStore = Database.query('SELECT Id FROM WebStore WHERE Name = :webstoreName LIMIT 1');
   String webStoreId = webStore.Id;
   System.debug('webStoreId:' + webStoreId);
    
   // locate apex class Id
   ApexClass apexClass = Database.query('SELECT Id FROM ApexClass WHERE Status=:status AND Name=:apexClassname LIMIT 1');
   String apexClassId = apexClass.Id;
   System.debug('apexClassId:' + apexClassId);
    
    
   // locate apex in RegisteredExternalService
   String registeredIntegrationId = null;
   try {
       RegisteredExternalService registeredExternalService = Database.query('SELECT Id FROM RegisteredExternalService WHERE ExternalServiceProviderId=:apexClassId AND DeveloperName=:registeredDevName AND ExternalServiceProviderType=:registeredProviderType LIMIT 1');
       registeredIntegrationId = registeredExternalService.Id;
       System.debug('apex class registration: FOUND ' + registeredIntegrationId);
       //delete registeredExternalService; // optionally remove     if needed    
   } 
   catch (QueryException q) {
       System.debug('apex class registration: MISSING ' + apexClassId);
       insert new RegisteredExternalService(DeveloperName = registeredDevName, MasterLabel = registeredLabel, ExternalServiceProviderId = apexClassId, ExternalServiceProviderType = registeredProviderType);
       RegisteredExternalService registeredExternalService = Database.query('SELECT Id FROM RegisteredExternalService WHERE     ExternalServiceProviderId = :apexClassId  LIMIT 1');
       registeredIntegrationId = registeredExternalService.Id;
       System.debug('apex class registration: INSERTED ' +     registeredIntegrationId);
   }
   ```

2. Verify that the `Tax Calculation Apex class ID` is registered with the`soql`query below. The `ExternalServiceProviderId` should match the Tax Calculation Apex Class `DRB2B_CartTaxCalculations` ID in the org.<br>

   <pre data-overflow="wrap"><code>Select Id, ExternalServiceProviderId, ExternalServiceProviderType, DeveloperName From RegisteredExternalService Where ExternalServiceProviderType = 'Tax'

   </code></pre>

3. Link the newly registered tax integration to the storefront. From the store’s Administration menu, select Tax Calculation. Under Integration, link the newly registered `DR_COMPUTE_TAXES` or whatever name you gave the integration in the script in step 2.

If you **have** already registered the external service for tax integration:

1. Run the following script to update the Tax Integration service to point to the Tax Calculation integration class from the Salesforce Lightning app.

   ```
   // Configure Tax Calculation Service
   RegisteredExternalService taxServiceRegistration = [
           SELECT Id
           FROM RegisteredExternalService
           WHERE ExternalServiceProviderType = 'Tax'
           LIMIT 1
   ];

   ApexClass taxCalculationService = [
           SELECT Id
           FROM ApexClass
           WHERE Name = 'DRB2B_CartTaxCalculations'
           LIMIT 1
   ];


   taxServiceRegistration.ExternalServiceProviderId = 
   taxCalculationService.Id;update taxServiceRegistration;
   ```
2. Verify that the `Tax Calculation Apex class ID` is registered by making the below `soql` query. The `ExternalServiceProviderId` should match the Tax Calculation Apex Class `DRB2B_CartTaxCalculations` ID in the org.<br>

   ```
   Select Id, ExternalServiceProviderId, ExternalServiceProviderType, 
   DeveloperName  From RegisteredExternalService Where 
   ExternalServiceProviderType = 'Tax'

   ```
3. Link the newly registered tax integration to the storefront. From the store’s Administration menu, select Tax Calculation. Under Integration, link the newly registered `DR_COMPUTE_TAXES` or whatever name you gave the integration in the script in step 2.

## Payment service

Use the following steps to create the Payment Gateway:

1. Capture the `WebStore Id`. This will be used in the script in step 4.
2. From the Named Credential menu, choose the New Legacy option. Create a Named Credential for Digital River with the following details:
   * **Label**: Digital River API
   * **Name**: Digital\_River\_API
   * **URL**: <https://api.digitalriver.com>
   * **Identity Type**: Named Principal
   * **Authentication Protocol**: No Authentication
   * **Generate Authorization Header**: false\
     ![](/files/YVn92FAsYfkP2xHe3Dy2)
3. Create a **Payment Gateway Provider**:
   * Sign in to Workbench from your commerce org.
   * Go to the **Data** tab and select **Insert**.
   * Go to the **Object Type** and select **PaymentGatewayProvider**.
   * Select **Single Record** and click **Next**.
   * Fill in the fields using your Payment Gateway Adapter information.

     | Field name           | Example                                                                |
     | -------------------- | ---------------------------------------------------------------------- |
     | ApexAdapterId        | ID of Payment Gateway Adapter Apex class `DRB2B_PaymentGatewayAdapter` |
     | DeveloperName        | DR\_PaymentGateway                                                     |
     | IdempotencySupported | Yes                                                                    |
     | MasterLabel          | DR Payment Gateway                                                     |
     | Comments             | Digital River Payment Gateway Provider                                 |
   * Click **Confirm Insert**.\
     ![](/files/pesZ6P3EZ35tiFYaT5vM)
4. Set up Payment Gateway and insert a `StoreIntegratedService` record for payment by executing the below script. Be sure to update the `WebstoreID` to the one captured in step 1 of this section.

```
String webstoreId = '<WebstoreId>';
PaymentGatewayProvider paymentGatewayProvider = [SELECT Id FROM PaymentGatewayProvider Where DeveloperName = 'DR_PaymentGateway' LIMIT 1];
NamedCredential namedCredential = [SELECT Id FROM NamedCredential WHERE DeveloperName = 'Digital_River_API' LIMIT 1];

String paymentGatewayId = '';

List<PaymentGateway> paymentGatewayList = [SELECT Id,MerchantCredentialId,PaymentGatewayName,PaymentGatewayProviderId FROM PaymentGateway WHERE PaymentGatewayProviderId = :paymentGatewayProvider.Id Limit 1];
if(null != paymentGatewayList && paymentGatewayList.size() > 0) {
	PaymentGateway paymentGateway = paymentGatewayList.get(0);
	paymentGatewayId = paymentGateway.Id;
	}
else{
	PaymentGateway paymentGateway = new PaymentGateway(
			PaymentGatewayName = 'Digital River Payment Gateway',
			MerchantCredentialId = namedCredential.Id,
			PaymentGatewayProviderId = paymentGatewayProvider.Id,
			Status = 'Active'
	);
	insert paymentGateway;
	paymentGatewayId = paymentGateway.Id;
	
}

List<StoreIntegratedService> storeIntegratedServiceList = [SELECT Id FROM StoreIntegratedService WHERE ServiceProviderType = 'Payment' and StoreId = :webstoreId Limit 1];

if(null != storeIntegratedServiceList && storeIntegratedServiceList.size() > 0) {
    StoreIntegratedService storeIntegratedService = storeIntegratedServiceList.get(0);
    storeIntegratedService.Integration = paymentGatewayId;
    update storeIntegratedServiceList;
}
else {
    StoreIntegratedService storeIntegratedService = new StoreIntegratedService(
        StoreId = webstoreId,
        Integration = paymentGatewayId,
        ServiceProviderType = 'Payment'
    );
    insert storeIntegratedService;
	
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.digitalriver.com/salesforce-lightning/salesforce-lightning-b2b-commerce-app-2.1/integrate-the-salesforce-lightning-app/step-3-register-external-services.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
