• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Paypal wrapper for React Native

react-native-paypal

A Cross platform React Native interface for the PayPal Payment UI. Supports both iOS and Android currently, but can be used seperately.

Demo of a Payment using PayPal

Usage

--

Initiating a payment is as simple as creating a single promise

let PayPal = require('react-native-paypal');
PayPal.paymentRequest({
  clientId: 'AbyfNDFV53djg6w4yYgiug_JaDfBSUiYI7o6NM9HE1CQ_qk9XxbUX0nwcPXXQHaNAWYtDfphQtWB3q4R',
  environment: PayPalAndroid.SANDBOX,
  price: '42.00',
  currency: 'USD',
  description: 'PayPal Test'
}).then((confirm, payment) => console.log('Paid'); /* At this point you should verify payment independently */)
.catch((error_code) => console.error('Failed to pay through PayPal'));

Callback parameters:

If all goes OK with the payment than the paymentRequest promise is resolved with the following arguments as JSON strings:

  • A confirm:
{
  "client": {
    "environment": "mock",
    "paypal_sdk_version": "2.12.4",
    "platform": "Android",
    "product_name": "PayPal-Android-SDK"
  },
  "response": {
    "create_time": "2014-02-12T22:29:49Z",
    "id": "PAY-6RV70583SB702805EKEYSZ6Y",
    "intent": "sale",
    "state": "approved"
  },
  "response_type": "payment"
}
  • A payment:
{
  "amount": "1.00",
  "currency_code": "USD",
  "short_description": "PayPal Test",
  "intent": "sale"
}

Handling callbacks:

PayPal.paymentRequest(...).then(function (payment, confirm) {
  sendPaymentToConfirmInServer(payment, confirm);
})

If anything fails the promise will be notify an error with a code which will be one of:

  • USER_CANCELLED
  • INVALID_CONFIG

Handling failures:

PayPal.paymentRequest(...).catch(function (error_code) {
    if (error_code == PayPal.USER_CANCELLED) {
        // User didn't complete the payment
    } else if (error_code == PayPal.INVALID_CONFIG) {
        // Invalid config was sent to PayPal
    }
})

Setup

Android

  1. Add react-navive-paypal to your project
npm install --save react-native-paypal
  1. Add the following to android/app/build.gradle
dependencies {
    // ...
    compile project(':react-native-paypal')
}
  1. Add the following to android/settings.gradle
include ':react-native-paypal'
project(':react-native-paypal').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-paypal/android')
  1. Edit android/src/.../MainActivity.java
// ...
import br.com.vizir.rn.paypal.PayPalPackage; // <--
import br.com.vizir.rn.paypal.PayPal; // <--
import android.content.Intent; // <--

public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
    // ...
    private static final int PAY_PAL_REQUEST_ID = 9; // <-- Can be any unique number
    private PayPalPackage payPalPackage; // <--

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
        payPalPackage = new PayPalPackage(this, PAY_PAL_REQUEST_ID); // <--

        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModuleName("index.android")
                .addPackage(new MainReactPackage())
                // ...
                .addPackage(payPalPackage) // <--
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        // ...
    }

    // ...

    @Override
    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == PAY_PAL_REQUEST_ID) {
           payPalPackage.handleActivityResult(requestCode, resultCode, data); // <--
       } else {
           otherModulesHandlers(requestCode, resultCode, data);
       }
    }
}

iOS

Sample App

You'll find an iOS example in <root>/ios/Example/. You will need to run npm install in that directory in order to download the dependencies required by the sample application. Note this sample still uses manual NativeModule usage, and you'll likely find the above Javascript simpler than the JS used in this sample.

Installation

Currently you have to install via npm from GitHub (or change the version specifier in package.json to MattFoley/react-native-paypal):

npm install --save MattFoley/react-native-paypal

There will be an updated npm release shortly, which will allow

Install the PayPal-iOS-SDK

You then have to install the PayPal-iOS-SDK into node_modules/react-native-paypal/ios/lib/Paypal

Here's a one-liner to download and unpack version 2.13.0:

mkdir -p node_modules/react-native-paypal/ios/lib/Paypal && curl -L --progress https://github.com/paypal/PayPal-iOS-SDK/archive/2.13.0.tar.gz | tar -xz - -C node_modules/react-native-paypal/ios/lib/Paypal --strip-components=1

Include PayPal as normally, following their directions. Their integration steps and iOS SDK can be found here. After doing that, also drag MFLReactNativePayPal.h and MFLReactNativePayPal.m into your project.

