Skip to content

Instantly share code, notes, and snippets.

@Urbiwanus
Last active November 13, 2019 15:38
Show Gist options
  • Save Urbiwanus/c1e456f889f53e940a11 to your computer and use it in GitHub Desktop.
Save Urbiwanus/c1e456f889f53e940a11 to your computer and use it in GitHub Desktop.
Cordova Paypal Integration AngularJS
app.factory('PaypalService', ['$q', '$ionicPlatform', 'shopSettings', '$filter', '$timeout', function ($q, $ionicPlatform, shopSettings, $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": shopSettings.payPalProductionId,
"PayPalEnvironmentSandbox": shopSettings.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, "EUR", "" + 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: shopSettings.payPalShopName, merchantPrivacyPolicyURL: shopSettings.payPalMerchantPrivacyPolicyURL, merchantUserAgreementURL: shopSettings.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(shopSettings.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;
}]);
//To use this service
PaypalService.initPaymentUI().then(function () {
PaypalService.makePayment($scope.total(), "Total").then(...)
});
// shop settings
// include appConstant into your app.js
angular.module('appConstant', []).constant('shopSettings',{
payPalSandboxId : 'sand box id here',
payPalProductionId : 'production id here',
payPalEnv: 'PayPalEnvironmentSandbox', // for testing production for production
payPalShopName : 'MyShopName',
payPalMerchantPrivacyPolicyURL : 'url to policy',
payPalMerchantUserAgreementURL : ' url to user agreement '
});
@renanss
Copy link

renanss commented Apr 7, 2016

Nice work! Working flawless.

@johncabs18
Copy link

johncabs18 commented Jun 13, 2016

why is it when the total payment is about $1000+ and it is automatically canceling the payment ? any suggestion about this . thanks

@alaick
Copy link

alaick commented Dec 2, 2016

Getting issue ReferenceError: PayPalMobile is not defined

@vinod023
Copy link

I am getting this Error. Please Help me.

ReferenceError: PayPalMobile is not defined
at payPalService.js:35
at processQueue (ionic.bundle.js:29132)
at ionic.bundle.js:29148
at Scope.$eval (ionic.bundle.js:30400)
at Scope.$digest (ionic.bundle.js:30216)
at ChildScope.$apply (ionic.bundle.js:30508)
at HTMLButtonElement. (ionic.bundle.js:65428)
at defaultHandlerWrapper (ionic.bundle.js:16792)
at HTMLButtonElement.eventHandler (ionic.bundle.js:16780)
at triggerMouseEvent (ionic.bundle.js:2953)

@rahultaurus46
Copy link

I am getting the error that "THE MERCHANT DOES NOT ACCEPT PAYMENT OF THIS TYPE".
Is that any code /application error / paypal merchant account/ country error??
Can any one help me plz

@Hitman666
Copy link

@AhammadAliPK you should just add it like this: <script src="js/paypal-mobile-js-helper.js" type="text/javascript"></script>, as they state in the docs.

@Shalaka-Kanki
Copy link

I integrated in my ionic app & successfully getting renderSinglePaymentUI but I am unable to use renderFuturePaymentUI, It goes in error callback saying "Passibly configuration submitted is invalid"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment