> For the complete documentation index, see [llms.txt](https://docs.gamepot.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gamepot.io/basics/gamepot-2.0/zhong-wen/unity-sdk/unity-sdk-zhi-fu-gong-neng.md).

# Unity SDK\_支付功能

> (参考)
>
> GAMEPOT结算仅支持游戏内消耗性商品类型的结算，One Store应用内SDK仅支持V17版本。
>
> 包含One Store应用内SDK：gamepot-billing-onestore.aar
>
> 包含Galaxy Store应用内SDK：gamepot-billing-galaxystore.aar
>
> 包含My Card应用内SDK：gamepot-billing-mycard.aar（请通过相应措施将其在Google Store构建中除外。）

可使用应用内购买所需的支付功能。

## 应用内商品查询功能 <a href="#ying-yong-nei-shang-pin-cha-xun-gong-neng" id="ying-yong-nei-shang-pin-cha-xun-gong-neng"></a>

***

如要使用查询商店内商品信息的功能，请使用下列代码。

如果登录成功后需要调用的API是getPurchaseItems API，由于它是异步提供从商店应用内SDK接收的内容的API，因此也可根据调用时间以空白值进行传递。 （以可支付的环境为准）

```
[Case1]:

NPurchaseItem[] items = GamePot.getPurchaseItems();
foreach(NPurchaseItem item in items) {
    Debug.Log(item.productId);        // 产品ID
    Debug.Log(item.price);            // 价格
    Debug.Log(item.title);            // 标题
    Debug.Log(item.description);    // 概述
}

[Case2]:

GamePot.getPurchaseDetailListAsync((bool success, NPurchaseItem[] purchaseInfoList, NError error) =>
{
   // string result = "data is empty!";
    if (success)
    {
         //PurchaseDetailList API Success
        // if (purchaseInfoList != null)
        //     result = purchaseInfoList[0].productId;
        //if (purchaseInfoList.Length > 1)
        //{
        //    for (int i = 1; i < purchaseInfoList.Length; i++)
        //        result += "\n" + purchaseInfoList[i].productId;
        //}
    }
    else
    {
        // API错误 
        // result = error.ToJson();
    }

});


public class NPurchaseItem
{
    public string productId { get; set; }   // 商品ID
    public string type { get; set; }        // 商品类型 固定为"inapp"
    public string price { get; set; }       // 价格 Google商店：$0.99，其他商店：0.99
    public string price_amount { get; set; }
    public string price_amount_micros { get; set; }
    public string price_currency_code { get; set; } // 货币代码 例）KRW、USD
    public string price_with_currency { get; set; } // （在UI中显示时推荐）货币与价格合并后的值。 One Store不会传输货币单位。例如）$0.99
    public string title { get; set; }       // 商品名称
    public string description { get; set; } // 商品描述
}
```

```
Case3]:

GamePot.getPurchaseDetailListAsync();

public void onPurchaseDetailListSuccess(NPurchaseItem[] purchaseInfoList)
{
    //PurchaseDetailList API Success
}

public void onPurchaseDetailListFailure(NError error)
{   
       // API错误 
}
```

## 支付尝试功能 <a href="#zhi-fu-chang-shi-gong-neng" id="zhi-fu-chang-shi-gong-neng"></a>

***

可以通过一个支付API，在Google Play商店、Apple App Store均可使用支付尝试功能。

（如为One Store版本，uniqueId+serverId+playerId+etc的长度小于85byte方可进行支付。）

&#x20;

如要使用支付尝试功能，请使用下列代码。

&#x20;

* 方式1\
  // productId：商店中添加的商品ID // uniqueId：单独管理的发票号 // serverId：付费角色的服务器ID // playerId：付费角色的角色ID // etc ： 付费角色的其他信息

  ```
  GamePot.purchase(string productId);

  GamePot.purchase(string productId, string uniqueId);

  GamePot.purchase(string productId, string uniqueId, string serverId, string playerId, string etc);

  /// 应用内支付成功
  public void onPurchaseSuccess(NPurchaseInfo purchase) {
  }

  /// 应用内支付失败
  public void onPurchaseFailure(NError error) {
      // 应用内支付失败
      // 请使用error.message显示错误消息。
  }

  /// 应用内支付失败
  public void onPurchaseCancel() {
  }
  ```

* 方式2

  ```
  // productId：商店中添加的商品ID
  GamePot.purchase(string productId, GamePotCallbackDelegate.CB_Purchase);

  GamePot.purchase(string productId, string uniqueId, GamePotCallbackDelegate.CB_Purchase);

  GamePot.purchase(string productId, string uniqueId, string serverId, string playerId, string etc, GamePotCallbackDelegate.CB_Purchase);

  GamePot.purchase(productId, (resultState, purchaseInfo, error) => {
        switch (resultState)
      {
          case NCommon.ResultPurchase.SUCCESS:
          // purchase success
          break;
          case NCommon.ResultPurchase.CANCELLED:
          // purchase cancel
          break;
          case NCommon.ResultPurchase.FAILED:
          // purchase fail
          break;
          default:
          break;
      }
  });
  ```

## 获取付费道具信息的功能 <a href="#huo-qu-fu-fei-dao-ju-xin-xi-de-gong-neng" id="huo-qu-fu-fei-dao-ju-xin-xi-de-gong-neng"></a>

***

如要使用获取由商店传递的应用内付费道具信息功能，请使用下列代码。

```
public class NPurchaseInfo
{
    public string price { get; set; }               // 付费道具的价格
    public string productId { get; set; }           // 付费道具ID
    public string currency { get; set; }            // 支付价格货币（KRW/USD）
    public string orderId { get; set; }            // 商店Order ID
    public string productName { get; set; }        // 付费道具名称
    public string gamepotOrderId { get; set; }      // 在GAMEPOT中创建的Order ID（不提供相应值） 
    public string uniqueId { get; set; }            // 开发商单独管理的发票ID
    public string serverId { get; set; }            // 付费角色的服务器ID
    public string playerId { get; set; }            // 付费角色的角色ID
    public string etc { get; set; }                 // 付费角色的其他信息
    public string signature { get; set; }           // 支付签名
    public string originalJSONData { get; set; }    // 发票数据
}
```

## 发放付费道具的功能 <a href="#fa-fang-fu-fei-dao-ju-de-gong-neng" id="fa-fang-fu-fei-dao-ju-de-gong-neng"></a>

***

可设置为与支付商店的发票明细进行对照并完成所有验证后向开发商服务器传递发放请求。 详细说明请参考[道具发放请求](https://nbase.atlassian.net/wiki/spaces/gamepotGuide/pages/44499433)。

&#x20;

## My Card支付 <a href="#mycard-zhi-fu" id="mycard-zhi-fu"></a>

***

Mycard库：[gamepot-billing-mycard.aar](https://xyuditqzezxs1008973.cdn.ntruss.com/patch/gamepot-billing-mycard.aar)

:::(Info) (参考)

请通过Mycard确认用于和Mycard关联的FacServiceID/KEY值。 :::

1. 在仪表盘 >> 支付 >> IAP的商店类型：Google项目 > 添加价格 > 货币（例如：TWD）/输入价格信息后保存。

2. 在Dashboard >> 项目设置 >> 外部支付项目中添加MyCard，并确认是否正常输入相应FacService ID / Sign Key。

3. 支付时调用以下SDK代码。 GamePot.getInstance().purchase("product id");
   * 在MyCard使用过程中，付费道具调用形式在调用现有的GamePot.getInstance().getPurchaseDetailList();时发生错误。 为进行替代，请调用GamePot.getInstance().getPurchaseThirdPaymentsDetailList();。

4. 在../Assets/Plugins/Android/AndroidManifest.xml文件中移除级别的名称。

   ```
     <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
     <application android:icon="@drawable/app_icon"
         android:label="@string/app_name"
         android:allowBackup="false"
         tools:replace="android:allowBackup"
         >
   ```

5. 在../Assets/Plugins/Android/mainTemplate.gradle文件中按照以下方法进行设置。 （从Unity 2019.3.X之后的版本开始修改launcherTemplate.gradle文件）

   ```
   resValue "string", "gamepot_store", "google"
   resValue "string"、"gamepot_payment"、"mycard" //商店为Google时运行。
   ```

6. 确认../Assets/Plugins/Android/libs文件夹内是否包含gamepot-billing-mycard.aar。

### **第三方支付**

如要关联第三方支付模块，首先参考[第三方支付服务关联](https://nbase.atlassian.net/wiki/spaces/gamepotGuide/pages/43090087)完成设置后使用下列代码。

* 方式1

  ```
  GamePot.purchaseThirdPayments(string productId);

  // 返回的数据格式与getPurchaseItems()相同。
  GamePot.getPurchaseThirdPaymentsItems();
  ```

* 方式2

  ```
  GamePot.purchaseThirdPayments(string productId, GamePotCallbackDelegate.CB_Purchase);

  // 返回的数据格式与getPurchaseItems()相同。
  GamePot.purchase(productId, (resultState, purchaseInfo, error) => {
        switch (resultState)
      {
          case NCommon.ResultPurchase.SUCCESS:
          // purchase success
          break;
          case NCommon.ResultPurchase.CANCELLED:
          // purchase cancel
          break;
          case NCommon.ResultPurchase.FAILED:
          // purchase fail
          break;
          default:
          break;
      }
  });
  ```
