📄 ordersmanager.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Providers;
/// <summary>
/// Summary description for OrderManager
/// </summary>
public class OrdersManager {
public const int WAITING_ON_PAYPAL_PAYMENT_ID = 1;
public const int RECEIVED_PAYMENT_PROCESSING_ORDER_ID = 2;
public const int ORDER_PROCESSED_AND_SHIPPED_ID = 3;
public const int ORDER_FULLY_REFUNDED_ID = 4;
public const int INVALID_TOTALS_DONT_MATCH = 6;
public const int WAITING_ON_ECHECK = 7;
#region Gateway Charger
/// <summary>
/// Runs a charge through the Payment Gateway provider.
/// </summary>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
/// <param name="address1"></param>
/// <param name="address2"></param>
/// <param name="city"></param>
/// <param name="state"></param>
/// <param name="zip"></param>
/// <param name="country"></param>
/// <param name="CCNumber"></param>
/// <param name="CCExpMonth"></param>
/// <param name="CCExpYear"></param>
/// <param name="CCType"></param>
/// <param name="CCCVV"></param>
/// <param name="subtotal"></param>
/// <param name="tax"></param>
/// <param name="shipping"></param>
/// <param name="shippingMethod"></param>
/// <param name="paymentMethod"></param>
/// <returns></returns>
public static string RunCharge(string firstName, string lastName,
string address1, string address2,string city, string state, string zip, string country, string CCNumber, int CCExpMonth, int CCExpYear,
CreditCardType CCType, string CCCVV,
double subtotal, double tax, double shipping, string shippingMethod,string paymentMethod) {
int orderID = 0;
string sOut="";
//get the currently logged on user
string userName = System.Web.HttpContext.Current.User.Identity.Name;
//concatenate the shippingAddress - this simplifies shipping demands for foreign shipping addresses
string shippingAddress=firstName+" "+lastName+"\r"+address1+"\r"+address2+"\r"+city+", "+state+" "+zip+
" "+country;
//create an order number
orderID = InitializeOrder(userName, shippingAddress, shippingMethod, subtotal, shipping, tax, zip, paymentMethod);
if (orderID > 0) {
try {
//run the gateway charger - returns a transactionID or auth code
string transactionID = Commerce.Providers.PaymentProvider.Instance.Charge(firstName,
lastName,address1,address2,city,state,zip,country,CCNumber,CCExpMonth,CCExpYear,
CCType,CCCVV,subtotal,tax,shipping,orderID.ToString());
sOut = transactionID;
//commit the order in the db
CommitOrder(orderID,transactionID);
//Log this
Log.Write("付款完成", "付款", "订单号: " + orderID.ToString(), transactionID, userName);
} catch (Exception x) {
//Kill the order
//rethrow the Exception
KillOrder(orderID);
throw x;
}
}
return sOut;
}
#endregion
#region API Calls
public static Commerce.PayPal.APIWrapper.OrderInfo RunExpressOrder(Order orderRec, string ppToken,
string payerID, Commerce.PayPal.APIWrapper.OrderItem[] items) {
Commerce.PayPal.APIWrapper wrapper = new Commerce.PayPal.APIWrapper(
SiteConfiguration.PayPalAPIAccountName, SiteConfiguration.PayPalAPIAccountPassword,
SiteConfiguration.PayPalAPICertificationPath, SiteConfiguration.PayPalAPICertificationPassword);
int orderID = 0;
orderID = InitializeOrder(orderRec.UserName, orderRec.ShippingAddress, orderRec.ShippingMethod,
orderRec.OrderSubTotal, orderRec.Shipping, orderRec.Tax, orderRec.OrderZipCode, orderRec.PaymentMethod);
//add the order and the items, then run the charge to make sure
Commerce.PayPal.APIWrapper.OrderInfo order = null;
try {
//create an order number
//orderID = InitializeOrder(orderRec);
if (orderID > 0) {
//run the payment - no errors after this
order = wrapper.DoExpressCheckout(orderRec.UserName,
ppToken, payerID,
orderRec.OrderSubTotal, orderRec.Tax, orderRec.Shipping, items, "", orderID.ToString());
if (order.Ack != "Success") {
throw new Exception(order.Ack);
} else {
orderRec.PayPalTransactionID = order.TransactionID;
orderRec.OrderID = orderID;
//commit the order
CommitOrder(orderID,order.TransactionID);
//Log this
Log.Write("快速付款完成", "付款", "订单 " + order.TransactionID + " 操作完成", order.TransactionID, System.Web.HttpContext.Current.User.Identity.Name);
}
} else {
throw new Exception("Invalid Order ID");
}
} catch (Exception x) {
//delete out the order and the order items
KillOrder(orderID);
//throw an exception
throw new Exception(x.Message);
}
//return the order info
return order;
}
/// <summary>
/// Refunds the entire sale amount using the PayPal API. This is NOT reversible.
/// </summary>
/// <param name="transactionID">The PayPal transactionID associated with the sale</param>
/// <returns>A string representing "Success" if successful, or an error list if not.</returns>
public static string RefundOrder(string transactionID) {
Commerce.PayPal.APIWrapper wrapper = new Commerce.PayPal.APIWrapper(SiteConfiguration.PayPalAPIAccountName,
SiteConfiguration.PayPalAPIAccountPassword, SiteConfiguration.PayPalAPICertificationPath,
SiteConfiguration.PayPalAPICertificationPassword);
//run the refund method. Errors are trapped in the routine
//and returned as strings.
string sOut=wrapper.RefundTransaction(transactionID);
//Log this
Log.Write("退款完成", "付款", "订单 " + transactionID + " 退款成功", transactionID, System.Web.HttpContext.Current.User.Identity.Name);
return sOut;
}
/// <summary>
/// Uses the PayPal API to get the transaction details of a particular transaction.
/// </summary>
/// <param name="transactionID">The PayPal transactionID associated with the sale</param>
/// <returns>A delimited string with the basic transaction information</returns>
public static string GetTransactionDetails(string transactionID) {
Commerce.PayPal.APIWrapper wrapper = new Commerce.PayPal.APIWrapper(SiteConfiguration.PayPalAPIAccountName,
SiteConfiguration.PayPalAPIAccountPassword, SiteConfiguration.PayPalAPICertificationPath,
SiteConfiguration.PayPalAPICertificationPassword);
return wrapper.GetTransactionDetail(transactionID, "<br>");
}
#endregion
#region Checkout Helper Functions
/// <summary>
/// Creates and order record in the database prior to a sale.
/// </summary>
/// <param name="orderRec">The OrderRecord Object which contains the details of the sale</param>
/// <param name="cart">The CartData object stored in the user's Profile</param>
/// <returns>The new order ID (integer value)</returns>
public static int InitializeOrder(string userName, string shippingAddress, string shippingMethod, double orderSubTotal,
double shippintAmount,double taxAmount,string orderZipCode,string paymentMethod) {
string context = "";
int orderID = 0;
try {
//create an order number
orderID = OrdersProvider.Instance.OrderInsert(WAITING_ON_PAYPAL_PAYMENT_ID, userName,
shippingAddress, shippingMethod, orderSubTotal,
shippintAmount, taxAmount, orderZipCode, paymentMethod);
} catch (Exception x) {
//Kill the order on fail
KillOrder(orderID);
//throw an exception
throw new Exception(x.Message);
}
return orderID;
}
/// <summary>
/// Deletes all order information, including the order details.
/// </summary>
/// <param name="orderID"></param>
public static void KillOrder(int orderID) {
OrdersProvider.Instance.OrderDelete(orderID);
}
/// <summary>
/// Finalizes the order in the database, which includes updating the stats tables and debiting the inventory.
/// Prior to the db routine running, the log enters that the payment has been received
/// in case of any problems.
/// </summary>
/// <param name="orderRec">The OrderRecord Object</param>
public static void CommitOrder(int orderID, string transactionID) {
//log the transaction
//Log.Write("Purchase", "PayPal Payment Completed; finalizing order", "order " +
//transactionID +
//" has been paid", transctionID, System.Web.HttpContext.Current.User.Identity.Name);
//finalize the order (checkout the cart, update the order status)
OrdersProvider.Instance.OrderFinalize(orderID, transactionID);
//debit the inventory
//get the order items and adjust the inventory for each
IDataReader rdr = OrdersManager.GetOrderItems(orderID);
while (rdr.Read()) {
int productID = (int)rdr["productID"];
int quantity=(int)rdr["quantity"];
CatalogManager.AdjustInventory(productID, quantity, "Debit from sale " + transactionID);
}
rdr.Close();
//email somebody to let them know an order was received...
//TODO:implement order notification
}
#endregion
#region Get Functions
/// <summary>
/// Gets an order by PayPal transactionID
/// </summary>
/// <param name="transactionID">PayPal transactionID</param>
/// <returns>DataReader of the order</returns>
public static IDataReader GetOrderByTransactionID(string transactionID) {
return OrdersProvider.Instance.OrderGetByTransactionID(transactionID);
}
/// <summary>
/// Gets an order by orderID
/// </summary>
/// <param name="transactionID">PayPal transactionID</param>
/// <returns>DataReader of the order</returns>
public static Order GetOrderByID(int orderID) {
IDataReader rdr=OrdersProvider.Instance.OrderGet(orderID);
Order order=new Order();
order.Load(rdr);
rdr.Close();
return order;
}
public static IDataReader GetOrderItems(int orderID) {
return OrdersProvider.Instance.OrdersGetItems(orderID);
}
/// <summary>
/// Gets all orders for a given user
/// </summary>
/// <param name="userName">The user's login name</param>
/// <returns>DataReader of the orders</returns>
public static IDataReader OrdersByUser(string userName) {
return OrdersProvider.Instance.OrderGetByUser(userName);
}
/// <summary>
/// Returns all orders with an order date falling within the specified range
/// </summary>
/// <param name="dateStart"></param>
/// <param name="dateEnd"></param>
/// <returns>DataReader</returns>
public static IDataReader OrdersByDateRange(string dateStart, string dateEnd) {
return OrdersProvider.Instance.OrdersByDateRange(dateStart, dateEnd);
}
/// <summary>
/// Returns all orders for a partial transaction ID (such as transactionIDs starting with '8'
/// </summary>
/// <param name="transactionID"></param>
/// <returns></returns>
public static IDataReader OrdersByTransactionPartial(string transactionID) {
return OrdersProvider.Instance.OrdersByTransactionIDPartial(transactionID);
}
#endregion
/// <summary>
/// Updates the shipping address for a given order
/// </summary>
/// <param name="orderID"></param>
/// <param name="shippingAddress">String blob of the the full address.</param>
public static void UpdateShippingAddress(int orderID, string shippingAddress) {
OrdersProvider.Instance.OrderUpdateShippingAddress(orderID, shippingAddress);
}
/// <summary>
/// Checks to see if the current user can view a specific order.
/// </summary>
/// <param name="userName"></param>
/// <param name="transactionID"></param>
/// <returns>True if the user put in the order</returns>
public static bool UserCanView(string userName, string transactionID) {
return OrdersProvider.Instance.OrderUserCanView(userName, transactionID);
}
#region Status Updates
/// <summary>
/// Sets the order's ship date and status to shipped.
/// </summary>
/// <param name="orderID"></param>
/// <param name="shipDate"></param>
/// <param name="trackingNumber">The tracking number supplied by the shipping vendor (e.g. the Fedex Number)</param>
public static void SetAsShipped(int orderID, DateTime shipDate,string trackingNumber) {
//depending on your shipping process, this is where you would notify the user
//of the shipping/tracking info via email
OrdersProvider.Instance.OrderSetShipped(orderID, shipDate,trackingNumber);
}
/// <summary>
/// Updates the order status to the supplied statusID.
/// </summary>
/// <param name="orderID"></param>
/// <param name="statusID"></param>
public static void UpdateStatus(int orderID, int statusID) {
OrdersProvider.Instance.OrderUpdateStatus(orderID, statusID);
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -