Easier in-app payments with fxpay

For developers building web applications on Firefox OS or Firefox Desktop, supporting payments is easy with Mozilla’s fxpay library. In addition to accepting credit cards, Mozilla’s payment system lets users charge purchases directly to their phone bill in many countries—making it ideal for mobile commerce.

Since our first introduction to fxpay the library has received a lot of bug fixes and new features. Based on developer feedback, we also decided to offer a new interface supporting native promises (shimmed on older browsers) for flexibility and better error handling. This article explains how to retrieve products, process payments, and restore products from receipts.

If you’ve already set up in-app payments using the mozPay API directly, consider porting to fxpay for the convenience of Mozilla-hosted products and additional features such as desktop payments. At the very least, please make sure your JWT libraries are patched for the latest security vulnerabilities.

Let’s get started! After installing the fxpay library, you can begin testing it out with some fake products.

fxpay.configure({fakeProducts: true});
fxpay.getProducts()
  .then(function(products) {
    products.forEach(function(product) {
      addBuyButtonForProduct(product);
    });
  })
  .catch(function(error) {
    console.error('error getting products: ' + error);
  });

This retrieves some pre-configured fake products that you can play around with. Once you’ve configured real products on the Firefox Marketplace Developer Hub, you can remove this configuration value to work with real products.

In the example function called above, you could display a buy button per product like this:

function addBuyButtonForProduct(product) {
  var button = document.createElement('button');
  button.textContent = 'Buy ' + product.name;
  button.addEventListener('click', function () {
 
    fxpay.purchase(product.productId)
      .then(function(purchasedProduct) {
        console.log('product purchased! ', 
                    purchasedProduct);
      })
      .catch(function(error) {
        console.error('error purchasing: ' + error);
      });
 
  });
  document.body.appendChild(button);
}

The fxpay library does all the payment processing behind the scenes using Mozilla’s web services so when the promise resolves, it’s safe to deliver the product. At this time, fxpay also installs a receipt on the user’s device. When the user returns to your app later on, you’ll want to check for any receipts so you can restore their purchases.

Here’s a rewrite of the product fetching code to restore purchased products:

fxpay.getProducts()
  .then(function(products) {
    products.forEach(function(product) {
    
      if (product.hasReceipt()) {
        product.validateReceipt()
          .then(function(restoredProduct) {
            console.log('restored product from receipt:', 
                        restoredProduct);
          })
          .catch(function(error) {
            console.error('error validating receipt: ' + 
                          error);
          });
      } else {
        addBuyButtonForProduct(product);
      }
    
    });
  });

We’re hoping this new interface makes experimenting with in-app payments even easier than before. You never know what kind of business model will work in your app so why not try out some ideas?

The complete usage guide to fxpay is here on MDN.

About kumar303

Kumar hacks on Mozilla web services and tools for various projects, such as those supporting Firefox Add-ons. He hacks on lots of random open source projects too.

More articles by kumar303…


4 comments

  1. Anders

    Does this work on normal webpages? and if not why?

    May 7th, 2015 at 12:23

  2. Kumar McMillan

    Hi Anders. It does work on normal web pages. We’ve tested on Chrome but not extensively. We’re not “officially” supporting other browsers right now only because we’re trying to keep our QA surface slim.

    May 7th, 2015 at 13:20

    1. Anders

      It do indeed seem to work. Demo:
      http://output.jsbin.com/ranajavure
      So I can use fxpay for my webshop now?

      May 7th, 2015 at 15:44

  3. Kumar McMillan

    Sure, I suppose so. Our main focus is apps at the minute so to sell real products for real money you’d have to submit an app for approval to the Firefox Marketplace.

    May 8th, 2015 at 11:13

Comments are closed for this article.