我们使用了AndroidBillingLibrary。
在 Eclipse 中将其作为项目安装,并让您的项目将其作为库导入。
我们实现了BillingController.IConfiguration,类似于
import net.robotmedia.billing.BillingController;
public class PhoneBillingConfiguration implements BillingController.IConfiguration{
@Override
public byte[] getObfuscationSalt() {
return new byte[] {1,-2,3,4,-5,6,-7,theseshouldallberandombyteshere,8,-9,0};
}
@Override
public String getPublicKey() {
return "superlongstringhereIforgothowwemadethis";
}
}
然后对于我们的应用程序,我们扩展了:Application
public class LocalizedApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// BillingController.setDebug(true);
BillingController.setConfiguration(new PhoneBillingConfiguration());
}
}
AndroidManifest包括这个(以及所有其他东西)
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:name=".LocalizedApplication" <!-- use your specific Application -->
android:largeHeap="true"
android:hardwareAccelerated="true"
>
<!-- For billing -->
<service android:name="net.robotmedia.billing.BillingService" />
<receiver android:name="net.robotmedia.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>
我们实施了ISignatureValidator
public class PhoneSignatureValidator implements ISignatureValidator {
private final String TAG = this.getClass().getSimpleName();
private PhoneServerLink mServerLink;
private BillingController.IConfiguration configuration;
public PhoneSignatureValidator(Context context, BillingController.IConfiguration configuration, String our_product_sku) {
this.configuration = configuration;
mServerLink = new PhoneServerLink(context);
mServerLink.setSku(our_product_sku);
}
@Override
public boolean validate(String signedData, String signature) {
final String publicKey;
if (configuration == null || TextUtils.isEmpty(publicKey = configuration.getPublicKey())) {
Log.w(BillingController.LOG_TAG, "Please set the public key or turn on debug mode");
return false;
}
if (signedData == null) {
Log.e(BillingController.LOG_TAG, "Data is null");
return false;
}
// mServerLink will talk to your server
boolean bool = mServerLink.validateSignature(signedData, signature);
return bool;
}
}
这是上面的最后几行,它调用您的类,该类实际上将与您的服务器通信。
我们的 PhoneServerLink 最初是这样的:
public class PhoneServerLink implements GetJSONListener {
public PhoneServerLink(Context context) {
mContext = context;
}
public boolean validateSignature(String signedData, String signature) {
return getPurchaseResultFromServer(signedData, signature, false);
}
private boolean getPurchaseResultFromServer(String signedData, String signature, boolean async) {
// send request to server using whatever protocols you like
}
}