{"id":1328,"date":"2014-07-10T07:40:59","date_gmt":"2014-07-10T07:40:59","guid":{"rendered":"http:\/\/blog.jaringanhosting.com\/?p=1328"},"modified":"2014-07-10T12:55:46","modified_gmt":"2014-07-10T12:55:46","slug":"asp-net-mvc-hosting-indonesia-jaringanhosting-com-cara-mengintegrasikan-paypal-dengan-asp-net-mvc","status":"publish","type":"post","link":"https:\/\/blog.jaringanhosting.com\/index.php\/asp-net-mvc-hosting-indonesia-jaringanhosting-com-cara-mengintegrasikan-paypal-dengan-asp-net-mvc\/","title":{"rendered":"ASP.NET MVC Hosting Indonesia \u2013 JaringanHosting.com :: Cara Mengintegrasikan PayPal Dengan ASP.NET MVC"},"content":{"rendered":"<p>Setelah pada beberapa waktu yang lalu kami membahas mengenai <a title=\"ASP.Net Hosting Indonesia - JaringanHosting.com\" href=\"http:\/\/blog.jaringanhosting.com\/index.php\/asp-net-4-5-1-hosting-indonesia-cara-mengintegrasikan-apikasi-asp-net-di-facebook\/\" target=\"_blank\">cara integrasi aplikasi Asp.net di facebook<\/a> maka kali ini kami akan menjelaskan cara mengintegrasikan payment gateway paypal di <a title=\"ASP.Net MVC Hosting Indonesia :: Jaringan Hosting\" href=\"http:\/\/jaringanhosting.com\/Indonesia-ASP-NET-MVC-Hosting\" target=\"_blank\">ASP.net<\/a> MVC web application.<\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p><!--more--><\/p>\n<p><a href=\"http:\/\/http:\/\/jaringanhosting.com\/Indonesia-ASP-NET-MVC-Hosting\"><img loading=\"lazy\" class=\"size-full wp-image-1278 aligncenter\" alt=\"ASP.NET MVC Hosting - JaringanHosting\" src=\"http:\/\/blog.jaringanhosting.com\/wp-content\/uploads\/2014\/06\/banner-jarhost.jpg\" width=\"712\" height=\"280\" srcset=\"https:\/\/blog.jaringanhosting.com\/wp-content\/uploads\/2014\/06\/banner-jarhost.jpg 712w, https:\/\/blog.jaringanhosting.com\/wp-content\/uploads\/2014\/06\/banner-jarhost-300x117.jpg 300w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Berikut ini adalah langkah sederhana untuk menarik user dari paypal payment page ke checkout.<\/p>\n<h5>1. Siapkan development tools<\/h5>\n<ul>\n<li>\n<h6><span style=\"font-size: 0.75em;\">Buat PayPal Account<\/span><\/h6>\n<\/li>\n<\/ul>\n<p>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.<\/p>\n<ul>\n<li>\n<h6>\u00a0<span style=\"font-size: 0.75em;\">Setting account PayPal<\/span><\/h6>\n<\/li>\n<\/ul>\n<p>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.<\/p>\n<pre>&lt;appSettings&gt;\r\n&lt;add key=\"PayPal:Sandbox\" value=\"True\" \/&gt;\r\n&lt;add key=\"PayPal:Username\" value=\"*\" \/&gt;\r\n&lt;add key=\"PayPal:Password\" value=\"*\" \/&gt;\r\n&lt;add key=\"PayPal:Signature\" value=\"*\" \/&gt;\r\n&lt;add key=\"PayPal:ReturnUrl\" value=\"http:\/\/www.jaringanhosting.com\" \/&gt;\r\n&lt;add key=\"PayPal:CancelUrl\" value=\"http:\/\/www.jaringanhosting.com\" \/&gt;\r\n&lt;\/appSettings&gt;<\/pre>\n<h5>2. Buat tombolnya.<\/h5>\n<p>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.<\/p>\n<pre>&lt;!-- When you are done with all testing and want to change to the real PayPal, use following instead--&gt;\r\n&lt;form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" method=\"post\"&gt;\r\n&lt;!-- end of Real PayPal example--&gt;\r\n &lt;form action=\"https:\/\/www.sandbox.paypal.com\/cgi-bin\/webscr\" method=\"post\"&gt;\r\n    &lt;fieldset&gt;\r\n        &lt;input class=\"full-width\" type=\"hidden\" name=\"business\" value=\"&lt;!--enter the Business account email here--&gt;\"&gt;\r\n        &lt;input type=\"hidden\" name=\"cmd\" value=\"_xclick\"&gt;\r\n        &lt;input type=\"hidden\" name=\"item_name\" value=\"The unlimited music download subscription\"&gt;\r\n        &lt;input type=\"hidden\" name=\"amount\" value=\"9\"&gt;\r\n        &lt;input type=\"hidden\" name=\"no_shipping\" value=\"1\"&gt;\r\n        &lt;input type=hidden name=RETURNURL\r\n               value=\"http:\/\/example.com\/User\/IPN\"&gt;\r\n        &lt;input type=\"hidden\" name=\"return\" value=\"http:\/\/example.com\/User\/IPN\"&gt;\r\n        &lt;input type=\"hidden\" name=\"notify_url\" value=\"http:\/\/example.com\/User\/IPN\"&gt;\r\n         &lt;button type=\"submit\"&gt;Order now!&lt;\/button&gt;\r\n    &lt;\/fieldset&gt;\r\n&lt;\/form&gt;<\/pre>\n<h5><span style=\"font-size: 0.83em;\">3.Memproses informasi balik dari PayPal<\/span><\/h5>\n<p>Contoh code berdasarkan dari code berikut. Coba perhatikan bagaimana cara kerjanya dengan beberapa variable.<\/p>\n<p>kini anda perlu membuat view baru pada controller. Sebut saja IPN masukkan code berikut.<\/p>\n<pre>public ActionResult IPN()\r\n{\r\nvar order = new Order(); \/\/ this is something I have defined in\r\norder to save the order in the database\r\n\/\/ Receive IPN request from PayPal and parse all the variables\r\nreturned\r\nvar formVals = new Dictionary&amp;lt;string, string&amp;gt;();\r\nformVals.Add(&amp;quot;cmd&amp;quot;, &amp;quot;_notify-synch&amp;quot;); \/\/notify-synch_notify\r\nvalidate\r\nformVals.Add(&amp;quot;at&amp;quot;, &amp;quot;this is a long token found in Buyers\r\naccount&amp;quot;); \/\/ this has to be adjusted\r\nformVals.Add(&amp;quot;tx&amp;quot;, Request[&amp;quot;tx&amp;quot;]);\r\n\/\/ if you want to use the PayPal sandbox change this from false to\r\ntrue\r\nstring response = GetPayPalResponse(formVals, false);\r\nif (response.Contains(&amp;quot;SUCCESS&amp;quot;))\r\n{\r\nstring transactionID = GetPDTValue(response, &amp;quot;txn_id&amp;quot;); \/\/\r\ntxn_id \/\/d\r\nstring sAmountPaid = GetPDTValue(response,&amp;quot;mc_gross&amp;quot;); \/\/ d\r\nstring deviceID = GetPDTValue(response, &amp;quot;custom&amp;quot;); \/\/ d\r\nstring payerEmail = GetPDTValue(response,&amp;quot;payer_email&amp;quot;); \/\/ d\r\nstring Item = GetPDTValue(response,&amp;quot;item_name&amp;quot;);\r\n\/\/validate the order\r\nDecimal amountPaid = 0;\r\nDecimal.TryParse(sAmountPaid,\r\nSystem.Globalization.NumberStyles.Number,\r\nSystem.Globalization.CultureInfo.InvariantCulture, out amountPaid);\r\nif (amountPaid == 9 ) \/\/ you might want to have a bigger than\r\nor equal to sign here!\r\n{\r\nif (orders.Count(d =&amp;gt; d.PayPalOrderRef == transactionID) &amp;lt;\r\n1)\r\n{\r\n\/\/if the transactionID is not found in the database,\r\nadd it\r\n\/\/then, add the additional features to the user\r\naccount\r\n}\r\nelse\r\n{\r\n\/\/if we are here, the user must have already used the\r\ntransaction ID for an account\r\n\/\/you might want to show the details of the order, but\r\ndo not upgrade it!\r\n}\r\n\/\/ take the information returned and store this into a\r\nsubscription table\r\n\/\/ this is where you would update your database with the\r\ndetails of the tran\r\n\/\/return View();\r\n}\r\nelse\r\n{\r\n\/\/ let fail - this is the IPN so there is no viewer\r\n\/\/ you may want to log something here\r\norder.Comments = &amp;quot;User did not pay the right ammount.&amp;quot;;\r\n\/\/ since the user did not pay the right amount, we still\r\nwant to log that for future reference.\r\n_db.Orders.Add(order); \/\/ order is your new Order\r\n_db.SaveChanges();\r\n}\r\n}\r\nelse\r\n{\r\n\/\/error\r\n}\r\nreturn View();\r\n}\r\nstring GetPayPalResponse(Dictionary&amp;lt;string, string&amp;gt; formVals, bool\r\nuseSandbox)\r\n{\r\nstring paypalUrl = useSandbox ?\r\n&amp;quot;https:\/\/www.sandbox.paypal.com\/cgi-bin\/webscr&amp;quot;\r\n: &amp;quot;https:\/\/www.paypal.com\/cgi-bin\/webscr&amp;quot;;\r\nHttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);\r\n\/\/ Set values for the request back\r\nreq.Method = &amp;quot;POST&amp;quot;;\r\nreq.ContentType = &amp;quot;application\/x-www-form-urlencoded&amp;quot;;\r\nbyte[] param = Request.BinaryRead(Request.ContentLength);\r\nstring strRequest = Encoding.ASCII.GetString(param);\r\nStringBuilder sb = new StringBuilder();\r\nsb.Append(strRequest);\r\nforeach (string key in formVals.Keys)\r\n{\r\nsb.AppendFormat(&amp;quot;&amp;amp;{0}={1}&amp;quot;, key, formVals[key]);\r\n}\r\nstrRequest += sb.ToString();\r\nreq.ContentLength = strRequest.Length;\r\n\/\/for proxy\r\n\/\/WebProxy proxy = new WebProxy(new Uri(&amp;quot;http:\/\/urlort#&amp;quot;);\r\n\/\/req.Proxy = proxy;\r\n\/\/Send the request to PayPal and get the response\r\nstring response = &amp;quot;&amp;quot;;\r\nusing (StreamWriter streamOut = new\r\nStreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))\r\n{\r\nstreamOut.Write(strRequest);\r\nstreamOut.Close();\r\nusing (StreamReader streamIn = new\r\nStreamReader(req.GetResponse().GetResponseStream()))\r\n{\r\nresponse = streamIn.ReadToEnd();\r\n}\r\n}\r\nreturn response;\r\n}\r\nstring GetPDTValue(string pdt, string key)\r\n{\r\nstring[] keys = pdt.Split('\\n');\r\nstring thisVal = &amp;quot;&amp;quot;;\r\nstring thisKey = &amp;quot;&amp;quot;;\r\nforeach (string s in keys)\r\n{\r\nstring[] bits = s.Split('=');\r\nif (bits.Length &amp;gt; 1)\r\n{\r\nthisVal = bits[1];\r\nthisKey = bits[0];\r\nif (thisKey.Equals(key,\r\nStringComparison.InvariantCultureIgnoreCase))\r\nbreak;\r\n}\r\n}\r\nreturn thisVal;\r\n}<\/pre>\n<p>Perlu dicatat! Activation Token dapat ditemukan pada Buyers Account di profil&gt; My Selling Tools&gt;Website Preference&gt;Update, setelah itu enable auto return and payment data transfer. Disana anda dapat langsung mencari Activation Token yang harus anda masakan kedalam:<\/p>\n<pre>formVals.Add(\"at\", \"this is a long token found in Buyers account\");<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":2144,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[10,24,26,271,11,519,222,520,521],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/posts\/1328"}],"collection":[{"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/users\/2144"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/comments?post=1328"}],"version-history":[{"count":0,"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/posts\/1328\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/media?parent=1328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/categories?post=1328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jaringanhosting.com\/index.php\/wp-json\/wp\/v2\/tags?post=1328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}