Showing posts with label Ionic Framework Paypal Payment Gateway. Show all posts
Showing posts with label Ionic Framework Paypal Payment Gateway. Show all posts

Ionic + Braintree JS integration

Ionic + Braintree JS integration



Hi Today Discussed  Ionic Framework Braintree JS Integration. Ionic Framework.Braintree Payments drop-in into an Ionic mobile app I have them with an ionic modal view. Here follows Code.
Ionic + Braintree JS integration
Ionic + Braintree JS integration

Html Page
<ion-content ng-controller="payController">

        <div ng-show="step == 'signup'">
          <div class="list">
            <label class="item item-input"><input type="text" placeholder="First name" ng-model="customer.firstname"></label>
            <label class="item item-input"><input type="text" placeholder="Last name" ng-model="customer.lastname"></label>
            <label class="item item-input"><input type="text" placeholder="Company" ng-model="customer.company"></label>
            <label class="item item-input"><input type="text" placeholder="email" ng-model="customer.email"></label>
            <label class="item item-input"><input type="text" placeholder="phone" ng-model="customer.phone"></label>
          </div>
          <div class="padding">
            <button class="button button-block button-positive" ng-click="signup(customer)" ng-disabled="!ready">Continue</button>
          </div>
        </div>

        <div ng-show="step == 'verification'">
          <div class="list">
            <label class="item item-input">
              <input type="text" placeholder="Cardholder name" ng-model="card.cardholder">
            </label>
            <label class="item item-input">
              <input type="text" placeholder="Number" ng-model="card.number">
            </label>
            <label class="item item-input">
              <input type="text" placeholder="CVV" ng-model="card.cvv">
            </label>

            <label class="item item-input">
              <span class="input-label">Expire date</span>
              <input type="text" placeholder="mm" ng-model="card.expiration_month">
              <input type="text" placeholder="yy" ng-model="card.expiration_year">
            </label>
          </div>
          <div class="padding">
            <button class="button button-block button-positive" ng-click="saveCard(card)" ng-disabled="!ready">Save</button>
          </div>
        </div>

        <div ng-show="step == 'checkout'">
          <div class="item range">
            <span>€</span>
            <input type="range" ng-model="amount" min="0" max="100">
            <span>{{ amount }}</span>
          </div>
          <div class="padding">
            <button class="button button-block button-positive" ng-click="pay(10)" ng-disabled="!ready">Pay</button>
          </div>
        </div>

        <div ng-show="step == 'done'">
          <p>Transaction completed with transaction id: {{ transactionId }}</p>
        </div>
App.js

 angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})


Services.js


angular
 .module('starter')
 .factory('braintreeService', braintreeFactory);

function braintreeFactory($http) {

 /**
  * Braintree server-side implementation
  * @type string
  */
 var Url = 'Api URL';

 return {

  getClientToken: function(customerId) {
   return $http
    .get(Url + '/Access_token' + (cus_Id ? '/' + cus_Id: ''))
    .then(function(response) {
     if (response.status === 200 && response.data !== undefined) {
      return response.data;
     }

     throw 'Invalid response';
    });
  },

  createCustomer: function(firstName, lastName, company, email, phone, fax, website) {
   var postData = {
    customer: {
     firstName: firstName,
     lastName: lastName,
     company: company,
     email: email,
     phone: phone,
     fax: fax,
     website: website
    }
   };

   return $http
    .post(Url + '/cus', postData)
    .then(function(response) {
     if (response.status === 200 && response.data !== undefined) {
      return response.data;
     }
    });
  },

  createPaymentMethod: function(customerId, nonce) {
   var postData = {
    paymentMethod: {
     customerId: customerId,
     paymentMethodNonce: nonce
    }
   };
   return $http
    .post(Url + '/payment_method', postData)
    .then(function(response) {
     if (response.status === 200 && response.data !== undefined) {
      return response.data;
     }
    });
  },

  sale: function(amount, paymentMethodToken) {
   var postData = {
    transaction: {
     amount: amount,
     paymentMethodToken, paymentMethodToken
    }
   };

   return $http
    .post(Url + '/sale', postData)
    .then(function(response) {
     if (response.status === 200 && response.data !== undefined) {
      return response.data;
     }
    });
  }

 };


};


Controller.js

angular
 .module('starter')
 .controller('payController', payController);

function payController($scope, braintreeService) {
 /**
  * Form steps
  * 1. signup - create customer
  * 2. verification - save & validate card
  * 3. checkout - charge the card
  * 4. done - displays transaction id
  */
 $scope.step = 'signup';

 /**
  * customerId in Braintree
  * reference to credit card in Braintree
  * 
  * These should be persisted for future reference
  */
 $scope.customerId = false;
 $scope.paymentMethodToken = false;
 $scope.amount = 10;

 /**
  * Bypass step forms for testing
  */
 /*$scope.customerId = '';
 $scope.step = 'checkout';
 $scope.paymentMethodToken = '';
 $scope.card = {
  cardholder: 'Michael',
  number:'4111111111111111',
  cvv: 444,
  expiration_month: 10,
  expiration_year: 18
 };*/

 /**
  * Setup Braintree client with clientToken generated on our server
  */
 $scope.ready = false;
 var braintreeClient;
 braintreeService.getClientToken().then(function(clientToken) {
  // braintree.setup(clientToken, "custom", {id: "checkout", enableCORS: true});
  braintreeClient = new braintree.api.Client({clientToken: clientToken, enableCORS: true});
  $scope.ready = true;
 });

 /**
  * Creates braintree customer
  */
 $scope.signup = function(customer) {
  braintreeService
   .createCustomer(customer.firstname, customer.lastname, customer.company, customer.email, customer.phone)
   .then(function(customerId) {
    console.log("customerId " + customerId);
    $scope.customerId = customerId;
    $scope.step = 'verification';
   });
 };

 /**
  * Save card
  */
 $scope.saveCard = function(card) {
  // console.log('tokenizing card', card);
  braintreeClient.tokenizeCard({
   number: card.number,
   cardholderName: card.cardholder,
   expirationMonth: card.expiration_month,
   expirationYear: card.expiration_year,
   cvv: card.cvv
   // billingAddress: {}
  }, function(err, nonce) {
   if (err) {
    throw err;
   }

   braintreeService
    .createPaymentMethod($scope.customerId, nonce)
    .then(function(paymentMethodToken) {
     console.log('paymentMethodToken ' + paymentMethodToken);
     $scope.paymentMethodToken = paymentMethodToken;
     $scope.step = 'checkout';
    });
  })
 };

 $scope.pay = function(amount) {
  braintreeService
   .sale(amount, $scope.paymentMethodToken)
   .then(function(transactionId) {
    $scope.step = 'done';
    $scope.transactionId = transactionId;
    console.log('transactionId ' + transactionId);
   });
 };
};