Add MFLReactNativePayPal.xcodeproj

Add node_modules/react-native-paypal/ios/MFLReactNativePayPal.xcodeproj to the Libraries group in iOS and link libMFLReactNativePayPal.a as described in Step 2 of the React Native Manual Linking docs.

Follow steps 4 and 5 of the PayPal instalation instructions, as well as the additional steps here. This has to be done for the main app, not for the library you included.

Manual Usage (iOS Only)

You can access the NativeModule directly on iOS using the following instructions, if you'd like to modify the functionality.

###Initialization:

 import {
   NativeModules
 } from 'react-native';
 let MFLReactNativePayPal = NativeModules.MFLReactNativePayPal;
 MFLReactNativePayPal.initializePaypalEnvironment(<environment>, "<Your client id>");

#####Environment Values that should be used here are declared in MFLReactNativePayPal.h

 • Sandbox = 0
 • Production = 1
 • No Network = 2

###Prepare a Payment:

 MFLReactNativePayPal.preparePaymentOfAmount(<Amount as a Float>, <Currency Code>, <Short description>);
 ie: MFLReactNativePayPal.preparePaymentOfAmount(100.00, "USD", "Bacon");

###Prepare Configuration:

 MFLReactNativePayPal.prepareConfigurationForMerchant(<Merchant Name>, <BOOL should accept credit cards>, <User email>);
 MFLReactNativePayPal.prepareConfigurationForMerchant("Bacon Truck", true, "[email protected]");

###Present the payment flow with a completion handler:

 MFLReactNativePayPal.presentPaymentViewControllerForPreparedPurchase((error, payload) => {
    if (error) {
      //Handle Error
      return;
    } else {

     console.log("payload: " + payload);
     if (payload.status == 1) {
        console.log(payload.confirmation);
     } else {
        console.log("User cancelled payment");
     }
    }
 });

TODO:

 * [ ] Android Sample App
 * [ ] Refactor & cleanup
 * [ ] Automated tests
 * [ ] Future payment (subscriptions)
 * [ ] Invoice and optional fields - https://github.com/MattFoley/react-native-paypal/issues/6

Thanks

The Android portion of this library was originally built by https://github.com/Vizir for https://github.com/Vizir/react-native-paypal. The merging of the API in order to provide cross platform support was done almost entirely by https://github.com/amiuhle.

More Repositories

1

MFLTransformingDigits

Beginnings of a fun animation project in UIBezierPaths, inspired by Timely for Android
Objective-C
265
star
2

Smerk

Repository for easily building video detection in iOS, faces to barcodes to QR codes
Objective-C
224
star
3

UIDynamics-Helpers

This repository is filled with helping code for UIDynamics, simple dynamics examples, collisions, motion detection, etc.
Objective-C
101
star
4

MFLHintLabel

MFLHintLabel is a text animation library based on manipulating the individual characters in a label.
Objective-C
84
star
5

SWCrawlView

Open source repo for interactive Star Wars Crawl View, as close to theatrically accurate as possible.
Objective-C
76
star
6

MFLFillableTextLoader

Loading slash progress indicator based on filling stroked text.
Objective-C
66
star
7

react-native-fancy-label

Cross platform (eventually) class for styling text with gradients, strokes, inner/outer shadows with blurring, as well as fading.
Objective-C
41
star
8

AppiraterPlus

This is Appirater that you all know and love but with a twist. This version is now ARC compatible, and is converted to use a .xib file instead of a UIAlertView.
Objective-C
35
star
9

MFRedditPostController

Open Source control w/ optional GUI to handle any type of Reddit Post you want to make.
Objective-C
26
star
10

MFLJoystick

Joystick for iOS - not Cocos2D. I wanted one that functioned as well as GTA Vice City, and there weren't any already existing. Written in ObjC and Swift!
Swift
19
star
11

VIWelcomeTutorial-iOS

VIIntroductionView class based on MYIntroductionView, but rewritten with deeper customization and following our coding standards.
Objective-C
9
star
12

MFLYoutubeUploader

Incredibly simple YouTube uploading
Objective-C
7
star
13

react-native-stanza-demo

Test repository for Stanza.io with React Native.
JavaScript
5
star
14

VIRemoteDocumentController

Class to handle opening remote documents in UIDocumentInteractionController
Objective-C
2
star
15

TjTickerView

iOS 5 Ticking text controller, with clickable UIButtons, ARC compatibility, and multiple simultaneous scrolling buttons.
Objective-C
2
star