Discussions

Ask a Question
Back to All

Add Commerce item to cart - 403 on second Order/_execute UpdatePricing

Hello,

Due to a bug in ProductDisplay list Addtocart functionality (ZD 267095) I am trying to implement my own. Here is my process:

Create cart (assume no existing cart for this example):

const user = await AVICapiData(`/api/Person/${uid}`);
var bid = user["PrimaryOrganization"]["OrganizationPartyId"];
if (bid == undefined) { bid = uid; }
const cartpost = {
  "$type": "Asi.Soa.Commerce.DataContracts.CartData, Asi.Contracts",
  "UserId": uid,
  "ComboOrder": {
    "$type": "Asi.Soa.Commerce.DataContracts.ComboOrderData, Asi.Contracts",
    "Order": {
      "$type": "Asi.Soa.Commerce.DataContracts.OrderData, Asi.Contracts",
      "Lines": {
        "$type": "Asi.Soa.Commerce.DataContracts.OrderLineDataCollection, Asi.Contracts",
        "$values": []
      },
      "SoldToCustomerParty": {
        "$type": "Asi.Soa.Commerce.DataContracts.CustomerPartyData, Asi.Contracts", 
        "PartyId": uid
      },
      "BillToCustomerParty": {
        "$type": "Asi.Soa.Commerce.DataContracts.CustomerPartyData, Asi.Contracts", 
        "PartyId": bid
      },
      "OriginatorCustomerParty": {
        "$type": "Asi.Soa.Commerce.DataContracts.CustomerPartyData, Asi.Contracts",
        "PartyId": uid
      },
      "Currency": {
        "$type": "Asi.Soa.Core.DataContracts.CurrencyData, Asi.Contracts",
        "CurrencyCode": "AUD",
        "DecimalPositions": 2
      },
      "SourceCode": ""
    },
    "Currency": {
      "$type": "Asi.Soa.Core.DataContracts.CurrencyData, Asi.Contracts",
      "CurrencyCode": "AUD",
      "DecimalPositions": 2
    },
  },
  "UpdateInformation": {
    "$type": "Asi.Soa.Core.DataContracts.EntityUpdateInformationData, Asi.Contracts",
    "CreatedBy": uid,
    "CreatedOn": new Date().toISOString(),
    "UpdatedBy": uid,
    "UpdatedOn": new Date().toISOString()
  }
};
return await AVICapiData("/api/cart", cartpost, "POST");

That is a pretty basic empty cart, of note: I set the Bill to ID to the organisation.

I get the item data from ItemSummary (also tried Item endpoint), pretty straight forward const itemresult = await AVICapiData(`/api/ItemSummary/${productID}`);

I wrap that item data in a basic OrderLine structure:

order["Lines"]["$values"].push(createOrderLine(itemresult, orderlines+1));
function createOrderLine(itemData, lineno) {
    return {
        "$type": "Asi.Soa.Commerce.DataContracts.OrderLineData, Asi.Contracts", 
        "OrderLineId": self.crypto.randomUUID(),
        "Item": itemData, 
        "QuantityOrdered": {
            "$type": "System.Nullable`1[[Asi.Soa.Commerce.DataContracts.QuantityData, Asi.Contracts]], mscorlib", 
            "Amount": 1
        },
        "LineNumber": lineno,
        "SourceCode": "",
        "CanCombine": true,
        "UsesPriceGroup" : true
    }
}

now I have my order object wrapped and ready to be UpdatePricing

order = await AVICapiData("/api/Order/_execute", {
        "$type": "Asi.Soa.Commerce.DataContracts.OrderPriceUpdateRequest, Asi.Contracts",
        "EntityTypeName": "Order",
        "OperationName": "UpdatePricing",
        "Order": order
    }, "POST");

Now, if I add this updated order object to the comboorder and PUT to /api/cart, the pricing will be incorrect...

That is with a single Order/_execute. Pricing is incorrect, and if you try to checkout with this cart you will get

So I UpdatePricing on the returned order object, which should then give me correct pricing. However if you use BillToCustomerParty = Organisation ID, I get 403 after the second UpdatePricing.

If I use BillToCustomerParty = user ID and change "Users who can bill to their organisation = All users", I can double UpdatePricing, see the correct cart, and no problem.

For some reason when Adding to cart from ItemDetail, it is able to add to cart without problem. If someone is able to point out any obvious flaw in what I am doing that would be massively useful. Or if it was possible to get a fiddler dump of someone on the latest update doing Add to Cart from ItemDetail with the following setup:

  • Settings -> Contacts -> General -> Users who can bill to their organisation: All users - organization selected by default
  • Add Pricing Group on the item -> Add new pricing group -> Group Type -> Member Type

Hopefully that will give me some insight in to what is happening behind the scenes, and will show me what steps if any I am missing.

Thank you for your time and assistance.