Hi Vulpes
Sorry to email you direct I have posted an advert on the forum but havent had a reply and I know your be able to answer this no problems.
Ive written Submit and GetTotal methods as follows:-
public decimal GetTotal(string cartID)
{
using (DCECommerceDBEntities db = new DCECommerceDBEntities())
{
decimal cartTotal = 0;
try
{
var mycart = (from c in db.ViewCarts where c.CartID == cartID select c);
if (mycart.Count() > 0)
{
cartTotal = mycart.Sum(od => (decimal)od.Quantity * (decimal)od.UnitCost);
}
}
catch (Exception exp)
{
throw new Exception("ERROR: Unable to Calculate Order Total - " + exp.Message.ToString(), exp);
}
return (cartTotal);
}
}
public bool SubmitOrder(string userName)
{
using (DCECommerceDBEntities db = new DCECommerceDBEntities())
{
try
{
Order newOrder = new Order();
newOrder.CustomerName = userName;
newOrder.OrderDate = DateTime.Now;
db.Orders.AddObject(newOrder);
db.SaveChanges();
int NewOrderKey = (from p in db.Orders select p.OrderID).Max();
String cartId = GetShoppingCartId();
var myCart = (from c in db.ViewCarts where c.CartID == cartId select c);
foreach (ViewCart item in myCart)
{
OrderDetail od = new OrderDetail();
od.OrderID = NewOrderKey;
od.ProductID = item.ProductID;
od.Quantity = item.Quantity;
od.UnitCost = item.UnitCost;
db.OrderDetails.AddObject(od);
var myItem = (from c in db.ShoppingCarts where c.CartID == item.CartID && c.ProductID == item.ProductID select c).FirstOrDefault();
if (myItem != null)
{
db.DeleteObject(myItem);
}
}
db.SaveChanges();
}
catch(Exception exp)
{
}
}
return true;
}
I basically want to implement the below so that I can pass the return values ie orderId etc to PayPal.
string redirect = "";
redirect += "https://www.paypal.com/xclick/[email protected]";
redirect += "&item_name=" + siteName + " Order " + orderId;
redirect += "&item_number=" + orderId;
redirect += "&amount=" + String.Format("{0:0.00} ", amount);
redirect += "¤cy=USD";
redirect += "&return=http://www." + siteName + ".com";
redirect += "&cancel_return=http://www." + siteName + ".com";
// Redirect to the payment page
Response.Redirect(redirect);
With regards to the siteName I can use a const string so thats fine and the currency is easily enough also, its mainly the Total Amount and OrderId I need help with please.
Many thanks
Steven
VulpesPosted May 7, 2012, 10:41 AM
Guest UserPosted May 7, 2012, 2:22 PM
Thank-you!
VulpesPosted May 7, 2012, 2:12 PM
usersShoppingCart.SubmitOrder(User.Identity.Name, out orderDetails);
Guest UserPosted May 7, 2012, 1:50 PM
Yes im using .NET 4.0, VS 2010.
Ive now added my DataAccess folder containing the EF to the CheckOut class.
Now within my Submit button Event Handler of the CheckOut class it looks like this:-
List
usersShoppingCart.SubmitOrder(User.Identity.Name, orderDetails);
foreach(OrderDetail od in orderDetails)
{
int quantity = od.Quantity;
// do something with Quantity (ie pass the Quantity value to PayPal!)
}
Does this look right to you?
Thanks
Steven
VulpesPosted May 7, 2012, 1:36 PM
Guest UserPosted May 7, 2012, 1:09 PM
Unfortunately I cant get that to work.
Basically I need to use the below code in a different class being the CheckOut class.
I currently access the SubmitOrder method like so:-
reference.SubmitOrder(User.Identity.Name);
Which works fine
But now ive modified SubmitOrder to provide an out parameter the List
The CheckOut class is where I pass the values to PayPal.
I guess I could just put my using statement in the CheckOut class to get access to the EF objects, enabling me to use the out parameter in the the CheckOut class? however this would be a New object and not the current object im working with.
Thanks
Steven
You can then do:
List
int orderId = SubmitOrder(userName, out orderDetails);
if (orderId > -1)
{
// iterate through your orderDetails list
foreach(OrderDetail od in orderDetails)
{
int quantity = od.Quantity;
// do something with quantity
}
// assemble your redirect string
}
else
{
// something went wrong
}
VulpesPosted May 7, 2012, 12:52 PM
The important thing is that you can iterate through all the OrderDetail objects using foreach, access the Quantity property (and any other properties you need) for each object in turn and do what you like with them.
Guest UserPosted May 7, 2012, 12:27 PM
Sorry to be a pain
Just reading through your code sample you kindly provided, could you explain this please
// iterate through your orderDetails list
foreach(OrderDetail od in orderDetails)
{
int quantity = od.Quantity;
// do something with quantity
}
Is...
int quantity = od.Quantity getting "all" the Quantities? or just the last ones entered into the OrderDetail table?
Thanks
Steven
Guest UserPosted May 7, 2012, 10:52 AM
Regards
Steven
Guest UserPosted May 7, 2012, 10:26 AM
So I guess I just need to Prefix the List onto the existing OrderDetail ie List
How could I then use the out parameter to get the required Quantities, could you provide a sample please?
VulpesPosted May 7, 2012, 10:21 AM
But do you just want the quantities themselves or do you need further information such as the product type?
Perhaps it would be easier just to return a List of all the OrderDetail objects and you could then pick out any info you wanted.
Guest UserPosted May 7, 2012, 10:10 AM
If there were 2 items in the cart and 1 had a quantity of 1 and the other item had a quantity of 1 id like both the 1 quantites if that makes sense? if this is too tricky it doesnt matter.
Thanks!
VulpesPosted May 7, 2012, 9:46 AM
Guest UserPosted May 7, 2012, 9:32 AM
If I needed to get another return value out of the SubmitOrder method ie Quantity in addition to the OrderId, how would you say id go about this?
I guess the other way would be to write a Query to retrieve the last entered info into the OrderDetail table of the database?
Regards
Steven
VulpesPosted May 3, 2012, 7:54 PM
Guest UserPosted May 3, 2012, 4:35 PM
As ive now changed the return type of SubmitOrder from bool to int. I ideally want to do a cast from int to bool in order to return a true or false as to whether the submission was successful or not. Once the result is rec'd I wish to go on and do something else.
for example given:-
if(usersShoppingCart.SubmitOrder(User.Identity.Name) == true);
{
PaymentButton.Visable = true; // Show or Display the Payment button
}
At the moment the SubmitOrder method is of Return Type int so a conversion error obviously occurs.
Therefore I want to do something like this:-
if((bool)usersShoppingCart.SubmitOrder(User.Identity.Name) == true);
{
PaymentButton.Visable = true; // Show or Display the Payment button
}
but this does'nt work!?
Any help appreciated.
Regards
steven
Guest UserPosted May 2, 2012, 12:14 PM
Regards
Steven
VulpesPosted May 2, 2012, 10:13 AM
Can you pinpoint on which line the error occurs ?
VulpesPosted May 2, 2012, 6:25 AM
Guest UserPosted May 2, 2012, 6:07 AM
Would you say its best to isolate\encapsulate the 2 method calls ie
decimal amount = GetTotal(cartId);
int orderId = SubmitOrder(userName);
if(orderId > -1)
{
// Redirect string
}
else
{
// Something went wrong
}
into a method of its own and then place the method at the end of the SubmitOrder method, once all the operations\actions have been completed?
Thanks
Steve
VulpesPosted Apr 30, 2012, 4:53 AM
I thought -1 would be a suitable value for this as your OrderIds are not going to be negative numbers.
Guest UserPosted Apr 30, 2012, 3:48 AM
I can see what you've done there regards to refactoring so that the OrderId can be retrieved easily, however
could you explain the reason for setting
NewOrderKey to -1 please?
Regards
Steven
VulpesPosted Apr 28, 2012, 8:25 AM