Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonas Eriksson 930 posts 1825 karma points
    Oct 26, 2014 @ 16:49
    Jonas Eriksson
    0

    Necessary steps to create and fullfil order

    I'm trying to get the Merchello order process and am a bit stuck. I'm trying to create a function which is supposed to add, pay and fullfill an order in one step. I hope someone can help me fill in the blanks

                // get product : do I really need to fetch all to get the product by sku?

                var product = _merchelloContext.Services.ProductService.GetAll().FirstOrDefault(p => p.Sku == productSku);

                // get customer

                var customer = _merchelloContext.Services.CustomerService.GetByLoginName(userName);

                // create customer with fake data if it does not exist already

                if (customer == null)

                {

                    customer = _merchelloContext.Services.CustomerService.CreateCustomerWithKey(userName);

                    customer.Email = "[email protected]";

                    customer.FirstName = "First";

                    customer.LastName = "Last name";

                    var destination = new Address()

                    {

                        Name = "Mindfly Web Design Studio",

                        Address1 = "115 W. Magnolia St.",

                        Address2 = "Suite 504",

                        Locality = "Bellingham",

                        Region = "WA",

                        PostalCode = "98225",

                        CountryCode = "SE"

                    };

                    customer.CreateCustomerAddress(destination, "defaultShipping", AddressType.Shipping);

                    customer.CreateCustomerAddress(destination, "defaultBilling", AddressType.Billing);

                    _merchelloContext.Services.CustomerService.Save(customer);

                }

     

                var invoiceService = MerchelloContext.Current.Services.InvoiceService;

                // how do I instantiate a new invoice for my newly created customer?

                var invoice = invoiceService.CreateInvoice(Merchello.Core.Constants.DefaultKeys.InvoiceStatus.Unpaid);

                // not sure if I need to add this manually?
                invoice.Total = 100;
                var extendedData = new ExtendedDataCollection();
                extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.CurrencyCode, "SEK");
                extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.Taxable, false.ToString());
                var orderline = new InvoiceLineItem(LineItemType.Product, product.Name, product.Sku, 1, product.Price, extendedData);
                invoice.Items.Add(orderline);
                var gateway = _merchelloContext.Gateways.Payment.GetAllActivatedProviders().FirstOrDefault();
                var provider = _merchelloContext.Gateways.Payment.GetProviderByKey(gateway.Key);
                var resource = provider.ListResourcesOffered().FirstOrDefault(x => x.ServiceCode == "Cash");
                var method = provider.CreatePaymentMethod(resource, "Cash", "Cash Payments");
                provider.SavePaymentMethod(method);
                var paymentMethodKey = method.PaymentMethod.Key;
                // if I like to mark the order as payed by cash I don't need to AuthorizePayment (?)
                // invoice.AuthorizePayment(paymentMethodKey);
                invoice.AuthorizeCapturePayment(paymentMethodKey);
                // now I like to fulfill the sale, how do I do that? 
                var order = invoice.PrepareOrder();

                invoiceService.Save(invoice);

    Thanks
    / learning step by step :)
  • Jonas Eriksson 930 posts 1825 karma points
    Oct 26, 2014 @ 18:25
    Jonas Eriksson
    0

    Hmm.. looks like its easier to set the customer in v 1.5 (currently using 1.4.1)

    ((Invoice)invoice1).CustomerKey = customer.Key;

    https://github.com/Merchello/Merchello/blob/c67fac10d2f044eac41a63d23017dfd5a7ba9522/test/Merchello.Tests.IntegrationTests/Examine/InvoiceAndOrderProviderTests.cs#L193

     

     

  • DFBerry 53 posts 130 karma points
    Oct 27, 2014 @ 02:29
    DFBerry
    1

    I have an example with a Merchello Surface controller and checkout controller. You are missing some steps.

     

    https://github.com/dfberry/Merchello-Example-Projects/tree/master/src/Checkout

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 27, 2014 @ 06:19
    Jonas Eriksson
    0

    Great example, I will check it out more for the frontend process, but unfortunately I forgot to add that I'm going to use this from the backend, and there I cannot use the basket as far as I understand (?). If I do I will reset whatever basket the user already has. Therefore I'm creating an invoice directly instead.

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 27, 2014 @ 14:59
    Jonas Eriksson
    1

    I think I'm in over my head here :-o Still not gotten this to work, this is what I have now : https://gist.github.com/joeriks/87b5f7ef3f5245763e93 ;

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 27, 2014 @ 17:57
    Rusty Swayne
    100

    Hi Jonas,

    I have created some example code for you. You were pretty close, but think the API is a bit confusing and we need to document it better.

    Here is my code https://gist.github.com/rustyswayne/598049064a02fce1597c

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 27, 2014 @ 18:46
    Jonas Eriksson
    0

    Awesome, thanks a lot Rusty, exacly what I needed.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 31, 2014 @ 03:26
    Rusty Swayne
    0

    Jonas,

    Just FYI I tested the code in my Gist outside of the Merchello Integration tests and there were a couple of internals I used. In the 1.5.0 build I have publicized several internals to make things work as intended.

    I did change the

    // since we know all the items in the invoice will be shipped we don't need to filter
    var shipment = new Shipment(shipmentStatus, warehouse.AsAddress(), billingAddress, invoice.Items);
    

    to

    var shipmentService = MerchelloContext.Current.Services.ShipmentService;
    
    var shipment = shipmentService.CreateShipment(... a few overloads ... );
    

    so that we could make sure things are setup the way they need to be - and left the actual Shipment class internal.

Please Sign in or register to post replies

Write your reply to:

Draft