Giter VIP home page Giter VIP logo

react-native-in-app-purchase's Introduction

react-native-in-app-utils

A react-native wrapper for handling in-app purchases.

Notes

  • You need an Apple Developer account to use in-app purchases.

  • You have to set up your in-app purchases in iTunes Connect first. Follow this tutorial for an easy explanation.

  • You have to test your in-app purchases on a real device, in-app purchases will always fail on the Simulator.

Add it to your project

  1. Run npm install react-native-in-app-utils --save.

  2. Open your project in XCode, right click on Libraries, click Add Files to "Your Project Name" and add InAppUtils.xcodeproj. (situated in node_modules/react-native-in-app-utils/ios) (This then this, just with InAppUtils).

  3. Link libInAppUtils.a with your Libararies. To do that, click on your project folder, select Build Phases in the top bar, scroll to Link Binary with Libraries, press the + at the very bottom and add libInAppUtils.a from the node_modules/react-native-in-app-utils/ios/InAppUtils folder. (Screenshot).

  4. Whenever you want to use it within React code now you just have to do: var InAppUtils = require('NativeModules').InAppUtils;

API

Loading products

You have to load the products first to get the correctly internationalized name and price in the correct currency.

var products = [
   'com.xyz.abc',
];
InAppUtils.loadProducts(products, (error, products) => {
   //update store here.
});

Buy product

var productIdentifier = 'com.xyz.abc';
InAppUtils.purchaseProduct(productIdentifier, (error, response) => {
   if(response && response.productIdentifier) {
      AlertIOS.alert('Purchase Successful', 'Your Transaction ID is ' + response.transactionIdentifier);
      //unlock store here.
   }
});

Restore payments

InAppUtils.restorePurchases((error, products)=> {
   if(error) {
      AlertIOS.alert('itunes Error', 'Could not connect to itunes store.');
   } else {
      AlertIOS.alert('Restore Successful', 'Successfully restores all your purchases.');
      //unlock store here again.
   }
});

Receipts

iTunes receipts are associated to the users iTunes account and can be retrieved without any product reference.

InAppUtils.receiptData((error, receiptData)=> {
  if(error) {
    AlertIOS.alert('itunes Error', 'Receipt not found.');
  } else {
    //send to validation server
  }
});

Testing

To test your in-app purchases, you have to run the app on an actual device. Using the iOS Simulator, they will always fail.

  1. Set up a test account ("Sandbox Tester") in iTunes Connect. See the official documentation here.

  2. Run your app on an actual iOS device. To do so, first run the react-native server on the local network instead of localhost. Then connect your iDevice to your Mac via USB and select it from the list of available devices and simulators in the very top bar. (Next to the build and stop buttons)

  3. Open the app and buy something with your Sandbox Tester Apple Account!

react-native-in-app-purchase's People

Contributors

chirag04 avatar satya164 avatar mxstbr avatar marcgreenstock avatar markburns avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.