Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c# - Xamarin Google Pay implementation

I am trying to make a simple Xamarin app for Android with opportunity to pay by Google Pay. Using Android.Gms.Wallet. Added [assembly: MetaData("com.google.android.gms.wallet.api.enabled", Value = "true")] in AssemlyInfo.

Tried to call GooglePay widget in MainActivity in OnCreate method:

openGooglePay(this,1000,5);

And this is the method:

void openGooglePay(Activity a, int money, int requestCode)
{
  PaymentsClient paymentsClient = WalletClass.GetPaymentsClient(this,
                 new WalletClass.WalletOptions.Builder()
                         .SetEnvironment(WalletConstants.EnvironmentTest)
                         .Build()
            );

  var request = PaymentDataRequest.FromJson(money.ToString());
  AutoResolveHelper.ResolveTask(paymentsClient.LoadPaymentData(request), a, requestCode);
}

But screen on my phone is empty.

What can be a problem?

question from:https://stackoverflow.com/questions/65937861/xamarin-google-pay-implementation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, I found the answer to my question - just to rewrite java/kotlin code to c#.

public const int LOAD_PAYMENT_DATA_REQUEST_CODE = 991;

call openGooglePay(this,1);in activity, e.g. in OnCreate

void openGooglePay(Activity act, int money)
    {
        PaymentsClient paymentsClient = WalletClass.GetPaymentsClient(
             this,
             new WalletClass.WalletOptions.Builder()
                     .SetEnvironment(WalletConstants.EnvironmentTest)
                     .Build()
        );

        TransactionInfo tran = TransactionInfo.NewBuilder()
            .SetTotalPriceStatus(WalletConstants.TotalPriceStatusFinal)
            .SetTotalPrice(money.ToString())
            .SetCurrencyCode("USD")
            .Build();

        var req = createPaymentDataRequest(tran);

        var futurePay = paymentsClient.LoadPaymentData(req);
        
    AutoResolveHelper.ResolveTask(futurePay, act, LOAD_PAYMENT_DATA_REQUEST_CODE);
}

PaymentDataRequest createPaymentDataRequest(TransactionInfo transactionInfo)
    {
        var paramsBuilder = PaymentMethodTokenizationParameters.NewBuilder()
            .SetPaymentMethodTokenizationType(
            WalletConstants.PaymentMethodTokenizationTypePaymentGateway)
            .AddParameter("gateway", "myGateway")
            .AddParameter("gatewayMerchantId", "myMerchant");

            return createPaymentDataRequest(transactionInfo, paramsBuilder.Build());

}

    private PaymentDataRequest createPaymentDataRequest(TransactionInfo transactionInfo, PaymentMethodTokenizationParameters paymentMethodTokenizationParameters)
    {
        return PaymentDataRequest.NewBuilder()
            .SetPhoneNumberRequired(false)
            .SetEmailRequired(false)
            .SetShippingAddressRequired(false)
   .SetTransactionInfo(transactionInfo)
   .AddAllowedPaymentMethods(new List<Integer>() { (Integer)WalletConstants.PaymentMethodCard, (Integer)WalletConstants.PaymentMethodTokenizedCard })
   .SetCardRequirements(
       CardRequirements.NewBuilder()
           .AddAllowedCardNetworks(new List<Integer>() { (Integer)WalletConstants.CardNetworkVisa, (Integer)WalletConstants.CardNetworkMastercard })
           .SetAllowPrepaidCards(true)
           .SetBillingAddressFormat(WalletConstants.BillingAddressFormatFull)
           .Build()
   )
   .SetPaymentMethodTokenizationParameters(paymentMethodTokenizationParameters)
   .SetUiRequired(true)
   .Build();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...