Click here to follow the steps to add the Magento reference to your asp.net C# application . I think, doing a order through Magento Api is a tricky process. You need to follow the perfect flow to make an order into Magento store. For placing an order, you should have a Magento Store_ID, Api_USER_Name, Api_User_Key. Get your SessionId(32-bit string) by using Api_USER_Name and Api_User_key. Beacause, you have to use the SessionId in every api call.
First, you have to create the Shopping cart Id. If Cart Id is generated successfull, then you have to add the Customer Info, Customer Address, Product, Shipping type and Payment type respectively. You should add the all details in the right flow then only order will be saved successfully. Follow the below code.
After successful order, you will get the Order Id otherwise zero. Product quantity will be decreased automatically after the order.
First, you have to create the Shopping cart Id. If Cart Id is generated successfull, then you have to add the Customer Info, Customer Address, Product, Shipping type and Payment type respectively. You should add the all details in the right flow then only order will be saved successfully. Follow the below code.
protected void SaveOrder()
{
string storeId = "YOUR_Store_Id"; string mlogin=null;
try { mlogin = mservice.login("YOUR_API_UserName", "YOUR_API_UserKey"); //Here mlogin means SessionID. We have to use it in every call. } catch (Exception ex) { // Handle the error if service is not availiable //Magento Save Order- Service login failed with response } int Shopping_Cart_ID = 0; try { // Creating Shopping CartID. Without CradID, you can't proceed next step. Shopping_Cart_ID = mservice.shoppingCartCreate(mlogin, storeId); } catch { // Handle the error if Shopping cart id not created } if (Shopping_Cart_ID != 0) // Open if Shopping CartID is created { try { shoppingCartCustomerEntity customer = new shoppingCartCustomerEntity(); //If you have customer ID then do the below method. Otherwise go for guest. if (Session["Customer_ID"].ToString() != null) { customerCustomerEntity customerCustomerEntity = mservice.customerCustomerInfo(mlogin, int.Parse(Session["Customer_ID"].ToString()), null); customer.mode = "customer"; customer.customer_id = customerCustomerEntity.customer_id; customer.customer_idSpecified = true; customer.email = customerCustomerEntity.email; customer.password = customerCustomerEntity.password_hash; customer.confirmation = customerCustomerEntity.confirmation.ToString(); customer.website_id = customerCustomerEntity.website_id; customer.website_idSpecified = true; customer.store_id = customerCustomerEntity.store_id; customer.store_idSpecified = true; customer.group_id = customerCustomerEntity.group_id; customer.group_idSpecified = true; } else { customer.mode = "guest"; customer.firstname = "Teja"; customer.lastname = "Prakash"; customer.email = "Info@tejaprakash.com"; customer.website_id = 0; customer.website_idSpecified = true; } bool result = false; //If call success then your result will be 'TRUE'. Otherwise 'False'. //Saving Customer Info to Cart result = mservice.shoppingCartCustomerSet(mlogin, Shopping_Cart_ID, customer, storeId); //Setting Customer address to Shopping Cart List<shoppingCartCustomerAddressEntity> customer_Address_List = new List<shoppingCartCustomerAddressEntity>(); shoppingCartCustomerAddressEntity customer_Address_Shipping = new shoppingCartCustomerAddressEntity(); //You have to set the both shipping and billing address of Customer. // If you have already Magento User address list then use below field "address_id". // customer_Address_Shipping.address_id = Session["Customer_Address_ID"].ToString(); customer_Address_Shipping.mode = "shipping"; customer_Address_Shipping.firstname = "Teja"; customer_Address_Shipping.lastname = "Prakash"; customer_Address_Shipping.company = ""; customer_Address_Shipping.street = "Road no.6,Madhapur"; customer_Address_Shipping.city = "Hyderabad"; customer_Address_Shipping.country_id = "IN"; customer_Address_Shipping.region = "Hyderabad"; customer_Address_Shipping.telephone = "123456789"; customer_Address_Shipping.fax = ""; customer_Address_Shipping.postcode = "500081"; // Below 4 fileds are used to set their Shipping address as default shipping, billing. If necessary. // customer_Address_Shipping.is_default_shipping = 1; // customer_Address_Shipping.is_default_shippingSpecified = true; // customer_Address_Shipping.is_default_billing = 1; // customer_Address_Shipping.is_default_billingSpecified = true; //Adding shipping address to list. customer_Address_List.Add(customer_Address_Shipping); //Set Billing address now as like shipping. shoppingCartCustomerAddressEntity customer_Address_Billing = new shoppingCartCustomerAddressEntity(); customer_Address_Billing.mode = "billing"; // If you have already a Magento User address list then use below field "address_id". //customer_Address_Billing.address_id = Session["Customer_Address_ID"].ToString(); customer_Address_Billing.firstname = "Teja"; customer_Address_Billing.lastname = "Prakash"; customer_Address_Billing.company = ""; customer_Address_Billing.street = "Road No.6, Madhapur"; customer_Address_Billing.city = "Hyderabad"; customer_Address_Billing.country_id = "IN"; customer_Address_Billing.region = "Hyderabad"; customer_Address_Billing.postcode = "500081"; customer_Address_Billing.telephone = ""; customer_Address_Billing.fax = ""; // Below 4 fileds are used to set their Shipping address as default shipping, billing. If necessary. // customer_Address_Billing.is_default_shipping = 1; // customer_Address_Billing.is_default_shippingSpecified = true; // customer_Address_Billing.is_default_billing = 1; // customer_Address_Billing.is_default_billingSpecified = true; //Adding billing address to the list. customer_Address_List.Add(customer_Address_Billing); //Saving Customer Address to the cart. result = mservice.shoppingCartCustomerAddresses(mlogin, Shopping_Cart_ID, customer_Address_List.ToArray(), null); //Add Product Information. List<shoppingCartProductEntity> Products_list = new List<shoppingCartProductEntity>(); shoppingCartProductEntity Product = new shoppingCartProductEntity(); //Give your Magento Product ID. Product must be in active state. Product.product_id = 2; Products_list.Add(Product); //Saving the Product. result = mservice.shoppingCartProductAdd(mlogin, Shopping_Cart_ID, Products_list.ToArray(), null); // Set your Shiping type method. Magento provides so many shipping service for you like Fedex, Ups, USPS,..etc. // If you need list of codes then go for 'shoppingCartShippingList' method. //For Flatrate - 'flatrate_flatrate', //FedEx Standard Overnight - 'fedex_STANDARD_OVERNIGHT' //UPS - UPS Second Day Air - 'ups_2DA'...etc result = mservice.shoppingCartShippingMethod(mlogin, Shopping_Cart_ID, "flatrate_flatrate", storeId); // Add Payment method to cart. shoppingCartPaymentMethodEntity payment_method_entity = new shoppingCartPaymentMethodEntity(); bool creditCardPayment = false; // For Non-Credit card payments use below condition. if (!creditCardPayment) { payment_method_entity.method = "checkmo"; payment_method_entity.cc_cid = null; payment_method_entity.cc_owner = null; payment_method_entity.cc_number = null; payment_method_entity.cc_type = null; payment_method_entity.cc_exp_month = null; payment_method_entity.cc_exp_year = null; } //For credit card Payment, you have to mention the card type. Few methods are below. //Or call 'shoppingCartPaymentList' to get the list of payments. //Sample call for Payments list method like...shoppingCartPaymentMethodResponseEntity[] list = mservice.shoppingCartPaymentList(mlogin, Shopping_Cart_ID, null); else { string cardtype = Session["CardType"].ToString(); if (string.Equals(cardtype, "Visa", StringComparison.OrdinalIgnoreCase)) { cardtype = "VI"; } else if (string.Equals(cardtype, "MasterCard", StringComparison.OrdinalIgnoreCase)) { cardtype = "MC"; } else if (string.Equals(cardtype, "American Express", StringComparison.OrdinalIgnoreCase)) { cardtype = "AE"; } else if (string.Equals(cardtype, "Discover", StringComparison.OrdinalIgnoreCase)) { cardtype = "DI"; } else { cardtype = "OT"; } payment_method_entity.po_number = "OrderNumber"; //OD123456 payment_method_entity.method = "ccsave"; //Fixed payment_method_entity.cc_cid = null; //optional payment_method_entity.cc_owner = "Name"; //Ex: Teja Prakash payment_method_entity.cc_number = "CreditCardNumber"; //Ex: 4111111111111111 payment_method_entity.cc_type = cardtype; //Ex: VI for VISA payment_method_entity.cc_exp_month = "CreditExpirationMonth"; //Ex: 09 payment_method_entity.cc_exp_year = "CreditExpirationYear"; //Ex: 2018 } //Saving Payment method details. result = mservice.shoppingCartPaymentMethod(mlogin, Shopping_Cart_ID, payment_method_entity, null); //If you added all the details to cart successfully then only you need to make an order otherwise it fails. if (result) { string order_ID = string.Empty; order_ID = mservice.shoppingCartOrder(mlogin, Shopping_Cart_ID, storeId, null); //On successfull order, you will get the ORDERID from Magento. if (order_ID != string.Empty) { return order_ID; } } } catch(Exception ex) { // Handle your Error } }// Close Shopping CartID return 0; }
After successful order, you will get the Order Id otherwise zero. Product quantity will be decreased automatically after the order.
Hi Tej Prakash,
ReplyDeleteThank you very much for the post and it is very useful. I am developing one c# application which create Products in Magento V1.8.1.0. We are using SOAP API V2. All are seems to be working fine except the below items
1) Not able to associate/add simple products to a configurable product.
2) Not able to add a product along with values for additional attributes.
Thanks in advance.
Thanks and Regards
Venkatesan
This comment has been removed by the author.
ReplyDeleteHave you been able to get the Credit Card payment to work successfully? Im using Authorize.Net on our store now, but when I try to use it using the Magento Soap v2 API payment_method_entity.method = "authorizenet" (thats what calling shoppingCartPaymentList() returns to me) it always returns the error message "Payment Method Not Available." or "Credit card number mismatch with credit card type." even though I'm verifying the correct cc type for Visa "VI" and the number is correct. We have Authorize.net enabled in the admin area and we take orders on the front end and backend of the website now. We just need to do if with the Soap API.
ReplyDeleteThe soap api does work when I set the payment_method_entity.method = "ccsave", but that is not the method we want to use for credit cards.
Any thoughts?
Thanks
Hi
DeleteDid you able to see the "authorizenet" in Payment list API method.
http://devdocs.magento.com/guides/m1x/api/soap/checkout/cartPayment/cart_payment.list.html
If you are not getting it in the list then check your admin settings once. You must need to enable it in admin side. Use the same method name if you able to see in the list.
Thanks for responding. Your code has been very helpful.
ReplyDeleteYes, as stated in my message it is set to use authorize.net and we are taking orders successfully on the frontend of our website. What I have found out is that the Soap API cc number is empty by the time its sending it to Authorize.Net web service.
Where you ever able to successfully make the transaction using the credit card and not the ccsave or checkmo methods? From what I have read on many searches, no one has been able to and your code is the closest I've been able to come to making a successful roundtrip payment to Authorize.Net's backend.
Thank you. That's great!
DeleteGreat post! Do you know if it's possible to add a Product with a Custom Price?
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteIt was the nice post and interesting to read
ReplyDeleteNebosh course in Chennai
Nebosh course in Dubai
Has anyone been able to process the transaction using "authorizenet" yet using this code? Its still not working for us. I had to make a separate API call to authorize.net's API and then update the magento product inventory and shipping. Not an optimal solution but it works.
ReplyDeleteHi - informative post. I'm doing this at the moment (1.9.4 API2) and find I can't add a payment method unless the cart has a value - Do you know how to set a value for the added product(s)? Cheers.
ReplyDeleteCreate the shopping cart first using the shoppingCartCreate() method, its at the top of his code above. Then once the cart is created you set the payment method using shoppingCartPaymentMethod() above and pass in the products using the shoppingCartProductAdd() method above.
DeleteI was able to get everything working accept the processing of the credit card. Magento zeros out the credit card number and Authorize.NET throws an error for an invalid credit card number. I would be interested in knowing if you get that working or not.
Hi,
DeleteFirst, without cart value you can't create an order in Magento. And I don't know whether you are working in production or sandbox environment. If you are using test credit cards then you should allow the settings for test transaction.
This comment has been removed by the author.
ReplyDelete