예제 #1
0
 /// <summary>
 /// Raises the onrecalculateorder event.
 /// </summary>
 /// <param name="args">The <see cref="Rendition.RecalculateOrderEventArgs"/> instance containing the event data.</param>
 internal void raiseOnrecalculateorder( RecalculateOrderEventArgs args )
 {
     if( RecalculatedOrder != null ) { RecalculatedOrder( this, args ); };
 }
예제 #2
0
            /// <summary>
            /// Recalculates the order.
            /// </summary>
            /// <param name="args">The order arguments.</param>
            /// <param name="fcn">The FCN.</param>
            /// <param name="ftrans">The ftrans.</param>
            /// <returns>{error:0,desc:""}</returns>
            public static Dictionary<string, object> RecalculateOrder(Dictionary<string, object> args, SqlConnection fcn, SqlTransaction ftrans)
            {
                Dictionary<string, object> vt;
                Dictionary<string, object> j;
                Commerce.CreditCard card = null;
                Commerce.Cash cash = null;
                Commerce.Wire wire = null;
                // never used -> Commerce.PayPal PayPal=null;
                Commerce.Check check = null;
                Commerce.PromiseToPay promiseToPay = null;
                decimal discountAmount = 0;
                Guid paymentMethodId = Guid.NewGuid();
                bool backorder = false;
                bool preview = false;
                int errorId = -1;
                ("FUNCTION /w SP,CN,TRANS recalculateOrder").Debug(10);
                string[] keys = { "userId", "orderSessionId", "cartSessionId", "preview", "purchaseOrder" };
                bool transactionSucsessStatus = false;
                int termId = 0;
                int orderId = 0;
                decimal difference = 0;
                foreach(string keyName in keys) {
                    if(!args.ContainsKey(keyName)) {
                        Dictionary<string, object> o = new Dictionary<string, object>();
                        string _msg = "The key \"" + keyName + "\" is missing from the argument dictionary.  All keys must be present even if they are blank.";
                        o.Add("error", -4010);
                        o.Add("description", _msg);
                        String.Format("recalculateOrder failed. {0}", _msg).Debug(1);
                        return o;
                    }
                }
                /* get the old order */
                SqlConnection cn;
                SqlTransaction trans;
                Guid orderSessionId = new Guid((string)args["orderSessionId"]);
                if(fcn == null) {
                    cn = Site.CreateConnection(true, true);
                    cn.Open();
                    trans = cn.BeginTransaction("Recalculate transaction");
                } else {
                    cn = fcn;
                    trans = ftrans;
                }
                Commerce.Order originalOrder = Commerce.Order.GetOrderBySessionId(orderSessionId, cn, trans);
                termId = originalOrder.TermId;
                preview = Convert.ToBoolean(args["preview"].ToString());
                if(!preview) {
                    /* preview the recalculation in preview to asertain the grand total for charging the card */
                    trans.Save("preChargePreview");
                    j = ExecPlaceOrder(
                        orderSessionId,
                        Convert.ToInt32(args["userId"].ToString()),
                        orderSessionId,
                        true,/*preview*/
                        new Guid(Main.Site.Defaults.SiteId),
                        new Guid((string)args["cartSessionId"]),
                        args["purchaseOrder"].ToString(),
                        DateTime.Now/* this value is ignored in the SP for recalculations */,
                        termId,
                        discountAmount,
                        cn,
                        trans
                    );
                    termId = (int)j["termId"];
                    orderId = (int)j["orderId"];
                    if(j["error"].ToString() != "0" && j["error"].ToString() != "5") {
                        Exception ex = new Exception(j["description"].ToString());
                        throw ex;
                    }
                    if(!decimal.TryParse(j["difference"].ToString(), out difference)) {
                        difference = 0;
                    }
                    difference = Math.Round(difference, 2, MidpointRounding.AwayFromZero);
                    if(difference != 0) {
                        if(backorder) {
                            /* no need to do anything */
                        } else if(termId == 0 && difference > 0) {/*this is a prepaid credit card transaction - termId 0 */
                            /* don't try and charge credit cards negitive amounts */
                            card = new Commerce.CreditCard(
                                args["cardType"].ToString().MaxLength(50, true),
                                args["cardNumber"].ToString().MaxLength(100, true),
                                args["nameOnCard"].ToString().MaxLength(100, true),
                                args["secNumber"].ToString().MaxLength(7, true),
                                args["expMonth"].ToString().MaxLength(4, true),
                                args["expYear"].ToString().MaxLength(4, true)
                            );
                            List<int> orderIds = new List<int>();
                            orderIds.Add(orderId);
                            card.Insert(paymentMethodId, orderSessionId, originalOrder.UserId, originalOrder.SessionId,
                            termId, "", difference, DateTime.Now, orderIds, "", cn, trans);
                        } else if(termId == 9 /* this is a COD Check transaction - termId 9 */ ) {
                            check = new Commerce.Check(
                                args["checkNumber"].ToString().MaxLength(50, true),
                                args["routingNumber"].ToString().MaxLength(50, true),
                                args["bankAccountNumber"].ToString().MaxLength(50, true),
                                args["checkNotes"].ToString().MaxLength(50, true)
                            );
                            List<int> orderIds = new List<int>();
                            orderIds.Add(orderId);
                            check.Insert(paymentMethodId, originalOrder.UserId, originalOrder.SessionId, termId, "", difference, DateTime.Now, orderIds, "", cn, trans);
                        } else if(termId == 20 /* this is a wire transfer - termId 20 */ ) {
                            wire = new Commerce.Wire(
                                args["swift"].ToString().MaxLength(50, true),
                                args["bankName"].ToString().MaxLength(50, true),
                                args["routingTransitNumber"].ToString().MaxLength(50, true)
                            );
                            List<int> orderIds = new List<int>();
                            orderIds.Add(orderId);
                            wire.Insert(paymentMethodId, originalOrder.UserId, originalOrder.SessionId, termId, "", difference, DateTime.Now, orderIds, "", cn, trans);
                        } else if(termId == 13 /* this order is prepaid in cash */) {
                            cash = new Commerce.Cash(); /*don't you wish it was really that easy?*/
                            List<int> orderIds = new List<int>();
                            orderIds.Add(orderId);
                            cash.Insert(paymentMethodId, originalOrder.UserId, originalOrder.SessionId, termId, "", difference, DateTime.Now, orderIds, "", cn, trans);
                        } else if(difference > 0) {
                            /* this order is an accrued order */
                            promiseToPay = new Commerce.PromiseToPay();
                            List<int> orderIds = new List<int>();
                            orderIds.Add(orderId);
                            promiseToPay.Insert(paymentMethodId, originalOrder.UserId, originalOrder.SessionId, termId, "", difference, DateTime.Now, orderIds, "", cn, trans);
                        }
                    }
                    trans.Rollback("preChargePreview");
                }
                /* do the recalculation */
                j = ExecPlaceOrder(
                    new Guid((string)args["orderSessionId"]),
                    Convert.ToInt32(args["userId"].ToString()),
                    new Guid((string)args["orderSessionId"]),
                    preview,
                    new Guid(Main.Site.Defaults.SiteId),
                    new Guid((string)args["cartSessionId"]),
                    args["purchaseOrder"].ToString(),
                    DateTime.Now/* this value is ignored in the SP for recalculations */,
                    termId,
                    discountAmount,
                    cn,
                    trans
                );
                errorId = Convert.ToInt32(j["error"].ToString());
                if(errorId != 0 && errorId != 5) {/* if there was an return the error without continuing */
                    if(fcn == null) {
                        trans.Rollback();
                        cn.Close();
                    }
                    return j;
                };

                if(termId != 0 || backorder == true) {
                    /* this order uses acrued payment method or has lost value, don't charge now */
                    ("order with payment terms, backorder.  No payment gateway now.").Debug(7);
                    transactionSucsessStatus = true;
                } else if(difference > 0) {
                    ("starting payment gateway...").Debug(5);
                    vt = Commerce.VirtualTerminal.ChargeCreditCard(
                        (Commerce.Address)j["billToAddress"], (Commerce.Address)j["shipToAddress"], card,
                        difference, new Guid((string)args["orderSessionId"]), originalOrder.OrderNumber,
                        originalOrder.PurchaseOrder, fcn, ftrans
                    );
                    if(vt == null) {
                        Dictionary<string, object> o = new Dictionary<string, object>();
                        trans.Rollback();
                        o.Add("error", -1754);
                        o.Add("description", "Internal virtual terminal error.  Unable to create virtual terminal object.");
                        ("Invalid credit card passed to local system").Debug(7);
                        ("placeOrder Failed with error code -1754").Debug(7);
                        if(fcn == null) {
                            cn.Dispose();
                        }
                        return o;
                    }
                    transactionSucsessStatus = vt["error"].ToString() == "0";
                    if(!transactionSucsessStatus) {
                        j.Add("error", -3);
                        j.Add("description", vt["description"]);
                        j.Add("virtualTerminal", vt);
                        if(fcn == null) {
                            cn.Dispose();
                        }
                        return j;
                    }
                }
                if(errorId != 0 || transactionSucsessStatus == false || preview) {
                    if(fcn == null) {
                        trans.Rollback();
                    }
                    if(!preview) {
                        Exception ex = new Exception("The trasnaction failed.");
                        throw ex;
                    }
                } else {
                    Commerce.Order order;
                    RecalculateOrderEventArgs e;
                    if(fcn == null) {
                        order = Commerce.Order.GetOrderByOrderId(orderId, cn, trans);
                        e = new RecalculateOrderEventArgs(order, cn, trans, args, Main.GetCurrentSession(), HttpContext.Current);
                    } else {
                        order = Commerce.Order.GetOrderByOrderId(orderId, fcn, ftrans);
                        e = new RecalculateOrderEventArgs(order, fcn, ftrans, args, Main.GetCurrentSession(), HttpContext.Current);
                    }
                    Main.Site.raiseOnrecalculateorder(e);
                    if(Site.AbortDefaultEvent == true) {
                        Site.AbortDefaultEvent = false;
                    }
                    if(fcn == null) {
                        trans.Commit();
                    }
                }
                if(fcn == null) {
                    cn.Dispose();
                }
                return j;
            }