Hello, recently im able to create a button that linked to paypal using https://github.com/paypal/PayPal-Cordova-Plugin. and now im using paypal sandbox to do all the testing. And can someone help me on how to create invoice after the payment : approved? maybe the apps will generate the receipt or send by email.
Here is my PaypalService
angular.module('starter.Payment', [])
.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, "MYR", "" + name, "");
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;
}]);
And how i using the service
$scope.paynow = function() {
PaypalService.initPaymentUI().then(function () {
PaypalService.makePayment($scope.pay, "MauKereta").then(function(success) {
console.log(success);
var result = success.response.state;
var new_result = result.toString();
if(new_result === "approved") {
console.log("Payment Approved")
$ionicLoading.show({
template: 'Loading...'
});
$scope.selectedCar = $stateParams.id;
var currentUser = Parse.User.current();
var Booking = Parse.Object.extend("Booking");
var query = new Parse.Query(Booking);
var currentBooking;
query.get($stateParams.id, {
success: function(booking) {
currentBooking = booking;
currentBooking.set("status", "Waiting to be Review")
currentBooking.set("isCompleted", false)
currentBooking.set("isCredit", true)
currentBooking.set("isDecline", false)
currentBooking.save(null, {
success: function(booking) {
console.log(booking);
var Car = Parse.Object.extend("Car");
var query = new Parse.Query(Car);
query.get(booking.attributes.car_select.id, {
success: function(car) {
car.set("isUsed", true)
car.save(null, {
success: function(save){
$ionicLoading.hide();
$state.go('home');
$ionicPopup.alert({
title: 'Thanks!',
template: 'Your Reservation Will Be Reviewed By Staff'
});
},
error: function(error) {
console.log(error)
$ionicLoading.hide();
}
})
},
error: function(error) {
console.log(error)
$ionicLoading.hide();
}
})
},
error: function(Booking, error) {
console.log(error);
$ionicLoading.hide();
}
})
},
error: function(Booking, error) {
console.log(error);
$ionicLoading.hide();
}
})
}
})
});
}