如何在 Xamarin Android 中验证应用内购买?

How to verify In app purchases in Xamarin Android?

我正在使用来自 https://www.nuget.org/packages/Plugin.InAppBilling 的 Xamarin Android 和 Plugin.InAppBilling。我已成功将插件集成到我的项目中,并且能够购买和使用测试产品。

现在我要验证购买。插件有这个文档 https://jamesmontemagno.github.io/InAppBillingPlugin/SecuringPurchases.html

什么是 DependencyService 以及如何获取 signedData、签名以传递给 InAppBillingSecurity.VerifyPurchase 方法?这个问题说它不需要 DependencyService https://github.com/jamesmontemagno/InAppBillingPlugin/issues/203

在同一文档中还提到将 play store public 键放在下面的行中。那么我是不是应该直接把key分成三部分,然后粘贴到XOR_key1、XOR_key2和XOR_key3的位置呢?


    const string key1 = @"XOR_key1";
    const string key2 = @"XOR_key2";
    const string key3 = @"XOR_key3";  


我很困惑,我没有找到任何实时示例或分步指南。任何人都可以帮助我理解这一点吗?

检查我的代码的附件图像。购买后returns这么多参数但没有signedData,signature

A Dependency Service 允许您从 .NET Standard 共享项目调用特定于平台的代码。

Plugin.InAppBilling已经创建了要在每个平台上实现的接口,所以你所要做的就是在每个平台项目中实现IInAppBillingVerifyPurchase接口。界面中只有一个方法:

Task<bool> VerifyPurchase(string signedData, string signature, string productId = null, string transactionId = null);

所以基本上在每个平台项目中,您都需要添加一个 class 文件,如下所示:

[assembly: Dependency (typeof (InAppBillingVerify))]
namespace YourPlatformProjectNameSpace
{
    public class InAppBillingVerify : IInAppBillingVerifyPurchase
    {
        const string key1 = @"XOR_key1";
        const string key2 = @"XOR_key2";
        const string key3 = @"XOR_key3";

        public Task<bool> VerifyPurchase(string signedData, string signature)
        {

#if __ANDROID__
            var key1Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key1, 1);
            var key2Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key2, 2);
            var key3Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key3, 3);

            return Task.FromResult(Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.VerifyPurchase(key1Transform + key2Transform + key3Transform, signedData, signature));
#else
            return Task.FromResult(true);
#endif
        }
    }

}

以上直接来自您链接的文档。您可以看到将密钥分为三个部分仅与 Android 相关,因此存在编译器指令,使得密钥转换的代码仅在 Android 上 运行(确保您有 __ANDROID__ symbol defined in the project properties for the Android project. For iOS and UWP, all it does is return true. This is fine for UWP (see below) but for iOS you might want to parse the signedData. Check Apples docs on InAppPurchases 来查看验证时返回的内容。

如果我正在正确阅读您链接的文档,那么插件应该调用 VerifyPurchase 方法。然后在插件中,VerifyPurchase 方法的参数值为(来自您链接的文档):

iOS
    signedData: Full Receipt as a string in Base 64 Encoding
    signature: Always empty string
Android
    signedData: Purchase Data returned from Google
    signature: Data Signature returned from Google
UWP
    No additional authentication is provided.

所以在 iOS 上,签名数据将是 "Full Receipt as a string in Base 64 Encoding,",而签名将为空。

在 Android 上,签名数据将是 "Purchase Data returned from Google,",而签名将是 return 从 Google 编辑的数据签名。

在 UWP 上,除了 return true 之外,你还不如不做任何事情,因为 UWP 不会将任何东西传递给这个方法。

有关处理 Android 键的更多讨论,请参阅 this discussion

编辑: 所以看来你需要将实现 IInAppBillingVerifyPurchase 的 class 传递给 PurchaseAsync 方法,以便要调用的 VerifyPurchase 方法,例如:

var verify = new Verify(); 
//try to purchase item
// Here is where you pass in the class that implements IInAppBillingVerifyPurchase.VerifyPurchase method
var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, "apppayload", verify); 
if(purchase == null)
{
    //Not purchased, may also throw excpetion to catch
}
else
{
    //Purchased!
}

并添加 Verify class:

public class Verify : IInAppBillingVerifyPurchase
{
    const string key1 = @"XOR_key1";
    const string key2 = @"XOR_key2";
    const string key3 = @"XOR_key3";

    public Task<bool> VerifyPurchase(string signedData, string signature)
    {

        var key1Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key1, 1);
        var key2Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key2, 2);
        var key3Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key3, 3);

        return Task.FromResult(Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.VerifyPurchase(key1Transform + key2Transform + key3Transform, signedData, signature));
    }
}