<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Avaaj Gyawali</title>
    <description>The latest articles on DEV Community by Avaaj Gyawali (@aawazgyawali).</description>
    <link>https://dev.to/aawazgyawali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F47419%2F2f49011b-938a-4403-a69c-fecc8d0ff692.jpg</url>
      <title>DEV Community: Avaaj Gyawali</title>
      <link>https://dev.to/aawazgyawali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aawazgyawali"/>
    <language>en</language>
    <item>
      <title>Google Pay and Apple pay using Stripe on Flutter</title>
      <dc:creator>Avaaj Gyawali</dc:creator>
      <pubDate>Sat, 29 May 2021 19:12:51 +0000</pubDate>
      <link>https://dev.to/aawazgyawali/how-to-use-google-pay-and-apple-pay-using-stripe-on-flutter-2i1j</link>
      <guid>https://dev.to/aawazgyawali/how-to-use-google-pay-and-apple-pay-using-stripe-on-flutter-2i1j</guid>
      <description>&lt;p&gt;After lots of switching packages, configuring gradles, installing pods to make it work. I am finally happy to see google putting an end to this battle with the introduction of pay library. As the name is, its really simple and straight forward, yet I felt there must be some resource  out on the internet to help devs to implement such a feature in short time so that you can focus on developing something  really awesome.&lt;/p&gt;

&lt;p&gt;I am making it very simple where you can just copy paste bunch of lines below and make it work.&lt;/p&gt;

&lt;p&gt;First off all you need the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stripe account and publishable key&lt;/li&gt;
&lt;li&gt;Enable apple pay on stripe and upload certificate generated from &lt;a href="https://developer.apple.com/account/resources/certificates/list"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Google merchant account from &lt;a href="https://pay.google.com/business"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now lets start, &lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Apple Pay
&lt;/h3&gt;

