2.6.9 • Published 7 years ago

nativescript-paypal v2.6.9

Weekly downloads
56
License
MIT
Repository
github
Last release
7 years ago

npm npm

NativeScript PayPal

NativeScript module for implementing simple PayPal checkouts using official SDK.

Donate

License

MIT license

Platforms

  • Android

Roadmap

Installation

Run

tns plugin add nativescript-paypal

inside your app project to install the module.

Android

AndroidManifest.xml

Keep sure to define the following permissions, activities and other data in your manifest file:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <action android:name="android.intent.action.MAIN" />
 
    <category android:name="android.intent.category.LAUNCHER" />

    <uses-feature android:name="android.hardware.camera"
                  android:required="false" />
    <uses-feature android:name="android.hardware.camera.autofocus"
                  android:required="false" />

    <application>
        <activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PayPalFuturePaymentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.FuturePaymentConsentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity" />
        <activity android:name="io.card.payment.DataEntryActivity" />
		
	    <service android:name="com.paypal.android.sdk.payments.PayPalService"
                 android:exported="false" />
    </application>
    
</manifest>

app.gradle

Keep sure to have a reference to the PayPal SDK in your app/App_Resources/Android/app.gradle file of your project.

dependencies {
    // PayPal
    compile 'com.paypal.sdk:paypal-android-sdk:2.14.2'
}

Demo

For quick start have a look at the demo/app/main-view-model.js file of the demo app to learn how it works.

Otherwise ...

Usage

Include

Include the module in your code-behind:

var PayPal = require("nativescript-paypal");

Initialize

Initialize the environment:

function onPageLoaded(args) {
    PayPal.init({
        clientId: '<YOUR-CLIENT-ID>'
    });
}
exports.onPageLoaded = onPageLoaded;

The (optional) object that is submitted to the PayPal.init function has the following structure:

Properties

NameDescription
acceptCreditCardsOPTIONAL Accept credit cards or not. Default: (true)
accountOPTIONAL Defines information about the account.
clientIdThe PayPal ID for your app that was generated in the PayPal Developer Portal.
defaultsOPTIONAL Defines default data.
environmentOPTIONAL The environment to use. Possible values are: 0 = ENVIRONMENT_SANDBOX, 1 = ENVIRONMENT_PRODUCTION, 2 = ENVIRONMENT_NO_NETWORK.
onActivityResultOPTIONAL Logic for Activity.onActivityResult method of the underlying Android activity that is used to invoke logic for other modules, e.g.
rememberUserOPTIONAL Remember the last user for the next payment or not. Default: (true)
requestCodeOPTIONAL The custom request code to use (e.g. for Activity.onActivityResult Android method). Default: 230958624
account

The account object has the following structure:

Properties
NameDescription
nameOPTIONAL The name of the merchant.
privacyPolicyOPTIONAL The URI to the privacy policy of the merchant.
userAgreementOPTIONAL The URI to the user agreement of the merchant.
defaults

The defaults object has the following structure:

Properties
NameDescription
userEmailOPTIONAL The default user email.
userPhoneOPTIONAL The default user phone.
userPhoneCountryCodeOPTIONAL The default user phone country code.

Start a payment

function buyProduct(args) {
    // configure
    var payment = PayPal.newPayment()
        .setDescription('My product')
        .setAmount(59.79);

    // start checkout / payment
    payment.start(function(cbResult) {
        switch (cbResult.code) {
            case 0:
                // SUCCESS
                // pay key is stored in 'cbResult.key'
                break;
                
            case 1:
                // operation was CANCELLED
                break;
                
            case -1:
                // checkout FAILED
                break;
                
            case -2:
                // "unhandled exception"
                break;
        }
    });
}
exports.buyProduct = buyProduct;

The payment object that is created by PayPal.newPayment function has the following structure.

Methods

NameDescription
getAmountGets the price. Example: var a = payment.getAmount();
getBnCodeGets the BN code. Example: var bc = payment.getBnCode();
getCurrencyGets the custom currency to use. Example: var c = payment.getCurrency();
getCustomGets the custom value for the payment. Example: var c = payment.getCustom();
getDescriptionGets the (short) description. Example: var d = payment.getDescription();
getDetailsGets an object with the payment details. Example: var d = payment.getDetails();
getInvoiceNumberGets the custom invoice number. Example: var i = payment.getInvoiceNumber();
setAmountSets the price. Example: payment.setAmount(1.25);
setBnCodeSets a BN code. Example: payment.setBnCode('Your BN Code');
setCurrencySets the custom currency to use. Example: payment.setCurrency('EUR');
setCustomSets the custom value for the payment. Example: payment.setCustom('MY-PRODUCT-ID');
setDetailsSets details (shipping, subtotal & tax). Example: payment.setDetails(4.95, 199.99, 1.19);
setDescriptionSets the (short) description. Example: payment.setDescription('This is really awesom!');
setInvoiceNumberSets the custom invoice number. Example: payment.setInvoiceNumber('MY_INVOICE-666');
startStarts the payment / checkout process.
start

The callback that is submitted to the payment.start method receives an object with the following properties:

NameDescription
codeThe result code. 0 = success, -3 = JSON parse error, -2 = unhandled exception, -1 = checkout failed, 1 = cancelled, 2 = no confirm data, 3 = no JSON data
keyThe key of the payment (if code = 0)

Enhancements

Logging

If you want to get the logging output of the module, you can use PayPal.addLogger function to add a callback that receives a message from the module:

PayPal.addLogger(function(msg) {
    console.log('[nativescript-paypal]: ' + msg);
});
2.6.9

7 years ago

2.6.8

7 years ago

2.6.7

8 years ago

2.6.5

8 years ago

2.6.4

8 years ago

2.6.3

8 years ago

2.6.2

8 years ago

2.6.1

8 years ago

2.6.0

8 years ago

2.5.0

8 years ago

2.4.0

8 years ago

2.3.0

8 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.3.5

8 years ago

1.3.4

8 years ago

1.3.3

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago