# InApp Billing

使用此模組：

{% embed url="<https://github.com/dooboolab/react-native-iap/>" %}

> 必須使用真實設備測試

### Android

```
1. 連上後選擇檔案傳輸，並在手機設定開啟 USB debug
2. 電腦 terminal 輸入： adb devices
3. 執行： npx react-native run-android --variant=release
4. 移除 app: adb uninstall <app 套件名稱 com 開頭>
```

> 安裝完 release 版本後下次要安裝 dev 版本必須先移除 release 版本

官方範例：

{% embed url="<https://github.com/dooboolab/react-native-iap/blob/master/IapExample/>" %}

## 安裝

```
yarn add react-native-iap
```

### Android 設置

1.新增 license key

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MYxM6LUPbpQ8gp-IhM3%2F-MYxNAkBRqmCYjlmwBq-%2F%E6%88%AA%E5%9C%96%202021-04-23%20%E4%B8%8B%E5%8D%882.17.54.png?alt=media\&token=d3b73f76-1ef4-4e11-b278-8d6a35541e94)

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MYxM6LUPbpQ8gp-IhM3%2F-MYxMy33WuSA86YZM5Ra%2F%E6%88%AA%E5%9C%96%202021-04-23%20%E4%B8%8B%E5%8D%882.16.56.png?alt=media\&token=e0a35cf5-bace-4bf6-bd0f-55a1b51c0401)

2.上傳 app 到封閉測試群組

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MYxM6LUPbpQ8gp-IhM3%2F-MYxNJK5H0v9inZVtadr%2F%E6%88%AA%E5%9C%96%202021-04-23%20%E4%B8%8B%E5%8D%882.18.29.png?alt=media\&token=3577b327-3cd1-4923-8ba1-f1bc6d2b950f)

3.新增產品

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MYxM6LUPbpQ8gp-IhM3%2F-MYxN_5uM_iZLmNJ5ITy%2F%E6%88%AA%E5%9C%96%202021-04-23%20%E4%B8%8B%E5%8D%882.19.39.png?alt=media\&token=5fad9f08-3487-4860-a67d-b8c32bcbf791)

4.新建  License Testing 使用者，並且測試的手機 Google play 要登入並選擇使用該帳號

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MYxM6LUPbpQ8gp-IhM3%2F-MYxNkow_aAaEcpaSD0t%2F%E6%88%AA%E5%9C%96%202021-04-23%20%E4%B8%8B%E5%8D%882.12.59.png?alt=media\&token=f1641100-c18c-45d2-acc3-11d0b949228f)

5.確保 build version 與 封閉測試的 APP 版本相同

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MYxM6LUPbpQ8gp-IhM3%2F-MYxO9t0q-6HyP31OyA6%2F%E6%88%AA%E5%9C%96%202021-04-23%20%E4%B8%8B%E5%8D%882.22.14.png?alt=media\&token=ee42734f-fee5-4a16-ae77-bd921d2806d8)

6.使用 release key 測試， react-native 可用如下指令

```
npx react-native run-android --variant=release
```

7\. 將範例改為自己的 product id

> 在 developer console 輸入什麼 product id 這邊就輸入什麼

```
const itemSkus = Platform.select({
  ios: [
    'com.cooni.point1000',
    'com.cooni.point5000',
  ],
  android: [
    'com.yicheng.paper',
    'android.test.canceled',
    'android.test.refunded',
    'android.test.item_unavailable',
  ],
});
```

> 之後即可開啟實體設備測試，通常遇到的問題都是回應 code 5，並且沒有詳細錯誤訊息，個人當時是沒有新增 License account 所導致

#### 其他問題可參考：

{% embed url="<https://stackoverflow.com/questions/11068686/this-version-of-the-application-is-not-configured-for-billing-through-google-pla>" %}

{% embed url="<https://developer.android.com/google/play/billing/billing_reference.html>" %}

{% embed url="<https://github.com/dooboolab/react-native-iap/issues/1308>" %}

#### 如果要切換 inapp purchase account 必須要把手機內的 google 帳號移除

<https://android.stackexchange.com/questions/70215/change-gmail-account-to-make-in-app-purchase>
