# 获取远程目录

> 获取付款提供商商店的 Remote Catalog。

您需要创建一个连接到 `StoreController` 的脚本来显式获取[部署的远程目录](./configure-remote-catalog.md)。将此脚本附加到与[初始化脚本](./initialize-services.md)相同的游戏对象。

要获取付款提供商商店的 Remote Catalog，您需要实例化 `RemoteCatalogProvider`，然后调用 `FetchRemoteCatalog` 从 Unity IAP 后端检索商品。

然后，您可以直接使用 `RemoteCatalogProvider` 调用 `FetchProducts`，也可以从 `IProductService` 或 `StoreController` 将商品列表传递给 `FetchProducts`。

## 参考信息##reference-information

获取付款提供商的 Remote Catalog 时，以下元素相关：

| **参数**                  | **描述**                                                                                             |
| ----------------------- | -------------------------------------------------------------------------------------------------- |
| `RemoteCatalogProvider` | 该类用于获取要与 IAP 一起使用的 Remote Catalog。远程目录是从 IAP 后端获取的。                                                |
| `UnityIAPServices`      | 这是与 IAP 通信的起点。您需要获得以下服务： `StoreService` `ProductService` `PurchaseService`                         |
| `StoreController`       | 这是 `UnityIAPServices` 上的包装器。如果希望与单个类交互，请将`storeName`传递给构造函数。                                       |
| `PaymentProvider.Name`  | 这是用于向 `UnityIAPServices` 和 `StoreController` 注册直接到消费者 (D2C) 付款提供商的标识符。请注意，此字段用于配置目的，不会返回提供程序的显示名称。 |

> **Important:**
>
> 调用 `RemoteCatalogProvider` 时，请确保指定正确的商店名称，对于 D2C 支付提供商，该名称为 `PaymentProvider.Name`。

## 获取 Remote Catalog 示例脚本##fetch-remote-catalog-example-script

请参阅以下 脚本示例：

```csharp
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Purchasing;

public class PurchaseManager :MonoBehaviour
{
    StoreController m_StoreController;  


    // Called by the ServiceOrchestrator after Auth is complete
    public async Task InitializeIAP()  
    {  
        m_StoreController = UnityIAPServices.StoreController(PaymentProvider.Name);


        m_StoreController.OnPurchasePending += OnPurchasePending;  


        await m_StoreController.Connect();  


        m_StoreController.OnProductsFetch += OnProductsFetch;


        // 1.初始化 RemoteCatalogProvider
        RemoteCatalogProvider catalogProvider = new RemoteCatalogProvider();

        // 2.为支付提供商显式获取目录
        var fetchRemoteCatalogResult = await catalogProvider.FetchRemoteCatalog();

        if (!fetchRemoteCatalogResult.Success)
            throw fetchRemoteCatalogResult.Exception!;

        // 3.将远程定义传递给 StoreController
        var productDefinitions = catalogProvider.GetProducts();
        m_StoreController.FetchProducts(productDefinitions);
    }

    private void OnPurchasePending(PendingOrder order)
    {
        // Handle purchase validation and rewarding before confirming the order
        Debug.Log($"Order pending: {order.Info}");
    }

    void OnProductsFetched(List<Product> products)  
    {
        // Handle fetched products (e.g., populate UI)
        Debug.Log("Fetched products:");
        foreach（商品中不同商品）
        {
            foreach（var catalogListing in product.catalogListings.Values）
            {
                Debug.Log($"ID: {catalogListing.definition.id} - Price: {catalogListing.metadata.localizedPriceString}");
            }
        }
    }
}
```

## 后续步骤##next-steps

本页面是使用 IAP 设置 D2C 支付提供商的工作流程的一部分。要继续此工作流程，请选择以下选项之一：

[Integrate D2C payment providers](./workflow.md#fetch-your-remote-catalog): 返回到 Integration D2C payment providers with IAP workflow（将 D2C 支付提供商与 IAP 工作流程集成）页面。
[Configure a webhook for purchase fulfillment](./configure-fulfilment.md): 继续工作流程中的下一步（可选）以设置 D2C 支付提供商。
