{"id":21168,"date":"2015-09-17T11:18:37","date_gmt":"2015-09-17T18:18:37","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=21168"},"modified":"2015-09-17T11:18:37","modified_gmt":"2015-09-17T18:18:37","slug":"bring-android-pay-to-your-apps-with-stripe","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/bring-android-pay-to-your-apps-with-stripe\/","title":{"rendered":"Bring Android Pay to Your Apps with Stripe"},"content":{"rendered":"<p>\t\t\t\t<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/android-pay-stripe1.png\"><img decoding=\"async\" class=\"alignright wp-image-21173 size-full\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/android-pay-stripe1.png\" alt=\"Android Pay + Stripe\" width=\"275\" height=\"434\" \/><\/a>Android Pay has just launched, and Xamarin has everything you need to get your apps ready. The <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.GooglePlayServices.Wallet\/\" title=\"Google Play Services - Wallet NuGet Package\" target=\"_blank\">Google Play Services &#8211; Wallet<\/a> NuGet adds the necessary APIs for Android Pay integration.<\/p>\n<p>We&#8217;re also proud to announce that the <a href=\"#\">Xamarin Stripe Component<\/a> is Android Pay compatible! We&#8217;ve updated the sample to show you just how easy it is to process Android Pay transactions through Stripe.<\/p>\n<p>After installing the <a href=\"#\">Google Play Services &#8211; Wallet<\/a> NuGet package and the <a href=\"#\">Xamarin Stripe Component<\/a>, start by initializing the Google API client in your Activity with the appropriate API:<\/p>\n<pre class=\"lang:csharp decode:true\">\ngoogleApiClient = new GoogleApiClientBuilder (this)\n    .AddConnectionCallbacks (this)\n    .AddOnConnectionFailedListener (this)\n    .AddApi (WalletClass.API, b)\n    .Build ();\n<\/pre>\n<p>Google provides the <em>SupportWalletFragment<\/em> class, which will display a branded <em>Purchase<\/em> button within the fragment:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar walletFragment = SupportWalletFragment.NewInstance (WalletFragmentOptions.NewBuilder ()\n    .SetEnvironment (WalletConstants.EnvironmentSandbox)\n    .SetMode (WalletFragmentMode.BuyButton)\n    .SetTheme (WalletConstants.ThemeLight)\n    .SetFragmentStyle (new WalletFragmentStyle ()\n        .SetBuyButtonText (BuyButtonText.BuyWithGoogle)\n        .SetBuyButtonAppearance (BuyButtonAppearance.Classic)\n        .SetBuyButtonWidth (Dimension.MatchParent))\n    .Build ());\n<\/pre>\n<p>The <em>MaskedWalletRequest<\/em> class is used to build a new purchase request. You could use any payment gateway that can accept EMVCO Network Tokens, or in this case set up your Stripe account as the payment gateway with some configuration options:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar maskedWalletRequest = MaskedWalletRequest.NewBuilder ()\n\n\/\/ Request credit card tokenization with Stripe\n.SetPaymentMethodTokenizationParameters (\n    PaymentMethodTokenizationParameters.NewBuilder ()\n        .SetPaymentMethodTokenizationType (PaymentMethodTokenizationType.PaymentGateway)\n        .AddParameter (\"gateway\", \"stripe\")\n        .AddParameter (\"stripe:publishableKey\", STRIPE_PUBLISHABLE_KEY)\n        .AddParameter (\"stripe:version\", \"1.15.1\")\n        .Build ())\n    .SetShippingAddressRequired (true)\n    .SetMerchantName (\"Xamarin\")\n    .SetPhoneNumberRequired (true)\n    .SetShippingAddressRequired (true)\n    .SetEstimatedTotalPrice (\"20.00\")\n    .SetCurrencyCode (\"USD\")\n    .Build();\n<\/pre>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/android-pay-flow-animated-cut.gif\"><img decoding=\"async\" class=\"alignright size-full wp-image-21186\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/android-pay-flow-animated-cut.gif\" alt=\"Android Pay UI Flow\" width=\"240\" height=\"426\" \/><\/a><\/p>\n<p>Once the request has been built, it needs to be assigned to the wallet fragment. This will cause the request to be used when the user taps the Payment button:<\/p>\n<pre class=\"lang:csharp decode:true\">\nwalletFragment.Initialize (WalletFragmentInitParams.NewBuilder ()\n    .SetMaskedWalletRequest (maskedWalletRequest)\n    .SetMaskedWalletRequestCode (LOAD_MASKED_WALLET_REQ_CODE)\n    .Build ());\n\nSupportFragmentManager.BeginTransaction ()\n    .Replace (Resource.Id.frameFragment, walletFragment).Commit ();\n<\/pre>\n<p>When the Buy button is tapped, your <em>MaskedWalletRequest<\/em> will be executed. At this point, Google will handle asking the user which account and payment method they&#8217;d like to use, as well as their shipping address (if you requested one) by presenting them with the necessary dialogues.<\/p>\n<p>Eventually when the user has entered the required information, your activity&#8217;s <em>OnActivityResult<\/em> will be called. At this point you can use the result to create a <em>FullWalletRequest<\/em> to finalize the transaction:<\/p>\n<pre class=\"lang:csharp decode:true\">\nprotected override void OnActivityResult (int requestCode, Result resultCode, Intent data)\n{\n    if (requestCode == LOAD_MASKED_WALLET_REQ_CODE &amp;&amp; resultCode == Result.Ok) {\n\n        var maskedWallet = data.GetParcelableExtra (WalletConstants.ExtraMaskedWallet)\n            .JavaCast ();\n\n        var fullWalletRequest = FullWalletRequest.NewBuilder ()\n            .SetCart (Cart.NewBuilder ()\n                .SetCurrencyCode (\"USD\")\n                .SetTotalPrice (\"20.00\")\n                .AddLineItem (LineItem.NewBuilder () \/\/ Identify item being purchased\n                    .SetCurrencyCode (\"USD\")\n                    .SetQuantity (\"1\")\n                    .SetDescription (\"Premium Banana\")\n                    .SetTotalPrice (\"20.00\")\n                    .SetUnitPrice (\"20.00\")\n                    .Build ())\n                .Build ())\n            .SetGoogleTransactionId (maskedWallet.GoogleTransactionId)\n            .Build ();\n\n            WalletClass.Payments.LoadFullWallet (googleApiClient, fullWalletRequest, \n                LOAD_FULL_WALLET_REQ_CODE);\n    }\n}\n<\/pre>\n<p>Once the request to load a full wallet completes, your activity&#8217;s <em>OnActivityResult<\/em> will once again be called with the results. You can parse the Stripe Token object from these results:<\/p>\n<pre class=\"lang:csharp decode:true\">\nprotected override void OnActivityResult (int requestCode, Result resultCode, Intent data)\n{\n    if (requestCode == LOAD_FULL_WALLET_REQ_CODE &amp;&amp; resultCode == Result.Ok) {\n        var fullWallet = data.GetParcelableExtra (WalletConstants.ExtraFullWallet)\n            .JavaCast ();\n\n        var tokenJson = fullWallet.PaymentMethodToken.Token;\n\n        var stripeToken = Stripe.Token.FromJson (tokenJson);\n    }\n}\n<\/pre>\n<p>The <em>stripeToken<\/em> is what you will send to your server, which is responsible for communicating the actual payment request to Stripe&#8217;s servers.<\/p>\n<p>For the complete example of integrating Android Pay and Stripe in your app, check out the sample in the <a href=\"#\">Xamarin Stripe Component<\/a>.<\/p>\n<p><em>Note: you may continue to see \u2018Google Wallet\u2019 branding until Android Pay is fully rolled out.<\/em>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android Pay has just launched, and Xamarin has everything you need to get your apps ready. The Google Play Services &#8211; Wallet NuGet adds the necessary APIs for Android Pay integration. We&#8217;re also proud to announce that the Xamarin Stripe Component is Android Pay compatible! We&#8217;ve updated the sample to show you just how easy [&hellip;]<\/p>\n","protected":false},"author":560,"featured_media":21185,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-21168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Android Pay has just launched, and Xamarin has everything you need to get your apps ready. The Google Play Services &#8211; Wallet NuGet adds the necessary APIs for Android Pay integration. We&#8217;re also proud to announce that the Xamarin Stripe Component is Android Pay compatible! We&#8217;ve updated the sample to show you just how easy [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/21168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/560"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=21168"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/21168\/revisions"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=21168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=21168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=21168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}