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

标签 c# android xamarin xamarin.android in-app-purchase

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

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

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

同一文档中还提到将 Play 商店公钥放在下面的行中。那么我应该直接将 key 分成三部分并粘贴到 XOR_key1、XOR_key2 和 XOR_key3 的位置吗?


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


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

检查我的代码的附加图像。购买后返回这么多参数但不返回signedData、signature

enter image description here

最佳答案

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

Plugin.InAppBilling 已经创建了要在每个平台上实现的接口(interface),因此您所要做的就是在每个平台项目中实现 IInAppBillingVerifyPurchase 接口(interface)。接口(interface)中只有一个方法:

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

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

[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
        }
    }

}

以上内容直接来自您链接的文档。您可以看到,将 key 分为三个部分仅与 Android 相关,因此存在编译器指令,使得 key 转换的代码只能在 Android 上运行(确保您具有 __ANDROID__对于 Android 项目,symbol defined in the project properties。对于 iOS 和 UWP,它所做的只是返回 true。这对于 UWP 来说没问题(见下文),但对于 iOS,您可能需要解析 signedData。检查 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 上,签名数据将是“完整收据作为 Base 64 编码中的字符串”,而签名将为空。

在 Android 上,签名数据将是“从 Google 返回的购买数据”,而签名将是从 Google 返回的数据签名。

在 UWP 上,您也可以不执行任何操作,只从此方法返回 true,因为 UWP 不会将任何内容传递到此方法中。

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

编辑:因此,您似乎需要将实现 IInAppBillingVerifyPurchase 的类传递给 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类:

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));
    }
}

关于c# - 如何在 Xamarin Android 中验证应用内购买?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54134858/

相关文章:

java - HTTP 状态 403 - 在请求参数中发现无效的 CSRF token 'null'

c# - 我面临着对 IEnumerable(LINQ) > List(RecyclerView) 的巨大性能影响

c# - 为什么 DynamicMethod 在 x64 上这么慢?

c# - XMLSerializer 更改元素的名称

javascript - XSD 正则表达式 : empty string OR something else

c# - 将所有列与所有搜索词组匹配

android - 在 Android Volley 中处理 session Cookie

安卓工作室 : Cannot change layout button properties

c# - Android,尝试在 Xamarin Android 应用程序中查看 PDF

xamarin - 如何在Xamarin Forms中为具有下划线效果的标签添加下划线?