Integrate PayPal with Cordova

Paypal is an international payment gateway provider where you can send and receive your money with multiple currencies.commission rates will be different based on their countries

Prerequisites

Paypal Integration requires an account with PayPal.you can create new PayPal account for free

Initialising PayPal

<span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"deviceready"</span>, onDeviceReady);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onDeviceReady</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> clientIDs = {
        <span class="hljs-string">"PayPalEnvironmentProduction"</span>: <span class="hljs-string">"YOUR_PRODUCTION_KEY"</span>, <span class="hljs-comment">// not needed while testing</span>
        <span class="hljs-string">"PayPalEnvironmentSandbox"</span>: <span class="hljs-string">"YOUR_SANDBOX_KEY"</span>
    };

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onPayPalMobileInit</span>(<span class="hljs-params"></span>) </span>{
        PayPalMobile.prepareToRender(
            <span class="hljs-string">"PayPalEnvironmentSandbox"</span>, <span class="hljs-comment">// or "PayPalEnvironmentProduction" </span>
            <span class="hljs-keyword">new</span> PayPalConfiguration({
                <span class="hljs-attr">merchantName</span>: <span class="hljs-string">"Shop Name"</span>,
                <span class="hljs-attr">acceptCreditCards</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">merchantPrivacyPolicyURL</span>: <span class="hljs-string">"http://shopname.com/policy"</span>,
                <span class="hljs-attr">merchantUserAgreementURL</span>: <span class="hljs-string">"http://shopname.com/agreement"</span>
            }),
            <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"OK, ready to accept payments!"</span>)
            });
    }
    PayPalMobile.init(clientIDs, onPayPalMobileInit);
}

Receiving Payment

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">payNow</span>(</span>) {
    var paymentDetails = <span class="hljs-keyword">new</span> PayPalPaymentDetails(<span class="hljs-string">"15.00"</span>, <span class="hljs-string">"3.00"</span>, <span class="hljs-string">"2.00"</span>);
    //<span class="hljs-keyword">new</span> PayPalPayment Details(<span class="hljs-string">"subtotal"</span>,<span class="hljs-string">"shipping"</span>,<span class="hljs-string">"tax"</span>);
    //PayPalPalment(<span class="hljs-string">"total"</span>,<span class="hljs-string">"currency in ISO 4217 format"</span>,<span class="hljs-string">"Description"</span>,<span class="hljs-string">"Sale"</span>,object);
    var payment = <span class="hljs-keyword">new</span> PayPalPayment(<span class="hljs-string">"20.00"</span>, <span class="hljs-string">"USD"</span>, <span class="hljs-string">"PhoneGapPro T-Shirt"</span>, <span class="hljs-string">"Sale"</span>, paymentDetails);

    PayPalMobile.renderSinglePaymentUI(payment,
        <span class="hljs-function"><span class="hljs-keyword">function</span>(</span>payment) {
            alert(<span class="hljs-string">"payment success: "</span> + JSON.stringify(payment));
        },
        <span class="hljs-function"><span class="hljs-keyword">function</span>(</span><span class="hljs-built_in">error</span>) {
            alert(<span class="hljs-built_in">error</span>);
        });
}

PayPalMobile.renderSinglePaymentUI() will invoke payment UI for receiving money.