Ionic Framework Paypal Payment Gateway

Ionic Framework Paypal Payment Gateway


Today Discussed Ionic Framework Paypal payment Gateway. First  Ionic Framework Cordova Plugin. and How to get payment gateway integrated follows. 
Ionic Framework Paypal Payment Gateway
Ionic Framework Paypal Payment Gateway
IK have developed a mobile app using ionic framework and cordova. In my backend mysql. and storing data using php code. Cordova Plugin ClientID here  api keys.

 $scope.buyNow = function ()
    {
        PaypalService.initPaymentUI().then(function () { PaypalService.makePayment(50, "Total").then();       });
    }
PaypalService is implemented as 
ngular.module('starter.PaypalService', ['starter.Constants'])
.factory('PaypalService', ['$q', '$ionicPlatform','Constants', '$filter', '$timeout', function ($q, $ionicPlatform, Constants, $filter, $timeout) {



    var init_defer;
    /**
     * Service object
     * @type object
     */
    var service = {
        initPaymentUI: initPaymentUI,
        createPayment: createPayment,
        configuration: configuration,
        onPayPalMobileInit: onPayPalMobileInit,
        makePayment: makePayment
    };


    /**
     * @ngdoc method
     * @name initPaymentUI
     * @methodOf app.PaypalService
     * @description
     * Inits the payapl ui with certain envs.
     *
     *
     * @returns {object} Promise paypal ui init done
     */
    function initPaymentUI() {

        init_defer = $q.defer();
        $ionicPlatform.ready().then(function () {

            var clientIDs = {
                "PayPalEnvironmentProduction": Constants.payPalProductionId,
                "PayPalEnvironmentSandbox": Constants.payPalSandboxId
            };
            PayPalMobile.init(clientIDs, onPayPalMobileInit);
        });

        return init_defer.promise;
    }

    /**
     * @ngdoc method
     * @name createPayment
     * @methodOf app.PaypalService
     * @param {string|number} total total sum. Pattern 12.23
     * @param {string} name name of the item in paypal
     * @description
     * Creates a paypal payment object
     *
     *
     * @returns {object} PayPalPaymentObject
     */
    function createPayment(total, name) {

        // "Sale  == >  immediate payment
        // "Auth" for payment authorization only, to be captured separately at a later time.
        // "Order" for taking an order, with authorization and capture to be done separately at a later time.
        var payment = new PayPalPayment("" + total, "INR", "" + name, "Sale");
        return payment;
    }
    /**
     * @ngdoc method
     * @name configuration
     * @methodOf app.PaypalService
     * @description
     * Helper to create a paypal configuration object
     *
     *
     * @returns {object} PayPal configuration
     */
    function configuration() {
        // for more options see `paypal-mobile-js-helper.js`
        var config = new PayPalConfiguration({merchantName: Constants.payPalShopName, merchantPrivacyPolicyURL: Constants.payPalMerchantPrivacyPolicyURL, merchantUserAgreementURL: Constants.payPalMerchantUserAgreementURL});
        return config;
    }

    function onPayPalMobileInit() {
        $ionicPlatform.ready().then(function () {
            // must be called
            // use PayPalEnvironmentNoNetwork mode to get look and feel of the flow
            PayPalMobile.prepareToRender(Constants.payPalEnv, configuration(), function () {

                $timeout(function () {
                    init_defer.resolve();
                });

            });
        });
    }
    /**
     * @ngdoc method
     * @name makePayment
     * @methodOf app.PaypalService
     * @param {string|number} total total sum. Pattern 12.23
     * @param {string} name name of the item in paypal
     * @description
     * Performs a paypal single payment
     *
     *
     * @returns {object} Promise gets resolved on successful payment, rejected on error
     */
    function makePayment(total, name) {


        var defer = $q.defer();
        total = $filter('number')(total, 2);
        $ionicPlatform.ready().then(function () {
            PayPalMobile.renderSinglePaymentUI(createPayment(total, name), function (result) {
                $timeout(function () {
                    defer.resolve(result);
                });
            }, function (error) {
                $timeout(function () {
                    defer.reject(error);
                });
            });
        });

        return defer.promise;
    }

    return service;
}]);
Followed Step

API Backend and Integrations

Every app talks to a server or backed and Appery.io provides a complete backend for your app:

  • Database - for storing app data.
  • Server Code - for writing app logic on the server using JavaScript.
  • API Express - exposing enterprise data sources such as an SQL database or SOAP service via REST APIs. Also, build advanced services with a visual service editor.
  • Push Notifications - send Push Notifications to your users.