Przejdź do treści

Przeliczanie koszyka

Możliwe, że użytkownik chce zmodyfikować koszyk zmieniając ilość produktu lub dodając kod rabatowy. W przypadku zmiany ilości, OpenApp musi zweryfikować tę zmianę, aby mieć pewność dostępności produktu w nowej ilości i wpływu tej zmiany na koszty dostawy. Podobnie w przypadku dodania kodu rabatowego bezpośrednio w aplikacji OpenApp.

sequenceDiagram
  autonumber
  actor User
  participant OpenApp
  participant Merchant
  Merchant->>User: show OpenApp widget
  User->>User: scan widget
  User->>+OpenApp: load basket
  OpenApp->>+Merchant: retrieve basket
  Merchant->>-OpenApp: send basket
  OpenApp->>-User: send basket
  User->>+OpenApp: add discount coupon
  OpenApp->>+Merchant: recalculate basket
  Merchant->>-OpenApp: send basket
  OpenApp->>-User: send basket
  User->>+OpenApp: confirm
  OpenApp->>OpenApp: validate payment
  OpenApp->>+Merchant: place order
  Merchant->>-OpenApp: confirm
  OpenApp->>-User: confirm

Request

W takim przypadku OpenApp wykona POST HTTP do endpointu skonfigurowanego w panelu sprzedawcy, ze zaktualizowanym koszykiem w treści żądania:

POST <recalculate-url>

Więc jeśli skonfigurowałeś URL przeliczania koszyka na https://shop.example.com/api/openapp/basket, OpenApp wyśle następujące żądanie do serwera:

POST https://shop.example.com/api/openapp/basket

Treść zapytania zawiera szczegóły koszyka zmienione przez użytkownika. Na przykład:

Basket recalculate request
1
2
3
4
5
6
7
{
  "requestId": "request-id",
  "id": "basket-id",
  "price": { "currency": "PLN", "discounts": [{ "code": "new-discount-code-text" }] },
  "products": [{ "ean": "12312", "id": "id123", "quantity": 3 }],
  "loggedUser": "user-id-from-webshop"
}
Basket recalculate schema
{
    "description": "Basket recalculate request",
    "additionalProperties": false,
    "type": "object",
    "properties": {
        "requestId": {
            "description": "The ID of the request as generated by OpenApp.",
            "maxLength": 36,
            "type": "string",
            "title": "requestId"
        },
        "id": {
            "description": "A null value for the basket id indicates that this is a request to create a new basket for a repurchase.",
            "maxLength": 36,
            "type": "string",
            "title": "id"
        },
        "price": {
            "$ref": "#/definitions/RecalculatePrice",
            "title": "price"
        },
        "products": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/RecalculateProduct"
            },
            "title": "products"
        },
        "loggedUser": {
            "type": "string",
            "title": "loggedUser"
        }
    },
    "required": [
        "price",
        "products",
        "requestId"
    ],
    "definitions": {
        "RecalculatePrice": {
            "title": "RecalculatePrice",
            "type": "object",
            "properties": {
                "currency": {
                    "description": "The currency",
                    "type": "string",
                    "title": "currency"
                },
                "discounts": {
                    "description": "Discounts to apply in price calculation.",
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/RecalculateDiscount"
                    },
                    "title": "discounts"
                }
            },
            "required": [
                "currency",
                "discounts"
            ]
        },
        "RecalculateDiscount": {
            "title": "RecalculateDiscount",
            "type": "object",
            "properties": {
                "code": {
                    "description": "The discount code.",
                    "maxLength": 36,
                    "type": "string",
                    "title": "code"
                }
            },
            "required": [
                "code"
            ]
        },
        "RecalculateProduct": {
            "title": "RecalculateProduct",
            "type": "object",
            "properties": {
                "ean": {
                    "description": "The ean (or other barcode on the product). Specifying this allows the user to search in his order history by scanning the barcode to do a re-purchase.",
                    "maxLength": 36,
                    "type": "string",
                    "title": "ean"
                },
                "id": {
                    "description": "The unique ID of the product.",
                    "maxLength": 36,
                    "type": "string",
                    "title": "id"
                },
                "quantity": {
                    "description": "The requested quantity",
                    "type": "number",
                    "title": "quantity"
                }
            },
            "required": [
                "id",
                "quantity"
            ]
        }
    },
    "$schema": "http://json-schema.org/draft-07/schema#"
}

Response

W odpowiedzi sklep powinien przesłać zawartość koszyka i dostępne opcje dostawy w takim samym formacie, jak w przypadku odpowiedzi na pobranie koszyka. Oczywiście może się zdarzyć, że sklep nie będzie w stanie sprostać żądanej zmianie koszyka, na przykład z powodu niedostępności większej liczby produktu. W tym scenariuszu sklep powininien odpowiedzieć z dostępną częścią koszyka oraz kodem błędu, aby OpenApp mógł wyświetlic te informacje użytkownikowi. Pokazuje to poniższy przykład:

Retrieve basket response
{
  "id": "basket-id",
  "expiresAt": "2022-03-23T21:00:00Z",
  "price": {
    "currency": "PLN",
    "discounts": [
      {
        "code": "discount-code-text",
        "value": 1000
      }
    ],
    "basketValue": 18000
  },
  "deliveryOptions": [
    {
      "key": "INPOST_APM",
      "cost": 0
    },
    {
      "key": "DPD_COURIER",
      "cost": 1000
    }
  ],
  "products": [
    {
      "ean": "12312",
      "id": "id123",
      "name": "Superb product",
      "images": ["http://cdn.merchant.com/static/products/id123/1", "http://cdn.merchant.com/static/products/id123/2"],
      "quantity": 2,
      "unitPrice": 6000,
      "linePrice": 12000,
      "originalUnitPrice": 7000,
      "originalLinePrice": 14000,
      "error": "QUANTITY_TOO_BIG"
    }
  ],
  "loggedUser": "user-id-from-webshop"
}
Recalculate basket response

Powyższy przykład reprezentuje bardzo prosty koszyk. Po pełny opis modelu koszyka sprawdź JSON schema koszyka