Created
December 10, 2018 10:49
-
-
Save TheAlphamerc/02aca370cb7eb7485b2fd43a3f45bfd9 to your computer and use it in GitHub Desktop.
Xamarin android activity to purchase or bye things by google wallet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Threading.Tasks; | |
| using Android.App; | |
| using Android.Content; | |
| using Android.Content.PM; | |
| using Android.Gms.Common; | |
| using Android.Gms.Common.Apis; | |
| using Android.Gms.Wallet; | |
| using Android.Gms.Wallet.Fragment; | |
| using Android.OS; | |
| using Android.Support.V7.App; | |
| using Android.Widget; | |
| using JudoDotNetXamarin; | |
| using JudoPayDotNet.Models; | |
| using Newtonsoft.Json; | |
| namespace Samples.Droid | |
| { | |
| [Activity(Label = "Android Pay", Theme = "@style/JudoTheme.ActionBar", ScreenOrientation = ScreenOrientation.Locked, MainLauncher = false)] | |
| public class AndroidPayActivity : AppCompatActivity, GoogleApiClient.IOnConnectionFailedListener | |
| { | |
| const int MaskedWalletRequest = 501; | |
| const int FullWalletRequest = 601; | |
| public const string JudoExtra = "Judo"; | |
| public const string IsPreAuthExtra = "IsPreAuth"; | |
| const int WalletEnvironment = WalletConstants.EnvironmentTest; | |
| GoogleApiClient _googleApiClient; | |
| IPaymentService _paymentService; | |
| SupportWalletFragment _walletFragment; | |
| Judo _judo; | |
| protected override void OnCreate(Bundle savedInstanceState) | |
| { | |
| base.OnCreate(savedInstanceState); | |
| SetContentView(Resource.Layout.AndroidPay); | |
| _judo = JsonConvert.DeserializeObject<Judo>(Intent.GetStringExtra(JudoExtra)); | |
| _paymentService = new PaymentService(_judo); | |
| if (savedInstanceState == null) | |
| { | |
| _googleApiClient = new GoogleApiClient.Builder(this) | |
| .AddApi(WalletClass.API, new WalletClass.WalletOptions.Builder() | |
| .SetEnvironment(WalletEnvironment) | |
| .Build()) | |
| .EnableAutoManage(this, this) | |
| .Build(); | |
| CreateWalletFragment(); | |
| } | |
| } | |
| protected override void OnActivityResult(int requestCode, Android.App.Result resultCode, Intent data) | |
| { | |
| base.OnActivityResult(requestCode, resultCode, data); | |
| switch (requestCode) | |
| { | |
| case MaskedWalletRequest: | |
| PerformFullWalletRequest((MaskedWallet)data.GetParcelableExtra(WalletConstants.ExtraMaskedWallet)); | |
| break; | |
| case FullWalletRequest: | |
| #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed | |
| PerformJudoPayment((FullWallet)data.GetParcelableExtra(WalletConstants.ExtraFullWallet)); | |
| #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed | |
| break; | |
| } | |
| } | |
| protected override void OnStart() | |
| { | |
| base.OnStart(); | |
| _googleApiClient.Connect(); | |
| } | |
| protected override void OnStop() | |
| { | |
| base.OnStop(); | |
| _googleApiClient.Disconnect(); | |
| } | |
| public void OnConnectionFailed(ConnectionResult result) | |
| { | |
| ShowToast("Connection failed! " + result.ErrorMessage + ", " + result.ErrorCode); | |
| } | |
| void ShowToast(string text) | |
| { | |
| Toast.MakeText(this, text, ToastLength.Short).Show(); | |
| } | |
| void CheckAndroidPayAvailable() | |
| { | |
| var result = WalletClass.Payments.IsReadyToPay(_googleApiClient, null); | |
| result.SetResultCallback(new AndroidPayCallback(_walletFragment)); | |
| } | |
| void PerformFullWalletRequest(MaskedWallet maskedWallet) | |
| { | |
| var request = Android.Gms.Wallet.FullWalletRequest.NewBuilder() | |
| .SetGoogleTransactionId(maskedWallet.GoogleTransactionId) | |
| .SetCart(Cart.NewBuilder() | |
| .SetCurrencyCode(_judo.Currency) | |
| .SetTotalPrice(_judo.Amount.ToString()) | |
| .Build()) | |
| .Build(); | |
| WalletClass.Payments.LoadFullWallet(_googleApiClient, request, FullWalletRequest); | |
| } | |
| async Task PerformJudoPayment(FullWallet fullWallet) | |
| { | |
| var androidPayModel = new AndroidPaymentModel() | |
| { | |
| JudoId = _judo.JudoId, | |
| Currency = _judo.Currency, | |
| Amount = _judo.Amount, | |
| Wallet = new AndroidWalletModel() | |
| { | |
| Environment = WalletEnvironment, | |
| PublicKey = Resources.GetString(Resource.String.public_key), | |
| GoogleTransactionId = fullWallet.GoogleTransactionId, | |
| InstrumentDetails = fullWallet.GetInstrumentInfos()[0].InstrumentDetails, | |
| InstrumentType = fullWallet.GetInstrumentInfos()[0].InstrumentType, | |
| PaymentMethodToken = fullWallet.PaymentMethodToken.Token | |
| } | |
| }; | |
| var result = await PerformTransaction(androidPayModel); | |
| if (result.HasError || "Success".Equals(result.Response.Result)) | |
| { | |
| Toast.MakeText(_walletFragment.Activity, "Payment successful", ToastLength.Short).Show(); | |
| } | |
| else if ("Declined".Equals(result.Response.Result)) | |
| { | |
| Toast.MakeText(_walletFragment.Activity, "Payment declined", ToastLength.Short).Show(); | |
| } | |
| } | |
| Task<IResult<ITransactionResult>> PerformTransaction(AndroidPaymentModel androidPayModel) | |
| { | |
| if (Intent.Extras.GetBoolean(IsPreAuthExtra)) | |
| { | |
| return _paymentService.AndroidPayPreAuth(androidPayModel); | |
| } | |
| else | |
| { | |
| return _paymentService.AndroidPayPayment(androidPayModel); | |
| } | |
| } | |
| void CreateWalletFragment() | |
| { | |
| var walletStyle = new WalletFragmentStyle() | |
| .SetBuyButtonText(WalletFragmentStyle.BuyButtonText.LogoOnly) | |
| .SetBuyButtonAppearance(WalletFragmentStyle.BuyButtonAppearance.AndroidPayLight) | |
| .SetBuyButtonWidth(WalletFragmentStyle.Dimension.MatchParent); | |
| var options = WalletFragmentOptions.NewBuilder() | |
| .SetEnvironment(WalletEnvironment) | |
| .SetTheme(WalletConstants.ThemeDark) | |
| .SetFragmentStyle(walletStyle) | |
| .SetMode(WalletFragmentMode.BuyButton) | |
| .Build(); | |
| _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()); | |
| var maskedWalletRequest = Android.Gms.Wallet.MaskedWalletRequest.NewBuilder() | |
| .SetPaymentMethodTokenizationParameters( | |
| PaymentMethodTokenizationParameters.NewBuilder() | |
| .SetPaymentMethodTokenizationType(PaymentMethodTokenizationType.PaymentGateway) | |
| .AddParameter("gateway", "stripe") | |
| .AddParameter("stripe:publishableKey", "pk_test_********") // Insert Stripe publish key | |
| .AddParameter("stripe:version", "1.15.1") | |
| .Build()) | |
| .SetShippingAddressRequired(true) | |
| .SetMerchantName("Xamarin") | |
| .SetPhoneNumberRequired(true) | |
| .SetShippingAddressRequired(true) | |
| .SetEstimatedTotalPrice("20.00") | |
| .SetCurrencyCode("USD") | |
| .Build(); | |
| _walletFragment.Initialize(WalletFragmentInitParams.NewBuilder() | |
| .SetMaskedWalletRequest(maskedWalletRequest) | |
| .SetMaskedWalletRequestCode(MaskedWalletRequest) | |
| .Build()); | |
| SupportFragmentManager.BeginTransaction() | |
| .Replace(Resource.Id.container, _walletFragment).Commit(); | |
| } | |
| class AndroidPayCallback : Java.Lang.Object, IResultCallback | |
| { | |
| readonly SupportWalletFragment _walletFragment; | |
| public AndroidPayCallback(SupportWalletFragment walletFragment) | |
| { | |
| _walletFragment = walletFragment; | |
| } | |
| public void OnResult(Java.Lang.Object result) | |
| { | |
| if (_walletFragment != null) | |
| { | |
| var enabled = ((BooleanResult)result).Status.IsSuccess && ((BooleanResult)result).Value; | |
| _walletFragment.SetEnabled(enabled); | |
| if (!enabled) | |
| { | |
| Toast.MakeText(_walletFragment.Activity, "Android Pay is not available on your device", ToastLength.Short).Show(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment