Søk i hjelpesidene:

Generate shop order by code with E-Site Shop API

This VB.Net example shows how to use the API to create a new order and redirect the user directly to the check out ordering step.

Code example
Language:
 VB.Net

' User control code behind. Connect this user control to your design og run it in your website.
  It creates a shop order based purely based on request parameters.
  (This kind of functionality should not be made publicly available,
  it serves only as an example of how to perform this action in the API)'

Partial Class Website_UserControl_CodeBehind
    Inherits ESiteControl



    Protected Sub PageLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        Dim cart = GetShoppingCart() 'This gets an instance to the current cart'
        cart.Clear() 'This clears all contents from the current cart'

        'This adds a product to the cart'
        Dim addID = Convert.ToInt32(Request("addid")) 'Object ID for product to add'

        Dim addCount = Convert.ToInt32(Request("count")) 'How many to add to order'
        Dim config = GetShoppingConfig()
        Dim addItem As New SCItem(addID, addCount, config)
        cart.Add(addItem) 'Now the product is added to the cart'


        'This will create an order from the current cart'
        Dim formular = cart.GetOrderFormular()

        'Set required fields according to your configured order formular'
        formular.SetField("Name", "Test 123") 
        cart.CustomerInfoXml = formular.FieldValues.ToXml()

        Dim newOrder = cart.Order(Request.UserHostAddress) 'Specify any user trace info here '

        'You may also consider to add a try catch clause here. This may throw exceptions.
         Your new order is now stored in the newOrder parameter'

    End Sub

End Class

Redirect to check out

This following example shows how to continue to redirect the user to the payment step after generating a shop order with the API. In this way the E-Site system will handle the online payment process with the user.

Dim co = EObject.GetById(config.ObjectID)
Response.Redirect(co.GetDisplayUrl() + "?Step=1&OrderId=" + newOrder.ObjectID.ToString())) 'Set correct step value here'

'Note: You must check what step is your check out step by opening your cart settings.
 If you are skipping the ´Customer details´ step, you must create the shop order
 manually as described in the code above. See picture below for cart settings.'


Adding an event handler for handling Payment fail and success events

Note: This last code is written in C#

// Make sure to initialize a payment handler at your application startup. Be careful so you don't
// initiate several handlers causing unwanted multiple handling.

public class PaymentHandler
{
    public PaymentHandler()
    {
        // This event is run every time an order has succeded or failed payment

        Ikx.ESite.Web.Shop.Extension.Order.PaymentCompleted += OrderPaymentCompleted;
    }

    void OrderPaymentCompleted(Shop_order order)
    {
         // The order variable contains the order, see the payment_failed and payment_completed
         // fields for the orders payment status.
    }

}