2016-11-16 1 views
1

Hallo ich bin die Integration von PayPal in ionischer App nennen diesen Linkwie Dienste von einem anderen Controller

https://ionicandroidlearning.blogspot.com/2015/11/ionic-paypal-integration.html?showComment=1479300394529#c5798386064000573129

, wenn ich auf den Knopf klicken PayPal-Zahlung zu beginnen, ich habe diesen Fehler. Ich habe alle Dateien in js Ordner hinzufügen und auch

ionic.bundle.js:26799 ReferenceError: PaypalService is not defined 
    at ChildScope.$scope.click (app.js:36) 
    at fn (eval at compile (ionic.bundle.js:27643), <anonymous>:4:206) 
    at ionic.bundle.js:65429 
    at ChildScope.$eval (ionic.bundle.js:30400) 
    at ChildScope.$apply (ionic.bundle.js:30500) 
    at HTMLButtonElement.<anonymous> (ionic.bundle.js:65428) 
    at defaultHandlerWrapper (ionic.bundle.js:16792) 
    at HTMLButtonElement.eventHandler (ionic.bundle.js:16780) 
    at triggerMouseEvent (ionic.bundle.js:2953) 
    at tapClick (ionic.bundle.js:2942) 

meine Datei index.html ist

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
    <title></title> 

    <link rel="manifest" href="manifest.json"> 

    <!-- un-comment this code to enable service worker 
    <script> 
     if ('serviceWorker' in navigator) { 
     navigator.serviceWorker.register('service-worker.js') 
      .then(() => console.log('service worker installed')) 
      .catch(err => console.log('Error', err)); 
     } 
    </script>--> 

    <link href="lib/ionic/css/ionic.css" rel="stylesheet"> 
    <link href="css/style.css" rel="stylesheet"> 

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above 
    <link href="css/ionic.app.css" rel="stylesheet"> 
    --> 

    <!-- ionic/angularjs js --> 
    <script src="lib/ionic/js/ionic.bundle.js"></script> 

    <!-- cordova script (this will be a 404 during development) --> 
    <script src="cordova.js"></script> 

    <!-- your app's js --> 
    <script src="js/app.js"></script> 
    <script src="js/paypal-mobile-js-helper.js"></script> 

    <script src="js/cdv-plugin-paypal-mobile-sdk.js"></script> 
    <script src="js/payPalService.js"></script> 
    </head> 
    <body ng-app="starter" ng-controller="MyCtrl"> 

    <ion-pane> 
     <ion-header-bar class="bar-stable"> 
     <h1 class="title">Ionic Blank Starter</h1> 
     </ion-header-bar> 
     <ion-content> 
     <button ng-click="click()"> payment</button> 
     </ion-content> 
    </ion-pane> 
    </body> 
</html> 
in index.html

ihre Referenz hinzufügen

app.js

// Ionic Starter App 

// angular.module is a global place for creating, registering and retrieving Angular modules 
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html) 
// the 2nd parameter is an array of 'requires' 
angular.module('starter', ['ionic']) 

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

     // Don't remove this line unless you know what you are doing. It stops the viewport 
     // from snapping when text inputs are focused. Ionic handles this internally for 
     // a much nicer keyboard experience. 
     cordova.plugins.Keyboard.disableScroll(true); 
    } 
    if(window.StatusBar) { 
     StatusBar.styleDefault(); 
    } 
    }); 
}) 
//.service('myService', function() { /* ... */ }) 

.controller('MyCtrl' , function($scope){ 



    $scope.click=function() 
    { 
console.log("i am call"); 

// console.log(error); 
PaypalService.initPaymentUI().then(function() { 

PaypalService.makePayment(90, "Total Amount").then(function (response) { 

alert("success"+JSON.stringify(response)); 

}, function (error) { 

alert('Transaction Canceled'); 

}); 

}); 

    }; 



}) 


.constant('shopSettings',{ 

payPalSandboxId :'Aar8HZzvc5NztVWodTBpOiOod9wWrBDrJUjyvRr4WsxcCD28xYig7oecfYsqxQUDu5QHptPpSALirxZD', 

payPalProductionId : 'production id here', 

payPalEnv: 'PayPalEnvironmentSandbox', // for testing production for production 

payPalShopName : 'MyShopName', 

payPalMerchantPrivacyPolicyURL : 'url to policy', 

payPalMerchantUserAgreementURL : ' url to user agreement' 

}) 

paypalServices Datei .js

var app=angular.module('starter.payPalService', []) 


    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; 
    }]); 
+0

Sie müssen Ihren Service erwähnen in Ihrem Controller –

Antwort

2

Da Sie starter.payPalService als separates Modul deklarieren, müssen Sie es auf das übergeordnete Modul injizieren,

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

Und in der Steuerung, stellen Sie sicher, dass Sie den Dienst injizieren,

.controller('MyCtrl' , function($scope,PaypalService){ 
} 
+0

Dank seiner Arbeit jetzt habe ich Anruf-Service erfolgreich. –

Verwandte Themen