How to integrate with payment
gateways when using angularjs?
When building angular web applications you
will at some point want to integrate with certain payment gateways or some 3rd
party external urls . Mostly these external urls accept certain post parameters
which needs be passed from the calling application .
After going through several implementations I
found the below mentioned implementation simple and easy without polluting our
angular code with jquery code. Here we will be using events to communicate with
our custom directive from our controller/service . Once your data i.e the
redirect url and the post parameters are ready , trigger an event using the
angular $broadcast .
$rootScope.$broadcast('form.submit',data);
Your data variable should look like :
var data = {
var url = '<external url>’;
var method = 'POST';
var params = {
param1: 'value1',
param2: 'value2'
};
Now , we have to listen to this event and get
the data which will be used by the new directive. First lets create the custom directive which listens to an event , uses the data available and creates a template .
app.directive('formSubmitter', formSubmitterDirective(){
return {
restrict: 'E',
replace: true,
template:
'<form action="{{ formData.url }}" method="{{
formData.method }}">' +
' <div
ng-repeat="(key,val) in formData.params">' + <input type="hidden" name="{{ key
}}" value="{{ val }}" />' +' </div>' +'</form>',
link: function($scope, $element, $attrs)
{
$scope.$on('form.submit', function(event, data) {
$scope.formData = data;
console.log('auto submitting
form...');
$timeout(function() {
$element.submit();
})
})
}
}
}
)
In the directive code , we use $scope.$on to
listen to the event . The template uses the ng-repeat on params and grabs the
property name and value to create hidden input fields in your page . Use the
element.submit function to submit the post request .
To implement the postback(gateway returns to
caller with some data) from the payment gateway , refer the implementation manual
as mentioned earlier . You might have to do some security configuration based
on the payment gateway that you prefer .
Hey Author!! Full Stack Programming Tips is really awesome. Thanks for sharing the useful blog about integrating Payment Gateway with Angular JS.
ReplyDeleteCustom Web Application Development Company in Coimbatore
Nice to read this article.... Thanks for sharing such great content.
ReplyDeleteAngular JS Training
React JS Training
Full Stack online Training
Thanks for sharing information...
ReplyDeleteangularjs online training in hyderabad
Payment Asia is an expert in providing comprehensive ecommerce payment solutions to various kinds of enterprises, assisting merchants to catch the wave of technological development. Our products and services can be simply categorized into Payment Solutions, Payment Gateway and Integration, and Artificial Intelligence.
ReplyDeletePaymentAsia
PaymentAsia Mobile Payment Solution
Thank you for sharing such a nice and interesting blog with us.
ReplyDeleteFull Stack Online Training
Thank you for sharing Great Information....Keep Move On
ReplyDeleteBest Angular JS Training Institutes in Hyderabad
Excellent Blog. Thank you so much for sharing..
ReplyDeletehttps://www.credosystemz.com/training-in-chennai/react-js-training/