GithubHelp home page GithubHelp logo

thanhit93 / react-native-braintree-xplat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from simonasdev/react-native-braintree-xplat

0.0 2.0 0.0 7.15 MB

Cross-platform Braintree module for React Native

License: MIT License

Java 0.51% Python 0.06% JavaScript 0.48% Objective-C 79.53% Ruby 0.98% C 5.01% Swift 13.36% Shell 0.07%

react-native-braintree-xplat's Introduction

react-native-braintree-xplat

npm version

An effort to merge react-native-braintree and react-native-braintree-android

iOS Installation

You can use the React Native CLI to add native dependencies automatically:

$ react-native link

or do it manually as described below:

  1. Run npm install react-native-braintree-xplat --save
  2. Open your project in XCode, right click on Libraries and click Add Files to "Your Project Name" Look under node_modules/react-native-braintree-xplat and add RCTBraintree.xcodeproj.
  3. Add libRCTBraintree.a to Build Phases -> Link Binary With Libraries
  4. Done!

Android Installation

Run npm install react-native-braintree-xplat --save

RN 0.29 and over

In android/settings.gradle

...

include ':react-native-braintree-xplat'
project(':react-native-braintree-xplat').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-braintree-xplat/android')

In android/app/build.gradle

...

dependencies {
    ...

    compile project(':react-native-braintree-xplat')
}

Register module (in MainApplication.java)

import com.pw.droplet.braintree.BraintreePackage; // <--- Import Package
import android.content.Intent; // <--- Import Intent

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new BraintreePackage() // <--- Initialize the package
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
      return mReactNativeHost;
  }
}

RN 0.28 and under

In android/settings.gradle

...

include ':react-native-braintree-xplat'
project(':react-native-braintree-xplat').projectDir = file('../node_modules/react-native-braintree-xplat/android')

In android/app/build.gradle

...

dependencies {
    ...

    compile project(':react-native-braintree-xplat')
}

Register module (in MainActivity.java)

import com.pw.droplet.braintree.BraintreePackage; // <--- Import Package
import android.content.Intent; // <--- Import Intent

public class MainActivity extends ReactActivity {
    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "example";
    }

    /**
     * Returns whether dev mode should be enabled.
     * This enables e.g. the dev menu.
     */
    @Override
    protected boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    /**
     * A list of packages used by the app. If the app uses additional views
     * or modules besides the default ones, add more packages here.
     */
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new BraintreePackage() // <---  Initialize the Package
        );
    }
}

Usage

Setup

var BTClient = require('react-native-braintree-xplat');
BTClient.setup(<token>);

You can find a demo client token here.

Show Payment Screen (Android & iOS)

v.zero

BTClient.showPaymentViewController(options).then(function(nonce) {
  //payment succeeded, pass nonce to server
})
.catch(function(err) {
  //error handling
});

Options

  • [iOS] bgColor - Background color for the view.
  • [iOS] tintColor - Tint color for the view.
  • [iOS] barBgColor - Background color for the navbar.
  • [iOS] barTintColor - Tint color for the navbar.
  • [iOS] callToActionText - Text for call to action button. (Works for both Android and iOS)
  • threeDSecure - If you want to enable 3DSecure, pass an object with an amount key that takes a number value

Example:

const options = {
  bgColor: '#FFF',
  tintColor: 'orange',
  callToActionText: 'Save',
  threeDSecure: {
    amount: 1.0
  }
}

PayPal only

BTClient.showPayPalViewController().then(function(nonce) {
  //payment succeeded, pass nonce to server
})
.catch(function(err) {
  //error handling
});

Custom Integration

If you only want to tokenize credit card information, you can use the following:

const card = {
  number: "4111111111111111",
  expirationDate: "10/20", // or "10/2020" or any valid date
  cvv: "400",
}

BTClient.getCardNonce(card).then(function(nonce) {
  //payment succeeded, pass nonce to server
})
.catch(function(err) {
  //error handling
});

// Full list of card parameters:
type Card = {
  number: string,
  cvv: string,
  expirationDate: string,
  cardholderName: string,
  firstName: string,
  lastName: string,
  company: string,
  countryName: string,
  countryCodeAlpha2: string,
  countryCodeAlpha3: string,
  countryCodeNumeric: string,
  locality: string,
  postalCode: string,
  region: string,
  streetAddress: string,
  extendedAddress: string,
}

One Touch on iOS

To take advantage of One Touch, there are additional setup required:

  1. Register a URL scheme in Xcode (should always start with YOUR Bundle ID) More info here TL;DR

Add CFBundleURLTypes to Info.Plist

	<key>CFBundleURLTypes</key>
	<array>
	<dict>
		<key>CFBundleTypeRole</key>
		<string>Editor</string>
		<key>CFBundleURLName</key>
		<string>your.bundle.id</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<string>your.bundle.id.payments</string>
		</array>
	</dict>
	</array>

WhiteList

If your app is built using iOS 9 as its Base SDK, then you must add URLs to a whitelist in your app's info.plist

   <key>LSApplicationQueriesSchemes</key>
   <array>
     <string>com.paypal.ppclient.touch.v1</string>
     <string>com.paypal.ppclient.touch.v2</string>
     <string>com.venmo.touch.v2</string>
   </array>
  1. For iOS: Use setupWithURLScheme instead, passing the url scheme you have registered in previous step
var BTClient = require('react-native-braintree');
BTClient.setupWithURLScheme(token, 'your.bundle.id.payments');

For xplat, you can do something like this:

 if (Platform.OS === 'ios') {
      BTClient.setupWithURLScheme(token, 'your.bundle.id.payments');
  } else {
      BTClient.setup(token);
  }
  1. Add this delegate method (callback) to your AppDelegate.m
#import "RCTBraintree.h"

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[RCTBraintree sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

Credits

Big thanks to @alanhhwong and @surialabs for the original ios & android modules.

react-native-braintree-xplat's People

Contributors

kraffslol avatar blakewilliams avatar edvinasbartkus avatar minishlink avatar the-simian avatar aroth avatar darron-haworth avatar adrianchinghc avatar andreyco avatar gameboyvito avatar ferdinandvhagen avatar tsubery avatar iancanderson avatar faceyspacey avatar damathryx avatar simonasdev avatar stevepotter avatar tsmmark avatar umars-menlo avatar vansonleung avatar kylejones avatar

Watchers

James Cloos avatar Thanh Truong 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.