Consume ASP.NET Web API Using PHP and JQuery




Background
Whether it is shortage of required skills, platform unavailability or even laziness, anything could make you want to cut some corners in development. And please get me right, by cutting corners I do not mean writing code that is cumbersome and difficult to maintain or leaving out important possible outcomes to favour low turnaround times. There's this popular quote that drives this best practice home:
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.

Issue
We had this project I was working on with a team. Its kind of like a payment aggregator, functioning in similar manner as your regular payment gateways like Paypal, UPL, Interswitch. You can find the project here (a1pay.net). I was the front-end guy, which meant that I was basically responsible for presentation. However, in a twist of events, I was also saddled with handling the POST request from our 3rd party payment gateways, consume an already prepared ASP.NET Web API to update the transaction, and display the transaction status to the user.

This shouldn't be a problem normally, but as the frontend guy, I had restricted my set of tools to Javascript/HTML/CSS, and this was because I didn't want the application to be platform dependent. How was I to achieve this without any server side code?

Solution
It was just impossible to handle POST requests using Javascript, because Javascript runs at the client-side, but POST requests are received at the server end. I had to improvise a little with PHP. My choice of PHP is basically because it allows me to write a single file and place it on the server without bothering about compilation issues.

I basically used PHP to compose my JQuery ajax request. This gets interpreted and executed as the page gets to the client. The JQuery ajax updates the transaction by consuming the ASP.NET Web API and redirects to the transaction status page.  The code sample below basically works for anyone who wishes to integrate with Interswitch. In a subsequent post, I will be explaining the drawbacks of this technique and why you should approach its usage with caution. Cheers.



<html>
<head>
 <script type="text/javascript">
  txnref = '<?php echo $_POST['txnref']; ?>';
  resp = '<?php echo $_POST['resp']; ?>';
  desc = '<?php echo $_POST['desc']; ?>';
  payRef = '<?php echo $_POST['payRef']; ?>';
  retRef = '<?php echo $_POST['retRef']; ?>';
  cardNum = '<?php echo $_POST['cardNum']; ?>';
  apprAmt = '<?php echo $_POST['apprAmt']; ?>';
  mac = '<?php echo $_POST['mac']; ?>';
  amount = '<?php echo $_POST['amount']; ?>';
 
  var URL = "https://mydomain.com/SaveResponse";
  var ds = {
   "ID": 1, "txnref": txnref,  "resp": resp,  "desc": desc, "payRef": payRef,  "retRef": retRef, "cardNum": cardNum
  }
  $.ajax({ url: URL, type: "POST", dataType: "json", data: JSON.stringify(ds),
     success: function(data){
       //do something here e.g: window.location.href = "status.html";
     },
     error: function(x, y, z){
     //do something here e.g: window.location.href = "error.html";
     }
   });
 </script>
</head>
<body></body>
</html>

Comments

Popular posts from this blog

Resize or Crop Image before Upload Using HTML5 File API

Exception from HRESULT: 0x80131040

Get Creative With Data Tables: Row Click Events