Saturday, September 6, 2014

Magento Soap API call in Asp.net C#: Save Order

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.


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.




Read More »