&lt;p&gt;And the following json file as your assets file inside &lt;code&gt;/assets/apple_pay.json&lt;/code&gt; folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "provider": "apple_pay",
  "data": {
    "merchantIdentifier": "&amp;lt;Merchant ID from developer.apple.com&amp;gt;",
    "displayName": "&amp;lt;Display Name&amp;gt;",
    "merchantCapabilities": [
      "3DS",
      "debit",
      "credit"
    ],
    "supportedNetworks": [
      "amex",
      "visa",
      "discover",
      "masterCard"
    ],
    "countryCode": "FR", // Country code
    "currencyCode": "EUR", // Currency code
    "requiredBillingContactFields": null, 
    "requiredShippingContactFields": null
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Google Pay
&lt;/h3&gt;

&lt;p&gt;And the following json file as your assets file inside &lt;code&gt;/assets/google_pay.json&lt;/code&gt; folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "provider": "google_pay",
  "data": {
    "environment": "TEST",
    "apiVersion": 2,
    "apiVersionMinor": 0,
    "allowedPaymentMethods": [
      {
        "type": "CARD",
        "tokenizationSpecification": {
          "type": "PAYMENT_GATEWAY",
          "parameters": {
            "gateway": "stripe",
            "stripe:version": "2020-08-27",
            "stripe:publishableKey": "&amp;lt;stripe publishable key, eg pk_.......&amp;gt;"
          }
        },
        "parameters": {
          "allowedCardNetworks": [
            "VISA",
            "MASTERCARD"
          ],
          "allowedAuthMethods": [
            "PAN_ONLY",
            "CRYPTOGRAM_3DS"
          ],
          "billingAddressRequired": false
        }
      }
    ],
    "merchantInfo": {
      "merchantId": "&amp;lt;Merchant ID from pay.google.com/business&amp;gt;",
      "merchantName": "&amp;lt;Display Name&amp;gt;"
    },
    "transactionInfo": {
      "countryCode": "FR",
      "currencyCode": "EUR"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Flutter Code
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Install Package
&lt;/h2&gt;

&lt;p&gt;Inside your &lt;code&gt;pubspec.yaml&lt;/code&gt; file add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pay: 1.0.3 # Latest one at the time of writing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Initialize the package
&lt;/h2&gt;

&lt;p&gt;Somewhere on your code base first enable the package with the above json files from assets that you just added.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Pay _pay = Pay.withAssets(["google_pay.json","apple_pay.json"]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Check the availability
&lt;/h2&gt;

&lt;p&gt;Once you enable the package, now you can check the availability of the payment service. I link to maintain an array of providers that are available, to do so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    List&amp;lt;PayProvider&amp;gt; availableProviders = [];
    PayProvider.values.forEach((e) async {
      bool canPay = await _pay.userCanPay(e);
      if (canPay) availableProviders.add(e);
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Show Apple Pay buttons
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Option 1 (The quickest) (Either this one or the other one, I prefer the 2nd one)
&lt;/h3&gt;

&lt;p&gt;You can use provided native pay button that can directly process payments by writing everything on the widget to start making the payment happen.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Google Pay Button
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  if(availableProviders.contains(PayProvider.google_pay))
    GooglePayButton(
      paymentConfigurationAsset: "google_pay.json",
      onPaymentResult: (Map data){
        print(data);
      },
      paymentItems: [ // You can have multiple items on your payment
        PaymentItem(
          amount: "100", // Your amount
          label: "AwesomeProduct", // Your amount title
          status: pay.PaymentItemStatus.final_price, // Status of the payment
          type: pay.PaymentItemType.total, // Type of the payment
        )
      ],
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Apple Pay Button
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  if(availableProviders.contains(PayProvider.apple_pay))
    ApplePayButton(
      paymentConfigurationAsset: "apple_pay.json",
      onPaymentResult: (Map data){
        print(data);
      },
      paymentItems: [ // You can have multiple items on your payment
        PaymentItem(
          amount: "100", // Your amount
          label: "AwesomeProduct", // Your amount title
          status: pay.PaymentItemStatus.final_price, // Status of the payment
          type: pay.PaymentItemType.total, // Type of the payment
        )
      ],
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Option 2 (The best, IMO)
&lt;/h3&gt;

&lt;p&gt;If you want to separate business code and the UI code, this is the one for you. Instead of using the provided widget you can use any widget (Make sure you comply with button guidlies from &lt;a href="https://developer.apple.com/design/human-interface-guidelines/apple-pay/overview/buttons-and-marks/"&gt;Apple Pay&lt;/a&gt; or &lt;a href="https://developers.google.com/pay/api/web/guides/brand-guidelines"&gt;Google Pay&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Map&amp;lt;String, dynamic&amp;gt; data = await _pay.showPaymentSelector(
      provider: provider // Can be supported provider (PayProvider.apple_pay, PayProvider.google_pay),
      paymentItems: [
        PaymentItem(
          label: "Awesome Product",
          amount: "100",
          type: PaymentItemType.item,
          status: PaymentItemStatus.final_price,
        ),
      ],
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After any of the option, you will get a response with a &lt;code&gt;Map data&lt;/code&gt;. The reason it's a Map and not a typed value is every provider returns response on their own pattern so to support multiple providers, pay packages is making to map to make the package as raw as possible.&lt;/p&gt;

&lt;p&gt;From the response, all you need is token which should be as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String tokenToBeSentToCloud = data["token"];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Server Side
&lt;/h2&gt;

&lt;p&gt;Making actual charge to the user's card&lt;/p&gt;

&lt;p&gt;After you have a token, you now need to have a backend code(can also be on on flutter, not recommended) to process the payment. I am using a node.js server to process the payment using the token from mobile.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install the package
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm install stripe&lt;/code&gt; or &lt;code&gt;yarn add stripe&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialize the package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const stripe = new Stripe("&amp;lt;stripe_secret_key&amp;gt;", {
    apiVersion: "2020-08-27"
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Make a charge
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const paymentMethod = await stripe.paymentMethods.create({
        type: "card",
        card: {
            token: "&amp;lt;token from pay package&amp;gt;"
        }
    })

    await stripe.paymentIntents.create({
        amount: 10000, // Amount in cents, can be different from what you presented on the pay dialog on mobiel
        currency: "EUR", // Currentcy
        capture_method: "automatic", // 
        customer: "&amp;lt;Optional, if you have customer on stripe&amp;gt;",
        payment_method: paymentMethod.id,
        receipt_email: "&amp;lt;If you want to send reciept in email&amp;gt;,
        statement_descriptor: "&amp;lt;Statement Label(max 22 chars)&amp;gt;",
        confirm: true, // True charges the card instantly
        description: "Product description"
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila, you just charges a user.&lt;/p&gt;

&lt;p&gt;Now there are other cool things that you can do with stripe.js SDK, for that checkout stripe &lt;a href="https://stripe.com/docs/js"&gt;documentation&lt;/a&gt; for more features.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>applepay</category>
      <category>googlepay</category>
      <category>stripe</category>
    </item>
  </channel>
</rss>
