private HttpWebRequest BuildSetRequest(PaypalExpressPart paypalExpressPart, OrderPart order) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Ensure order totals are up to date order.UpdateTotals(); var pr = new PaypalRequest(paypalExpressPart, "SetExpressCheckout"); pr.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale"); pr.Add("PAYMENTREQUEST_0_AMT", order.SubTotal.ToString("F2")); // before shipping and tax pr.Add("PAYMENTREQUEST_0_ITEMAMT", order.SubTotal.ToString("F2")); // including shipping and tax pr.Add("PAYMENTREQUEST_0_CURRENCYCODE", paypalExpressPart.Currency); pr.Add("returnUrl", paypalExpressPart.SuccessUrl); pr.Add("cancelUrl", paypalExpressPart.CancelUrl); // order details AddAllItems(pr, order); // Set Address SetShippingAddress(pr, order); // format the data pr.SetData(request); return(request); }
public ActionResult Index(string orderReference, int amount) { PaypalExpressPart paypalExpressPart = _services.WorkContext.CurrentSite.As <PaypalExpressPart>(); // set the success and cancel urls string baseUrl = "http://" + Request.Url.Authority + Request.Path; paypalExpressPart.CancelUrl = baseUrl + "/Cancel"; paypalExpressPart.SuccessUrl = baseUrl + "/Success"; // retrieve the order from the repo OrderPart order = _orderService.GetOrderByNumber(orderReference); // call PaymentExpressService PaypalExpressResult result = _paypalExpressServices.SetExpressCheckout(paypalExpressPart, order); // log paypal transaction _paypalTransactionService.LogTransaction(order, result); if (string.Compare(result.Ack, "success", true) == 0) { // Redirect to PayPal return(new RedirectResult(paypalExpressPart.AuthorizationUrl + "&token=" + result.Token)); } // if we get here then we were unable to get a token from SetExpressCheckout // advise user we have a technical problem, please try again/later var shape = _services.New.UnableToContactPaypal(RedirectTo: "~/Cascade.WebShop/Checkout/Summary"); return(new ShapeResult(this, shape)); }
/// <summary> /// Call the Paypal GetExpressCheckoutDetails API (https://www.x.com/developers/paypal/documentation-tools/api/getexpresscheckoutdetails-api-operation-nvp) /// </summary> /// <param name="paypalExpressPart">part</param> /// <param name="token">Paypal token</param> /// <returns>result containing token, ack and payerId</returns> public PaypalExpressResult GetExpressCheckoutDetails(PaypalExpressPart paypalExpressPart, string token) { PaypalExpressResult result = new PaypalExpressResult { Method = "GetExpressCheckoutDetails" }; HttpWebRequest request = BuildGetDetailsRequest(paypalExpressPart, token); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { string[] lines = GetResponseLines(response); result.Ack = GetValue(lines, "Ack").FirstOrDefault(); result.Token = GetValue(lines, "Token").FirstOrDefault(); result.PayerId = GetValue(lines, "PayerId").FirstOrDefault(); result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault(); // TODO: parse out useful information about the payer, address, etc // and store somewhere: update the order? add to PaypalTransaction? customer? if (string.Compare(result.Ack, "success", true) != 0) { ExtractErrors(lines, result); } } return(result); }
private HttpWebRequest BuildExpressCheckoutPaymentRequest(PaypalExpressPart paypalExpressPart, OrderPart order, string token, string payerId) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Create a paypal request PaypalRequest pr = new PaypalRequest(paypalExpressPart, "DoExpressCheckoutPayment", token); // Add data pr.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale"); pr.Add("PAYERID", payerId); pr.Add("PAYMENTREQUEST_0_AMT", order.SubTotal.ToString("F2")); // before shipping and tax pr.Add("PAYMENTREQUEST_0_ITEMAMT", order.SubTotal.ToString("F2")); // including shipping and tax pr.Add("PAYMENTREQUEST_0_CURRENCYCODE", paypalExpressPart.Currency); // order details AddAllItems(pr, order); // format the request with data from the paypal request pr.SetData(request); return(request); }
/// <summary> /// Call the Paypal SetExpressCheckout API (https://www.x.com/developers/paypal/documentation-tools/api/setexpresscheckout-api-operation-nvp) /// </summary> /// <param name="paypalExpressPart">part</param> /// <param name="order">WebShop order</param> /// <returns>result containing token and ack</returns> public PaypalExpressResult SetExpressCheckout(PaypalExpressPart paypalExpressPart, OrderRecord order) { PaypalExpressResult result = new PaypalExpressResult { Method = "SetExpressCheckout" }; HttpWebRequest request = BuildSetRequest(paypalExpressPart, order); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { string[] lines = GetResponseLines(response); result.Ack = GetValue(lines, "Ack").FirstOrDefault(); result.Token = GetValue(lines, "Token").FirstOrDefault(); result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault(); if (string.Compare(result.Ack, "success", true) != 0) ExtractErrors(lines, result); } return result; }
void LazyLoadHandlers(LoadContentContext context, PaypalExpressPart part) { part.PwdField.Getter(() => { try { return String.IsNullOrWhiteSpace(part.Record.PaypalPwd) ? String.Empty : Encoding.UTF8.GetString(_encryptionService.Decode(Convert.FromBase64String(part.Record.PaypalPwd))); } catch { Logger.Error("The PayPal password could not be decrypted. It might be corrupted, try to reset it."); return null; } }); part.PwdField.Setter(value => part.Record.PaypalPwd = String.IsNullOrWhiteSpace(value) ? String.Empty : Convert.ToBase64String(_encryptionService.Encode(Encoding.UTF8.GetBytes(value)))); }
private HttpWebRequest BuildGetDetailsRequest(PaypalExpressPart paypalExpressPart, string token) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Create a paypal request PaypalRequest pr = new PaypalRequest(paypalExpressPart, "GetExpressCheckoutDetails", token); // format the request with data from the paypal request pr.SetData(request); return(request); }
void LazyLoadHandlers(LoadContentContext context, PaypalExpressPart part) { part.PwdField.Getter(() => { try { return(String.IsNullOrWhiteSpace(part.Record.PaypalPwd) ? String.Empty : Encoding.UTF8.GetString(_encryptionService.Decode(Convert.FromBase64String(part.Record.PaypalPwd)))); } catch { Logger.Error("The PayPal password could not be decrypted. It might be corrupted, try to reset it."); return(null); } }); part.PwdField.Setter(value => part.Record.PaypalPwd = String.IsNullOrWhiteSpace(value) ? String.Empty : Convert.ToBase64String(_encryptionService.Encode(Encoding.UTF8.GetBytes(value)))); }
/// <summary> /// Create a PaypalRequest with some basic data already set up /// </summary> /// <param name="part">Settings</param> /// <param name="method">The Paypal Method(SetExpressCheckout, GetExpressCheckoutDetails, or DoExpressCheckoutPayment)</param> /// <param name="token">Paypal Token (not required for SetExpressCheckout)</param> public PaypalRequest(PaypalExpressPart part, string method, string token = null) { values = new Dictionary <string, string>(); // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_NVPAPIOverview // Method must be first (although doesn't seem to make any diff) Add("METHOD", method); Add("VERSION", part.Version); // Credentials Add("USER", part.User); Add("PWD", part.Pwd); Add("SIGNATURE", part.Signature); if (!string.IsNullOrEmpty(token)) { Add("TOKEN", token); } }
public ActionResult Success(string token, string PayerID) { // NOTE: // This code calls PayPal web services synchronously // // Retrieve the order number int orderId = _paypalTransactionService.GetOrderId(token); string orderReference = (orderId + 1000).ToString(); // retrieve the order OrderPart order = _orderService.GetOrderByNumber(orderReference); // Call GetExpressCheckoutDetails PaypalExpressPart paypalExpressPart = _services.WorkContext.CurrentSite.As <PaypalExpressPart>(); var result = _paypalExpressServices.GetExpressCheckoutDetails(paypalExpressPart, token); // Log the result _paypalTransactionService.LogTransaction(order, result); if (string.Compare(result.Ack, "success", true) == 0) { // Call DoExpressCheckoutPayment var doResult = _paypalExpressServices.DoExpressCheckoutPayment(paypalExpressPart, order, token, result.PayerId); // Log the result _paypalTransactionService.LogTransaction(order, doResult); if (string.Compare(doResult.Ack, "success", true) == 0) { // Display success page string paymentId = token; string command = "Success"; return(RedirectToAction("PaymentResponse", "Order", new { area = "Cascade.WebShop", paymentId = paymentId, result = command, orderReference })); } } // advise user we have a technical problem, please try again/later var shape = _services.New.UnableToContactPaypal(RedirectTo: "~/Cascade.WebShop/Checkout/Summary"); return(new ShapeResult(this, shape)); }
/// <summary> /// Call the Paypal SetExpressCheckout API (https://www.x.com/developers/paypal/documentation-tools/api/setexpresscheckout-api-operation-nvp) /// </summary> /// <param name="paypalExpressPart">part</param> /// <param name="order">WebShop order</param> /// <returns>result containing token and ack</returns> public PaypalExpressResult SetExpressCheckout(PaypalExpressPart paypalExpressPart, OrderPart order) { PaypalExpressResult result = new PaypalExpressResult { Method = "SetExpressCheckout" }; HttpWebRequest request = BuildSetRequest(paypalExpressPart, order); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { string[] lines = GetResponseLines(response); result.Ack = GetValue(lines, "Ack").FirstOrDefault(); result.Token = GetValue(lines, "Token").FirstOrDefault(); result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault(); if (string.Compare(result.Ack, "success", true) != 0) { ExtractErrors(lines, result); } } return(result); }
/// <summary> /// Call the Paypal GetExpressCheckoutDetails API (https://www.x.com/developers/paypal/documentation-tools/api/getexpresscheckoutdetails-api-operation-nvp) /// </summary> /// <param name="paypalExpressPart">part</param> /// <param name="token">Paypal token</param> /// <returns>result containing token, ack and payerId</returns> public PaypalExpressResult GetExpressCheckoutDetails(PaypalExpressPart paypalExpressPart, string token) { PaypalExpressResult result = new PaypalExpressResult { Method = "GetExpressCheckoutDetails" }; HttpWebRequest request = BuildGetDetailsRequest(paypalExpressPart, token); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { string[] lines = GetResponseLines(response); result.Ack = GetValue(lines, "Ack").FirstOrDefault(); result.Token = GetValue(lines, "Token").FirstOrDefault(); result.PayerId = GetValue(lines, "PayerId").FirstOrDefault(); result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault(); // TODO: parse out useful information about the payer, address, etc // and store somewhere: update the order? add to PaypalTransaction? customer? if (string.Compare(result.Ack, "success", true) != 0) ExtractErrors(lines, result); } return result; }
/// <summary> /// Create a PaypalRequest with some basic data already set up /// </summary> /// <param name="part">Settings</param> /// <param name="method">The Paypal Method(SetExpressCheckout, GetExpressCheckoutDetails, or DoExpressCheckoutPayment)</param> /// <param name="token">Paypal Token (not required for SetExpressCheckout)</param> public PaypalRequest(PaypalExpressPart part, string method, string token = null) { values = new Dictionary<string, string>(); // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_NVPAPIOverview // Method must be first (although doesn't seem to make any diff) Add("METHOD", method); Add("VERSION", part.Version); // Credentials Add("USER", part.User); Add("PWD", part.Pwd); Add("SIGNATURE", part.Signature); if (!string.IsNullOrEmpty(token)) Add("TOKEN", token); }
private HttpWebRequest BuildGetDetailsRequest(PaypalExpressPart paypalExpressPart, string token) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Create a paypal request PaypalRequest pr = new PaypalRequest(paypalExpressPart, "GetExpressCheckoutDetails", token); // format the request with data from the paypal request pr.SetData(request); return request; }
private HttpWebRequest BuildSetRequest(PaypalExpressPart paypalExpressPart, OrderRecord order) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Ensure order totals are up to date order.UpdateTotals(); var pr = new PaypalRequest(paypalExpressPart, "SetExpressCheckout"); pr.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale"); pr.Add("PAYMENTREQUEST_0_AMT", order.SubTotal.ToString("F2")); // before shipping and tax pr.Add("PAYMENTREQUEST_0_ITEMAMT", order.SubTotal.ToString("F2")); // including shipping and tax pr.Add("PAYMENTREQUEST_0_CURRENCYCODE", paypalExpressPart.Currency); pr.Add("returnUrl", paypalExpressPart.SuccessUrl); pr.Add("cancelUrl", paypalExpressPart.CancelUrl); // order details AddAllItems(pr, order); // Set Address SetShippingAddress(pr, order); // format the data pr.SetData(request); return request; }
private HttpWebRequest BuildExpressCheckoutPaymentRequest(PaypalExpressPart paypalExpressPart, OrderRecord order, string token, string payerId) { // Create the web request HttpWebRequest request = WebRequest.Create(paypalExpressPart.ApiUrl) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // Create a paypal request PaypalRequest pr = new PaypalRequest(paypalExpressPart, "DoExpressCheckoutPayment", token); // Add data pr.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale"); pr.Add("PAYERID", payerId); pr.Add("PAYMENTREQUEST_0_AMT", order.SubTotal.ToString("F2")); // before shipping and tax pr.Add("PAYMENTREQUEST_0_ITEMAMT", order.SubTotal.ToString("F2")); // including shipping and tax pr.Add("PAYMENTREQUEST_0_CURRENCYCODE", paypalExpressPart.Currency); // order details AddAllItems(pr, order); // format the request with data from the paypal request pr.SetData(request); return request; }