private string buildCredentialsNVPString() { NVPCodec codec = new NVPCodec(); if (!IsEmpty(APIUsername)) codec["USER"] = APIUsername; if (!IsEmpty(APIPassword)) codec[PWD] = APIPassword; if (!IsEmpty(APISignature)) codec[SIGNATURE] = APISignature; if (!IsEmpty(Subject)) codec["SUBJECT"] = Subject; codec["VERSION"] = "88.0"; return codec.Encode(); }
public bool DoCheckoutPayment(string finalPaymentAmount, long ShoppingCartID, string token, string PayerID, ref NVPCodec decoder, ref string retMsg) { pEndPointURL = System.Configuration.ConfigurationManager.AppSettings["pEndPointURL"]; host = System.Configuration.ConfigurationManager.AppSettings["host"]; APIUsername = System.Configuration.ConfigurationManager.AppSettings["APIUsername"]; APIPassword = System.Configuration.ConfigurationManager.AppSettings["APIPassword"]; APISignature = System.Configuration.ConfigurationManager.AppSettings["APISignature"]; if (bSandbox) { pEndPointURL = ConfigurationManager.AppSettings["pEndPointURL_SB"]; host = ConfigurationManager.AppSettings["host_SB"]; APIUsername = ConfigurationManager.AppSettings["APIUsername_SB"]; APIPassword = ConfigurationManager.AppSettings["APIPassword_SB"]; APISignature = ConfigurationManager.AppSettings["APISignature_SB"]; } NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "DoExpressCheckoutPayment"; encoder["TOKEN"] = token; encoder["PAYERID"] = PayerID; encoder["PAYMENTREQUEST_0_AMT"] = finalPaymentAmount; encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD"; encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale"; encoder["NOSHIPPING"] = "1"; encoder["ALLOWNOTE"] = "1"; encoder["BRANDNAME"] = ConfigurationManager.AppSettings["brandName"]; encoder["INVNUM"] = ShoppingCartID.ToString(); Cart cart = controller.LoadCart(ShoppingCartID); int i = 0; foreach (CartItem cartitem in cart.CartItems) { encoder["L_PAYMENTREQUEST_0_NUMBER" + i] = cartitem.product_id.ToString(); encoder["L_PAYMENTREQUEST_0_NAME" + i] = cartitem.product_title; encoder["L_PAYMENTREQUEST_0_DESC" + i] = cartitem.product_title; encoder["L_PAYMENTREQUEST_0_AMT" + i] = cartitem.final_price.ToString(); encoder["L_PAYMENTREQUEST_0_QTY" + i] = cartitem.quantity.ToString(); i++; } string pStrrequestforNvp = encoder.Encode(); string pStresponsenvp = HttpCall(pStrrequestforNvp); decoder = new NVPCodec(); decoder.Decode(pStresponsenvp); string strAck = decoder["ACK"].ToLower(); if (strAck != null && (strAck == "success" || strAck == "successwithwarning")) { return true; } else { retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" + "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" + "Desc2=" + decoder["L_LONGMESSAGE0"]; return false; } }
public bool ShortcutExpressCheckout(string amt, long ShoppingCartID, ref string token, ref string retMsg, bool isWorkshop) { pEndPointURL = ConfigurationManager.AppSettings["pEndPointURL"]; host = ConfigurationManager.AppSettings["host"]; APIUsername = ConfigurationManager.AppSettings["APIUsername"]; APIPassword = ConfigurationManager.AppSettings["APIPassword"]; APISignature = ConfigurationManager.AppSettings["APISignature"]; if (bSandbox) { pEndPointURL = ConfigurationManager.AppSettings["pEndPointURL_SB"]; host = ConfigurationManager.AppSettings["host_SB"]; APIUsername = ConfigurationManager.AppSettings["APIUsername_SB"]; APIPassword = ConfigurationManager.AppSettings["APIPassword_SB"]; APISignature = ConfigurationManager.AppSettings["APISignature_SB"]; } string returnURL = ConfigurationManager.AppSettings["returnURL"]; string cancelURL = ConfigurationManager.AppSettings["cancelURL"]; if (isWorkshop) { returnURL = ConfigurationManager.AppSettings["returnURLWorkshop"]; cancelURL = ConfigurationManager.AppSettings["cancelWorkshop"]; } NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "SetExpressCheckout"; encoder["NOSHIPPING"] = "1"; encoder["ALLOWNOTE"] = "1"; encoder["RETURNURL"] = returnURL; encoder["CANCELURL"] = cancelURL; encoder["BRANDNAME"] = ConfigurationManager.AppSettings["brandName"]; encoder["INVNUM"] = ShoppingCartID.ToString(); encoder["PAYMENTREQUEST_0_AMT"] = amt; encoder["PAYMENTREQUEST_0_ITEMAMT"] = amt; encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale"; encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD"; // Get the Shopping Cart Products Cart cart = controller.LoadCart(ShoppingCartID); int i = 0; foreach (CartItem cartitem in cart.CartItems) { encoder["L_PAYMENTREQUEST_0_NUMBER" + i] = cartitem.product_id.ToString(); encoder["L_PAYMENTREQUEST_0_NAME" + i] = cartitem.product_title; encoder["L_PAYMENTREQUEST_0_DESC" + i] = cartitem.product_title; encoder["L_PAYMENTREQUEST_0_AMT" + i] = cartitem.final_price.ToString(); encoder["L_PAYMENTREQUEST_0_QTY" + i] = cartitem.quantity.ToString(); i++; } string pStrrequestforNvp = encoder.Encode(); string pStresponsenvp = HttpCall(pStrrequestforNvp); NVPCodec decoder = new NVPCodec(); decoder.Decode(pStresponsenvp); string strAck = decoder["ACK"].ToLower(); if (strAck != null && (strAck == "success" || strAck == "successwithwarning")) { token = decoder["TOKEN"]; string ECURL = "https://" + host + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + token + "&useraction=COMMIT"; retMsg = ECURL; return true; } else { retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" + "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" + "Desc2=" + decoder["L_LONGMESSAGE0"]; return false; } }
public bool GetCheckoutDetails(string token, ref NVPCodec decoder, ref string retMsg) { pEndPointURL = System.Configuration.ConfigurationManager.AppSettings["pEndPointURL"]; host = System.Configuration.ConfigurationManager.AppSettings["host"]; APIUsername = System.Configuration.ConfigurationManager.AppSettings["APIUsername"]; APIPassword = System.Configuration.ConfigurationManager.AppSettings["APIPassword"]; APISignature = System.Configuration.ConfigurationManager.AppSettings["APISignature"]; if (bSandbox) { pEndPointURL = ConfigurationManager.AppSettings["pEndPointURL_SB"]; host = ConfigurationManager.AppSettings["host_SB"]; APIUsername = ConfigurationManager.AppSettings["APIUsername_SB"]; APIPassword = ConfigurationManager.AppSettings["APIPassword_SB"]; APISignature = ConfigurationManager.AppSettings["APISignature_SB"]; } NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "GetExpressCheckoutDetails"; encoder["TOKEN"] = token; string pStrrequestforNvp = encoder.Encode(); string pStresponsenvp = HttpCall(pStrrequestforNvp); decoder = new NVPCodec(); decoder.Decode(pStresponsenvp); string strAck = decoder["ACK"].ToLower(); if (strAck != null && (strAck == "success" || strAck == "successwithwarning")) { return true; } else { retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" + "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" + "Desc2=" + decoder["L_LONGMESSAGE0"]; return false; } }
protected void btn_SubmitPaypal_Click(object sender, ImageClickEventArgs e) { var id = txtID.Value; if (Request.Cookies["email"] == null) { FormsAuthentication.SignOut(); FormsAuthentication.RedirectToLoginPage(); } var email = Request.Cookies["email"].Value; CMEDWorkShopBalance objbalance = controller.GetCMEDWorkShopBalancePayment(Convert.ToInt32(id), email.ToString()); workshop = controller.GetCMEDWorkShop(Convert.ToInt32(id)); if (!string.IsNullOrEmpty(id)) { if (Helper.IsNumeric(id)) { Price = Convert.ToInt32(objbalance.balance); } else { Response.Redirect("http://www.myss.com/"); } long ShoppingCartID = controller.IssueNewGlobalID(); Response.Cookies["ShoppingCartID"].Value = ShoppingCartID.ToString(); try { controller.SaveCartItem(workshop.ProductID, Price, ShoppingCartID, EmailTextBox.Text.Trim(), GetUserIP()); } catch (Exception ex) { throw ex; } string auth_status = string.Empty; string auth_code = string.Empty; string auth_message = string.Empty; string auth_response_code = string.Empty; string auth_transaction_id = string.Empty; CMEDShopTransaction transaction = new CMEDShopTransaction { order_id = ShoppingCartID, first_name = FirstNameTextBox.Text, last_name = LastNameTextBox.Text, address = AddressTextBox.Text, city = CityTextBox.Text, state = State.SelectedValue, postal_code = ZipTextBox.Text, email = EmailTextBox.Text, phone_number = PhoneNumberTextBox.Text, transaction_type = "Credit Card", status = "in-progress" }; try { if (Request.Cookies["TransactionID"] == null) { long transaction_id = controller.CreateCMEDShopTransaction(transaction); //write the cookie of transaction_id for use later Response.Cookies["TransactionID"].Value = transaction_id.ToString(); } Cart cart = controller.LoadCart(ShoppingCartID); if (cart.order_amount == 0) { Response.Redirect(redirectPage); } NVPAPICaller payPalCaller = new NVPAPICaller(); string retTokenMsg = ""; string token = ""; NVPCodec decoder = new NVPCodec(); string amt01 = cart.order_amount.ToString(); bool retToken = payPalCaller.ShortcutExpressCheckout(amt01, ShoppingCartID, ref token, ref retTokenMsg, true); if (retToken) { HttpContext.Current.Session["token"] = token; Response.Redirect(retTokenMsg); } else { lblErrorMessage.Text = "PayPal is not responding, please try again in a few moments."; } } catch (Exception ex) { throw ex; } } }
protected void Page_Load(object sender, EventArgs e) { NVPAPICaller PPAPICaller = new NVPAPICaller(); NVPCodec decoder = new NVPCodec(); string token = string.Empty; string payerID = string.Empty; string finalPaymentAmount = string.Empty; string retMsg = string.Empty; string currency = string.Empty; token = Session["token"].ToString(); //use the PayPal token to get the details of payment - this could include shipping details bool ret = PPAPICaller.GetCheckoutDetails(token, ref decoder, ref retMsg); if (ret) { payerID = decoder["PayerID"]; token = decoder["token"]; finalPaymentAmount = decoder["PAYMENTREQUEST_0_AMT"]; currency = decoder["CURRENCYCODE"]; } else { //error.LogError(); return; } NVPCodec confirmdecoder = new NVPCodec(); if (Request.Cookies["ShoppingCartID"] != null) { ShoppingCartID = long.Parse(Request.Cookies["ShoppingCartID"].Value); } //confirm that payment was taken bool ret2 = PPAPICaller.DoCheckoutPayment(finalPaymentAmount, ShoppingCartID, token, payerID, ref confirmdecoder, ref retMsg); if (ret2) { //if payment was taken do some back end processing to mark order as paid //use token to work out which order to mark as paid token = confirmdecoder["token"]; string auth_status = "approved"; string auth_code = token; string auth_message = "paypal transaction approved"; string auth_response_code = string.Empty; string auth_transaction_id = token; try { controller.UpateCMEDShopTransactionByOrderID(ShoppingCartID, auth_status, auth_code, auth_message, auth_response_code, auth_transaction_id); //create order from cart long order_id = controller.CreateOrderFromCart(ShoppingCartID); HttpCookie Checkout = new HttpCookie("Confirmation"); Checkout["Email"] = Request.Cookies["email"].Value; Checkout["WorkShopName"] = Request.Cookies["WorkShopName"].Value; Checkout.Expires = DateTime.Now.AddMinutes(30); Response.Cookies.Add(Checkout); CMEDShopOrder order = controller.LoadOrder(order_id); CMEDShopTransaction transaction = controller.GetCMEDShopTransactionByOrderID(order_id); Util.SendWelcomeMailWorkShopBalance(transaction, decimal.Parse(Request.Cookies["Balance"].Value), Request.Cookies["WorkShopName"].Value); String s = "CMEDWorkshopPurchaseThankYou.aspx"; Response.Redirect(s); } catch (PayPalAuthourize ex) { throw ex; } } else { string msg = "PayPal is not responding, please try again in a few moments."; throw new PayPalAuthourize(msg); } }
protected void btn_SubmitPaypal_Click(object sender, ImageClickEventArgs e) { //grab shopping cart id cookie if (Request.Cookies["ShoppingCartID"] != null) { ShoppingCartID = long.Parse(Request.Cookies["ShoppingCartID"].Value); } else { Response.Redirect(redirectPage); } //decimal amt = 0; CMEDShopTransaction transaction = new CMEDShopTransaction { order_id = ShoppingCartID, first_name = FirstNameTextBox.Text, last_name = LastNameTextBox.Text, address = AddressTextBox.Text, city = CityTextBox.Text, state = drpState.SelectedValue, postal_code = ZipTextBox.Text, email = EmailTextBox.Text, phone_number = PhoneNumberTextBox.Text, transaction_type = "PayPal", status = "in-progress" }; try { //check transaction_id cookie and if there then dont call create transaction if (Request.Cookies["TransactionID"] == null) { long transaction_id = controller.CreateCMEDShopTransaction(transaction); //write the cookie of transaction_id for use later Response.Cookies["TransactionID"].Value = transaction_id.ToString(); } Cart cart = controller.LoadCart(ShoppingCartID); if (cart.order_amount == 0) { Response.Redirect(redirectPage); } NVPAPICaller payPalCaller = new NVPAPICaller(); string retTokenMsg = ""; string token = ""; NVPCodec decoder = new NVPCodec(); string amt01 = cart.order_amount.ToString(); bool retToken = payPalCaller.ShortcutExpressCheckout(amt01, ShoppingCartID, ref token, ref retTokenMsg, false); if (retToken) { HttpContext.Current.Session["token"] = token; Response.Redirect(retTokenMsg); } else { lblErrorMessage.Text = "PayPal is not responding, please try again in a few moments."; } } catch (Exception ex) { throw ex; } }