Setelah pada beberapa waktu yang lalu kami membahas mengenai cara integrasi aplikasi Asp.net di facebook maka kali ini kami akan menjelaskan cara mengintegrasikan payment gateway paypal di ASP.net MVC web application.
Paypal adalah layanan pembayaran online yang mengijinkan seseorang untuk membeli, menerima pembayaran ataupun mengirim dan menerima uang. Untuk mendapatkan layanan ini seseorang harus memasukkan beberapa rincian keuangan ke paypal, misalnya nomor credit card. Pengiriman dapat dilakukan dengan menggunakan mail. Setelah itu transaksinya dikirimkan tanpa mengungkapkan rincian keuangan, cukup hanya dengan email address dan password.
Berikut ini adalah langkah sederhana untuk menarik user dari paypal payment page ke checkout.
1. Siapkan development tools
-
Buat PayPal Account
Anda membutuhkan aku sandbox untuk mengetes transaksi. Sandbox account terpisah dari account regular, anda hanya perlu mendaftarkan terlebih dulu sebelum dapat mengembangkan dan mengetes code anda.
-
Setting account PayPal
Sebagai latihan yang baik jadikanlah seperti setting account pada web.config. kode pada artikel ini akan mengakses setting berikut. Anda perlu mengganti settingan sandboxnya dengan False jika menggunakan account regular.
1 2 3 4 5 6 7 8 |
<appSettings> <add key="PayPal:Sandbox" value="True" /> <add key="PayPal:Username" value="*" /> <add key="PayPal:Password" value="*" /> <add key="PayPal:Signature" value="*" /> <add key="PayPal:ReturnUrl" value="http://www.jaringanhosting.com" /> <add key="PayPal:CancelUrl" value="http://www.jaringanhosting.com" /> </appSettings> |
2. Buat tombolnya.
Sekarang anda perlu membuat dua view dalam satu controller, satu ditempat button, dan satunya ditempat validation mechanism. Dimulai dari button! Masukkan kode dibawah ini pada sebuah view. Nama iew yang kami gunakan adalah IPN pada User Controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- When you are done with all testing and want to change to the real PayPal, use following instead--> <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <!-- end of Real PayPal example--> <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> <fieldset> <input class="full-width" type="hidden" name="business" value="<!--enter the Business account email here-->"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="item_name" value="The unlimited music download subscription"> <input type="hidden" name="amount" value="9"> <input type="hidden" name="no_shipping" value="1"> <input type=hidden name=RETURNURL value="http://example.com/User/IPN"> <input type="hidden" name="return" value="http://example.com/User/IPN"> <input type="hidden" name="notify_url" value="http://example.com/User/IPN"> <button type="submit">Order now!</button> </fieldset> </form> |
3.Memproses informasi balik dari PayPal
Contoh code berdasarkan dari code berikut. Coba perhatikan bagaimana cara kerjanya dengan beberapa variable.
kini anda perlu membuat view baru pada controller. Sebut saja IPN masukkan code berikut.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
public ActionResult IPN() { var order = new Order(); // this is something I have defined in order to save the order in the database // Receive IPN request from PayPal and parse all the variables returned var formVals = new Dictionary&lt;string, string&gt;(); formVals.Add(&quot;cmd&quot;, &quot;_notify-synch&quot;); //notify-synch_notify validate formVals.Add(&quot;at&quot;, &quot;this is a long token found in Buyers account&quot;); // this has to be adjusted formVals.Add(&quot;tx&quot;, Request[&quot;tx&quot;]); // if you want to use the PayPal sandbox change this from false to true string response = GetPayPalResponse(formVals, false); if (response.Contains(&quot;SUCCESS&quot;)) { string transactionID = GetPDTValue(response, &quot;txn_id&quot;); // txn_id //d string sAmountPaid = GetPDTValue(response,&quot;mc_gross&quot;); // d string deviceID = GetPDTValue(response, &quot;custom&quot;); // d string payerEmail = GetPDTValue(response,&quot;payer_email&quot;); // d string Item = GetPDTValue(response,&quot;item_name&quot;); //validate the order Decimal amountPaid = 0; Decimal.TryParse(sAmountPaid, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out amountPaid); if (amountPaid == 9 ) // you might want to have a bigger than or equal to sign here! { if (orders.Count(d =&gt; d.PayPalOrderRef == transactionID) &lt; 1) { //if the transactionID is not found in the database, add it //then, add the additional features to the user account } else { //if we are here, the user must have already used the transaction ID for an account //you might want to show the details of the order, but do not upgrade it! } // take the information returned and store this into a subscription table // this is where you would update your database with the details of the tran //return View(); } else { // let fail - this is the IPN so there is no viewer // you may want to log something here order.Comments = &quot;User did not pay the right ammount.&quot;; // since the user did not pay the right amount, we still want to log that for future reference. _db.Orders.Add(order); // order is your new Order _db.SaveChanges(); } } else { //error } return View(); } string GetPayPalResponse(Dictionary&lt;string, string&gt; formVals, bool useSandbox) { string paypalUrl = useSandbox ? &quot;https://www.sandbox.paypal.com/cgi-bin/webscr&quot; : &quot;https://www.paypal.com/cgi-bin/webscr&quot;; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl); // Set values for the request back req.Method = &quot;POST&quot;; req.ContentType = &quot;application/x-www-form-urlencoded&quot;; byte[] param = Request.BinaryRead(Request.ContentLength); string strRequest = Encoding.ASCII.GetString(param); StringBuilder sb = new StringBuilder(); sb.Append(strRequest); foreach (string key in formVals.Keys) { sb.AppendFormat(&quot;&amp;{0}={1}&quot;, key, formVals[key]); } strRequest += sb.ToString(); req.ContentLength = strRequest.Length; //for proxy //WebProxy proxy = new WebProxy(new Uri(&quot;http://urlort#&quot;); //req.Proxy = proxy; //Send the request to PayPal and get the response string response = &quot;&quot;; using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)) { streamOut.Write(strRequest); streamOut.Close(); using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream())) { response = streamIn.ReadToEnd(); } } return response; } string GetPDTValue(string pdt, string key) { string[] keys = pdt.Split('\n'); string thisVal = &quot;&quot;; string thisKey = &quot;&quot;; foreach (string s in keys) { string[] bits = s.Split('='); if (bits.Length &gt; 1) { thisVal = bits[1]; thisKey = bits[0]; if (thisKey.Equals(key, StringComparison.InvariantCultureIgnoreCase)) break; } } return thisVal; } |
1 |
formVals.Add("at", "this is a long token found in Buyers account"); |