---
title: "google in-app-billing 应用内付费的简介与实现"
author: "AnhuiOSS技术分享"
date: "2012-10-29T11:04:00Z"
tags: ["Android"]
categories: ["移动开发"]
slug: "google-in-app-billing"
draft: false
lastmod: "2012-10-29T11:04:00Z"
id: 1707154
url: "https://1679554191.iteye.com/blog/1707154"
---

android平台下， 谷歌应用收费提供了最新的google-billing接口，该接口用来发送请求应用收费和管理应用收费事物。下面简单介绍应用收费的实现过程。

 

      实现android应用的支付功能需要做到以下几点：

1.查看google应用支付功能提供的最新开发文档，了解应用内支付功能的机制，具体参考http://developer.android.com/guide/google/play/billing/index.html

 

2.升级android sdk开发环境，安装google-billing的api，添加IMarketBillingService，将aild文件添加到项目中(参看附加图片，aidl路径。须确认最新公布google billing的aidl文件已经为V2版本)。

用于实现五类的计费请求：

1)检查账单的支付请求(request);

2)请求进行购买的请求(purchast);

3)获取购买信息的请求(receiver);

4)确认通知的请求(confirm);

5)恢复交易的请求(restore)。

 

 

3.更新AndroidManifest.xml文件。添加权限设置以及调用服务的设置。

```
<uses-permission android:name="com.android.vending.BILLING" />

<service android:name=".billing.BillingService" />
<application
        <receiver android:name=".billing.BillingReceiver">
            <intent-filter>
                <action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
                <action android:name="com.android.vending.billing.RESPONSE_CODE" />
                <action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
            </intent-filter>
        </receiver>
</application>
```

 

 4.创建服务用于绑定到IMarketBillingService,android应用程序可以通过该service进行发送用户的购买请求和接收来自google play store的计费响应账单。

```
BillingService mService = IMarketBillingService.Stub.asInterface(service);
```

 

 5.创建BillingReceiver用于接受来自IPC发送的广播信息。

```
public class BillingReceiver extends BroadcastReceiver {
  
        private static final String TAG = "BillingReceiver";
  
        @Override
        public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.i(TAG, "Received action: " + action);
        if (Const.ACTION_PURCHASE_STATE_CHANGED.equals(action)) {
            String signedData = intent.getStringExtra(Const.INAPP_SIGNED_DATA);
            String signature = intent.getStringExtra(Const.INAPP_SIGNATURE);
            purchaseStateChanged(context, signedData, signature);
        } else if (Const.ACTION_NOTIFY.equals(action)) {
            String notifyId = intent.getStringExtra(Const.NOTIFICATION_ID);
            notify(context, notifyId);
        } else if (Const.ACTION_RESPONSE_CODE.equals(action)) {
            long requestId = intent.getLongExtra(Const.INAPP_REQUEST_ID, -1);
            int responseCodeIndex = intent.getIntExtra(Const.INAPP_RESPONSE_CODE, Const.ResponseCode.RESULT_ERROR.ordinal());
            checkResponseCode(context, requestId, responseCodeIndex);
        } else {
           Log.e(TAG, "unexpected action: " + action);
        }
        }
        private void purchaseStateChanged(Context context, String signedData, String signature) {
                Log.i(TAG, "purchaseStateChanged got signedData: " + signedData);
                Log.i(TAG, "purchaseStateChanged got signature: " + signature);
                BillingHelper.verifyPurchase(signedData, signature);
        }
        
        private void notify(Context context, String notifyId) {
                Log.i(TAG, "notify got id: " + notifyId);
                String[] notifyIds = {notifyId};
                BillingHelper.getPurchaseInformation(notifyIds);
        }
        
        private void checkResponseCode(Context context, long requestId, int responseCodeIndex) {
                Log.i(TAG, "checkResponseCode got requestId: " + requestId);
                Log.i(TAG, "checkResponseCode got responseCode: " + Const.ResponseCode.valueOf(responseCodeIndex));
        }
}
```

 

 6.创建一个安全处理的组件BillingSecurity，用于包含应用的支付功能以及账户的安全问题,将google play 上的发布应用的public key 作为验证的密钥，进行交易的安全验证。

 

7.修改应用代码，实现各接口，用于支持应用的收费功能。

 

注：

1)以上仅是简单介绍in-app-billing的实现过程，在操作之前，需要了解完成的支付机制，明确各接口需要的参数以及应用内产品设置。根据需求，开发者需要明确，应用是属于购买还是订阅功能，在google play上传时应用，可以参考： http://developer.android.com/guide/google/play/billing/billing\_admin.html

 

2)在应用功能完成上架之前，必须对支付功能进行测试。用于测试的方式有两种：

一种利用静态响应对应用的内够功能进行测试；

另一种是使用开发者的商品ID对应用的内够功能进行测试。

具体可以参考：http://developer.android.com/guide/google/play/billing/billing\_testing.html。

 

3)测试中会遇到一系列的问题，国内用户会有诸多障碍，测试发布流程：

(1)将应用作为测试apk上传到google play；

(2)设置应用商品的购买属性，产品IP等；

(3)在实体android 设备上安装应用；

(4)将google play store上某一账户，设置为设备的主账户；

(5)确保设备上的play store或apps 是最新版本。