Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using com.paypal.sdk.services;
  8. using com.paypal.sdk.profiles;
  9. using com.paypal.sdk.util;
  10. using System.Data;
  11. using System.Text;
  12. using System.Net.Mail;
  13. using System.Web.Security;
  14.  
  15. public partial class OrderConfirm : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.         if (Request.QueryString["token"].ToString() != "")
  20.         {
  21.             if (ECGetExpressCheckoutCode(Request.QueryString["token"].ToString()) == "Success" ||
  22.                 ECGetExpressCheckoutCode(Request.QueryString["token"].ToString()) == "SuccessWithWarning")
  23.             {
  24.                 pnlOrderconfirm.Visible = true;
  25.             }
  26.             else
  27.             {
  28.                 pnlConfirmOrderNoReciepe.Visible = true;
  29.             }
  30.         }
  31.         else
  32.             Response.Redirect("Default.aspx");
  33.     }
  34.  
  35.     public string ECGetExpressCheckoutCode(string token)
  36.     {
  37.         NVPCallerServices caller = new NVPCallerServices();
  38.         IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();
  39.  
  40.         // Set up your API credentials, PayPal end point, API operation and version.
  41.         profile.APIUsername = "";
  42.         profile.APIPassword = "";
  43.         profile.APISignature = "";
  44.         profile.Environment = "live";
  45.         caller.APIProfile = profile;
  46.  
  47.         NVPCodec encoder = new NVPCodec();
  48.         //encoder["VERSION"] = "51.0";
  49.         encoder["VERSION"] = "65.0";
  50.         encoder["METHOD"] = "GetExpressCheckoutDetails";
  51.  
  52.         encoder["TOKEN"] = token; // Pass the token returned in SetExpressCheckout.
  53.  
  54.         // Execute the API operation and obtain the response.
  55.         string pStrrequestforNvp = encoder.Encode();
  56.         string pStresponsenvp = caller.Call(pStrrequestforNvp);
  57.  
  58.         NVPCodec decoder = new NVPCodec();
  59.         decoder.Decode(pStresponsenvp);
  60.  
  61.         lblNamn.Text = decoder["SHIPTONAME"];
  62.         lblAdress.Text = decoder["SHIPTOSTREET"];
  63.         lblPostnr.Text = decoder["SHIPTOZIP"];
  64.         lblOrt.Text = decoder["SHIPTOCITY"];
  65.         lblTele.Text = decoder["SHIPTOPHONENUM"];
  66.         lblEpost.Text = decoder["EMAIL"];
  67.         if(Session["payment_amt"] != null)
  68.             lblBelopp.Text = Session["payment_amt"].ToString();
  69.         else
  70.             lblBelopp.Text = decoder["PAYMENTREQUEST_0_AMT"];
  71.  
  72.         return decoder["ACK"];
  73.     }
  74.  
  75.     public class ECDoExpressCheckout
  76.     {
  77.         public ECDoExpressCheckout()
  78.         {
  79.         }
  80.     }
  81.  
  82.     public string ECDoExpressCheckoutCode(string token, string payerID, string amount, string paymentType, string currencyCode)
  83.     {
  84.         NVPCallerServices caller = new NVPCallerServices();
  85.         IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();
  86.  
  87.         // Set up your API credentials, PayPal end point, API operation and version.
  88.         profile.APIUsername = "";
  89.         profile.APIPassword = "";
  90.         profile.APISignature = "";
  91.         profile.Environment = "live";
  92.         caller.APIProfile = profile;
  93.  
  94.         NVPCodec encoder = new NVPCodec();
  95.         encoder["VERSION"] = "65.0";
  96.         encoder["METHOD"] = "DoExpressCheckoutPayment";
  97.  
  98.         // Add request-specific fields to the request.
  99.         // Pass the token returned in SetExpressCheckout.
  100.         encoder["TOKEN"] = token;
  101.         encoder["PAYERID"] = payerID;
  102.         encoder["AMT"] = amount;
  103.         encoder["PAYMENTACTION"] = paymentType;
  104.         encoder["CURRENCYCODE"] = currencyCode;
  105.  
  106.         // Execute the API operation and obtain the response.
  107.         string pStrrequestforNvp = encoder.Encode();
  108.         string pStresponsenvp = caller.Call(pStrrequestforNvp);
  109.  
  110.         NVPCodec decoder = new NVPCodec();
  111.         decoder.Decode(pStresponsenvp);
  112.  
  113.         lblUsermsg.Text += pStresponsenvp.ToString();
  114.  
  115.         return decoder["ACK"];
  116.     }
  117.  
  118.     protected void Confirm(object sender, EventArgs e)
  119.     {
  120.         if (ECDoExpressCheckoutCode(Request.QueryString["token"].ToString(), Request.QueryString["PayerID"].ToString(),
  121.             Session["payment_amt"].ToString(), "Sale", "SEK") == "Success")
  122.         {
  123.             try
  124.             {
  125.                 // Skicka mail
  126.                 SendCustomerMail(lblEpost.Text);
  127.                 SendOrderMail();
  128.  
  129.                 // Skicka vidare till bekräftelsesida
  130.                 Response.Redirect("Completed.aspx");
  131.             }
  132.             catch (Exception ex)
  133.             {
  134.                 lblUsermsg.Text = "Ett fel uppstod: " + ex;
  135.             }
  136.         }
  137.         else
  138.         {
  139.             pnlOrderconfirm.Visible = false;
  140.             pnlConfirmOrderNoReciepe.Visible = false;
  141.            
  142.         }
  143.     }
  144.  
  145.     //Skicka bekräftande mail till beställaren
  146.     protected void SendCustomerMail(string email)
  147.     {
  148.         DataTable ordertable = ManageShoppingCart.getProductsFromShoppingCart();
  149.         DataTable totaltable = ManageShoppingCart.getSumFromShoppingCart();
  150.  
  151.         StringBuilder sb = new StringBuilder();
  152.         foreach (DataRow row in ordertable.Rows)
  153.         {
  154.             sb.AppendLine("<tr>");
  155.             sb.AppendLine("<td>" + row["Name"] + "</td>");
  156.             sb.AppendLine("<td>" + String.Format("{0:0 kr}", row["Price"]) + "</td>");
  157.             sb.AppendLine("<td>" + row["Quantity"] + "</td>");
  158.             sb.AppendLine("<td>" + row["AttributeOne"].ToString() + row["AttributeTwo"] + "</td>");
  159.             sb.AppendLine("</tr>");
  160.         }
  161.  
  162.         SmtpClient client = new SmtpClient("");
  163.         MailMessage mail = new MailMessage();
  164.         mail.From = new MailAddress("", "Bekräftelse på köp ");
  165.         mail.To.Add(email);
  166.         mail.Subject = "Din beställning";
  167.  
  168.         mail.Body =
  169.             "<html><body><head><title>Din beställning från</title>" +
  170.             "<style>body {font-family:'Trebuchet MS',Arial,Helvetica,sans-serif;} h1,h2,h3 {margin-bottom:0; color:#840D16; " +
  171.             "font-family:'Trebuchet MS',Arial,Helvetica,sans-serif;} " +
  172.             "p {font-family:'Trebuchet MS',Arial,Helvetica,sans-serif;}</style>" +
  173.             "</head>" +
  174.             "<h2>Tack för din beställning!</h2>" +
  175.             "<p>Detta är en bekräftelse på att din beställning är mottagen." +
  176.             "<h3>Dina uppgifter</h3>" +
  177.             "<p><strong>Personliga uppgifter: </strong><br>" +
  178.             lblNamn.Text + "<br>" +
  179.             lblAdress.Text + "<br>" + lblPostnr.Text + " " + lblOrt.Text + "</p>" +
  180.             "<p>Telefon: " + lblTele.Text + "<br>" +
  181.             "E-post: " + email + "</p>" +
  182.             "<p><strong>Beställningen gjordes:</strong> " + DateTime.Now.ToShortDateString() +
  183.             "</p><p>Betalning har skett via Paypal.</p>" +
  184.             "<p><strong>Leverans: </strong>" +
  185.             "75 kr </p>" +
  186.             "<h3>Beställda varor </h3><table cellpadding='4'><tr><th>Namn</th><th>Pris</th>" +
  187.             "<th>Antal</th><th>Attibut</th></tr>" +
  188.             sb.ToString() +
  189.             "</table>" +
  190.             "<h3>Totalsumma: <span style='color:#000;'>" +
  191.             Session["payment_amt"].ToString() +
  192.             "</span></h3>" +
  193.             "<p>Tack för Er beställning!Ert smycke kommer att skickas inom 10 dagar, vanligtvis 3-4 dagar. Ni får exakt besked när orden är mottagen.</p>" +
  194.             "<p>Alla smycken levereras i fin ask med logga.</p>" +
  195.             "<p>Svensk köplag gäller.</p>" +
  196.             "<p><strong>Vi hoppas att du blir nöjd med varorna.</strong></p>" +
  197.             "<p>Tack för din beställnig!</p>" +
  198.             "</body></html>";
  199.  
  200.            
  201.  
  202.  
  203.  
  204.         mail.IsBodyHtml = true;
  205.         try
  206.         {
  207.             client.Send(mail);
  208.         }
  209.         catch (Exception ex)
  210.         {
  211.         }
  212.     }
  213.  
  214.     //Skicka mail till administratören.
  215.     //I mailet är leverans och betalsätt hårdkodat, eftersom det just nu bara finns ett alternativ vardera.
  216.     protected void SendOrderMail()
  217.     {
  218.         DataTable ordertable = ManageShoppingCart.getProductsFromShoppingCart();
  219.         DataTable totaltable = ManageShoppingCart.getSumFromShoppingCart();
  220.  
  221.         StringBuilder sb = new StringBuilder();
  222.         foreach (DataRow row in ordertable.Rows)
  223.         {
  224.             sb.AppendLine("<tr>");
  225.             sb.AppendLine("<td>" + row["Name"] + "</td>");
  226.             sb.AppendLine("<td>" + String.Format("{0:0 kr}", row["Price"]) + "</td>");
  227.             sb.AppendLine("<td>" + row["Quantity"] + "</td>");
  228.             sb.AppendLine("<td>" + row["AttributeOne"].ToString() + row["AttributeTwo"] + "</td>");
  229.             sb.AppendLine("</tr>");
  230.         }
  231.  
  232.         SmtpClient client = new SmtpClient("213...28");
  233.         MailMessage mail = new MailMessage();
  234.         mail.From = new MailAddress("order@.se", "Ny order från .se");
  235.         mail.To.Add("info@.se");
  236.         mail.Subject = "Order från .se";
  237.  
  238.         mail.Body =
  239.             "<html><body><head><title>Din beställning från .se</title>" +
  240.             "<style>body {font-family:'Trebuchet MS',Arial,Helvetica,sans-serif;} h1,h2,h3 {margin-bottom:0; color:#840D16; font-family:'Trebuchet MS',Arial,Helvetica,sans-serif;} " +
  241.             "p {font-family:'Trebuchet MS',Arial,Helvetica,sans-serif;}</style>" +
  242.             "</head>" +
  243.             "<h2>En ny beställning har gjorts i webshoppen</h2>" +
  244.             "<p>Ett mail har skickats till kunden.</p>" +
  245.             "<h3>Kundens uppgifter</h3>" +
  246.             "<p><strong>Personliga uppgifter: </strong><br>" +
  247.             lblNamn.Text + "<br>" +
  248.             lblAdress.Text + "<br>" + lblPostnr.Text + " " + lblOrt.Text + "</p>" +
  249.             "<p>Telefon: " + lblTele.Text + "<br>" +
  250.             "E-post: " + lblEpost.Text + "</p>" +
  251.             "<p><strong>Beställningen gjordes:</strong> " + DateTime.Now.ToShortDateString() +
  252.             "</p><p>Betalning har skett via Paypal.</p>" +
  253.             "<p><strong>Leverans: </strong>" +
  254.             "75 kr </p>" +
  255.             "<h3>Beställda varor </h3><table cellpadding='4'><tr><th>Namn</th><th>Pris</th>" +
  256.             "<th>Antal</th><th>Attibut</th></tr>" +
  257.             sb.ToString() +
  258.             "</table>" +
  259.             "<h3>Totalsumma: <span style='color:#000;'>" +
  260.             Session["payment_amt"].ToString() +
  261.             "</span></h3>";
  262.         mail.IsBodyHtml = true;
  263.         try
  264.         {
  265.             client.Send(mail);
  266.         }
  267.         catch (Exception ex)
  268.         {
  269.         }
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement