问题描述
我有跨平台应用,但我的应用中的一项功能只能在 Android (GooglePay) 中使用。我为它创建了一个活动,并尝试使用 DependencyService 在 Xamarin 表单页面中打开此活动:
IPay pay = DependencyService.Get<IPay>();
Console.WriteLine(pay.GetResult());
我的活动代码在 this answer
所以,据我所知,我需要用 Bundle 对象调用它,因为我在这段代码中遇到了一个异常:
PaymentsClient paymentsClient = WalletClass.GetPaymentsClient(
this,new WalletClass.Walletoptions.Builder()
.SetEnvironment(WalletConstants.EnvironmentTest)
.Build()
);
据我所知,没有捆绑包的活动是空的。例外是:
java.lang.NullPointerException. Message = Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
所以,我找不到打开 Activity 的正确方法,以及如何创建正确的 Bundle。
解决方法
Google 提供了 SupportWalletFragment 类,该类将在片段中显示带有品牌标识的购买按钮:
var walletFragment = SupportWalletFragment.NewInstance (WalletFragmentOptions.NewBuilder ()
.SetEnvironment (WalletConstants.EnvironmentSandbox)
.SetMode (WalletFragmentMode.BuyButton)
.SetTheme (WalletConstants.ThemeLight)
.SetFragmentStyle (new WalletFragmentStyle ()
.SetBuyButtonText (BuyButtonText.BuyWithGoogle)
.SetBuyButtonAppearance (BuyButtonAppearance.Classic)
.SetBuyButtonWidth (Dimension.MatchParent))
.Build ());
MaskedWalletRequest
类用于构建新的购买请求。您可以使用任何可以接受 EMVCO 网络令牌的支付网关,或者在这种情况下,使用一些配置选项将您的 Stripe 帐户设置为支付网关:
var maskedWalletRequest = MaskedWalletRequest.NewBuilder ()
// Request credit card tokenization with Stripe
.SetPaymentMethodTokenizationParameters (
PaymentMethodTokenizationParameters.NewBuilder ()
.SetPaymentMethodTokenizationType (PaymentMethodTokenizationType.PaymentGateway)
.AddParameter ("gateway","stripe")
.AddParameter ("stripe:publishableKey",STRIPE_PUBLISHABLE_KEY)
.AddParameter ("stripe:version","1.15.1")
.Build ())
.SetShippingAddressRequired (true)
.SetMerchantName ("Xamarin")
.SetPhoneNumberRequired (true)
.SetShippingAddressRequired (true)
.SetEstimatedTotalPrice ("20.00")
.SetCurrencyCode ("USD")
.Build();
有关详细信息,您可以查看 devblogs。
您也可以使用 Nuget 的插件 InAppBillingPlugin。并开始阅读In-App Billing Plugin documentation。
,好吧,我终于找到了解决这个问题的方法。
使用过 this 源 - 不是功能性的,仅适用于 GPay India,但有一些优点:
- 使用 MessagingCenter 将数据发送到本机代码并取回
- 使用 MainActivity,因为它是唯一的活动,可以与 '空' 捆绑
现在我的代码:
long orderId = 1;
long sum = 1;
long[] data = new long[2] {orderId,sum };
MessagingCenter.Send((App)Xamarin.Forms.Application.Current,"PayViaGooglePay",data);
MessagingCenter.Subscribe<App,string>((App)Application.Current,$"tokenteleportation{orderId}",async (sender,arg) =>
{
// getting result from arg
}
以及 MainActivity 中的代码:
MessagingCenter.Subscribe<App,long[]>((App)Xamarin.Forms.Application.Current,(sender,arg) =>
{
OrderId = arg[0];
PayViaGooglePay(this,arg[1]);
});
//sending request,getting token
MessagingCenter.Send((App)Xamarin.Forms.Application.Current,$"tokenteleportation{OrderId}",TOKEN);